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 | ||
7 | static char *version="secnet version " VERSION " $Date: 1996/03/13 22:27:41 $"; | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <string.h> | |
11 | #include <getopt.h> | |
12 | #include <stdarg.h> | |
13 | #include <errno.h> | |
14 | #include <sys/socket.h> | |
15 | #include <sys/poll.h> | |
16 | #include <sys/time.h> | |
17 | #include <netinet/in.h> | |
18 | #include <arpa/inet.h> | |
19 | #include <adns.h> | |
20 | #include <pwd.h> | |
21 | #include <sys/types.h> | |
22 | ||
23 | #include "secnet.h" | |
24 | #include "util.h" | |
25 | #include "conffile.h" | |
26 | #include "modules.h" | |
27 | ||
28 | /* Command-line options (possibly config-file options too) */ | |
29 | static char *configfile="/etc/secnet/secnet.conf"; | |
30 | static char *userid=NULL; | |
31 | static uid_t uid=0; | |
32 | static bool_t background=True; | |
33 | static char *pidfile=NULL; | |
34 | ||
35 | /* Structures dealing with poll() call */ | |
36 | struct poll_interest { | |
37 | beforepoll_fn *before; | |
38 | afterpoll_fn *after; | |
39 | void *state; | |
40 | uint32_t max_nfds; | |
41 | uint32_t nfds; | |
42 | string_t desc; | |
43 | struct poll_interest *next; | |
44 | }; | |
45 | static struct poll_interest *reg=NULL; | |
46 | static uint32_t total_nfds=10; | |
47 | ||
48 | static bool_t finished=False; | |
49 | ||
50 | /* Parse the command line options */ | |
51 | static void parse_options(int argc, char **argv) | |
52 | { | |
53 | int c; | |
54 | ||
55 | while (True) { | |
56 | int option_index = 0; | |
57 | static struct option long_options[] = { | |
58 | {"verbose", 0, 0, 'v'}, | |
59 | {"nowarnings", 0, 0, 'w'}, | |
60 | {"help", 0, 0, 2}, | |
61 | {"version", 0, 0, 1}, | |
62 | {"nodetach", 0, 0, 'n'}, | |
63 | {"silent", 0, 0, 'f'}, | |
64 | {"quiet", 0, 0, 'f'}, | |
65 | {"debug", 1, 0, 'd'}, | |
66 | {"config", 1, 0, 'c'}, | |
67 | {0,0,0,0} | |
68 | }; | |
69 | ||
70 | c=getopt_long(argc, argv, "vwdnc:ft:", | |
71 | long_options, &option_index); | |
72 | if (c==-1) | |
73 | break; | |
74 | ||
75 | switch(c) { | |
76 | case 2: | |
77 | /* Help */ | |
78 | fprintf(stderr, | |
79 | "Usage: secnet [OPTION]...\n\n" | |
80 | " -f, --silent, --quiet suppress error messages\n" | |
81 | " -w, --nowarnings suppress warnings\n" | |
82 | " -v, --verbose output extra diagnostics\n" | |
83 | " -c, --config=filename specify a configuration file\n" | |
84 | " -n, --nodetach do not run in background\n" | |
85 | " -d, --debug=item,... set debug options\n" | |
86 | " --help display this help and exit\n" | |
87 | " --version output version information and exit\n" | |
88 | ); | |
89 | exit(0); | |
90 | break; | |
91 | ||
92 | case 1: | |
93 | /* Version */ | |
94 | fprintf(stderr,"%s\n",version); | |
95 | exit(0); | |
96 | break; | |
97 | ||
98 | case 'v': | |
99 | message_level|=M_INFO|M_WARNING|M_ERROR|M_FATAL; | |
100 | break; | |
101 | ||
102 | case 'n': | |
103 | background=False; | |
104 | break; | |
105 | ||
106 | case 'd': | |
107 | message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE; | |
108 | break; | |
109 | ||
110 | case 'f': | |
111 | message_level=M_FATAL; | |
112 | break; | |
113 | ||
114 | case 'c': | |
115 | if (optarg) | |
116 | configfile=safe_strdup(optarg,"config_filename"); | |
117 | else | |
118 | fatal("secnet: no config filename specified"); | |
119 | break; | |
120 | ||
121 | case '?': | |
122 | break; | |
123 | ||
124 | default: | |
125 | Message(M_WARNING,"secnet: Unknown getopt code %c\n",c); | |
126 | } | |
127 | } | |
128 | ||
129 | if (argc-optind != 0) { | |
130 | Message(M_WARNING,"secnet: You gave extra command line parameters, " | |
131 | "which were ignored.\n"); | |
132 | } | |
133 | } | |
134 | ||
135 | static void setup(dict_t *config) | |
136 | { | |
137 | list_t *l; | |
138 | item_t *site; | |
139 | dict_t *system; | |
140 | struct log_if *log; | |
141 | struct passwd *pw; | |
142 | struct cloc loc; | |
143 | int i; | |
144 | ||
145 | l=dict_lookup(config,"system"); | |
146 | ||
147 | if (!l || list_elem(l,0)->type!=t_dict) { | |
148 | fatal("configuration does not include a \"system\" dictionary\n"); | |
149 | } | |
150 | system=list_elem(l,0)->data.dict; | |
151 | loc=list_elem(l,0)->loc; | |
152 | ||
153 | /* Arrange systemwide log facility */ | |
154 | l=dict_lookup(system,"log"); | |
155 | if (!l) { | |
156 | fatal("configuration does not include a system/log facility\n"); | |
157 | } | |
158 | log=init_log(l); | |
159 | log->log(log->st,LOG_DEBUG,"setup: logging started"); | |
160 | ||
161 | /* Who are we supposed to run as? */ | |
162 | userid=dict_read_string(system,"userid",False,"system",loc); | |
163 | if (userid) { | |
164 | do { | |
165 | pw=getpwent(); | |
166 | if (pw && strcmp(pw->pw_name,userid)==0) { | |
167 | uid=pw->pw_uid; | |
168 | break; | |
169 | } | |
170 | } while(pw); | |
171 | endpwent(); | |
172 | if (uid==0) { | |
173 | fatal("userid \"%s\" not found\n",userid); | |
174 | } | |
175 | } | |
176 | ||
177 | /* Pidfile name */ | |
178 | pidfile=dict_read_string(system,"pidfile",False,"system",loc); | |
179 | ||
180 | /* Go along site list, starting sites */ | |
181 | l=dict_lookup(config,"sites"); | |
182 | if (!l) { | |
183 | fatal("configuration did not define any remote sites\n"); | |
184 | } | |
185 | i=0; | |
186 | while ((site=list_elem(l, i++))) { | |
187 | struct site_if *s; | |
188 | if (site->type!=t_closure) { | |
189 | cfgfatal(site->loc,"system","non-closure in site list"); | |
190 | } | |
191 | if (site->data.closure->type!=CL_SITE) { | |
192 | cfgfatal(site->loc,"system","non-site closure in site list"); | |
193 | } | |
194 | s=site->data.closure->interface; | |
195 | s->control(s->st,True); | |
196 | } | |
197 | } | |
198 | ||
199 | void register_for_poll(void *st, beforepoll_fn *before, | |
200 | afterpoll_fn *after, uint32_t max_nfds, string_t desc) | |
201 | { | |
202 | struct poll_interest *i; | |
203 | ||
204 | i=safe_malloc(sizeof(*i),"register_for_poll"); | |
205 | i->before=before; | |
206 | i->after=after; | |
207 | i->state=st; | |
208 | i->max_nfds=max_nfds; | |
209 | i->nfds=0; | |
210 | i->desc=desc; | |
211 | total_nfds+=max_nfds; | |
212 | i->next=reg; | |
213 | reg=i; | |
214 | return; | |
215 | } | |
216 | ||
217 | static void system_phase_hook(void *sst, uint32_t newphase) | |
218 | { | |
219 | if (newphase==PHASE_SHUTDOWN && pidfile) { | |
220 | /* Try to unlink the pidfile; don't care if it fails */ | |
221 | unlink(pidfile); | |
222 | } | |
223 | } | |
224 | ||
225 | static void run(void) | |
226 | { | |
227 | struct timeval tv_now; | |
228 | uint64_t now; | |
229 | struct poll_interest *i; | |
230 | int rv, nfds, remain, idx; | |
231 | int timeout; | |
232 | struct pollfd *fds; | |
233 | ||
234 | fds=alloca(sizeof(*fds)*total_nfds); | |
235 | if (!fds) { | |
236 | fatal("run: couldn't alloca\n"); | |
237 | } | |
238 | ||
239 | while (!finished) { | |
240 | if (gettimeofday(&tv_now, NULL)!=0) { | |
241 | fatal_perror("main loop: gettimeofday"); | |
242 | } | |
243 | now=(tv_now.tv_sec*1000)+(tv_now.tv_usec/1000); | |
244 | idx=0; | |
245 | for (i=reg; i; i=i->next) { | |
246 | i->after(i->state, fds+idx, i->nfds, &tv_now, &now); | |
247 | idx+=i->nfds; | |
248 | } | |
249 | remain=total_nfds; | |
250 | idx=0; | |
251 | timeout=-1; | |
252 | for (i=reg; i; i=i->next) { | |
253 | nfds=remain; | |
254 | rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now); | |
255 | if (rv!=0) { | |
256 | /* XXX we need to handle this properly: increase the | |
257 | nfds available */ | |
258 | fatal("run: beforepoll_fn (%s) returns %d\n",i->desc,rv); | |
259 | } | |
260 | if (timeout<-1) { | |
261 | fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout); | |
262 | } | |
263 | idx+=nfds; | |
264 | remain-=nfds; | |
265 | i->nfds=nfds; | |
266 | } | |
267 | do { | |
268 | rv=poll(fds, idx, timeout); | |
269 | if (rv<0) { | |
270 | if (errno!=EINTR) { | |
271 | fatal_perror("run: poll"); | |
272 | } | |
273 | } | |
274 | } while (rv<0); | |
275 | } | |
276 | } | |
277 | ||
278 | static void droppriv(void) | |
279 | { | |
280 | FILE *pf=NULL; | |
281 | pid_t p; | |
282 | ||
283 | add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL); | |
284 | ||
285 | /* Background now, if we're supposed to: we may be unable to write the | |
286 | pidfile if we don't. */ | |
287 | if (background) { | |
288 | printf("goto background\n"); | |
289 | /* Open the pidfile before forking - that way the parent can tell | |
290 | whether it succeeds */ | |
291 | if (pidfile) { | |
292 | pf=fopen(pidfile,"w"); | |
293 | if (!pf) { | |
294 | fatal_perror("cannot open pidfile \"%s\"",pidfile); | |
295 | } | |
296 | } else { | |
297 | Message(M_WARNING,"secnet: no pidfile configured, but " | |
298 | "backgrounding anyway\n"); | |
299 | } | |
300 | p=fork(); | |
301 | if (p>0) { | |
302 | if (pf) { | |
303 | /* Parent process - write pidfile, exit */ | |
304 | fprintf(pf,"%d\n",p); | |
305 | fclose(pf); | |
306 | } | |
307 | exit(0); | |
308 | } else if (p==0) { | |
309 | /* Child process - all done, just carry on */ | |
310 | if (pf) fclose(pf); | |
311 | printf("child\n"); | |
312 | } else { | |
313 | /* Error */ | |
314 | fatal_perror("cannot fork"); | |
315 | exit(1); | |
316 | } | |
317 | } else { | |
318 | if (pidfile) { | |
319 | pf=fopen(pidfile,"w"); | |
320 | if (!pf) { | |
321 | fatal_perror("cannot open pidfile \"%s\"",pidfile); | |
322 | } | |
323 | fprintf(pf,"%d\n",getpid()); | |
324 | fclose(pf); | |
325 | } | |
326 | } | |
327 | ||
328 | /* Drop privilege now, if configured to do so */ | |
329 | if (uid!=0) { | |
330 | if (setuid(uid)!=0) { | |
331 | fatal_perror("can't set uid to \"%s\"",userid); | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
336 | int main(int argc, char **argv) | |
337 | { | |
338 | dict_t *config; | |
339 | ||
340 | enter_phase(PHASE_GETOPTS); | |
341 | parse_options(argc,argv); | |
342 | ||
343 | enter_phase(PHASE_READCONFIG); | |
344 | config=read_conffile(configfile); | |
345 | ||
346 | enter_phase(PHASE_SETUP); | |
347 | setup(config); | |
348 | ||
349 | enter_phase(PHASE_DROPPRIV); | |
350 | droppriv(); | |
351 | ||
352 | enter_phase(PHASE_RUN); | |
353 | run(); | |
354 | ||
355 | enter_phase(PHASE_SHUTDOWN); | |
356 | ||
357 | return 0; | |
358 | } | |
359 |