2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3 * Copyright (c) 1990,1993 Regents of The University of Michigan.
4 * All Rights Reserved. See COPYRIGHT.
6 * modified from main.c. this handles afp over asp.
23 #include <netatalk/endian.h>
24 #include <atalk/atp.h>
25 #include <atalk/asp.h>
26 #include <atalk/compat.h>
27 #include <atalk/util.h>
34 extern struct oforks *writtenfork;
38 static __inline__ void afp_asp_close(AFPObj *obj)
40 ASP asp = obj->handle;
46 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
47 asp->read_count / 1024.0, asp->write_count / 1024.0);
50 static void afp_asp_die(const int sig)
52 ASP asp = child->handle;
54 asp_attention(asp, AFPATTN_SHUTDOWN);
55 if ( asp_shutdown( asp ) < 0 ) {
56 syslog( LOG_ERR, "afp_die: asp_shutdown: %m" );
60 if (sig == SIGTERM || sig == SIGALRM)
66 static void afp_asp_timedown()
71 /* shutdown and don't reconnect. server going down in 5 minutes. */
72 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
75 it.it_interval.tv_sec = 0;
76 it.it_interval.tv_usec = 0;
77 it.it_value.tv_sec = 300;
78 it.it_value.tv_usec = 0;
79 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
80 syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
84 memset(&sv, 0, sizeof(sv));
85 sv.sa_handler = afp_asp_die;
86 sigemptyset( &sv.sa_mask );
87 sv.sa_flags = SA_RESTART;
88 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
89 syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
94 void afp_over_asp(AFPObj *obj)
97 struct sigaction action;
98 int func, ccnt = 0, reply = 0;
100 obj->exit = afp_asp_die;
101 obj->reply = (int (*)()) asp_cmdreply;
102 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
104 asp = (ASP) obj->handle;
106 /* install signal handlers */
107 memset(&action, 0, sizeof(action));
108 action.sa_handler = afp_asp_timedown;
109 sigemptyset( &action.sa_mask );
110 action.sa_flags = SA_RESTART;
111 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
112 syslog( LOG_ERR, "afp_over_asp: sigaction: %m" );
116 action.sa_handler = afp_asp_die;
117 sigemptyset( &action.sa_mask );
118 action.sa_flags = SA_RESTART;
119 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
120 syslog( LOG_ERR, "afp_over_asp: sigaction: %m" );
124 syslog( LOG_INFO, "session from %u.%u:%u on %u.%u:%u",
125 ntohs( asp->asp_sat.sat_addr.s_net ),
126 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
127 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
128 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
129 atp_sockaddr( asp->asp_atp )->sat_port );
131 while ((reply = asp_getrequest(asp))) {
134 if (obj->options.authprintdir) {
135 char addr_filename[256];
138 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
139 ntohs( asp->asp_sat.sat_addr.s_net )/256,
140 ntohs( asp->asp_sat.sat_addr.s_net )%256,
141 asp->asp_sat.sat_addr.s_node );
143 if(stat(addr_filename, &cap_st) == 0) {
144 if(unlink(addr_filename) == 0) {
145 syslog(LOG_INFO, "removed %s", addr_filename);
147 syslog(LOG_INFO, "error removing %s: %m", addr_filename);
150 syslog(LOG_INFO, "error stat'ing %s: %m", addr_filename);
155 syslog( LOG_INFO, "done" );
157 if ( obj->options.flags & OPTION_DEBUG ) {
166 if ( flushfork( writtenfork ) < 0 ) {
167 syslog( LOG_ERR, "main flushfork: %m" );
172 func = (u_char) asp->commands[0];
173 if ( obj->options.flags & OPTION_DEBUG ) {
174 printf( "command: %d\n", func );
175 bprint( asp->commands, asp->cmdlen );
177 if ( afp_switch[ func ] != NULL ) {
179 * The function called from afp_switch is expected to
180 * read its parameters out of buf, put its
181 * results in replybuf (updating rbuflen), and
182 * return an error code.
184 asp->datalen = ASP_DATASIZ;
185 reply = (*afp_switch[ func ])(obj,
186 asp->commands, asp->cmdlen,
187 asp->data, &asp->datalen);
189 syslog( LOG_ERR, "bad function %X", func );
193 if ( obj->options.flags & OPTION_DEBUG ) {
194 printf( "reply: %d, %d\n", reply, ccnt++ );
195 bprint( asp->data, asp->datalen );
198 if ( asp_cmdreply( asp, reply ) < 0 ) {
199 syslog( LOG_ERR, "asp_cmdreply: %m" );
205 func = (u_char) asp->commands[0];
206 if ( obj->options.flags & OPTION_DEBUG ) {
207 printf( "(write) command: %d\n", func );
208 bprint( asp->commands, asp->cmdlen );
210 if ( afp_switch[ func ] != NULL ) {
211 asp->datalen = ASP_DATASIZ;
212 reply = (*afp_switch[ func ])(obj,
213 asp->commands, asp->cmdlen,
214 asp->data, &asp->datalen);
216 syslog( LOG_ERR, "(write) bad function %X", func );
220 if ( obj->options.flags & OPTION_DEBUG ) {
221 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
222 bprint( asp->data, asp->datalen );
224 if ( asp_wrtreply( asp, reply ) < 0 ) {
225 syslog( LOG_ERR, "asp_wrtreply: %m" );
231 * Bad asp packet. Probably should have asp filter them,
232 * since they are typically things like out-of-order packet.
234 syslog( LOG_INFO, "main: asp_getrequest: %d", reply );
238 if ( obj->options.flags & OPTION_DEBUG ) {
242 of_pforkdesc( stdout );