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>
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(lstat(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) {
92 addr_filename_buff[len] = 0;
93 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) {
96 file_pid = atoi(p_filepid);
97 if (file_pid == (int)getpid()) {
98 if(unlink(addr_filename) == 0) {
99 LOG(log_info, logtype_afpd, "removed %s", addr_filename);
101 LOG(log_info, logtype_afpd, "error removing %s: %s",
102 addr_filename, strerror(errno));
105 LOG(log_info, logtype_afpd, "%s belongs to another pid %d",
106 addr_filename, file_pid );
108 } else { /* no pid info */
109 if (unlink(addr_filename) == 0) {
110 LOG(log_info, logtype_afpd, "removed %s", addr_filename );
112 LOG(log_info, logtype_afpd, "error removing %s: %s",
113 addr_filename, strerror(errno));
117 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename );
120 LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename );
123 LOG(log_info, logtype_afpd, "error stat'ing %s: %s",
124 addr_filename, strerror(errno));
128 /* ------------------------
131 static void afp_asp_die(const int sig)
133 ASP asp = child->handle;
135 asp_attention(asp, AFPATTN_SHUTDOWN);
136 if ( asp_shutdown( asp ) < 0 ) {
137 LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
140 afp_asp_close(child);
141 if (sig == SIGTERM || sig == SIGALRM)
147 /* -----------------------------
150 static void afp_asp_timedown(int sig _U_)
155 /* shutdown and don't reconnect. server going down in 5 minutes. */
156 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
159 it.it_interval.tv_sec = 0;
160 it.it_interval.tv_usec = 0;
161 it.it_value.tv_sec = 300;
162 it.it_value.tv_usec = 0;
163 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
164 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
165 afp_asp_die(EXITERR_SYS);
168 memset(&sv, 0, sizeof(sv));
169 sv.sa_handler = afp_asp_die;
170 sigemptyset( &sv.sa_mask );
171 sigaddset(&sv.sa_mask, SIGHUP);
172 sigaddset(&sv.sa_mask, SIGTERM);
173 sv.sa_flags = SA_RESTART;
174 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
175 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
176 afp_asp_die(EXITERR_SYS);
180 sv.sa_handler = SIG_IGN;
181 sigemptyset( &sv.sa_mask );
182 sv.sa_flags = SA_RESTART;
183 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
184 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGUSR1: %s", strerror(errno) );
185 afp_asp_die(EXITERR_SYS);
189 /* ---------------------------------
190 * SIGHUP reload configuration file
192 extern volatile int reload_request;
194 static void afp_asp_reload(int sig _U_)
199 /* ---------------------- */
201 static void afp_asp_getmesg (int sig _U_)
204 asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5));
206 #endif /* SERVERTEXT */
208 /* ---------------------- */
209 void afp_over_asp(AFPObj *obj)
212 struct sigaction action;
219 obj->exit = afp_asp_die;
220 obj->reply = (int (*)()) asp_cmdreply;
221 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
223 asp = (ASP) obj->handle;
225 /* install signal handlers
226 * With ASP tickle handler is done in the parent process
228 memset(&action, 0, sizeof(action));
231 action.sa_handler = afp_asp_reload;
232 sigemptyset( &action.sa_mask );
233 sigaddset(&action.sa_mask, SIGTERM);
234 sigaddset(&action.sa_mask, SIGUSR1);
236 sigaddset(&action.sa_mask, SIGUSR2);
238 action.sa_flags = SA_RESTART;
239 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
240 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
241 afp_asp_die(EXITERR_SYS);
244 /* install SIGTERM */
245 action.sa_handler = afp_asp_die;
246 sigemptyset( &action.sa_mask );
247 sigaddset(&action.sa_mask, SIGHUP);
248 sigaddset(&action.sa_mask, SIGUSR1);
250 sigaddset(&action.sa_mask, SIGUSR2);
252 action.sa_flags = SA_RESTART;
253 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
254 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
255 afp_asp_die(EXITERR_SYS);
259 /* Added for server message support */
260 action.sa_handler = afp_asp_getmesg;
261 sigemptyset( &action.sa_mask );
262 sigaddset(&action.sa_mask, SIGTERM);
263 sigaddset(&action.sa_mask, SIGUSR1);
264 sigaddset(&action.sa_mask, SIGHUP);
265 action.sa_flags = SA_RESTART;
266 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
267 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
268 afp_asp_die(EXITERR_SYS);
270 #endif /* SERVERTEXT */
272 /* SIGUSR1 - set down in 5 minutes */
273 action.sa_handler = afp_asp_timedown;
274 sigemptyset( &action.sa_mask );
275 sigaddset(&action.sa_mask, SIGHUP);
276 sigaddset(&action.sa_mask, SIGTERM);
278 sigaddset(&action.sa_mask, SIGUSR2);
280 action.sa_flags = SA_RESTART;
281 if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) {
282 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
283 afp_asp_die(EXITERR_SYS);
286 LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
287 ntohs( asp->asp_sat.sat_addr.s_net ),
288 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
289 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
290 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
291 atp_sockaddr( asp->asp_atp )->sat_port );
293 while ((reply = asp_getrequest(asp))) {
294 if (reload_request) {
301 LOG(log_info, logtype_afpd, "done" );
304 if ( obj->options.flags & OPTION_DEBUG ) {
314 if ( flushfork( writtenfork ) < 0 ) {
315 LOG(log_error, logtype_afpd, "main flushfork: %s",
321 func = (u_char) asp->commands[0];
323 if ( obj->options.flags & OPTION_DEBUG ) {
324 printf("command: %d (%s)\n", func, AfpNum2name(func));
325 bprint( asp->commands, asp->cmdlen );
328 if ( afp_switch[ func ] != NULL ) {
330 * The function called from afp_switch is expected to
331 * read its parameters out of buf, put its
332 * results in replybuf (updating rbuflen), and
333 * return an error code.
335 asp->datalen = ASP_DATASIZ;
336 reply = (*afp_switch[ func ])(obj,
337 asp->commands, asp->cmdlen,
338 asp->data, &asp->datalen);
340 /* bring everything back to old euid, egid */
342 restore_uidgid ( &obj->uidgid );
343 #endif /* FORCE_UIDGID */
345 LOG(log_error, logtype_afpd, "bad function %X", func );
350 if ( obj->options.flags & OPTION_DEBUG ) {
351 printf( "reply: %d, %d\n", reply, ccnt++ );
352 bprint( asp->data, asp->datalen );
355 if ( asp_cmdreply( asp, reply ) < 0 ) {
356 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
357 afp_asp_die(EXITERR_CLNT);
362 func = (u_char) asp->commands[0];
364 if ( obj->options.flags & OPTION_DEBUG ) {
365 printf( "(write) command: %d\n", func );
366 bprint( asp->commands, asp->cmdlen );
369 if ( afp_switch[ func ] != NULL ) {
370 asp->datalen = ASP_DATASIZ;
371 reply = (*afp_switch[ func ])(obj,
372 asp->commands, asp->cmdlen,
373 asp->data, &asp->datalen);
375 /* bring everything back to old euid, egid */
377 restore_uidgid ( &obj->uidgid );
378 #endif /* FORCE_UIDGID */
380 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
385 if ( obj->options.flags & OPTION_DEBUG ) {
386 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
387 bprint( asp->data, asp->datalen );
390 if ( asp_wrtreply( asp, reply ) < 0 ) {
391 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
392 afp_asp_die(EXITERR_CLNT);
397 * Bad asp packet. Probably should have asp filter them,
398 * since they are typically things like out-of-order packet.
400 LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
404 if ( obj->options.flags & OPTION_DEBUG ) {
408 of_pforkdesc( stdout );