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