]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Use function from 2-1 for local uuid generation
[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
21 #include <errno.h>
22
23 #include <atalk/adouble.h>
24
25 #include <netatalk/at.h>
26 #include <atalk/compat.h>
27 #include <atalk/dsi.h>
28 #include <atalk/atp.h>
29 #include <atalk/asp.h>
30 #include <atalk/afp.h>
31 #include <atalk/paths.h>
32 #include <atalk/util.h>
33 #include <atalk/server_child.h>
34 #include <atalk/server_ipc.h>
35
36 #include "globals.h"
37 #include "afp_config.h"
38 #include "status.h"
39 #include "fork.h"
40 #include "uam_auth.h"
41 #include "afp_zeroconf.h"
42
43 #ifdef TRU64
44 #include <sys/security.h>
45 #include <prot.h>
46 #include <sia.h>
47
48 static int argc = 0;
49 static char **argv = NULL;
50 #endif /* TRU64 */
51
52 unsigned char   nologin = 0;
53
54 struct afp_options default_options;
55 static AFPConfig *configs;
56 static server_child *server_children;
57 static fd_set save_rfds;
58 static int    Ipc_fd = -1;
59 static sig_atomic_t reloadconfig = 0;
60
61 #ifdef TRU64
62 void afp_get_cmdline( int *ac, char ***av)
63 {
64     *ac = argc;
65     *av = argv;
66 }
67 #endif /* TRU64 */
68
69 static void afp_exit(const int i)
70 {
71     server_unlock(default_options.pidfile);
72     exit(i);
73 }
74
75 /* ------------------
76    initialize fd set we are waiting for.
77 */
78 static void set_fd(int ipc_fd)
79 {
80     AFPConfig   *config;
81
82     FD_ZERO(&save_rfds);
83     for (config = configs; config; config = config->next) {
84         if (config->fd < 0) /* for proxies */
85             continue;
86         FD_SET(config->fd, &save_rfds);
87     }
88     if (ipc_fd >= 0) {
89         FD_SET(ipc_fd, &save_rfds);
90     }
91 }
92  
93 /* ------------------ */
94 static void afp_goaway(int sig)
95 {
96
97 #ifndef NO_DDP
98     asp_kill(sig);
99 #endif /* ! NO_DDP */
100
101     dsi_kill(sig);
102     switch( sig ) {
103
104     case SIGTERM :
105         LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
106         AFPConfig *config;
107         for (config = configs; config; config = config->next)
108             if (config->server_cleanup)
109                 config->server_cleanup(config);
110         afp_exit(0);
111         break;
112
113     case SIGUSR1 :
114         nologin++;
115         auth_unload();
116         LOG(log_info, logtype_afpd, "disallowing logins");        
117         break;
118
119     case SIGHUP :
120         /* w/ a configuration file, we can force a re-read if we want */
121         reloadconfig = 1;
122         break;
123
124     default :
125         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
126     }
127     return;
128 }
129
130 static void child_handler(int sig _U_)
131 {
132     server_child_handler(server_children);
133 }
134
135 int main(int ac, char **av)
136 {
137     AFPConfig           *config;
138     fd_set              rfds;
139     void                *ipc;
140     struct sigaction    sv;
141     sigset_t            sigs;
142     int                 ret;
143
144 #ifdef TRU64
145     argc = ac;
146     argv = av;
147     set_auth_parameters( ac, av );
148 #endif /* TRU64 */
149
150     /* Log SIGBUS/SIGSEGV SBT */
151     fault_setup(NULL);
152
153     /* Default log setup: log to syslog */
154     setuplog("default log_note");
155
156     afp_options_init(&default_options);
157     if (!afp_options_parse(ac, av, &default_options))
158         exit(EXITERR_CONF);
159
160     /* Save the user's current umask for use with CNID (and maybe some 
161      * other things, too). */
162     default_options.save_mask = umask( default_options.umask );
163
164     switch(server_lock("afpd", default_options.pidfile,
165                        default_options.flags & OPTION_DEBUG)) {
166     case -1: /* error */
167         exit(EXITERR_SYS);
168     case 0: /* child */
169         break;
170     default: /* server */
171         exit(0);
172     }
173
174     /* install child handler for asp and dsi. we do this before afp_goaway
175      * as afp_goaway references stuff from here. 
176      * XXX: this should really be setup after the initial connections. */
177     if (!(server_children = server_child_alloc(default_options.connections,
178                             CHILD_NFORKS))) {
179         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
180         afp_exit(EXITERR_SYS);
181     }
182
183     memset(&sv, 0, sizeof(sv));    
184 #ifdef AFP3x
185     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
186        even if the file is open with O_LARGEFILE ! */
187 #ifdef SIGXFSZ
188     sv.sa_handler = SIG_IGN;
189     sigemptyset( &sv.sa_mask );
190     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
191         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
192         afp_exit(EXITERR_SYS);
193     }
194 #endif
195 #endif
196     
197     sv.sa_handler = child_handler;
198     sigemptyset( &sv.sa_mask );
199     sigaddset(&sv.sa_mask, SIGALRM);
200     sigaddset(&sv.sa_mask, SIGHUP);
201     sigaddset(&sv.sa_mask, SIGTERM);
202     sigaddset(&sv.sa_mask, SIGUSR1);
203     
204     sv.sa_flags = SA_RESTART;
205     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
206         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
207         afp_exit(EXITERR_SYS);
208     }
209
210     sv.sa_handler = afp_goaway;
211     sigemptyset( &sv.sa_mask );
212     sigaddset(&sv.sa_mask, SIGALRM);
213     sigaddset(&sv.sa_mask, SIGTERM);
214     sigaddset(&sv.sa_mask, SIGHUP);
215     sigaddset(&sv.sa_mask, SIGCHLD);
216     sv.sa_flags = SA_RESTART;
217     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
218         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
219         afp_exit(EXITERR_SYS);
220     }
221
222     sigemptyset( &sv.sa_mask );
223     sigaddset(&sv.sa_mask, SIGALRM);
224     sigaddset(&sv.sa_mask, SIGTERM);
225     sigaddset(&sv.sa_mask, SIGUSR1);
226     sigaddset(&sv.sa_mask, SIGCHLD);
227     sv.sa_flags = SA_RESTART;
228     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
229         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
230         afp_exit(EXITERR_SYS);
231     }
232
233
234     sigemptyset( &sv.sa_mask );
235     sigaddset(&sv.sa_mask, SIGALRM);
236     sigaddset(&sv.sa_mask, SIGHUP);
237     sigaddset(&sv.sa_mask, SIGUSR1);
238     sigaddset(&sv.sa_mask, SIGCHLD);
239     sv.sa_flags = SA_RESTART;
240     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
241         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
242         afp_exit(EXITERR_SYS);
243     }
244
245     /* afpd.conf: not in config file: lockfile, connections, configfile
246      *            preference: command-line provides defaults.
247      *                        config file over-writes defaults.
248      *
249      * we also need to make sure that killing afpd during startup
250      * won't leave any lingering registered names around.
251      */
252
253     sigemptyset(&sigs);
254     sigaddset(&sigs, SIGALRM);
255     sigaddset(&sigs, SIGHUP);
256     sigaddset(&sigs, SIGUSR1);
257 #if 0
258     /* don't block SIGTERM */
259     sigaddset(&sigs, SIGTERM);
260 #endif
261     sigaddset(&sigs, SIGCHLD);
262
263     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
264     if (!(configs = configinit(&default_options))) {
265         LOG(log_error, logtype_afpd, "main: no servers configured");
266         afp_exit(EXITERR_CONF);
267     }
268     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
269
270     /* Register CNID  */
271     cnid_init();
272
273     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
274     if ((ipc = server_ipc_create())) {
275         Ipc_fd = server_ipc_parent(ipc);
276     }
277     set_fd(Ipc_fd);
278
279     /* wait for an appleshare connection. parent remains in the loop
280      * while the children get handled by afp_over_{asp,dsi}.  this is
281      * currently vulnerable to a denial-of-service attack if a
282      * connection is made without an actual login attempt being made
283      * afterwards. establishing timeouts for logins is a possible 
284      * solution. */
285     while (1) {
286         rfds = save_rfds;
287         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
288         ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
289         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
290         int saveerrno = errno;
291
292         if (reloadconfig) {
293             nologin++;
294             auth_unload();
295             AFPConfig *config;
296
297             LOG(log_info, logtype_afpd, "re-reading configuration file");
298             for (config = configs; config; config = config->next)
299                 if (config->server_cleanup)
300                     config->server_cleanup(config);
301
302             /* configfree close atp socket used for DDP tickle, there's an issue
303              * with atp tid. */
304             configfree(configs, NULL);
305             if (!(configs = configinit(&default_options))) {
306                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
307                 afp_exit(EXITERR_CONF);
308             }
309             set_fd(Ipc_fd);
310             nologin = 0;
311             reloadconfig = 0;
312             errno = saveerrno;
313         }
314         
315         if (ret < 0) {
316             if (errno == EINTR)
317                 continue;
318             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
319             break;
320         }
321         if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
322             server_ipc_read(server_children);
323         }
324         for (config = configs; config; config = config->next) {
325             if (config->fd < 0)
326                 continue;
327             if (FD_ISSET(config->fd, &rfds)) {
328                 config->server_start(config, configs, server_children);
329             }
330         }
331     }
332
333     return 0;
334 }