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