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