]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Sam Noble's ADMIN_GRP patch, removed Marc's old ADMIN_GRP patch.
[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
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <signal.h>
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/param.h>
20 #include <sys/uio.h>
21 #include <sys/syslog.h>
22 #include <sys/time.h>
23 #include <sys/socket.h>
24
25 #include <errno.h>
26
27 #include <netatalk/endian.h>
28 #include <netatalk/at.h>
29 #include <atalk/compat.h>
30 #include <atalk/dsi.h>
31 #include <atalk/atp.h>
32 #include <atalk/asp.h>
33 #include <atalk/afp.h>
34 #include <atalk/adouble.h>
35 #include <atalk/paths.h>
36 #include <atalk/util.h>
37 #include <atalk/server_child.h>
38
39 #include "globals.h"
40 #include "afp_config.h"
41 #include "status.h"
42 #include "fork.h"
43 #include "uam_auth.h"
44
45 unsigned char   nologin = 0;
46
47 struct afp_options default_options;
48 static AFPConfig *configs;
49 static server_child *server_children;
50 static fd_set save_rfds;
51
52 #ifdef CAPDIR
53 int addr_net, addr_node, addr_uid;
54 char addr_name[32];
55 #endif CAPDIR
56
57 static void afp_exit(const int i)
58 {
59   server_unlock(default_options.pidfile);
60   exit(i);
61 }
62
63 static void afp_goaway(int sig)
64 {
65 #ifndef NO_DDP
66     asp_kill(sig);
67 #endif
68     dsi_kill(sig);
69     switch( sig ) {
70     case SIGTERM :
71       syslog( LOG_INFO, "shutting down on signal %d", sig );
72       break;
73     case SIGHUP :
74       /* w/ a configuration file, we can force a re-read if we want */
75       nologin++;
76       if ((nologin + 1) & 1) {
77         AFPConfig *config;
78
79         syslog(LOG_INFO, "re-reading configuration file");
80         for (config = configs; config; config = config->next)
81           if (config->server_cleanup)
82             config->server_cleanup(config);
83
84         configfree(configs, NULL);
85         if (!(configs = configinit(&default_options))) {
86           syslog(LOG_ERR, "config re-read: no servers configured");
87           afp_exit(1);
88         }
89         FD_ZERO(&save_rfds);
90         for (config = configs; config; config = config->next) {
91           if (config->fd < 0)
92             continue;
93           FD_SET(config->fd, &save_rfds);
94         }
95       } else {
96         syslog(LOG_INFO, "disallowing logins");
97         auth_unload();
98       }
99       break;
100     default :
101         syslog( LOG_ERR, "afp_goaway: bad signal" );
102     }
103     if ( sig == SIGTERM ) {
104       AFPConfig *config;
105       
106       for (config = configs; config; config = config->next) 
107         if (config->server_cleanup) 
108           config->server_cleanup(config);
109
110       afp_exit(0);
111     }
112     return;
113 }
114
115 static void child_handler()
116 {
117   server_child_handler(server_children);
118 }
119
120 int main( ac, av )
121     int         ac;
122     char        **av;
123 {
124     AFPConfig           *config;
125     fd_set              rfds;
126     struct sigaction    sv;
127     sigset_t            sigs;
128
129     umask( 0 );         /* so inherited file permissions work right */
130
131     afp_options_init(&default_options);
132     if (!afp_options_parse(ac, av, &default_options))
133       exit(1);
134     
135     switch(server_lock("afpd", default_options.pidfile, 
136                        default_options.flags & OPTION_DEBUG)) {
137     case -1: /* error */
138       exit(1);
139     case 0: /* child */
140       break;
141     default: /* server */
142       exit(0);
143     }
144
145     /* install child handler for asp and dsi. we do this before afp_goaway
146      * as afp_goaway references stuff from here. 
147      * XXX: this should really be setup after the initial connections. */
148     if (!(server_children = server_child_alloc(default_options.connections,
149                                                CHILD_NFORKS))) {
150       syslog(LOG_ERR, "main: server_child alloc: %m");
151       afp_exit(1);
152     }
153       
154     memset(&sv, 0, sizeof(sv));
155     sv.sa_handler = child_handler;
156     sigemptyset( &sv.sa_mask );
157     sv.sa_flags = SA_RESTART;
158     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
159         syslog( LOG_ERR, "main: sigaction: %m" );
160         afp_exit(1);
161     }
162
163     sv.sa_handler = afp_goaway;
164     sigemptyset( &sv.sa_mask );
165     sigaddset(&sv.sa_mask, SIGHUP);
166     sigaddset(&sv.sa_mask, SIGTERM);
167     sv.sa_flags = SA_RESTART;
168     if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
169         syslog( LOG_ERR, "main: sigaction: %m" );
170         afp_exit(1);
171     }
172     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
173         syslog( LOG_ERR, "main: sigaction: %m" );
174         afp_exit(1);
175     }
176     
177     /* afpd.conf: not in config file: lockfile, connections, configfile
178      *            preference: command-line provides defaults.
179      *                        config file over-writes defaults.
180      *
181      * we also need to make sure that killing afpd during startup
182      * won't leave any lingering registered names around.
183      */
184
185     sigemptyset(&sigs);
186     sigaddset(&sigs, SIGHUP);
187     sigaddset(&sigs, SIGTERM);
188     sigprocmask(SIG_BLOCK, &sigs, NULL);
189     if (!(configs = configinit(&default_options))) {
190       syslog(LOG_ERR, "main: no servers configured: %m\n");
191       afp_exit(1);
192     }
193     sigprocmask(SIG_UNBLOCK, &sigs, NULL);
194
195     /* watch atp and dsi sockets. */
196     FD_ZERO(&save_rfds);
197     for (config = configs; config; config = config->next) {
198       if (config->fd < 0) /* for proxies */
199         continue;
200       FD_SET(config->fd, &save_rfds);
201     }
202     
203     /* wait for an appleshare connection. parent remains in the loop
204      * while the children get handled by afp_over_{asp,dsi}.  this is
205      * currently vulnerable to a denial-of-service attack if a
206      * connection is made without an actual login attempt being made
207      * afterwards. establishing timeouts for logins is a possible 
208      * solution. */
209     while (1) {
210       rfds = save_rfds;
211       if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
212         if (errno == EINTR)
213           continue;
214         syslog(LOG_ERR, "main: can't wait for input: %m");
215         break;
216       }
217       
218       for (config = configs; config; config = config->next) {
219         if (config->fd < 0)
220           continue;
221         if (FD_ISSET(config->fd, &rfds)) 
222           config->server_start(config, configs, server_children); 
223       }
224     }
225
226     return 0;
227 }