Eliminated the unpacking of uid in the chkpwd
[citadel.git] / citadel / server_main.c
1 /*
2  * citserver's main() function lives here.
3  *
4  * $Id$
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <syslog.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <limits.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <sys/un.h>
35 #include <string.h>
36 #include <pwd.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #ifdef HAVE_PTHREAD_H
42 #include <pthread.h>
43 #endif
44 #ifdef HAVE_SYS_PRCTL_H
45 #include <sys/prctl.h>
46 #endif
47 #include "citadel.h"
48 #include "server.h"
49 #include "serv_extensions.h"
50 #include "sysdep_decls.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "control.h"
55 #include "database.h"
56 #include "user_ops.h"
57 #include "housekeeping.h"
58 #include "tools.h"
59 #include "citadel_dirs.c"
60
61 #ifdef HAVE_SYS_SELECT_H
62 #include <sys/select.h>
63 #endif
64
65 #ifndef HAVE_SNPRINTF
66 #include "snprintf.h"
67 #endif
68
69 int running_as_daemon = 0;
70
71 /*
72  * Here's where it all begins.
73  */
74 int main(int argc, char **argv)
75 {
76         char facility[32];
77         int a, i;                       /* General-purpose variables */
78         struct passwd pw, *pwp = NULL;
79         char pwbuf[SIZ];
80         int drop_root_perms = 1;
81         size_t size;
82         int relh=0;
83         int home=0;
84         char relhome[PATH_MAX]="";
85         char ctdldir[PATH_MAX]=CTDLDIR;
86 #ifdef HAVE_RUN_DIR
87         struct stat filestats;
88 #endif
89         
90         /* initialize the master context */
91         InitializeMasterCC();
92
93         /* parse command-line arguments */
94         for (a=1; a<argc; ++a) {
95
96                 if (!strncmp(argv[a], "-l", 2)) {
97                         safestrncpy(facility, &argv[a][2], sizeof(facility));
98                         syslog_facility = SyslogFacility(facility);
99                         enable_syslog = 1;
100                 }
101
102                 /* run in the background if -d was specified */
103                 else if (!strcmp(argv[a], "-d")) {
104                         running_as_daemon = 1;
105                 }
106
107                 /* -x specifies the desired logging level */
108                 else if (!strncmp(argv[a], "-x", 2)) {
109                         verbosity = atoi(&argv[a][2]);
110                 }
111
112                 else if (!strncmp(argv[a], "-h", 2)) {
113                         relh=argv[a][2]!='/';
114                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
115                                                                    sizeof ctdl_home_directory);
116                         else
117                                 safestrncpy(relhome, &argv[a][2],
118                                                         sizeof relhome);
119                         home_specified = 1;
120                         home=1;
121                 }
122
123                 else if (!strncmp(argv[a], "-t", 2)) {
124                         freopen(&argv[a][2], "w", stderr);
125                 }
126
127                 else if (!strncmp(argv[a], "-f", 2)) {
128                         do_defrag = 1;
129                 }
130
131                 /* -r tells the server not to drop root permissions. don't use
132                  * this unless you know what you're doing. this should be
133                  * removed in the next release if it proves unnecessary. */
134                 else if (!strcmp(argv[a], "-r"))
135                         drop_root_perms = 0;
136
137                 /* any other parameter makes it crash and burn */
138                 else {
139                         lprintf(CTDL_EMERG,     "citserver: usage: "
140                                         "citserver "
141                                         "[-lLogFacility] "
142                                         "[-d] [-f]"
143                                         " [-tTraceFile]"
144                                         " [-xLogLevel] [-hHomeDir]\n");
145                         exit(1);
146                 }
147
148         }
149
150         calc_dirs_n_files(relh, home, relhome, ctdldir);
151
152         /* daemonize, if we were asked to */
153         if (running_as_daemon) {
154                 start_daemon(0);
155                 drop_root_perms = 1;
156         }
157
158         /* Initialize the syslogger.  Yes, we are really using 0 as the
159          * facility, because we are going to bitwise-OR the facility to
160          * the severity of each message, allowing us to write to other
161          * facilities when we need to...
162          */
163         if (enable_syslog) {
164                 openlog("citadel", LOG_NDELAY, 0);
165                 setlogmask(LOG_UPTO(verbosity));
166         }
167         
168         /* Tell 'em who's in da house */
169         lprintf(CTDL_NOTICE, "\n");
170         lprintf(CTDL_NOTICE, "\n");
171         lprintf(CTDL_NOTICE,
172                 "*** Citadel server engine v%d.%02d ***\n",
173                 (REV_LEVEL/100), (REV_LEVEL%100));
174         lprintf(CTDL_NOTICE,
175                 "Copyright (C) 1987-2007 by the Citadel development team.\n");
176         lprintf(CTDL_NOTICE,
177                 "This program is distributed under the terms of the GNU "
178                 "General Public License.\n");
179         lprintf(CTDL_NOTICE, "\n");
180         lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
181
182         /* Load site-specific parameters, and set the ipgm secret */
183         lprintf(CTDL_INFO, "Loading citadel.config\n");
184         get_config();
185         config.c_ipgm_secret = rand();
186         put_config();
187
188         lprintf(CTDL_INFO, "Acquiring control record\n");
189         get_control();
190
191 #ifdef HAVE_RUN_DIR
192         /* on some dists rundir gets purged on startup. so we need to recreate it. */
193
194         if (stat(ctdl_run_dir, &filestats)==-1){
195 #ifdef SOLARIS_GETPWUID
196                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
197 #else
198                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
199 #endif
200                 mkdir(ctdl_run_dir, 0755);
201                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
202         }
203                         
204
205 #endif
206
207         /* Initialize... */
208         init_sysdep();
209
210         /*
211          * Do non system dependent startup functions.
212          */
213         master_startup();
214
215         /*
216          * Bind the server to a Unix-domain socket.
217          */
218         CtdlRegisterServiceHook(0,
219                                                         file_citadel_socket,
220                                                         citproto_begin_session,
221                                                         do_command_loop,
222                                                         do_async_loop);
223
224         /*
225          * Bind the server to our favorite TCP port (usually 504).
226          */
227         CtdlRegisterServiceHook(config.c_port_number,
228                                 NULL,
229                                 citproto_begin_session,
230                                 do_command_loop,
231                                 do_async_loop);
232
233         /*
234          * Load any server-side extensions available here.
235          */
236         lprintf(CTDL_INFO, "Initializing server extensions\n");
237         size = strlen(ctdl_home_directory) + 9;
238         initialize_server_extensions();
239
240         /*
241          * If we need host auth, start our chkpwd daemon.
242          */
243         if (config.c_auth_mode == 1) {
244                 start_chkpwd_daemon();
245         }
246
247         /*
248          * Now that we've bound the sockets, change to the Citadel user id and its
249          * corresponding group ids
250          */
251         if (drop_root_perms) {
252                 cdb_chmod_data();       /* make sure we own our data files */
253
254 #ifdef SOLARIS_GETPWUID
255                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
256 #else
257                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
258 #endif
259                 if (pwp == NULL)
260                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
261                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
262                                 strerror(errno));
263                 else {
264                         initgroups(pw.pw_name, pw.pw_gid);
265                         if (setgid(pw.pw_gid))
266                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
267                                         strerror(errno));
268                 }
269                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
270                 if (setuid(CTDLUID) != 0) {
271                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
272                 }
273 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
274                 prctl(PR_SET_DUMPABLE, 1);
275 #endif
276         }
277
278         /* We want to check for idle sessions once per minute */
279         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
280
281         /*
282          * Now create a bunch of worker threads.
283          */
284         lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
285                 config.c_min_workers-1);
286         begin_critical_section(S_WORKER_LIST);
287         for (i=0; i<(config.c_min_workers-1); ++i) {
288                 create_worker();
289         }
290         end_critical_section(S_WORKER_LIST);
291
292         /* Create the indexer thread. */
293         create_maintenance_threads();
294
295         /* This thread is now useless.  It can't be turned into a worker
296          * thread because its stack is too small, but it can't be killed
297          * either because the whole server process would exit.  So we just
298          * join to the first worker thread and exit when it exits.
299          */
300         pthread_join(worker_list->tid, NULL);
301         master_cleanup(0);
302         return(0);
303 }