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.
19 #include <netatalk/endian.h>
20 #include <atalk/atp.h>
21 #include <atalk/asp.h>
22 #include <atalk/compat.h>
23 #include <atalk/util.h>
30 extern struct oforks *writtenfork;
32 /* for CAP style authenticated printing */
34 extern int addr_net, addr_node, addr_uid;
39 static __inline__ void afp_asp_close(AFPObj *obj)
41 ASP asp = obj->handle;
47 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
48 asp->read_count / 1024.0, asp->write_count / 1024.0);
51 static void afp_asp_die(const int sig)
53 ASP asp = child->handle;
55 asp_attention(asp, AFPATTN_SHUTDOWN);
56 if ( asp_shutdown( asp ) < 0 ) {
57 syslog( LOG_ERR, "afp_die: asp_shutdown: %m" );
61 if (sig == SIGTERM || sig == SIGALRM)
67 static void afp_asp_timedown()
72 /* shutdown and don't reconnect. server going down in 5 minutes. */
73 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
76 it.it_interval.tv_sec = 0;
77 it.it_interval.tv_usec = 0;
78 it.it_value.tv_sec = 300;
79 it.it_value.tv_usec = 0;
80 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
81 syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
85 memset(&sv, 0, sizeof(sv));
86 sv.sa_handler = afp_asp_die;
87 sigemptyset( &sv.sa_mask );
88 sv.sa_flags = SA_RESTART;
89 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
90 syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
95 void afp_over_asp(AFPObj *obj)
98 struct sigaction action;
99 int func, ccnt = 0, reply = 0;
102 char addr_filename[256];
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" );
131 addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
132 addr_node = asp->asp_sat.sat_addr.s_node;
135 syslog( LOG_INFO, "session from %u.%u:%u on %u.%u:%u",
136 ntohs( asp->asp_sat.sat_addr.s_net ),
137 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
138 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
139 atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
140 atp_sockaddr( asp->asp_atp )->sat_port );
142 while ((reply = asp_getrequest(asp))) {
146 syslog( LOG_INFO, "done" );
149 sprintf(addr_filename, "%s/net%d.%dnode%d", CAPDIR, addr_net/256, addr_net%256, addr_node);
150 if(stat(addr_filename, &cap_st) == 0) {
151 if(unlink(addr_filename) == 0) {
152 syslog(LOG_INFO, "removed %s", addr_filename);
154 syslog(LOG_INFO, "error removing %s: %m", addr_filename);
157 syslog(LOG_INFO, "error stat'ing %s: %m", addr_filename);
161 if ( obj->options.flags & OPTION_DEBUG ) {
170 if ( flushfork( writtenfork ) < 0 ) {
171 syslog( LOG_ERR, "main flushfork: %m" );
176 func = (u_char) asp->commands[0];
177 if ( obj->options.flags & OPTION_DEBUG ) {
178 printf( "command: %d\n", func );
179 bprint( asp->commands, asp->cmdlen );
181 if ( afp_switch[ func ] != NULL ) {
183 * The function called from afp_switch is expected to
184 * read its parameters out of buf, put its
185 * results in replybuf (updating rbuflen), and
186 * return an error code.
188 asp->datalen = ASP_DATASIZ;
189 reply = (*afp_switch[ func ])(obj,
190 asp->commands, asp->cmdlen,
191 asp->data, &asp->datalen);
193 syslog( LOG_ERR, "bad function %X", func );
197 if ( obj->options.flags & OPTION_DEBUG ) {
198 printf( "reply: %d, %d\n", reply, ccnt++ );
199 bprint( asp->data, asp->datalen );
202 if ( asp_cmdreply( asp, reply ) < 0 ) {
203 syslog( LOG_ERR, "asp_cmdreply: %m" );
209 func = (u_char) asp->commands[0];
210 if ( obj->options.flags & OPTION_DEBUG ) {
211 printf( "(write) command: %d\n", func );
212 bprint( asp->commands, asp->cmdlen );
214 if ( afp_switch[ func ] != NULL ) {
215 asp->datalen = ASP_DATASIZ;
216 reply = (*afp_switch[ func ])(obj,
217 asp->commands, asp->cmdlen,
218 asp->data, &asp->datalen);
220 syslog( LOG_ERR, "(write) bad function %X", func );
224 if ( obj->options.flags & OPTION_DEBUG ) {
225 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
226 bprint( asp->data, asp->datalen );
228 if ( asp_wrtreply( asp, reply ) < 0 ) {
229 syslog( LOG_ERR, "asp_wrtreply: %m" );
235 * Bad asp packet. Probably should have asp filter them,
236 * since they are typically things like out-of-order packet.
238 syslog( LOG_INFO, "main: asp_getrequest: %d", reply );
242 if ( obj->options.flags & OPTION_DEBUG ) {
246 of_pforkdesc( stdout );