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