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