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