]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
added new type logtype_afpd
[netatalk.git] / etc / afpd / main.c
1 /*
2  * $Id: main.c,v 1.18 2002-03-24 01:23:41 sibaz Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif /* HAVE_UNISTD_H */
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif /* HAVE_FCNTL_H */
21 #include <signal.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/param.h>
26 #include <sys/uio.h>
27 #include <atalk/logger.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30
31 #include <errno.h>
32
33 #include <netatalk/endian.h>
34 #include <netatalk/at.h>
35 #include <atalk/compat.h>
36 #include <atalk/dsi.h>
37 #include <atalk/atp.h>
38 #include <atalk/asp.h>
39 #include <atalk/afp.h>
40 #include <atalk/adouble.h>
41 #include <atalk/paths.h>
42 #include <atalk/util.h>
43 #include <atalk/server_child.h>
44
45 #include "globals.h"
46 #include "afp_config.h"
47 #include "status.h"
48 #include "fork.h"
49 #include "uam_auth.h"
50
51 #ifdef TRU64
52 #include <sys/security.h>
53 #include <prot.h>
54 #include <sia.h>
55
56 static int argc = 0;
57 static char **argv = NULL;
58 #endif /* TRU64 */
59
60 #ifdef DID_MTAB
61 #include "parse_mtab.h"
62 #endif /* DID_MTAB */
63
64 unsigned char   nologin = 0;
65
66 #ifdef DID_MTAB
67 /* global mount table; afpd_st_cnid uses this to lookup the right entry.  */
68 static struct afpd_mount_table *afpd_mount_table = NULL;
69 #endif /* DID_MTAB */
70
71 struct afp_options default_options;
72 static AFPConfig *configs;
73 static server_child *server_children;
74 static fd_set save_rfds;
75
76 #ifdef TRU64
77 void afp_get_cmdline( int *ac, char ***av)
78 {
79     *ac = argc;
80     *av = argv;
81 }
82 #endif /* TRU64 */
83
84 static void afp_exit(const int i)
85 {
86     server_unlock(default_options.pidfile);
87     exit(i);
88 }
89
90 static void afp_goaway(int sig)
91 {
92 #ifndef NO_DDP
93     asp_kill(sig);
94 #endif /* ! NO_DDP */
95     dsi_kill(sig);
96     switch( sig ) {
97     case SIGTERM :
98         LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
99         break;
100     case SIGHUP :
101         /* w/ a configuration file, we can force a re-read if we want */
102         nologin++;
103         if ((nologin + 1) & 1) {
104             AFPConfig *config;
105
106             LOG(log_info, logtype_afpd, "re-reading configuration file");
107             for (config = configs; config; config = config->next)
108                 if (config->server_cleanup)
109                     config->server_cleanup(config);
110
111             configfree(configs, NULL);
112             if (!(configs = configinit(&default_options))) {
113                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
114                 afp_exit(1);
115             }
116             FD_ZERO(&save_rfds);
117             for (config = configs; config; config = config->next) {
118                 if (config->fd < 0)
119                     continue;
120                 FD_SET(config->fd, &save_rfds);
121             }
122         } else {
123             LOG(log_info, logtype_afpd, "disallowing logins");
124             auth_unload();
125         }
126         break;
127     default :
128         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
129     }
130     if ( sig == SIGTERM ) {
131         AFPConfig *config;
132
133         for (config = configs; config; config = config->next)
134             if (config->server_cleanup)
135                 config->server_cleanup(config);
136
137         afp_exit(0);
138     }
139     return;
140 }
141
142 static void child_handler()
143 {
144     server_child_handler(server_children);
145 }
146
147 int main( ac, av )
148 int             ac;
149 char    **av;
150 {
151     AFPConfig           *config;
152     fd_set              rfds;
153     struct sigaction    sv;
154     sigset_t            sigs;
155
156 #ifdef TRU64
157     argc = ac;
158     argv = av;
159     set_auth_parameters( ac, av );
160 #endif /* TRU64 */
161
162     afp_options_init(&default_options);
163     if (!afp_options_parse(ac, av, &default_options))
164         exit(1);
165
166     umask( default_options.umask );
167
168     switch(server_lock("afpd", default_options.pidfile,
169                        default_options.flags & OPTION_DEBUG)) {
170     case -1: /* error */
171         exit(1);
172     case 0: /* child */
173         break;
174     default: /* server */
175         exit(0);
176     }
177
178 #ifdef DID_MTAB
179     /* if we are going to use afpd.mtab, load the file */
180     afpd_mount_table = afpd_mtab_parse ( AFPD_MTAB_FILE );
181 #endif /* DID_MTAB */
182
183     /* install child handler for asp and dsi. we do this before afp_goaway
184      * as afp_goaway references stuff from here. 
185      * XXX: this should really be setup after the initial connections. */
186     if (!(server_children = server_child_alloc(default_options.connections,
187                             CHILD_NFORKS))) {
188         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
189         afp_exit(1);
190     }
191
192     memset(&sv, 0, sizeof(sv));
193     sv.sa_handler = child_handler;
194     sigemptyset( &sv.sa_mask );
195     sv.sa_flags = SA_RESTART;
196     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
197         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
198         afp_exit(1);
199     }
200
201     sv.sa_handler = afp_goaway;
202     sigemptyset( &sv.sa_mask );
203     sigaddset(&sv.sa_mask, SIGHUP);
204     sigaddset(&sv.sa_mask, SIGTERM);
205     sv.sa_flags = SA_RESTART;
206     if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
207         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
208         afp_exit(1);
209     }
210     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
211         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
212         afp_exit(1);
213     }
214
215     /* afpd.conf: not in config file: lockfile, connections, configfile
216      *            preference: command-line provides defaults.
217      *                        config file over-writes defaults.
218      *
219      * we also need to make sure that killing afpd during startup
220      * won't leave any lingering registered names around.
221      */
222
223     sigemptyset(&sigs);
224     sigaddset(&sigs, SIGHUP);
225     sigaddset(&sigs, SIGTERM);
226     sigprocmask(SIG_BLOCK, &sigs, NULL);
227     if (!(configs = configinit(&default_options))) {
228         LOG(log_error, logtype_afpd, "main: no servers configured: %s\n", strerror(errno));
229         afp_exit(1);
230     }
231     sigprocmask(SIG_UNBLOCK, &sigs, NULL);
232
233     /* watch atp and dsi sockets. */
234     FD_ZERO(&save_rfds);
235     for (config = configs; config; config = config->next) {
236         if (config->fd < 0) /* for proxies */
237             continue;
238         FD_SET(config->fd, &save_rfds);
239     }
240
241     /* wait for an appleshare connection. parent remains in the loop
242      * while the children get handled by afp_over_{asp,dsi}.  this is
243      * currently vulnerable to a denial-of-service attack if a
244      * connection is made without an actual login attempt being made
245      * afterwards. establishing timeouts for logins is a possible 
246      * solution. */
247     while (1) {
248         rfds = save_rfds;
249         if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
250             if (errno == EINTR)
251                 continue;
252             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
253             break;
254         }
255
256         for (config = configs; config; config = config->next) {
257             if (config->fd < 0)
258                 continue;
259             if (FD_ISSET(config->fd, &rfds))
260                 config->server_start(config, configs, server_children);
261         }
262     }
263
264     return 0;
265 }