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