]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Primary reconnect
[netatalk.git] / etc / afpd / main.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <signal.h>
14
15 #include <sys/param.h>
16 #include <sys/uio.h>
17 #include <atalk/logger.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <sys/poll.h>
21 #include <errno.h>
22 #include <sys/wait.h>
23
24 #include <atalk/adouble.h>
25
26 #include <netatalk/at.h>
27 #include <atalk/compat.h>
28 #include <atalk/dsi.h>
29 #include <atalk/atp.h>
30 #include <atalk/asp.h>
31 #include <atalk/afp.h>
32 #include <atalk/paths.h>
33 #include <atalk/util.h>
34 #include <atalk/server_child.h>
35 #include <atalk/server_ipc.h>
36
37 #include "globals.h"
38 #include "afp_config.h"
39 #include "status.h"
40 #include "fork.h"
41 #include "uam_auth.h"
42 #include "afp_zeroconf.h"
43
44 #ifdef TRU64
45 #include <sys/security.h>
46 #include <prot.h>
47 #include <sia.h>
48
49 static int argc = 0;
50 static char **argv = NULL;
51 #endif /* TRU64 */
52
53 unsigned char   nologin = 0;
54
55 struct afp_options default_options;
56 static AFPConfig *configs;
57 static server_child *server_children;
58 static sig_atomic_t reloadconfig = 0;
59
60 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
61 static struct pollfd *fdset;
62 static struct polldata *polldata;
63 static int fdset_size;          /* current allocated size */
64 static int fdset_used;          /* number of used elements */
65
66
67 #ifdef TRU64
68 void afp_get_cmdline( int *ac, char ***av)
69 {
70     *ac = argc;
71     *av = argv;
72 }
73 #endif /* TRU64 */
74
75 /* This is registered with atexit() */
76 static void afp_exit(void)
77 {
78     if (parent_or_child == 0)
79         /* Only do this in the parent */
80         server_unlock(default_options.pidfile);
81 }
82
83
84 /* ------------------
85    initialize fd set we are waiting for.
86 */
87 static void fd_set_listening_sockets(void)
88 {
89     AFPConfig   *config;
90
91     for (config = configs; config; config = config->next) {
92         if (config->fd < 0) /* for proxies */
93             continue;
94         fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
95     }
96 }
97  
98 static void fd_reset_listening_sockets(void)
99 {
100     AFPConfig   *config;
101
102     for (config = configs; config; config = config->next) {
103         if (config->fd < 0) /* for proxies */
104             continue;
105         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
106     }
107     fd_set_listening_sockets();
108 }
109
110 /* ------------------ */
111 static void afp_goaway(int sig)
112 {
113
114 #ifndef NO_DDP
115     asp_kill(sig);
116 #endif /* ! NO_DDP */
117
118     if (server_children)
119         server_child_kill(server_children, CHILD_DSIFORK, sig);
120
121     switch( sig ) {
122
123     case SIGTERM :
124         LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
125         AFPConfig *config;
126         for (config = configs; config; config = config->next)
127             if (config->server_cleanup)
128                 config->server_cleanup(config);
129         server_unlock(default_options.pidfile);
130         exit(0);
131         break;
132
133     case SIGUSR1 :
134         nologin++;
135         auth_unload();
136         LOG(log_info, logtype_afpd, "disallowing logins");        
137         break;
138
139     case SIGHUP :
140         /* w/ a configuration file, we can force a re-read if we want */
141         reloadconfig = 1;
142         break;
143
144     default :
145         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
146     }
147     return;
148 }
149
150 static void child_handler(int sig _U_)
151 {
152     int fd;
153     int status, i;
154     pid_t pid;
155   
156 #ifndef WAIT_ANY
157 #define WAIT_ANY (-1)
158 #endif /* ! WAIT_ANY */
159
160     while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
161         for (i = 0; i < server_children->nforks; i++) {
162             if ((fd = server_child_remove(server_children, i, pid)) != -1) {
163                 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);        
164                 break;
165             }
166         }
167
168         if (WIFEXITED(status)) {
169             if (WEXITSTATUS(status))
170                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
171             else
172                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
173         } else {
174             if (WIFSIGNALED(status))
175                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
176             else
177                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
178         }
179     }
180 }
181
182 int main(int ac, char **av)
183 {
184     AFPConfig           *config;
185     fd_set              rfds;
186     void                *ipc;
187     struct sigaction    sv;
188     sigset_t            sigs;
189     int                 ret;
190
191 #ifdef TRU64
192     argc = ac;
193     argv = av;
194     set_auth_parameters( ac, av );
195 #endif /* TRU64 */
196
197     /* Log SIGBUS/SIGSEGV SBT */
198     fault_setup(NULL);
199
200     /* Default log setup: log to syslog */
201     setuplog("default log_note");
202
203     afp_options_init(&default_options);
204     if (!afp_options_parse(ac, av, &default_options))
205         exit(EXITERR_CONF);
206
207     /* Save the user's current umask for use with CNID (and maybe some 
208      * other things, too). */
209     default_options.save_mask = umask( default_options.umask );
210
211     switch(server_lock("afpd", default_options.pidfile,
212                        default_options.flags & OPTION_DEBUG)) {
213     case -1: /* error */
214         exit(EXITERR_SYS);
215     case 0: /* child */
216         break;
217     default: /* server */
218         exit(0);
219     }
220     atexit(afp_exit);
221
222     /* install child handler for asp and dsi. we do this before afp_goaway
223      * as afp_goaway references stuff from here. 
224      * XXX: this should really be setup after the initial connections. */
225     if (!(server_children = server_child_alloc(default_options.connections,
226                             CHILD_NFORKS))) {
227         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
228         exit(EXITERR_SYS);
229     }
230
231     memset(&sv, 0, sizeof(sv));    
232 #ifdef AFP3x
233     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
234        even if the file is open with O_LARGEFILE ! */
235 #ifdef SIGXFSZ
236     sv.sa_handler = SIG_IGN;
237     sigemptyset( &sv.sa_mask );
238     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
239         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
240         exit(EXITERR_SYS);
241     }
242 #endif
243 #endif
244     
245     sv.sa_handler = child_handler;
246     sigemptyset( &sv.sa_mask );
247     sigaddset(&sv.sa_mask, SIGALRM);
248     sigaddset(&sv.sa_mask, SIGHUP);
249     sigaddset(&sv.sa_mask, SIGTERM);
250     sigaddset(&sv.sa_mask, SIGUSR1);
251     
252     sv.sa_flags = SA_RESTART;
253     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
254         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
255         exit(EXITERR_SYS);
256     }
257
258     sv.sa_handler = afp_goaway;
259     sigemptyset( &sv.sa_mask );
260     sigaddset(&sv.sa_mask, SIGALRM);
261     sigaddset(&sv.sa_mask, SIGTERM);
262     sigaddset(&sv.sa_mask, SIGHUP);
263     sigaddset(&sv.sa_mask, SIGCHLD);
264     sv.sa_flags = SA_RESTART;
265     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
266         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
267         exit(EXITERR_SYS);
268     }
269
270     sigemptyset( &sv.sa_mask );
271     sigaddset(&sv.sa_mask, SIGALRM);
272     sigaddset(&sv.sa_mask, SIGTERM);
273     sigaddset(&sv.sa_mask, SIGUSR1);
274     sigaddset(&sv.sa_mask, SIGCHLD);
275     sv.sa_flags = SA_RESTART;
276     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
277         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
278         exit(EXITERR_SYS);
279     }
280
281
282     sigemptyset( &sv.sa_mask );
283     sigaddset(&sv.sa_mask, SIGALRM);
284     sigaddset(&sv.sa_mask, SIGHUP);
285     sigaddset(&sv.sa_mask, SIGUSR1);
286     sigaddset(&sv.sa_mask, SIGCHLD);
287     sv.sa_flags = SA_RESTART;
288     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
289         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
290         exit(EXITERR_SYS);
291     }
292
293     /* afpd.conf: not in config file: lockfile, connections, configfile
294      *            preference: command-line provides defaults.
295      *                        config file over-writes defaults.
296      *
297      * we also need to make sure that killing afpd during startup
298      * won't leave any lingering registered names around.
299      */
300
301     sigemptyset(&sigs);
302     sigaddset(&sigs, SIGALRM);
303     sigaddset(&sigs, SIGHUP);
304     sigaddset(&sigs, SIGUSR1);
305 #if 0
306     /* don't block SIGTERM */
307     sigaddset(&sigs, SIGTERM);
308 #endif
309     sigaddset(&sigs, SIGCHLD);
310
311     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
312     if (!(configs = configinit(&default_options))) {
313         LOG(log_error, logtype_afpd, "main: no servers configured");
314         exit(EXITERR_CONF);
315     }
316     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
317
318     /* Register CNID  */
319     cnid_init();
320
321     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
322     fd_set_listening_sockets();
323
324     afp_child_t *child;
325
326     /* wait for an appleshare connection. parent remains in the loop
327      * while the children get handled by afp_over_{asp,dsi}.  this is
328      * currently vulnerable to a denial-of-service attack if a
329      * connection is made without an actual login attempt being made
330      * afterwards. establishing timeouts for logins is a possible 
331      * solution. */
332     while (1) {
333         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
334         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
335         ret = poll(fdset, fdset_used, -1);
336         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
337         int saveerrno = errno;
338
339         if (reloadconfig) {
340             nologin++;
341             auth_unload();
342
343             LOG(log_info, logtype_afpd, "re-reading configuration file");
344             for (config = configs; config; config = config->next)
345                 if (config->server_cleanup)
346                     config->server_cleanup(config);
347
348             /* configfree close atp socket used for DDP tickle, there's an issue
349              * with atp tid. */
350             configfree(configs, NULL);
351             if (!(configs = configinit(&default_options))) {
352                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
353                 exit(EXITERR_CONF);
354             }
355             fd_reset_listening_sockets();
356             nologin = 0;
357             reloadconfig = 0;
358             errno = saveerrno;
359         }
360
361         if (ret == 0)
362             continue;
363         
364         if (ret < 0) {
365             if (errno == EINTR)
366                 continue;
367             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
368             break;
369         }
370
371         for (int i = 0; i < fdset_used; i++) {
372             if (fdset[i].revents & POLLIN) {
373                 switch (polldata[i].fdtype) {
374                 case LISTEN_FD:
375                     config = (AFPConfig *)polldata[i].data;
376                     /* config->server_start is afp_config.c:dsi_start() for DSI */
377                     if (child = config->server_start(config, configs, server_children)) {
378                         /* Add IPC fd to select fd set */
379                         fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
380                     }
381                     break;
382                 case IPC_FD:
383                     child = (afp_child_t *)polldata[i].data;
384                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
385                     if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
386                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
387                         close(child->ipc_fds[0]);
388                         child->ipc_fds[0] = -1;
389                     }
390                     break;
391                 default:
392                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
393                     break;
394                 } /* switch */
395             }  /* if */
396         } /* for (i)*/
397     } /* while (1) */
398
399     return 0;
400 }