Options work
[yaz-moved-to-github.git] / server / statserv.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: statserv.c,v $
7  * Revision 1.35  1996-05-29 10:03:28  quinn
8  * Options work
9  *
10  * Revision 1.34  1996/02/21  13:12:07  quinn
11  * *** empty log message ***
12  *
13  * Revision 1.33  1996/02/10  12:23:49  quinn
14  * Enable inetd operations fro TCP/IP stack
15  *
16  * Revision 1.32  1996/01/19  15:41:52  quinn
17  * *** empty log message ***
18  *
19  * Revision 1.31  1995/11/17  11:09:39  adam
20  * Added new option '-c' to specify configuration name in control block.
21  *
22  * Revision 1.30  1995/11/01  13:54:59  quinn
23  * Minor adjustments
24  *
25  * Revision 1.29  1995/10/30  12:41:29  quinn
26  * Added hostname lookup for server.
27  *
28  * Revision 1.28  1995/09/29  17:12:30  quinn
29  * Smallish
30  *
31  * Revision 1.27  1995/09/27  15:03:02  quinn
32  * Modified function heads & prototypes.
33  *
34  * Revision 1.26  1995/08/29  14:44:51  quinn
35  * Reset timeouts.
36  *
37  * Revision 1.25  1995/08/29  11:18:02  quinn
38  * Added code to receive close
39  *
40  * Revision 1.24  1995/06/16  10:31:39  quinn
41  * Added session timeout.
42  *
43  * Revision 1.23  1995/06/15  12:30:48  quinn
44  * Setuid-facility.
45  *
46  * Revision 1.22  1995/06/15  07:45:17  quinn
47  * Moving to v3.
48  *
49  * Revision 1.21  1995/06/06  08:15:40  quinn
50  * Cosmetic.
51  *
52  * Revision 1.20  1995/05/29  08:12:09  quinn
53  * Moved oid to util
54  *
55  * Revision 1.19  1995/05/16  09:37:27  quinn
56  * Fixed bug
57  *
58  * Revision 1.18  1995/05/16  08:51:09  quinn
59  * License, documentation, and memory fixes
60  *
61  * Revision 1.17  1995/05/15  11:56:42  quinn
62  * Asynchronous facilities. Restructuring of seshigh code.
63  *
64  * Revision 1.16  1995/04/10  10:23:40  quinn
65  * Some work to add scan and other things.
66  *
67  * Revision 1.15  1995/03/31  10:16:51  quinn
68  * Fixed logging.
69  *
70  * Revision 1.14  1995/03/31  09:18:58  quinn
71  * Added logging.
72  *
73  * Revision 1.13  1995/03/30  16:08:39  quinn
74  * Little mods.
75  *
76  * Revision 1.12  1995/03/30  13:29:02  quinn
77  * Smallish
78  *
79  * Revision 1.11  1995/03/30  12:18:17  quinn
80  * Fixed bug.
81  *
82  * Revision 1.10  1995/03/29  15:40:16  quinn
83  * Ongoing work. Statserv is now dynamic by default
84  *
85  * Revision 1.9  1995/03/27  08:34:30  quinn
86  * Added dynamic server functionality.
87  * Released bindings to session.c (is now redundant)
88  *
89  * Revision 1.8  1995/03/20  09:46:26  quinn
90  * Added osi support.
91  *
92  * Revision 1.7  1995/03/16  13:29:04  quinn
93  * Partitioned server.
94  *
95  * Revision 1.6  1995/03/15  15:18:52  quinn
96  * Little changes to better support nonblocking I/O
97  * Added backend.h
98  *
99  * Revision 1.5  1995/03/15  08:37:45  quinn
100  * Now we're pretty much set for nonblocking I/O.
101  *
102  * Revision 1.4  1995/03/14  16:59:48  quinn
103  * Bug-fixes
104  *
105  * Revision 1.3  1995/03/14  11:30:15  quinn
106  * Works better now.
107  *
108  * Revision 1.2  1995/03/14  10:28:03  quinn
109  * More work on demo server.
110  *
111  * Revision 1.1  1995/03/10  18:22:45  quinn
112  * The rudiments of an asynchronous server.
113  *
114  */
115
116 /*
117  * Simple, static server. I wouldn't advise a static server unless you
118  * really have to, but it's great for debugging memory management.  :)
119  */
120
121 #include <stdio.h>
122 #include <unistd.h>
123 #include <fcntl.h>
124 #include <sys/wait.h>
125 #include <signal.h>
126 #include <errno.h>
127 #include <sys/types.h>
128 #include <pwd.h>
129 #include <sys/time.h>
130
131 #include <options.h>
132 #include <eventl.h>
133 #include <session.h>
134 #include <eventl.h>
135 #include <comstack.h>
136 #include <tcpip.h>
137 #ifdef USE_XTIMOSI
138 #include <xmosi.h>
139 #endif
140 #include <log.h>
141 #include <statserv.h>
142
143 static char *me = "statserver";
144 /*
145  * default behavior.
146  */
147 static statserv_options_block control_block = {
148     1,                          /* dynamic mode */
149     LOG_DEFAULT_LEVEL,          /* log level */
150     "",                         /* no PDUs */
151     "",                         /* diagnostic output to stderr */
152     "tcp:@:9999",               /* default listener port */
153     PROTO_Z3950,                /* default application protocol */
154     60,                         /* idle timeout (minutes) */
155     1024*1024,                  /* maximum PDU size (approx.) to allow */
156     "default-config",           /* configuration name to pass to backend */
157     ""                          /* set user id */
158 };
159
160 /*
161  * handle incoming connect requests.
162  * The dynamic mode is a bit tricky mostly because we want to avoid
163  * doing all of the listening and accepting in the parent - it's
164  * safer that way.
165  */
166 static void listener(IOCHAN h, int event)
167 {
168     COMSTACK line = (COMSTACK) iochan_getdata(h);
169     association *newas;
170     static int hand[2];
171     static int child = 0;
172     int res;
173
174     if (event == EVENT_INPUT)
175     {
176         if (control_block.dynamic && !child) 
177         {
178             int res;
179
180             if (pipe(hand) < 0)
181             {
182                 logf(LOG_FATAL|LOG_ERRNO, "pipe");
183                 exit(1);
184             }
185             if ((res = fork()) < 0)
186             {
187                 logf(LOG_FATAL|LOG_ERRNO, "fork");
188                 exit(1);
189             }
190             else if (res == 0) /* child */
191             {
192                 char nbuf[100];
193                 IOCHAN pp;
194
195                 close(hand[0]);
196                 child = 1;
197                 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
198                 {
199                     if (pp != h)
200                     {
201                         COMSTACK l = iochan_getdata(pp);
202                         cs_close(l);
203                         iochan_destroy(pp);
204                     }
205                 }
206                 sprintf(nbuf, "%s(%d)", me, getpid());
207                 log_init(control_block.loglevel, nbuf, 0);
208             }
209             else /* parent */
210             {
211                 close(hand[1]);
212                 /* wait for child to take the call */
213                 for (;;)
214                 {
215                     char dummy[1];
216                     int res;
217                     
218                     if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
219                     {
220                         logf(LOG_FATAL|LOG_ERRNO, "handshake read");
221                         exit(1);
222                     }
223                     else if (res >= 0)
224                         break;
225                 }
226                 logf(LOG_DEBUG, "P: Child has taken the call");
227                 close(hand[0]);
228                 return;
229             }
230         }
231         if ((res = cs_listen(line, 0, 0)) < 0)
232         {
233             logf(LOG_FATAL, "cs_listen failed.");
234             return;
235         }
236         else if (res == 1)
237             return;
238         logf(LOG_DEBUG, "listen ok");
239         iochan_setevent(h, EVENT_OUTPUT);
240         iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
241     }
242     /* in dynamic mode, only the child ever comes down here */
243     else if (event == EVENT_OUTPUT)
244     {
245         COMSTACK new_line;
246         IOCHAN new_chan;
247         char *a;
248
249         if (!(new_line = cs_accept(line)))
250         {
251             logf(LOG_FATAL, "Accept failed.");
252             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
253             return;
254         }
255         logf(LOG_DEBUG, "accept ok");
256         if (control_block.dynamic)
257         {
258             IOCHAN pp;
259             /* close our half of the listener socket */
260             for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
261             {
262                 COMSTACK l = iochan_getdata(pp);
263                 cs_close(l);
264                 iochan_destroy(pp);
265             }
266             /* release dad */
267             logf(LOG_DEBUG, "Releasing parent");
268             close(hand[1]);
269         }
270         else
271             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
272
273         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
274             EVENT_INPUT)))
275         {
276             logf(LOG_FATAL, "Failed to create iochan");
277             exit(1);
278         }
279         if (!(newas = create_association(new_chan, new_line)))
280         {
281             logf(LOG_FATAL, "Failed to create new assoc.");
282             exit(1);
283         }
284         iochan_setdata(new_chan, newas);
285         iochan_settimeout(new_chan, control_block.idle_timeout * 60);
286         a = cs_addrstr(new_line);
287         logf(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
288     }
289     else
290     {
291         logf(LOG_FATAL, "Bad event on listener.");
292         exit(1);
293     }
294 }
295
296 static void inetd_connection(int what)
297 {
298     COMSTACK line;
299     IOCHAN chan;
300     association *assoc;
301     char *addr;
302
303     if (!(line = cs_createbysocket(0, tcpip_type, 0, what)))
304     {
305         logf(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
306         exit(1);
307     }
308     if (!(chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
309     {
310         logf(LOG_FATAL, "Failed to create iochan");
311         exit(1);
312     }
313     if (!(assoc = create_association(chan, line)))
314     {
315         logf(LOG_FATAL, "Failed to create association structure");
316         exit(1);
317     }
318     iochan_setdata(chan, assoc);
319     iochan_settimeout(chan, control_block.idle_timeout * 60);
320     addr = cs_addrstr(line);
321     logf(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
322 }
323
324 /*
325  * Set up a listening endpoint, and give it to the event-handler.
326  */
327 static void add_listener(char *where, int what)
328 {
329     COMSTACK l;
330     CS_TYPE type;
331     char mode[100], addr[100];
332     void *ap;
333     IOCHAN lst;
334
335     if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
336     {
337         fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
338             me);
339         exit(1);
340     }
341     if (!strcmp(mode, "tcp"))
342     {
343         if (!(ap = tcpip_strtoaddr(addr)))
344         {
345             fprintf(stderr, "Address resolution failed for TCP.\n");
346             exit(1);
347         }
348         type = tcpip_type;
349     }
350     else if (!strcmp(mode, "osi"))
351     {
352 #ifdef USE_XTIMOSI
353         if (!(ap = mosi_strtoaddr(addr)))
354         {
355             fprintf(stderr, "Address resolution failed for TCP.\n");
356             exit(1);
357         }
358         type = mosi_type;
359 #else
360         fprintf(stderr, "OSI Transport not allowed by configuration.\n");
361         exit(1);
362 #endif
363     }
364     else
365     {
366         fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
367         exit(1);
368     }
369     logf(LOG_LOG, "Adding %s %s listener on %s",
370         control_block.dynamic ? "dynamic" : "static",
371         what == PROTO_SR ? "SR" : "Z3950", where);
372     if (!(l = cs_create(type, 0, what)))
373     {
374         logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
375         exit(1);
376     }
377     if (cs_bind(l, ap, CS_SERVER) < 0)
378     {
379         logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
380         exit(1);
381     }
382     if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
383          EVENT_EXCEPT)))
384     {
385         logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
386         exit(1);
387     }
388     iochan_setdata(lst, l);
389 }
390
391 static void catchchld(int num)
392 {
393     while (waitpid(-1, 0, WNOHANG) > 0)
394         ;
395     signal(SIGCHLD, catchchld);
396 }
397
398 statserv_options_block *statserv_getcontrol(void)
399 {
400     static statserv_options_block cb;
401
402     memcpy(&cb, &control_block, sizeof(cb));
403     return &cb;
404 }
405
406 void statserv_setcontrol(statserv_options_block *block)
407 {
408     memcpy(&control_block, block, sizeof(*block));
409 }
410
411 int statserv_main(int argc, char **argv)
412 {
413     int ret, listeners = 0, inetd = 0, r;
414     char *arg;
415     int protocol = control_block.default_proto;
416
417     me = argv[0];
418     while ((ret = options("a:iszSl:v:u:c:w:t:k:", argv, argc, &arg)) != -2)
419     {
420         switch (ret)
421         {
422             case 0:
423                 add_listener(arg, protocol);
424                 listeners++;
425                 break;
426             case 'z': protocol = PROTO_Z3950; break;
427             case 's': protocol = PROTO_SR; break;
428             case 'S': control_block.dynamic = 0; break;
429             case 'l':
430                 strcpy(control_block.logfile, arg ? arg : "");
431                 log_init(control_block.loglevel, me, control_block.logfile);
432                 break;
433             case 'v':
434                 control_block.loglevel = log_mask_str(arg);
435                 log_init(control_block.loglevel, me, control_block.logfile);
436                 break;
437             case 'a':
438                 strcpy(control_block.apdufile, arg ? arg : ""); break;
439             case 'u':
440                 strcpy(control_block.setuid, arg ? arg : ""); break;
441             case 'c':
442                 strcpy(control_block.configname, arg ? arg : ""); break;
443             case 't':
444                 if (!arg || !(r = atoi(arg)))
445                 {
446                     fprintf(stderr, "%s: Specify positive timeout for -t.\n",
447                         me);
448                     exit(1);
449                 }
450                 control_block.idle_timeout = r;
451                 break;
452             case  'k':
453                 if (!arg || !(r = atoi(arg)))
454                 {
455                     fprintf(stderr, "%s: Specify positive timeout for -t.\n",
456                         me);
457                     exit(1);
458                 }
459                 control_block.maxrecordsize = r * 1024;
460                 break;
461             case 'i':
462                 inetd = 1; break;
463             case 'w':
464                 if (chdir(arg))
465                 {
466                     perror(arg);
467                     exit(1);
468                 }
469                 break;
470             default:
471                 fprintf(stderr, "Usage: %s [ -i -a <pdufile> -v <loglevel>"
472                         " -l <logfile> -u <user> -c <config> -t <minutes>"
473                         " -k <kilobytes>"
474                         " -zsS <listener-addr> -w <directory> ... ]\n", me);
475                 exit(1);
476             }
477     }
478     if (inetd)
479         inetd_connection(protocol);
480     else
481     {
482         if (control_block.dynamic)
483             signal(SIGCHLD, catchchld);
484         if (!listeners && *control_block.default_listen)
485             add_listener(control_block.default_listen, protocol);
486     }
487     if (*control_block.setuid)
488     {
489         struct passwd *pw;
490         
491         if (!(pw = getpwnam(control_block.setuid)))
492         {
493             logf(LOG_FATAL, "%s: Unknown user", control_block.setuid);
494             exit(1);
495         }
496         if (setuid(pw->pw_uid) < 0)
497         {
498             logf(LOG_FATAL|LOG_ERRNO, "setuid");
499             exit(1);
500         }
501     }
502     logf(LOG_LOG, "Entering event loop.");
503             
504     return event_loop();
505 }