2 * $Id: afp_asp.c,v 1.21 2003-08-29 23:35:34 bfernhomberg 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 */
46 extern struct oforks *writtenfork;
50 static __inline__ void afp_authprint_remove(AFPObj *);
52 static __inline__ void afp_asp_close(AFPObj *obj)
54 ASP asp = obj->handle;
56 if (obj->options.authprintdir) afp_authprint_remove(obj);
61 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
62 asp->read_count / 1024.0, asp->write_count / 1024.0);
67 /* removes the authprint trailing when appropriate */
68 static __inline__ 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(stat(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 static void afp_asp_die(const int sig)
128 ASP asp = child->handle;
130 asp_attention(asp, AFPATTN_SHUTDOWN);
131 if ( asp_shutdown( asp ) < 0 ) {
132 LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
135 afp_asp_close(child);
136 if (sig == SIGTERM || sig == SIGALRM)
142 static void afp_asp_timedown()
147 /* shutdown and don't reconnect. server going down in 5 minutes. */
148 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
151 it.it_interval.tv_sec = 0;
152 it.it_interval.tv_usec = 0;
153 it.it_value.tv_sec = 300;
154 it.it_value.tv_usec = 0;
155 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
156 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
160 memset(&sv, 0, sizeof(sv));
161 sv.sa_handler = afp_asp_die;
162 sigemptyset( &sv.sa_mask );
163 sv.sa_flags = SA_RESTART;
164 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
165 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
170 sv.sa_handler = SIG_IGN;
171 sigemptyset( &sv.sa_mask );
172 sv.sa_flags = SA_RESTART;
173 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
174 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
179 /* ---------------------- */
181 static void afp_asp_getmesg (int sig)
184 asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5));
186 #endif /* SERVERTEXT */
189 void afp_over_asp(AFPObj *obj)
192 struct sigaction action;
193 int func, ccnt = 0, reply = 0;
195 obj->exit = afp_asp_die;
196 obj->reply = (int (*)()) asp_cmdreply;
197 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
199 asp = (ASP) obj->handle;
201 /* install signal handlers */
202 memset(&action, 0, sizeof(action));
203 action.sa_handler = afp_asp_timedown;
204 sigemptyset( &action.sa_mask );
205 action.sa_flags = SA_RESTART;
206 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
207 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
211 action.sa_handler = afp_asp_die;
212 sigemptyset( &action.sa_mask );
213 action.sa_flags = SA_RESTART;
214 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
215 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
220 /* Added for server message support */
221 action.sa_handler = afp_asp_getmesg;
222 sigemptyset( &action.sa_mask );
223 sigaddset(&action.sa_mask, SIGUSR2);
224 action.sa_flags = SA_RESTART;
225 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
226 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
229 #endif /* SERVERTEXT */
232 LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
233 ntohs( asp->asp_sat.sat_addr.s_net ),
234 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
235 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
236 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
237 atp_sockaddr( asp->asp_atp )->sat_port );
239 while ((reply = asp_getrequest(asp))) {
243 LOG(log_info, logtype_afpd, "done" );
245 if ( obj->options.flags & OPTION_DEBUG ) {
254 if ( flushfork( writtenfork ) < 0 ) {
255 LOG(log_error, logtype_afpd, "main flushfork: %s",
261 func = (u_char) asp->commands[0];
262 if ( obj->options.flags & OPTION_DEBUG ) {
263 printf("command: %d (%s)\n", func, AfpNum2name(func));
264 bprint( asp->commands, asp->cmdlen );
266 if ( afp_switch[ func ] != NULL ) {
268 * The function called from afp_switch is expected to
269 * read its parameters out of buf, put its
270 * results in replybuf (updating rbuflen), and
271 * return an error code.
273 asp->datalen = ASP_DATASIZ;
274 reply = (*afp_switch[ func ])(obj,
275 asp->commands, asp->cmdlen,
276 asp->data, &asp->datalen);
278 /* bring everything back to old euid, egid */
280 restore_uidgid ( &obj->uidgid );
281 #endif /* FORCE_UIDGID */
283 LOG(log_error, logtype_afpd, "bad function %X", func );
287 if ( obj->options.flags & OPTION_DEBUG ) {
288 printf( "reply: %d, %d\n", reply, ccnt++ );
289 bprint( asp->data, asp->datalen );
292 if ( asp_cmdreply( asp, reply ) < 0 ) {
293 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
299 func = (u_char) asp->commands[0];
300 if ( obj->options.flags & OPTION_DEBUG ) {
301 printf( "(write) command: %d\n", func );
302 bprint( asp->commands, asp->cmdlen );
304 if ( afp_switch[ func ] != NULL ) {
305 asp->datalen = ASP_DATASIZ;
306 reply = (*afp_switch[ func ])(obj,
307 asp->commands, asp->cmdlen,
308 asp->data, &asp->datalen);
310 /* bring everything back to old euid, egid */
312 restore_uidgid ( &obj->uidgid );
313 #endif /* FORCE_UIDGID */
315 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
319 if ( obj->options.flags & OPTION_DEBUG ) {
320 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
321 bprint( asp->data, asp->datalen );
323 if ( asp_wrtreply( asp, reply ) < 0 ) {
324 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
330 * Bad asp packet. Probably should have asp filter them,
331 * since they are typically things like out-of-order packet.
333 LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
337 if ( obj->options.flags & OPTION_DEBUG ) {
341 of_pforkdesc( stdout );