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