2 * $Id: afp_asp.c,v 1.29 2010-03-09 06:55:12 franklahm Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * Copyright (c) 1990,1993 Regents of The University of Michigan.
6 * All Rights Reserved. See COPYRIGHT.
8 * modified from main.c. this handles afp over asp.
13 #endif /* HAVE_CONFIG_H */
21 #include <atalk/logger.h>
24 #ifdef HAVE_SYS_STAT_H
26 #endif /* HAVE_SYS_STAT_H */
28 #include <netatalk/endian.h>
29 #include <atalk/atp.h>
30 #include <atalk/asp.h>
31 #include <atalk/compat.h>
32 #include <atalk/util.h>
42 #endif /* FORCE_UIDGID */
46 static void afp_authprint_remove(AFPObj *);
48 static void afp_asp_close(AFPObj *obj)
50 ASP asp = obj->handle;
52 if (seteuid( obj->uid ) < 0) {
53 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
57 if (obj->options.authprintdir) afp_authprint_remove(obj);
62 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
63 asp->read_count / 1024.0, asp->write_count / 1024.0);
67 /* removes the authprint trailing when appropriate */
68 static void afp_authprint_remove(AFPObj *obj)
70 ASP asp = obj->handle;
71 char addr_filename[256];
72 char addr_filename_buff[256];
75 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
76 ntohs( asp->asp_sat.sat_addr.s_net )/256,
77 ntohs( asp->asp_sat.sat_addr.s_net )%256,
78 asp->asp_sat.sat_addr.s_node );
80 memset( addr_filename_buff, 0, 256 );
82 if(lstat(addr_filename, &cap_st) == 0) {
83 if( S_ISREG(cap_st.st_mode) ) {
85 int capfd = open( addr_filename, O_RDONLY );
86 if ((len = read( capfd, addr_filename_buff, 256 )) > 0) {
90 addr_filename_buff[len] = 0;
91 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) {
94 file_pid = atoi(p_filepid);
95 if (file_pid == (int)getpid()) {
96 if(unlink(addr_filename) == 0) {
97 LOG(log_info, logtype_afpd, "removed %s", addr_filename);
99 LOG(log_info, logtype_afpd, "error removing %s: %s",
100 addr_filename, strerror(errno));
103 LOG(log_info, logtype_afpd, "%s belongs to another pid %d",
104 addr_filename, file_pid );
106 } else { /* no pid info */
107 if (unlink(addr_filename) == 0) {
108 LOG(log_info, logtype_afpd, "removed %s", addr_filename );
110 LOG(log_info, logtype_afpd, "error removing %s: %s",
111 addr_filename, strerror(errno));
115 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename );
118 LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename );
121 LOG(log_info, logtype_afpd, "error stat'ing %s: %s",
122 addr_filename, strerror(errno));
126 /* ------------------------
129 static void afp_asp_die(const int sig)
131 ASP asp = child->handle;
133 asp_attention(asp, AFPATTN_SHUTDOWN);
134 if ( asp_shutdown( asp ) < 0 ) {
135 LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
138 afp_asp_close(child);
139 if (sig == SIGTERM || sig == SIGALRM)
145 /* -----------------------------
148 static void afp_asp_timedown(int sig _U_)
153 /* shutdown and don't reconnect. server going down in 5 minutes. */
154 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
157 it.it_interval.tv_sec = 0;
158 it.it_interval.tv_usec = 0;
159 it.it_value.tv_sec = 300;
160 it.it_value.tv_usec = 0;
161 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
162 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
163 afp_asp_die(EXITERR_SYS);
166 memset(&sv, 0, sizeof(sv));
167 sv.sa_handler = afp_asp_die;
168 sigemptyset( &sv.sa_mask );
169 sigaddset(&sv.sa_mask, SIGHUP);
170 sigaddset(&sv.sa_mask, SIGTERM);
171 sv.sa_flags = SA_RESTART;
172 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
173 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
174 afp_asp_die(EXITERR_SYS);
178 sv.sa_handler = SIG_IGN;
179 sigemptyset( &sv.sa_mask );
180 sv.sa_flags = SA_RESTART;
181 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
182 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGUSR1: %s", strerror(errno) );
183 afp_asp_die(EXITERR_SYS);
187 /* ---------------------------------
188 * SIGHUP reload configuration file
190 extern volatile int reload_request;
192 static void afp_asp_reload(int sig _U_)
197 /* ---------------------- */
199 static void afp_asp_getmesg (int sig _U_)
202 asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5));
204 #endif /* SERVERTEXT */
206 /* ---------------------- */
207 void afp_over_asp(AFPObj *obj)
210 struct sigaction action;
217 obj->exit = afp_asp_die;
218 obj->reply = (int (*)()) asp_cmdreply;
219 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
221 asp = (ASP) obj->handle;
223 /* install signal handlers
224 * With ASP tickle handler is done in the parent process
226 memset(&action, 0, sizeof(action));
229 action.sa_handler = afp_asp_reload;
230 sigemptyset( &action.sa_mask );
231 sigaddset(&action.sa_mask, SIGTERM);
232 sigaddset(&action.sa_mask, SIGUSR1);
234 sigaddset(&action.sa_mask, SIGUSR2);
236 action.sa_flags = SA_RESTART;
237 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
238 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
239 afp_asp_die(EXITERR_SYS);
242 /* install SIGTERM */
243 action.sa_handler = afp_asp_die;
244 sigemptyset( &action.sa_mask );
245 sigaddset(&action.sa_mask, SIGHUP);
246 sigaddset(&action.sa_mask, SIGUSR1);
248 sigaddset(&action.sa_mask, SIGUSR2);
250 action.sa_flags = SA_RESTART;
251 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
252 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
253 afp_asp_die(EXITERR_SYS);
257 /* Added for server message support */
258 action.sa_handler = afp_asp_getmesg;
259 sigemptyset( &action.sa_mask );
260 sigaddset(&action.sa_mask, SIGTERM);
261 sigaddset(&action.sa_mask, SIGUSR1);
262 sigaddset(&action.sa_mask, SIGHUP);
263 action.sa_flags = SA_RESTART;
264 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
265 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
266 afp_asp_die(EXITERR_SYS);
268 #endif /* SERVERTEXT */
270 /* SIGUSR1 - set down in 5 minutes */
271 action.sa_handler = afp_asp_timedown;
272 sigemptyset( &action.sa_mask );
273 sigaddset(&action.sa_mask, SIGHUP);
274 sigaddset(&action.sa_mask, SIGTERM);
276 sigaddset(&action.sa_mask, SIGUSR2);
278 action.sa_flags = SA_RESTART;
279 if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) {
280 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
281 afp_asp_die(EXITERR_SYS);
284 LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
285 ntohs( asp->asp_sat.sat_addr.s_net ),
286 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
287 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
288 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
289 atp_sockaddr( asp->asp_atp )->sat_port );
291 while ((reply = asp_getrequest(asp))) {
292 if (reload_request) {
299 LOG(log_info, logtype_afpd, "done" );
302 if ( obj->options.flags & OPTION_DEBUG ) {
312 if ( flushfork( writtenfork ) < 0 ) {
313 LOG(log_error, logtype_afpd, "main flushfork: %s",
319 func = (u_char) asp->commands[0];
321 if ( obj->options.flags & OPTION_DEBUG ) {
322 printf("command: %d (%s)\n", func, AfpNum2name(func));
323 bprint( asp->commands, asp->cmdlen );
326 if ( afp_switch[ func ] != NULL ) {
328 * The function called from afp_switch is expected to
329 * read its parameters out of buf, put its
330 * results in replybuf (updating rbuflen), and
331 * return an error code.
333 asp->datalen = ASP_DATASIZ;
334 reply = (*afp_switch[ func ])(obj,
335 asp->commands, asp->cmdlen,
336 asp->data, &asp->datalen);
338 /* bring everything back to old euid, egid */
340 restore_uidgid ( &obj->uidgid );
341 #endif /* FORCE_UIDGID */
343 LOG(log_error, logtype_afpd, "bad function %X", func );
348 if ( obj->options.flags & OPTION_DEBUG ) {
349 printf( "reply: %d, %d\n", reply, ccnt++ );
350 bprint( asp->data, asp->datalen );
353 if ( asp_cmdreply( asp, reply ) < 0 ) {
354 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
355 afp_asp_die(EXITERR_CLNT);
360 func = (u_char) asp->commands[0];
362 if ( obj->options.flags & OPTION_DEBUG ) {
363 printf( "(write) command: %d\n", func );
364 bprint( asp->commands, asp->cmdlen );
367 if ( afp_switch[ func ] != NULL ) {
368 asp->datalen = ASP_DATASIZ;
369 reply = (*afp_switch[ func ])(obj,
370 asp->commands, asp->cmdlen,
371 asp->data, &asp->datalen);
373 /* bring everything back to old euid, egid */
375 restore_uidgid ( &obj->uidgid );
376 #endif /* FORCE_UIDGID */
378 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
383 if ( obj->options.flags & OPTION_DEBUG ) {
384 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
385 bprint( asp->data, asp->datalen );
388 if ( asp_wrtreply( asp, reply ) < 0 ) {
389 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
390 afp_asp_die(EXITERR_CLNT);
395 * Bad asp packet. Probably should have asp filter them,
396 * since they are typically things like out-of-order packet.
398 LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
402 if ( obj->options.flags & OPTION_DEBUG ) {
406 of_pforkdesc( stdout );