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