2 * $Id: afp_asp.c,v 1.5 2001-06-20 18:33:04 rufustfirefly 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 */
22 #ifdef HAVE_SYS_TIME_H
24 #endif /* HAVE_SYS_TIME_H */
25 #ifdef HAVE_SYS_STAT_H
27 #endif /* HAVE_SYS_STAT_H */
29 #include <netatalk/endian.h>
30 #include <atalk/atp.h>
31 #include <atalk/asp.h>
32 #include <atalk/compat.h>
33 #include <atalk/util.h>
40 extern struct oforks *writtenfork;
44 static __inline__ void afp_asp_close(AFPObj *obj)
46 ASP asp = obj->handle;
52 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
53 asp->read_count / 1024.0, asp->write_count / 1024.0);
56 static void afp_asp_die(const int sig)
58 ASP asp = child->handle;
60 asp_attention(asp, AFPATTN_SHUTDOWN);
61 if ( asp_shutdown( asp ) < 0 ) {
62 syslog( LOG_ERR, "afp_die: asp_shutdown: %m" );
66 if (sig == SIGTERM || sig == SIGALRM)
72 static void afp_asp_timedown()
77 /* shutdown and don't reconnect. server going down in 5 minutes. */
78 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
81 it.it_interval.tv_sec = 0;
82 it.it_interval.tv_usec = 0;
83 it.it_value.tv_sec = 300;
84 it.it_value.tv_usec = 0;
85 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
86 syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
90 memset(&sv, 0, sizeof(sv));
91 sv.sa_handler = afp_asp_die;
92 sigemptyset( &sv.sa_mask );
93 sv.sa_flags = SA_RESTART;
94 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
95 syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
100 void afp_over_asp(AFPObj *obj)
103 struct sigaction action;
104 int func, ccnt = 0, reply = 0;
106 obj->exit = afp_asp_die;
107 obj->reply = (int (*)()) asp_cmdreply;
108 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
110 asp = (ASP) obj->handle;
112 /* install signal handlers */
113 memset(&action, 0, sizeof(action));
114 action.sa_handler = afp_asp_timedown;
115 sigemptyset( &action.sa_mask );
116 action.sa_flags = SA_RESTART;
117 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
118 syslog( LOG_ERR, "afp_over_asp: sigaction: %m" );
122 action.sa_handler = afp_asp_die;
123 sigemptyset( &action.sa_mask );
124 action.sa_flags = SA_RESTART;
125 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
126 syslog( LOG_ERR, "afp_over_asp: sigaction: %m" );
130 syslog( LOG_INFO, "session from %u.%u:%u on %u.%u:%u",
131 ntohs( asp->asp_sat.sat_addr.s_net ),
132 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
133 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
134 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
135 atp_sockaddr( asp->asp_atp )->sat_port );
137 while ((reply = asp_getrequest(asp))) {
140 if (obj->options.authprintdir) {
141 char addr_filename[256];
144 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
145 ntohs( asp->asp_sat.sat_addr.s_net )/256,
146 ntohs( asp->asp_sat.sat_addr.s_net )%256,
147 asp->asp_sat.sat_addr.s_node );
149 if(stat(addr_filename, &cap_st) == 0) {
150 if(unlink(addr_filename) == 0) {
151 syslog(LOG_INFO, "removed %s", addr_filename);
153 syslog(LOG_INFO, "error removing %s: %m", addr_filename);
156 syslog(LOG_INFO, "error stat'ing %s: %m", addr_filename);
161 syslog( LOG_INFO, "done" );
163 if ( obj->options.flags & OPTION_DEBUG ) {
172 if ( flushfork( writtenfork ) < 0 ) {
173 syslog( LOG_ERR, "main flushfork: %m" );
178 func = (u_char) asp->commands[0];
179 if ( obj->options.flags & OPTION_DEBUG ) {
180 printf( "command: %d\n", func );
181 bprint( asp->commands, asp->cmdlen );
183 if ( afp_switch[ func ] != NULL ) {
185 * The function called from afp_switch is expected to
186 * read its parameters out of buf, put its
187 * results in replybuf (updating rbuflen), and
188 * return an error code.
190 asp->datalen = ASP_DATASIZ;
191 reply = (*afp_switch[ func ])(obj,
192 asp->commands, asp->cmdlen,
193 asp->data, &asp->datalen);
195 syslog( LOG_ERR, "bad function %X", func );
199 if ( obj->options.flags & OPTION_DEBUG ) {
200 printf( "reply: %d, %d\n", reply, ccnt++ );
201 bprint( asp->data, asp->datalen );
204 if ( asp_cmdreply( asp, reply ) < 0 ) {
205 syslog( LOG_ERR, "asp_cmdreply: %m" );
211 func = (u_char) asp->commands[0];
212 if ( obj->options.flags & OPTION_DEBUG ) {
213 printf( "(write) command: %d\n", func );
214 bprint( asp->commands, asp->cmdlen );
216 if ( afp_switch[ func ] != NULL ) {
217 asp->datalen = ASP_DATASIZ;
218 reply = (*afp_switch[ func ])(obj,
219 asp->commands, asp->cmdlen,
220 asp->data, &asp->datalen);
222 syslog( LOG_ERR, "(write) bad function %X", func );
226 if ( obj->options.flags & OPTION_DEBUG ) {
227 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
228 bprint( asp->data, asp->datalen );
230 if ( asp_wrtreply( asp, reply ) < 0 ) {
231 syslog( LOG_ERR, "asp_wrtreply: %m" );
237 * Bad asp packet. Probably should have asp filter them,
238 * since they are typically things like out-of-order packet.
240 syslog( LOG_INFO, "main: asp_getrequest: %d", reply );
244 if ( obj->options.flags & OPTION_DEBUG ) {
248 of_pforkdesc( stdout );