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