Commit | Line | Data |
---|---|---|
2fe58dfd SE |
1 | /* $Log: secnet.c,v $ |
2 | * Revision 1.1 1996/03/13 22:27:41 sde1000 | |
3 | * Initial revision | |
4 | * | |
5 | */ | |
6 | ||
974d0468 | 7 | extern char version[]; |
2fe58dfd | 8 | |
8689b3a9 | 9 | #include "secnet.h" |
2fe58dfd SE |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include <getopt.h> | |
2fe58dfd | 13 | #include <errno.h> |
8689b3a9 | 14 | #include <unistd.h> |
2fe58dfd | 15 | #include <sys/socket.h> |
2fe58dfd | 16 | #include <arpa/inet.h> |
2fe58dfd | 17 | #include <pwd.h> |
2fe58dfd | 18 | |
2fe58dfd SE |
19 | #include "util.h" |
20 | #include "conffile.h" | |
7138d0c5 | 21 | #include "process.h" |
2fe58dfd | 22 | |
b2a56f7c | 23 | /* XXX should be from autoconf */ |
2fe58dfd | 24 | static char *configfile="/etc/secnet/secnet.conf"; |
794f2398 | 25 | static char *sites_key="sites"; |
baa06aeb | 26 | bool_t just_check_config=False; |
2fe58dfd SE |
27 | static char *userid=NULL; |
28 | static uid_t uid=0; | |
b2a56f7c | 29 | bool_t background=True; |
2fe58dfd | 30 | static char *pidfile=NULL; |
9d3a4132 SE |
31 | bool_t require_root_privileges=False; |
32 | string_t require_root_privileges_explanation=NULL; | |
2fe58dfd | 33 | |
7138d0c5 SE |
34 | static pid_t secnet_pid; |
35 | ||
36 | /* from log.c */ | |
37 | extern uint32_t message_level; | |
38 | extern bool_t secnet_is_daemon; | |
39 | extern struct log_if *system_log; | |
40 | ||
41 | /* from process.c */ | |
42 | extern void start_signal_handling(void); | |
43 | ||
2fe58dfd SE |
44 | /* Structures dealing with poll() call */ |
45 | struct poll_interest { | |
46 | beforepoll_fn *before; | |
47 | afterpoll_fn *after; | |
48 | void *state; | |
49 | uint32_t max_nfds; | |
50 | uint32_t nfds; | |
51 | string_t desc; | |
52 | struct poll_interest *next; | |
53 | }; | |
54 | static struct poll_interest *reg=NULL; | |
55 | static uint32_t total_nfds=10; | |
56 | ||
57 | static bool_t finished=False; | |
58 | ||
59 | /* Parse the command line options */ | |
60 | static void parse_options(int argc, char **argv) | |
61 | { | |
62 | int c; | |
63 | ||
64 | while (True) { | |
65 | int option_index = 0; | |
66 | static struct option long_options[] = { | |
67 | {"verbose", 0, 0, 'v'}, | |
68 | {"nowarnings", 0, 0, 'w'}, | |
69 | {"help", 0, 0, 2}, | |
70 | {"version", 0, 0, 1}, | |
71 | {"nodetach", 0, 0, 'n'}, | |
72 | {"silent", 0, 0, 'f'}, | |
73 | {"quiet", 0, 0, 'f'}, | |
74 | {"debug", 1, 0, 'd'}, | |
75 | {"config", 1, 0, 'c'}, | |
baa06aeb | 76 | {"just-check-config", 0, 0, 'j'}, |
794f2398 | 77 | {"sites-key", 1, 0, 's'}, |
2fe58dfd SE |
78 | {0,0,0,0} |
79 | }; | |
80 | ||
794f2398 | 81 | c=getopt_long(argc, argv, "vwdnjc:ft:s:", |
2fe58dfd SE |
82 | long_options, &option_index); |
83 | if (c==-1) | |
84 | break; | |
85 | ||
86 | switch(c) { | |
87 | case 2: | |
88 | /* Help */ | |
89 | fprintf(stderr, | |
90 | "Usage: secnet [OPTION]...\n\n" | |
91 | " -f, --silent, --quiet suppress error messages\n" | |
92 | " -w, --nowarnings suppress warnings\n" | |
93 | " -v, --verbose output extra diagnostics\n" | |
94 | " -c, --config=filename specify a configuration file\n" | |
794f2398 SE |
95 | " -j, --just-check-config stop after reading " |
96 | "configuration file\n" | |
97 | " -s, --sites-key=name configuration key that " | |
98 | "specifies active sites\n" | |
2fe58dfd SE |
99 | " -n, --nodetach do not run in background\n" |
100 | " -d, --debug=item,... set debug options\n" | |
101 | " --help display this help and exit\n" | |
794f2398 SE |
102 | " --version output version information " |
103 | "and exit\n" | |
2fe58dfd SE |
104 | ); |
105 | exit(0); | |
106 | break; | |
107 | ||
108 | case 1: | |
109 | /* Version */ | |
110 | fprintf(stderr,"%s\n",version); | |
111 | exit(0); | |
112 | break; | |
113 | ||
114 | case 'v': | |
469fd1d9 | 115 | message_level|=M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY| |
b2a56f7c | 116 | M_FATAL; |
2fe58dfd SE |
117 | break; |
118 | ||
b2a56f7c SE |
119 | case 'w': |
120 | message_level&=(~M_WARNING); | |
2fe58dfd SE |
121 | break; |
122 | ||
123 | case 'd': | |
b2a56f7c | 124 | message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE|M_DEBUG; |
2fe58dfd SE |
125 | break; |
126 | ||
127 | case 'f': | |
128 | message_level=M_FATAL; | |
129 | break; | |
130 | ||
b2a56f7c SE |
131 | case 'n': |
132 | background=False; | |
133 | break; | |
134 | ||
2fe58dfd SE |
135 | case 'c': |
136 | if (optarg) | |
137 | configfile=safe_strdup(optarg,"config_filename"); | |
138 | else | |
139 | fatal("secnet: no config filename specified"); | |
140 | break; | |
141 | ||
baa06aeb SE |
142 | case 'j': |
143 | just_check_config=True; | |
144 | break; | |
145 | ||
794f2398 SE |
146 | case 's': |
147 | if (optarg) | |
148 | sites_key=safe_strdup(optarg,"sites-key"); | |
149 | else | |
150 | fatal("secnet: no sites key specified"); | |
151 | break; | |
152 | ||
2fe58dfd SE |
153 | case '?': |
154 | break; | |
155 | ||
156 | default: | |
469fd1d9 | 157 | Message(M_ERR,"secnet: Unknown getopt code %c\n",c); |
2fe58dfd SE |
158 | } |
159 | } | |
160 | ||
161 | if (argc-optind != 0) { | |
469fd1d9 | 162 | Message(M_ERR,"secnet: You gave extra command line parameters, " |
2fe58dfd SE |
163 | "which were ignored.\n"); |
164 | } | |
165 | } | |
166 | ||
167 | static void setup(dict_t *config) | |
168 | { | |
169 | list_t *l; | |
170 | item_t *site; | |
171 | dict_t *system; | |
2fe58dfd SE |
172 | struct passwd *pw; |
173 | struct cloc loc; | |
174 | int i; | |
175 | ||
176 | l=dict_lookup(config,"system"); | |
177 | ||
178 | if (!l || list_elem(l,0)->type!=t_dict) { | |
179 | fatal("configuration does not include a \"system\" dictionary\n"); | |
180 | } | |
181 | system=list_elem(l,0)->data.dict; | |
182 | loc=list_elem(l,0)->loc; | |
183 | ||
184 | /* Arrange systemwide log facility */ | |
185 | l=dict_lookup(system,"log"); | |
186 | if (!l) { | |
187 | fatal("configuration does not include a system/log facility\n"); | |
188 | } | |
b2a56f7c | 189 | system_log=init_log(l); |
2fe58dfd SE |
190 | |
191 | /* Who are we supposed to run as? */ | |
192 | userid=dict_read_string(system,"userid",False,"system",loc); | |
193 | if (userid) { | |
194 | do { | |
195 | pw=getpwent(); | |
196 | if (pw && strcmp(pw->pw_name,userid)==0) { | |
197 | uid=pw->pw_uid; | |
198 | break; | |
199 | } | |
200 | } while(pw); | |
201 | endpwent(); | |
202 | if (uid==0) { | |
203 | fatal("userid \"%s\" not found\n",userid); | |
204 | } | |
205 | } | |
206 | ||
207 | /* Pidfile name */ | |
208 | pidfile=dict_read_string(system,"pidfile",False,"system",loc); | |
209 | ||
9d3a4132 SE |
210 | /* Check whether we need root privileges */ |
211 | if (require_root_privileges && uid!=0) { | |
212 | fatal("the following configured feature (\"%s\") requires " | |
213 | "that secnet retain root privileges while running.\n", | |
214 | require_root_privileges_explanation); | |
215 | } | |
216 | ||
2fe58dfd | 217 | /* Go along site list, starting sites */ |
794f2398 | 218 | l=dict_lookup(config,sites_key); |
2fe58dfd | 219 | if (!l) { |
794f2398 SE |
220 | Message(M_WARNING,"secnet: configuration key \"%s\" is missing; no " |
221 | "remote sites are defined\n",sites_key); | |
9d3a4132 SE |
222 | } else { |
223 | i=0; | |
224 | while ((site=list_elem(l, i++))) { | |
225 | struct site_if *s; | |
226 | if (site->type!=t_closure) { | |
227 | cfgfatal(site->loc,"system","non-closure in site list"); | |
228 | } | |
229 | if (site->data.closure->type!=CL_SITE) { | |
230 | cfgfatal(site->loc,"system","non-site closure in site list"); | |
231 | } | |
232 | s=site->data.closure->interface; | |
233 | s->control(s->st,True); | |
2fe58dfd | 234 | } |
2fe58dfd SE |
235 | } |
236 | } | |
237 | ||
238 | void register_for_poll(void *st, beforepoll_fn *before, | |
239 | afterpoll_fn *after, uint32_t max_nfds, string_t desc) | |
240 | { | |
241 | struct poll_interest *i; | |
242 | ||
243 | i=safe_malloc(sizeof(*i),"register_for_poll"); | |
244 | i->before=before; | |
245 | i->after=after; | |
246 | i->state=st; | |
247 | i->max_nfds=max_nfds; | |
248 | i->nfds=0; | |
249 | i->desc=desc; | |
250 | total_nfds+=max_nfds; | |
251 | i->next=reg; | |
252 | reg=i; | |
253 | return; | |
254 | } | |
255 | ||
256 | static void system_phase_hook(void *sst, uint32_t newphase) | |
257 | { | |
258 | if (newphase==PHASE_SHUTDOWN && pidfile) { | |
259 | /* Try to unlink the pidfile; don't care if it fails */ | |
260 | unlink(pidfile); | |
261 | } | |
262 | } | |
263 | ||
264 | static void run(void) | |
265 | { | |
266 | struct timeval tv_now; | |
267 | uint64_t now; | |
268 | struct poll_interest *i; | |
269 | int rv, nfds, remain, idx; | |
270 | int timeout; | |
271 | struct pollfd *fds; | |
272 | ||
273 | fds=alloca(sizeof(*fds)*total_nfds); | |
274 | if (!fds) { | |
275 | fatal("run: couldn't alloca\n"); | |
276 | } | |
277 | ||
7138d0c5 | 278 | Message(M_NOTICE,"%s [%d]: starting\n",version,secnet_pid); |
b2a56f7c | 279 | |
7138d0c5 | 280 | do { |
2fe58dfd SE |
281 | if (gettimeofday(&tv_now, NULL)!=0) { |
282 | fatal_perror("main loop: gettimeofday"); | |
283 | } | |
284 | now=(tv_now.tv_sec*1000)+(tv_now.tv_usec/1000); | |
285 | idx=0; | |
286 | for (i=reg; i; i=i->next) { | |
287 | i->after(i->state, fds+idx, i->nfds, &tv_now, &now); | |
288 | idx+=i->nfds; | |
289 | } | |
290 | remain=total_nfds; | |
291 | idx=0; | |
292 | timeout=-1; | |
293 | for (i=reg; i; i=i->next) { | |
294 | nfds=remain; | |
295 | rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now); | |
296 | if (rv!=0) { | |
297 | /* XXX we need to handle this properly: increase the | |
298 | nfds available */ | |
299 | fatal("run: beforepoll_fn (%s) returns %d\n",i->desc,rv); | |
300 | } | |
301 | if (timeout<-1) { | |
302 | fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout); | |
303 | } | |
304 | idx+=nfds; | |
305 | remain-=nfds; | |
306 | i->nfds=nfds; | |
307 | } | |
308 | do { | |
7138d0c5 | 309 | if (finished) break; |
2fe58dfd SE |
310 | rv=poll(fds, idx, timeout); |
311 | if (rv<0) { | |
312 | if (errno!=EINTR) { | |
313 | fatal_perror("run: poll"); | |
314 | } | |
315 | } | |
316 | } while (rv<0); | |
7138d0c5 | 317 | } while (!finished); |
2fe58dfd SE |
318 | } |
319 | ||
320 | static void droppriv(void) | |
321 | { | |
322 | FILE *pf=NULL; | |
323 | pid_t p; | |
324 | ||
325 | add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL); | |
326 | ||
b2a56f7c SE |
327 | /* Open the pidfile for writing now: we may be unable to do so |
328 | once we drop privileges. */ | |
329 | if (pidfile) { | |
330 | pf=fopen(pidfile,"w"); | |
331 | if (!pf) { | |
332 | fatal_perror("cannot open pidfile \"%s\"",pidfile); | |
2fe58dfd | 333 | } |
b2a56f7c SE |
334 | } |
335 | if (!background && pf) { | |
336 | fprintf(pf,"%d\n",getpid()); | |
337 | fclose(pf); | |
338 | } | |
339 | ||
340 | /* Now drop privileges */ | |
341 | if (uid!=0) { | |
342 | if (setuid(uid)!=0) { | |
343 | fatal_perror("can't set uid to \"%s\"",userid); | |
344 | } | |
345 | } | |
346 | if (background) { | |
2fe58dfd SE |
347 | p=fork(); |
348 | if (p>0) { | |
349 | if (pf) { | |
350 | /* Parent process - write pidfile, exit */ | |
351 | fprintf(pf,"%d\n",p); | |
352 | fclose(pf); | |
353 | } | |
354 | exit(0); | |
355 | } else if (p==0) { | |
356 | /* Child process - all done, just carry on */ | |
357 | if (pf) fclose(pf); | |
8dea8d37 SE |
358 | /* Close stdin, stdout and stderr; we don't need them any more */ |
359 | close(0); | |
360 | close(1); | |
361 | close(2); | |
b2a56f7c | 362 | secnet_is_daemon=True; |
2fe58dfd SE |
363 | } else { |
364 | /* Error */ | |
365 | fatal_perror("cannot fork"); | |
366 | exit(1); | |
367 | } | |
2fe58dfd | 368 | } |
7138d0c5 SE |
369 | secnet_pid=getpid(); |
370 | } | |
2fe58dfd | 371 | |
7138d0c5 SE |
372 | static signal_notify_fn finish,ignore_hup; |
373 | static void finish(void *st, int signum) | |
374 | { | |
375 | finished=True; | |
376 | Message(M_NOTICE,"%s [%d]: received %s\n",version,secnet_pid,(string_t)st); | |
377 | } | |
378 | static void ignore_hup(void *st, int signum) | |
379 | { | |
380 | Message(M_INFO,"%s [%d]: received SIGHUP\n",version,secnet_pid); | |
381 | return; | |
2fe58dfd SE |
382 | } |
383 | ||
384 | int main(int argc, char **argv) | |
385 | { | |
386 | dict_t *config; | |
387 | ||
388 | enter_phase(PHASE_GETOPTS); | |
389 | parse_options(argc,argv); | |
390 | ||
391 | enter_phase(PHASE_READCONFIG); | |
392 | config=read_conffile(configfile); | |
393 | ||
394 | enter_phase(PHASE_SETUP); | |
395 | setup(config); | |
baa06aeb SE |
396 | |
397 | if (just_check_config) { | |
398 | Message(M_INFO,"configuration file check complete\n"); | |
399 | exit(0); | |
400 | } | |
401 | ||
402 | enter_phase(PHASE_GETRESOURCES); | |
403 | /* Appropriate phase hooks will have been run */ | |
2fe58dfd SE |
404 | |
405 | enter_phase(PHASE_DROPPRIV); | |
406 | droppriv(); | |
407 | ||
7138d0c5 SE |
408 | start_signal_handling(); |
409 | request_signal_notification(SIGTERM,finish,"SIGTERM"); | |
410 | if (!background) request_signal_notification(SIGINT,finish,"SIGINT"); | |
411 | request_signal_notification(SIGHUP,ignore_hup,NULL); | |
042a8da9 | 412 | enter_phase(PHASE_RUN); |
2fe58dfd SE |
413 | run(); |
414 | ||
415 | enter_phase(PHASE_SHUTDOWN); | |
7138d0c5 | 416 | Message(M_NOTICE,"%s [%d]: finished\n",version,secnet_pid); |
2fe58dfd SE |
417 | |
418 | return 0; | |
419 | } |