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