]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_asp.c
Sanitized some LOG messages.
[netatalk.git] / etc / afpd / afp_asp.c
1 /*
2  * $Id: afp_asp.c,v 1.12 2002-02-28 21:20:39 jmarcus Exp $
3  *
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.
7  *
8  * modified from main.c. this handles afp over asp. 
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #ifndef NO_DDP
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <signal.h>
21 #include <atalk/logger.h>
22 #include <errno.h>
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif /* HAVE_SYS_TIME_H */
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif /* HAVE_SYS_STAT_H */
29
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>
35
36 #include "globals.h"
37 #include "switch.h"
38 #include "auth.h"
39 #include "fork.h"
40
41 extern struct oforks    *writtenfork;
42
43 static AFPObj *child;
44
45 static __inline__ void afp_asp_close(AFPObj *obj)
46 {
47     ASP asp = obj->handle;
48
49     if (obj->logout)
50         (*obj->logout)();
51
52     LOG(log_info, logtype_default, "%.2fKB read, %.2fKB written",
53         asp->read_count / 1024.0, asp->write_count / 1024.0);
54     asp_close( asp );
55 }
56
57 static void afp_asp_die(const int sig)
58 {
59     ASP asp = child->handle;
60
61     asp_attention(asp, AFPATTN_SHUTDOWN);
62     if ( asp_shutdown( asp ) < 0 ) {
63         LOG(log_error, logtype_default, "afp_die: asp_shutdown: %s", strerror(errno) );
64     }
65
66     afp_asp_close(child);
67     if (sig == SIGTERM || sig == SIGALRM)
68         exit( 0 );
69     else
70         exit(sig);
71 }
72
73 static void afp_asp_timedown()
74 {
75     struct sigaction    sv;
76     struct itimerval    it;
77
78     /* shutdown and don't reconnect. server going down in 5 minutes. */
79     asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
80                   AFPATTN_TIME(5));
81
82     it.it_interval.tv_sec = 0;
83     it.it_interval.tv_usec = 0;
84     it.it_value.tv_sec = 300;
85     it.it_value.tv_usec = 0;
86     if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
87         LOG(log_error, logtype_default, "afp_timedown: setitimer: %s", strerror(errno) );
88         afp_asp_die(1);
89     }
90
91     memset(&sv, 0, sizeof(sv));
92     sv.sa_handler = afp_asp_die;
93     sigemptyset( &sv.sa_mask );
94     sv.sa_flags = SA_RESTART;
95     if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
96         LOG(log_error, logtype_default, "afp_timedown: sigaction: %s", strerror(errno) );
97         afp_asp_die(1);
98     }
99 }
100
101 void afp_over_asp(AFPObj *obj)
102 {
103     ASP asp;
104     struct sigaction  action;
105     int         func, ccnt = 0, reply = 0;
106
107     obj->exit = afp_asp_die;
108     obj->reply = (int (*)()) asp_cmdreply;
109     obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
110     child = obj;
111     asp = (ASP) obj->handle;
112
113     /* install signal handlers */
114     memset(&action, 0, sizeof(action));
115     action.sa_handler = afp_asp_timedown;
116     sigemptyset( &action.sa_mask );
117     action.sa_flags = SA_RESTART;
118     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
119         LOG(log_error, logtype_default, "afp_over_asp: sigaction: %s", strerror(errno) );
120         afp_asp_die(1);
121     }
122
123     action.sa_handler = afp_asp_die;
124     sigemptyset( &action.sa_mask );
125     action.sa_flags = SA_RESTART;
126     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
127         LOG(log_error, logtype_default, "afp_over_asp: sigaction: %s", strerror(errno) );
128         afp_asp_die(1);
129     }
130
131     LOG(log_info, logtype_default, "session from %u.%u:%u on %u.%u:%u",
132         ntohs( asp->asp_sat.sat_addr.s_net ),
133         asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
134         ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
135         atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
136         atp_sockaddr( asp->asp_atp )->sat_port );
137
138     while ((reply = asp_getrequest(asp))) {
139         switch (reply) {
140         case ASPFUNC_CLOSE :
141             if (obj->options.authprintdir) {
142                 char addr_filename[256];
143                 struct stat cap_st;
144
145                 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
146                         ntohs( asp->asp_sat.sat_addr.s_net )/256,
147                         ntohs( asp->asp_sat.sat_addr.s_net )%256,
148                         asp->asp_sat.sat_addr.s_node );
149
150                 if(stat(addr_filename, &cap_st) == 0) {
151                     if(unlink(addr_filename) == 0) {
152                         LOG(log_info, logtype_default, "removed %s", addr_filename);
153                     } else {
154                         LOG(log_info, logtype_default, "error removing %s: %s",
155                             addr_filename, strerror(errno));
156                     }
157                 } else {
158                     LOG(log_info, logtype_default, "error stat'ing %s: %s",
159                         addr_filename, strerror(errno));
160                 }
161             }
162
163             afp_asp_close(obj);
164             LOG(log_info, logtype_default, "done" );
165
166             if ( obj->options.flags & OPTION_DEBUG ) {
167                 printf( "done\n" );
168             }
169             return;
170             break;
171
172         case ASPFUNC_CMD :
173 #ifdef AFS
174             if ( writtenfork ) {
175                 if ( flushfork( writtenfork ) < 0 ) {
176                     LOG(log_error, logtype_default, "main flushfork: %s",
177                                                 strerror(errno));
178                 }
179                 writtenfork = NULL;
180             }
181 #endif /* AFS */
182             func = (u_char) asp->commands[0];
183             if ( obj->options.flags & OPTION_DEBUG ) {
184                 printf( "command: %d\n", func );
185                 bprint( asp->commands, asp->cmdlen );
186             }
187             if ( afp_switch[ func ] != NULL ) {
188                 /*
189                  * The function called from afp_switch is expected to
190                  * read its parameters out of buf, put its
191                  * results in replybuf (updating rbuflen), and
192                  * return an error code.
193                 */
194                 asp->datalen = ASP_DATASIZ;
195                 reply = (*afp_switch[ func ])(obj,
196                                               asp->commands, asp->cmdlen,
197                                               asp->data, &asp->datalen);
198             } else {
199                 LOG(log_error, logtype_default, "bad function %X", func );
200                 asp->datalen = 0;
201                 reply = AFPERR_NOOP;
202             }
203             if ( obj->options.flags & OPTION_DEBUG ) {
204                 printf( "reply: %d, %d\n", reply, ccnt++ );
205                 bprint( asp->data, asp->datalen );
206             }
207
208             if ( asp_cmdreply( asp, reply ) < 0 ) {
209                 LOG(log_error, logtype_default, "asp_cmdreply: %s", strerror(errno) );
210                 afp_asp_die(1);
211             }
212             break;
213
214         case ASPFUNC_WRITE :
215             func = (u_char) asp->commands[0];
216             if ( obj->options.flags & OPTION_DEBUG ) {
217                 printf( "(write) command: %d\n", func );
218                 bprint( asp->commands, asp->cmdlen );
219             }
220             if ( afp_switch[ func ] != NULL ) {
221                 asp->datalen = ASP_DATASIZ;
222                 reply = (*afp_switch[ func ])(obj,
223                                               asp->commands, asp->cmdlen,
224                                               asp->data, &asp->datalen);
225             } else {
226                 LOG(log_error, logtype_default, "(write) bad function %X", func );
227                 asp->datalen = 0;
228                 reply = AFPERR_NOOP;
229             }
230             if ( obj->options.flags & OPTION_DEBUG ) {
231                 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
232                 bprint( asp->data, asp->datalen );
233             }
234             if ( asp_wrtreply( asp, reply ) < 0 ) {
235                 LOG(log_error, logtype_default, "asp_wrtreply: %s", strerror(errno) );
236                 afp_asp_die(1);
237             }
238             break;
239         default:
240             /*
241                * Bad asp packet.  Probably should have asp filter them,
242                * since they are typically things like out-of-order packet.
243                */
244             LOG(log_info, logtype_default, "main: asp_getrequest: %d", reply );
245             break;
246         }
247
248         if ( obj->options.flags & OPTION_DEBUG ) {
249 #ifdef notdef
250             pdesc( stdout );
251 #endif /* notdef */
252             of_pforkdesc( stdout );
253             fflush( stdout );
254         }
255     }
256 }
257
258 #endif