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