1 /* ggpd.c - The GNUPG daemon (keyserver)
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GNUPG.
6 * GNUPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNUPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 * This is a spinning server for most purposes, the server does only
23 * fork for updates (which may require signature checks and lengthy DB
26 * see ks-proto.c for the used protocol.
27 * see ks-db.c for the layout of the database.
37 #include <sys/types.h>
46 static ARGPARSE_OPTS opts[] = {
47 { 'v', "verbose", 0, "verbose" },
48 { 501, "options" ,2, "read options from file"},
49 { 502, "no-daemon", 0, "do not operate as a daemon" },
50 { 510, "debug" ,4|16, "set debugging flags"},
51 { 511, "debug-all" ,0, "enable full debugging"},
56 static char *build_list( const char *text,
57 const char * (*mapf)(int), int (*chkf)(int) );
58 static void become_daemon(void);
63 static char *digests, *pubkeys, *ciphers;
66 case 11: p = "gpgd (GNUPG)"; break;
67 case 13: p = VERSION; break;
68 case 17: p = PRINTABLE_OS_NAME; break;
70 "Please report bugs to <gnupg-bugs@gnu.org>.\n";
73 case 40: p = "Usage: gpgd [options] (-h for help)";
75 case 41: p = "Syntax: gpgd [options] [files]\n"
78 case 31: p = "\n"; break;
81 ciphers = build_list("Supported ciphers: ", cipher_algo_to_string,
87 pubkeys = build_list("Supported pubkeys: ", pubkey_algo_to_string,
93 digests = build_list("Supported digests: ", digest_algo_to_string,
98 default: p = default_strusage(level);
105 build_list( const char *text, const char * (*mapf)(int), int (*chkf)(int) )
109 size_t n=strlen(text)+2;
112 for(i=1; i < 100; i++ )
113 if( !chkf(i) && (s=mapf(i)) )
115 list = m_alloc( 21 + n ); *list = 0;
116 for(p=NULL, i=1; i < 100; i++ ) {
117 if( !chkf(i) && (s=mapf(i)) ) {
119 p = stpcpy( list, text );
121 p = stpcpy( p, ", ");
126 p = stpcpy(p, "\n" );
134 if( opt.debug & DBG_MEMORY_VALUE )
135 memory_debug_mode = 1;
136 if( opt.debug & DBG_MEMSTAT_VALUE )
137 memory_stat_debug_mode = 1;
138 if( opt.debug & DBG_MPI_VALUE )
140 if( opt.debug & DBG_CIPHER_VALUE )
142 if( opt.debug & DBG_IOBUF_VALUE )
143 iobuf_debug_mode = 1;
148 main( int argc, char **argv )
153 FILE *configfp = NULL;
154 char *configname = NULL;
155 unsigned configlineno;
157 int default_config =1;
160 secmem_init( 0 ); /* disable use of secmem */
161 log_set_name("gpgd");
162 log_set_pid( getpid() );
163 opt.compress = -1; /* defaults to standard compress level */
166 /* check whether we have a config file on the commandline */
171 pargs.flags= 1; /* do not remove the args */
172 while( arg_parse( &pargs, opts) ) {
173 if( pargs.r_opt == 510 || pargs.r_opt == 511 )
175 else if( pargs.r_opt == 501 ) {
176 /* yes there is one, so we do not try the default one, but
177 * read the option file when it is encountered at the commandline
184 configname = make_filename("/etc/gpgd.conf", NULL );
190 pargs.flags= 1; /* do not remove the args */
194 configfp = fopen( configname, "r" );
196 if( default_config ) {
198 log_info("note: no default option file '%s'\n",
202 log_error("option file '%s': %s\n",
203 configname, strerror(errno) );
206 m_free(configname); configname = NULL;
208 if( parse_debug && configname )
209 log_info("reading options from '%s'\n", configname );
213 while( optfile_parse( configfp, configname, &configlineno,
215 switch( pargs.r_opt ) {
216 case 'v': opt.verbose++; break;
220 configname = m_strdup(pargs.r.ret_str);
224 case 502: daemon = 0; break;
225 case 510: opt.debug |= pargs.r.ret_ulong; break;
226 case 511: opt.debug = ~0; break;
227 default : pargs.err = configfp? 1:2; break;
233 m_free(configname); configname = NULL;
236 m_free( configname ); configname = NULL;
237 if( log_get_errorcount(0) )
240 fprintf(stderr, "%s %s; %s\n", strusage(11), strusage(13), strusage(14) );
241 fprintf(stderr, "%s\n", strusage(15) );
249 return 8; /*NEVER REACHED*/
257 rc = rc? rc : log_get_errorcount(0)? 2:0;
264 static int did_rsa_note = 0;
266 if( !did_rsa_note ) {
268 log_info("RSA keys are depreciated; please consider "
269 "creating a new key and use this key in the future\n");
282 log_info("becoming a daemon ...\n");
285 /* FIXME: handle the TTY signals */
287 if( (childpid = fork()) == -1 )
288 log_fatal("can't fork first child: %s\n", strerror(errno));
289 else if( childpid > 0 )
290 exit(0); /* terminate parent */
292 /* Disassociate from controlling terminal etc. */
294 log_fatal("setsid() failed: %s\n", strerror(errno) );
296 log_set_pid( getpid() );
297 /* close all files but not the log files */
298 if( (nfile=sysconf( _SC_OPEN_MAX )) < 0 )
299 #ifdef _POSIX_OPEN_MAX
300 nfile = _POSIX_OPEN_MAX;
302 nfile = 20; /* assume a common value */
304 n = fileno( stderr );
305 for(i=0; i < nfile; i++ )
311 log_fatal("chdir to root failed: %s\n", strerror(errno) );
314 /* do not let possible children become zombies */
315 signal(SIGCHLD, SIG_IGN);
317 log_info("now running as daemon\n");