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