]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_asp.c
MFH: Sanitize some syslog messages.
[netatalk.git] / etc / afpd / afp_asp.c
1 /*
2  * $Id: afp_asp.c,v 1.6.2.4 2002-03-05 02:08:12 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 <syslog.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     syslog(LOG_INFO, "%.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         syslog( LOG_ERR, "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         syslog( LOG_ERR, "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         syslog( LOG_ERR, "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         syslog( LOG_ERR, "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         syslog( LOG_ERR, "afp_over_asp: sigaction: %s", strerror(errno) );
128         afp_asp_die(1);
129     }
130
131     syslog( LOG_INFO, "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                         syslog(LOG_INFO, "removed %s", addr_filename);
153                     } else {
154                         syslog(LOG_INFO, "error removing %s: %s",
155                                addr_filename, strerror(errno));
156                     }
157                 } else {
158                     syslog(LOG_INFO, "error stat'ing %s: %s",
159                            addr_filename, strerror(errno));
160                 }
161             }
162
163             afp_asp_close(obj);
164             syslog( LOG_INFO, "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                     syslog( LOG_ERR, "main flushfork: %s", strerror(errno) );
177                 }
178                 writtenfork = NULL;
179             }
180 #endif /* AFS */
181             func = (u_char) asp->commands[0];
182             if ( obj->options.flags & OPTION_DEBUG ) {
183                 printf( "command: %d\n", func );
184                 bprint( asp->commands, asp->cmdlen );
185             }
186             if ( afp_switch[ func ] != NULL ) {
187                 /*
188                  * The function called from afp_switch is expected to
189                  * read its parameters out of buf, put its
190                  * results in replybuf (updating rbuflen), and
191                  * return an error code.
192                 */
193                 asp->datalen = ASP_DATASIZ;
194                 reply = (*afp_switch[ func ])(obj,
195                                               asp->commands, asp->cmdlen,
196                                               asp->data, &asp->datalen);
197             } else {
198                 syslog( LOG_ERR, "bad function %X", func );
199                 asp->datalen = 0;
200                 reply = AFPERR_NOOP;
201             }
202             if ( obj->options.flags & OPTION_DEBUG ) {
203                 printf( "reply: %d, %d\n", reply, ccnt++ );
204                 bprint( asp->data, asp->datalen );
205             }
206
207             if ( asp_cmdreply( asp, reply ) < 0 ) {
208                 syslog( LOG_ERR, "asp_cmdreply: %s", strerror(errno) );
209                 afp_asp_die(1);
210             }
211             break;
212
213         case ASPFUNC_WRITE :
214             func = (u_char) asp->commands[0];
215             if ( obj->options.flags & OPTION_DEBUG ) {
216                 printf( "(write) command: %d\n", func );
217                 bprint( asp->commands, asp->cmdlen );
218             }
219             if ( afp_switch[ func ] != NULL ) {
220                 asp->datalen = ASP_DATASIZ;
221                 reply = (*afp_switch[ func ])(obj,
222                                               asp->commands, asp->cmdlen,
223                                               asp->data, &asp->datalen);
224             } else {
225                 syslog( LOG_ERR, "(write) bad function %X", func );
226                 asp->datalen = 0;
227                 reply = AFPERR_NOOP;
228             }
229             if ( obj->options.flags & OPTION_DEBUG ) {
230                 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
231                 bprint( asp->data, asp->datalen );
232             }
233             if ( asp_wrtreply( asp, reply ) < 0 ) {
234                 syslog( LOG_ERR, "asp_wrtreply: %s", strerror(errno) );
235                 afp_asp_die(1);
236             }
237             break;
238         default:
239             /*
240                * Bad asp packet.  Probably should have asp filter them,
241                * since they are typically things like out-of-order packet.
242                */
243             syslog( LOG_INFO, "main: asp_getrequest: %d", reply );
244             break;
245         }
246
247         if ( obj->options.flags & OPTION_DEBUG ) {
248 #ifdef notdef
249             pdesc( stdout );
250 #endif /* notdef */
251             of_pforkdesc( stdout );
252             fflush( stdout );
253         }
254     }
255 }
256
257 #endif