]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_asp.c
Add support for long filename mangling. Basically, tis code will take
[netatalk.git] / etc / afpd / afp_asp.c
1 /*
2  * $Id: afp_asp.c,v 1.16 2002-03-24 01:23:40 sibaz 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_authprint_remove(AFPObj *);
46
47 static __inline__ void afp_asp_close(AFPObj *obj)
48 {
49     ASP asp = obj->handle;
50
51     if (obj->options.authprintdir) afp_authprint_remove(obj);
52
53     if (obj->logout)
54         (*obj->logout)();
55
56     LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
57         asp->read_count / 1024.0, asp->write_count / 1024.0);
58     asp_close( asp );
59 }
60
61 /* removes the authprint trailing when appropriate */
62 static __inline__ void afp_authprint_remove(AFPObj *obj)
63 {
64     ASP asp = obj->handle;
65     char addr_filename[256];
66     char addr_filename_buff[256];
67     struct stat cap_st;
68
69     sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
70                 ntohs( asp->asp_sat.sat_addr.s_net )/256,
71                 ntohs( asp->asp_sat.sat_addr.s_net )%256,
72                 asp->asp_sat.sat_addr.s_node );
73
74     memset( addr_filename_buff, 0, 256 );
75
76     if(stat(addr_filename, &cap_st) == 0) {
77         if( S_ISREG(cap_st.st_mode) ) {
78             int len;
79             int capfd = open( addr_filename, O_RDONLY );
80             if ((len = read( capfd, addr_filename_buff, 256 )) > 0) {
81                 int file_pid;
82                 char *p_filepid;
83                 close(capfd);
84                 addr_filename_buff[len] = 0;
85                 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) {
86                     *p_filepid = '\0';
87                     p_filepid++;
88                     file_pid = atoi(p_filepid);
89                     if (file_pid == (int)getpid()) {
90                         if(unlink(addr_filename) == 0) {
91                             LOG(log_info, logtype_afpd, "removed %s", addr_filename);
92                         } else {
93                             LOG(log_info, logtype_afpd, "error removing %s: %s",
94                                     addr_filename, strerror(errno));
95                         }
96                     } else {
97                         LOG(log_info, logtype_afpd, "%s belongs to another pid %d",
98                              addr_filename, file_pid );
99                     }
100                 } else { /* no pid info */
101                     if (unlink(addr_filename) == 0) {
102                         LOG(log_info, logtype_afpd, "removed %s", addr_filename );
103                     } else {
104                         LOG(log_info, logtype_afpd, "error removing %s: %s",
105                                 addr_filename, strerror(errno));
106                     }
107                 }
108             } else {
109                 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename );
110             }
111         } else {
112             LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename );
113         }
114     } else {
115         LOG(log_info, logtype_afpd, "error stat'ing %s: %s",
116                    addr_filename, strerror(errno));
117     }
118 }
119
120 static void afp_asp_die(const int sig)
121 {
122     ASP asp = child->handle;
123
124     asp_attention(asp, AFPATTN_SHUTDOWN);
125     if ( asp_shutdown( asp ) < 0 ) {
126         LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
127     }
128
129     afp_asp_close(child);
130     if (sig == SIGTERM || sig == SIGALRM)
131         exit( 0 );
132     else
133         exit(sig);
134 }
135
136 static void afp_asp_timedown()
137 {
138     struct sigaction    sv;
139     struct itimerval    it;
140
141     /* shutdown and don't reconnect. server going down in 5 minutes. */
142     asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
143                   AFPATTN_TIME(5));
144
145     it.it_interval.tv_sec = 0;
146     it.it_interval.tv_usec = 0;
147     it.it_value.tv_sec = 300;
148     it.it_value.tv_usec = 0;
149     if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
150         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
151         afp_asp_die(1);
152     }
153
154     memset(&sv, 0, sizeof(sv));
155     sv.sa_handler = afp_asp_die;
156     sigemptyset( &sv.sa_mask );
157     sv.sa_flags = SA_RESTART;
158     if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
159         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
160         afp_asp_die(1);
161     }
162 }
163
164 void afp_over_asp(AFPObj *obj)
165 {
166     ASP asp;
167     struct sigaction  action;
168     int         func, ccnt = 0, reply = 0;
169
170     obj->exit = afp_asp_die;
171     obj->reply = (int (*)()) asp_cmdreply;
172     obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
173     child = obj;
174     asp = (ASP) obj->handle;
175
176     /* install signal handlers */
177     memset(&action, 0, sizeof(action));
178     action.sa_handler = afp_asp_timedown;
179     sigemptyset( &action.sa_mask );
180     action.sa_flags = SA_RESTART;
181     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
182         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
183         afp_asp_die(1);
184     }
185
186     action.sa_handler = afp_asp_die;
187     sigemptyset( &action.sa_mask );
188     action.sa_flags = SA_RESTART;
189     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
190         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
191         afp_asp_die(1);
192     }
193
194     LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
195         ntohs( asp->asp_sat.sat_addr.s_net ),
196         asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
197         ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
198         atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
199         atp_sockaddr( asp->asp_atp )->sat_port );
200
201     while ((reply = asp_getrequest(asp))) {
202         switch (reply) {
203         case ASPFUNC_CLOSE :
204             afp_asp_close(obj);
205             LOG(log_info, logtype_afpd, "done" );
206
207             if ( obj->options.flags & OPTION_DEBUG ) {
208                 printf( "done\n" );
209             }
210             return;
211             break;
212
213         case ASPFUNC_CMD :
214 #ifdef AFS
215             if ( writtenfork ) {
216                 if ( flushfork( writtenfork ) < 0 ) {
217                     LOG(log_error, logtype_afpd, "main flushfork: %s",
218                                                 strerror(errno));
219                 }
220                 writtenfork = NULL;
221             }
222 #endif /* AFS */
223             func = (u_char) asp->commands[0];
224             if ( obj->options.flags & OPTION_DEBUG ) {
225                 printf("command: %d (%s)\n", func, AfpNum2name(func));
226                 bprint( asp->commands, asp->cmdlen );
227             }
228             if ( afp_switch[ func ] != NULL ) {
229                 /*
230                  * The function called from afp_switch is expected to
231                  * read its parameters out of buf, put its
232                  * results in replybuf (updating rbuflen), and
233                  * return an error code.
234                 */
235                 asp->datalen = ASP_DATASIZ;
236                 reply = (*afp_switch[ func ])(obj,
237                                               asp->commands, asp->cmdlen,
238                                               asp->data, &asp->datalen);
239             } else {
240                 LOG(log_error, logtype_afpd, "bad function %X", func );
241                 asp->datalen = 0;
242                 reply = AFPERR_NOOP;
243             }
244             if ( obj->options.flags & OPTION_DEBUG ) {
245                 printf( "reply: %d, %d\n", reply, ccnt++ );
246                 bprint( asp->data, asp->datalen );
247             }
248
249             if ( asp_cmdreply( asp, reply ) < 0 ) {
250                 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
251                 afp_asp_die(1);
252             }
253             break;
254
255         case ASPFUNC_WRITE :
256             func = (u_char) asp->commands[0];
257             if ( obj->options.flags & OPTION_DEBUG ) {
258                 printf( "(write) command: %d\n", func );
259                 bprint( asp->commands, asp->cmdlen );
260             }
261             if ( afp_switch[ func ] != NULL ) {
262                 asp->datalen = ASP_DATASIZ;
263                 reply = (*afp_switch[ func ])(obj,
264                                               asp->commands, asp->cmdlen,
265                                               asp->data, &asp->datalen);
266             } else {
267                 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
268                 asp->datalen = 0;
269                 reply = AFPERR_NOOP;
270             }
271             if ( obj->options.flags & OPTION_DEBUG ) {
272                 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
273                 bprint( asp->data, asp->datalen );
274             }
275             if ( asp_wrtreply( asp, reply ) < 0 ) {
276                 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
277                 afp_asp_die(1);
278             }
279             break;
280         default:
281             /*
282                * Bad asp packet.  Probably should have asp filter them,
283                * since they are typically things like out-of-order packet.
284                */
285             LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
286             break;
287         }
288
289         if ( obj->options.flags & OPTION_DEBUG ) {
290 #ifdef notdef
291             pdesc( stdout );
292 #endif /* notdef */
293             of_pforkdesc( stdout );
294             fflush( stdout );
295         }
296     }
297 }
298
299 #endif