2 * $Id: afp_asp.c,v 1.16 2002-03-24 01:23:40 sibaz 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>
41 extern struct oforks *writtenfork;
45 static __inline__ void afp_authprint_remove(AFPObj *);
47 static __inline__ void afp_asp_close(AFPObj *obj)
49 ASP asp = obj->handle;
51 if (obj->options.authprintdir) afp_authprint_remove(obj);
56 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
57 asp->read_count / 1024.0, asp->write_count / 1024.0);
61 /* removes the authprint trailing when appropriate */
62 static __inline__ void afp_authprint_remove(AFPObj *obj)
64 ASP asp = obj->handle;
65 char addr_filename[256];
66 char addr_filename_buff[256];
69 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
70 ntohs( asp->asp_sat.sat_addr.s_net )/256,
71 ntohs( asp->asp_sat.sat_addr.s_net )%256,
72 asp->asp_sat.sat_addr.s_node );
74 memset( addr_filename_buff, 0, 256 );
76 if(stat(addr_filename, &cap_st) == 0) {
77 if( S_ISREG(cap_st.st_mode) ) {
79 int capfd = open( addr_filename, O_RDONLY );
80 if ((len = read( capfd, addr_filename_buff, 256 )) > 0) {
84 addr_filename_buff[len] = 0;
85 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) {
88 file_pid = atoi(p_filepid);
89 if (file_pid == (int)getpid()) {
90 if(unlink(addr_filename) == 0) {
91 LOG(log_info, logtype_afpd, "removed %s", addr_filename);
93 LOG(log_info, logtype_afpd, "error removing %s: %s",
94 addr_filename, strerror(errno));
97 LOG(log_info, logtype_afpd, "%s belongs to another pid %d",
98 addr_filename, file_pid );
100 } else { /* no pid info */
101 if (unlink(addr_filename) == 0) {
102 LOG(log_info, logtype_afpd, "removed %s", addr_filename );
104 LOG(log_info, logtype_afpd, "error removing %s: %s",
105 addr_filename, strerror(errno));
109 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename );
112 LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename );
115 LOG(log_info, logtype_afpd, "error stat'ing %s: %s",
116 addr_filename, strerror(errno));
120 static void afp_asp_die(const int sig)
122 ASP asp = child->handle;
124 asp_attention(asp, AFPATTN_SHUTDOWN);
125 if ( asp_shutdown( asp ) < 0 ) {
126 LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
129 afp_asp_close(child);
130 if (sig == SIGTERM || sig == SIGALRM)
136 static void afp_asp_timedown()
141 /* shutdown and don't reconnect. server going down in 5 minutes. */
142 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
145 it.it_interval.tv_sec = 0;
146 it.it_interval.tv_usec = 0;
147 it.it_value.tv_sec = 300;
148 it.it_value.tv_usec = 0;
149 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
150 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
154 memset(&sv, 0, sizeof(sv));
155 sv.sa_handler = afp_asp_die;
156 sigemptyset( &sv.sa_mask );
157 sv.sa_flags = SA_RESTART;
158 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
159 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
164 void afp_over_asp(AFPObj *obj)
167 struct sigaction action;
168 int func, ccnt = 0, reply = 0;
170 obj->exit = afp_asp_die;
171 obj->reply = (int (*)()) asp_cmdreply;
172 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
174 asp = (ASP) obj->handle;
176 /* install signal handlers */
177 memset(&action, 0, sizeof(action));
178 action.sa_handler = afp_asp_timedown;
179 sigemptyset( &action.sa_mask );
180 action.sa_flags = SA_RESTART;
181 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
182 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
186 action.sa_handler = afp_asp_die;
187 sigemptyset( &action.sa_mask );
188 action.sa_flags = SA_RESTART;
189 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
190 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
194 LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
195 ntohs( asp->asp_sat.sat_addr.s_net ),
196 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
197 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
198 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
199 atp_sockaddr( asp->asp_atp )->sat_port );
201 while ((reply = asp_getrequest(asp))) {
205 LOG(log_info, logtype_afpd, "done" );
207 if ( obj->options.flags & OPTION_DEBUG ) {
216 if ( flushfork( writtenfork ) < 0 ) {
217 LOG(log_error, logtype_afpd, "main flushfork: %s",
223 func = (u_char) asp->commands[0];
224 if ( obj->options.flags & OPTION_DEBUG ) {
225 printf("command: %d (%s)\n", func, AfpNum2name(func));
226 bprint( asp->commands, asp->cmdlen );
228 if ( afp_switch[ func ] != NULL ) {
230 * The function called from afp_switch is expected to
231 * read its parameters out of buf, put its
232 * results in replybuf (updating rbuflen), and
233 * return an error code.
235 asp->datalen = ASP_DATASIZ;
236 reply = (*afp_switch[ func ])(obj,
237 asp->commands, asp->cmdlen,
238 asp->data, &asp->datalen);
240 LOG(log_error, logtype_afpd, "bad function %X", func );
244 if ( obj->options.flags & OPTION_DEBUG ) {
245 printf( "reply: %d, %d\n", reply, ccnt++ );
246 bprint( asp->data, asp->datalen );
249 if ( asp_cmdreply( asp, reply ) < 0 ) {
250 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
256 func = (u_char) asp->commands[0];
257 if ( obj->options.flags & OPTION_DEBUG ) {
258 printf( "(write) command: %d\n", func );
259 bprint( asp->commands, asp->cmdlen );
261 if ( afp_switch[ func ] != NULL ) {
262 asp->datalen = ASP_DATASIZ;
263 reply = (*afp_switch[ func ])(obj,
264 asp->commands, asp->cmdlen,
265 asp->data, &asp->datalen);
267 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
271 if ( obj->options.flags & OPTION_DEBUG ) {
272 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
273 bprint( asp->data, asp->datalen );
275 if ( asp_wrtreply( asp, reply ) < 0 ) {
276 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
282 * Bad asp packet. Probably should have asp filter them,
283 * since they are typically things like out-of-order packet.
285 LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
289 if ( obj->options.flags & OPTION_DEBUG ) {
293 of_pforkdesc( stdout );