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>
23 #ifdef HAVE_SYS_TIME_H
25 #endif /* HAVE_SYS_TIME_H */
26 #ifdef HAVE_SYS_STAT_H
28 #endif /* HAVE_SYS_STAT_H */
30 #include <netatalk/endian.h>
31 #include <atalk/atp.h>
32 #include <atalk/asp.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
35 #include <atalk/globals.h>
44 #endif /* FORCE_UIDGID */
48 static void afp_authprint_remove(AFPObj *);
50 static void afp_asp_close(AFPObj *obj)
52 ASP asp = obj->handle;
54 if (seteuid( obj->uid ) < 0) {
55 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
59 if (obj->options.authprintdir) afp_authprint_remove(obj);
64 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
65 asp->read_count / 1024.0, asp->write_count / 1024.0);
69 /* removes the authprint trailing when appropriate */
70 static void afp_authprint_remove(AFPObj *obj)
72 ASP asp = obj->handle;
73 char addr_filename[256];
74 char addr_filename_buff[256];
77 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
78 ntohs( asp->asp_sat.sat_addr.s_net )/256,
79 ntohs( asp->asp_sat.sat_addr.s_net )%256,
80 asp->asp_sat.sat_addr.s_node );
82 memset( addr_filename_buff, 0, 256 );
84 if (stat(addr_filename, &cap_st) == 0) {
85 if( S_ISREG(cap_st.st_mode) ) {
87 int capfd = open( addr_filename, O_RDONLY );
88 if ((len = read( capfd, addr_filename_buff, 256 )) > 0) {
91 addr_filename_buff[len] = 0;
92 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) {
95 file_pid = atoi(p_filepid);
96 if (file_pid == (int)getpid()) {
97 if(unlink(addr_filename) == 0) {
98 LOG(log_info, logtype_afpd, "removed %s", addr_filename);
100 LOG(log_info, logtype_afpd, "error removing %s: %s",
101 addr_filename, strerror(errno));
104 LOG(log_info, logtype_afpd, "%s belongs to another pid %d",
105 addr_filename, file_pid );
107 } else { /* no pid info */
108 if (unlink(addr_filename) == 0) {
109 LOG(log_info, logtype_afpd, "removed %s", addr_filename );
111 LOG(log_info, logtype_afpd, "error removing %s: %s",
112 addr_filename, strerror(errno));
116 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename );
121 LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename );
124 LOG(log_info, logtype_afpd, "error stat'ing %s: %s",
125 addr_filename, strerror(errno));
129 /* ------------------------
132 static void afp_asp_die(const int sig)
134 ASP asp = child->handle;
136 asp_attention(asp, AFPATTN_SHUTDOWN);
137 if ( asp_shutdown( asp ) < 0 ) {
138 LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
141 afp_asp_close(child);
142 if (sig == SIGTERM || sig == SIGALRM)
148 /* -----------------------------
151 static void afp_asp_timedown(int sig _U_)
156 /* shutdown and don't reconnect. server going down in 5 minutes. */
157 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
160 it.it_interval.tv_sec = 0;
161 it.it_interval.tv_usec = 0;
162 it.it_value.tv_sec = 300;
163 it.it_value.tv_usec = 0;
164 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
165 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
166 afp_asp_die(EXITERR_SYS);
169 memset(&sv, 0, sizeof(sv));
170 sv.sa_handler = afp_asp_die;
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( SIGALRM, &sv, NULL ) < 0 ) {
176 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
177 afp_asp_die(EXITERR_SYS);
181 sv.sa_handler = SIG_IGN;
182 sigemptyset( &sv.sa_mask );
183 sv.sa_flags = SA_RESTART;
184 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
185 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGUSR1: %s", strerror(errno) );
186 afp_asp_die(EXITERR_SYS);
190 /* ---------------------------------
191 * SIGHUP reload configuration file
193 extern volatile int reload_request;
195 static void afp_asp_reload(int sig _U_)
200 /* ---------------------- */
202 static void afp_asp_getmesg (int sig _U_)
205 asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5));
207 #endif /* SERVERTEXT */
209 /* ---------------------- */
210 void afp_over_asp(AFPObj *obj)
213 struct sigaction action;
220 obj->exit = afp_asp_die;
221 obj->reply = (int (*)()) asp_cmdreply;
222 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
224 asp = (ASP) obj->handle;
226 /* install signal handlers
227 * With ASP tickle handler is done in the parent process
229 memset(&action, 0, sizeof(action));
232 action.sa_handler = afp_asp_reload;
233 sigemptyset( &action.sa_mask );
234 sigaddset(&action.sa_mask, SIGTERM);
235 sigaddset(&action.sa_mask, SIGUSR1);
237 sigaddset(&action.sa_mask, SIGUSR2);
239 action.sa_flags = SA_RESTART;
240 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
241 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
242 afp_asp_die(EXITERR_SYS);
245 /* install SIGTERM */
246 action.sa_handler = afp_asp_die;
247 sigemptyset( &action.sa_mask );
248 sigaddset(&action.sa_mask, SIGHUP);
249 sigaddset(&action.sa_mask, SIGUSR1);
251 sigaddset(&action.sa_mask, SIGUSR2);
253 action.sa_flags = SA_RESTART;
254 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
255 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
256 afp_asp_die(EXITERR_SYS);
260 /* Added for server message support */
261 action.sa_handler = afp_asp_getmesg;
262 sigemptyset( &action.sa_mask );
263 sigaddset(&action.sa_mask, SIGTERM);
264 sigaddset(&action.sa_mask, SIGUSR1);
265 sigaddset(&action.sa_mask, SIGHUP);
266 action.sa_flags = SA_RESTART;
267 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
268 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
269 afp_asp_die(EXITERR_SYS);
271 #endif /* SERVERTEXT */
273 /* SIGUSR1 - set down in 5 minutes */
274 action.sa_handler = afp_asp_timedown;
275 sigemptyset( &action.sa_mask );
276 sigaddset(&action.sa_mask, SIGHUP);
277 sigaddset(&action.sa_mask, SIGTERM);
279 sigaddset(&action.sa_mask, SIGUSR2);
281 action.sa_flags = SA_RESTART;
282 if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) {
283 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
284 afp_asp_die(EXITERR_SYS);
287 if (dircache_init(obj->options.dircachesize) != 0) {
288 LOG(log_error, logtype_afpd, "afp_over_asp: dircache_init error");
289 afp_asp_die(EXITERR_SYS);
292 LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
293 ntohs( asp->asp_sat.sat_addr.s_net ),
294 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
295 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
296 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
297 atp_sockaddr( asp->asp_atp )->sat_port );
299 while ((reply = asp_getrequest(asp))) {
300 if (reload_request) {
307 LOG(log_info, logtype_afpd, "done" );
310 if ( obj->options.flags & OPTION_DEBUG ) {
320 if ( flushfork( writtenfork ) < 0 ) {
321 LOG(log_error, logtype_afpd, "main flushfork: %s",
327 func = (u_char) asp->commands[0];
329 if ( obj->options.flags & OPTION_DEBUG ) {
330 printf("command: %d (%s)\n", func, AfpNum2name(func));
331 bprint( asp->commands, asp->cmdlen );
334 if ( afp_switch[ func ] != NULL ) {
336 * The function called from afp_switch is expected to
337 * read its parameters out of buf, put its
338 * results in replybuf (updating rbuflen), and
339 * return an error code.
341 asp->datalen = ASP_DATASIZ;
342 reply = (*afp_switch[ func ])(obj,
343 asp->commands, asp->cmdlen,
344 asp->data, &asp->datalen);
346 /* bring everything back to old euid, egid */
348 restore_uidgid ( &obj->uidgid );
349 #endif /* FORCE_UIDGID */
351 LOG(log_error, logtype_afpd, "bad function %X", func );
356 if ( obj->options.flags & OPTION_DEBUG ) {
357 printf( "reply: %d, %d\n", reply, ccnt++ );
358 bprint( asp->data, asp->datalen );
361 if ( asp_cmdreply( asp, reply ) < 0 ) {
362 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
363 afp_asp_die(EXITERR_CLNT);
368 func = (u_char) asp->commands[0];
370 if ( obj->options.flags & OPTION_DEBUG ) {
371 printf( "(write) command: %d\n", func );
372 bprint( asp->commands, asp->cmdlen );
375 if ( afp_switch[ func ] != NULL ) {
376 asp->datalen = ASP_DATASIZ;
377 reply = (*afp_switch[ func ])(obj,
378 asp->commands, asp->cmdlen,
379 asp->data, &asp->datalen);
381 /* bring everything back to old euid, egid */
383 restore_uidgid ( &obj->uidgid );
384 #endif /* FORCE_UIDGID */
386 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
391 if ( obj->options.flags & OPTION_DEBUG ) {
392 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
393 bprint( asp->data, asp->datalen );
396 if ( asp_wrtreply( asp, reply ) < 0 ) {
397 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
398 afp_asp_die(EXITERR_CLNT);
403 * Bad asp packet. Probably should have asp filter them,
404 * since they are typically things like out-of-order packet.
406 LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
410 if ( obj->options.flags & OPTION_DEBUG ) {
414 of_pforkdesc( stdout );