]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_asp.c
Symlink patch from Anton Starikov
[netatalk.git] / etc / afpd / afp_asp.c
1 /*
2  * $Id: afp_asp.c,v 1.27.2.1 2010-01-02 10:22:32 franklahm 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 static AFPObj *child;
47
48 static void afp_authprint_remove(AFPObj *);
49
50 static void afp_asp_close(AFPObj *obj)
51 {
52     ASP asp = obj->handle;
53
54     if (seteuid( obj->uid ) < 0) {
55         LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
56         exit(EXITERR_SYS);
57     }
58     close_all_vol();
59     if (obj->options.authprintdir) afp_authprint_remove(obj);
60
61     if (obj->logout)
62         (*obj->logout)();
63
64     LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
65         asp->read_count / 1024.0, asp->write_count / 1024.0);
66     asp_close( asp );
67 }
68
69 /* removes the authprint trailing when appropriate */
70 static void afp_authprint_remove(AFPObj *obj)
71 {
72     ASP asp = obj->handle;
73     char addr_filename[256];
74     char addr_filename_buff[256];
75     struct stat cap_st;
76
77     sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir,
78                 ntohs( asp->asp_sat.sat_addr.s_net )/256,
79                 ntohs( asp->asp_sat.sat_addr.s_net )%256,
80                 asp->asp_sat.sat_addr.s_node );
81
82     memset( addr_filename_buff, 0, 256 );
83
84     if(lstat(addr_filename, &cap_st) == 0) {
85         if( S_ISREG(cap_st.st_mode) ) {
86             int len;
87             int capfd = open( addr_filename, O_RDONLY );
88             if ((len = read( capfd, addr_filename_buff, 256 )) > 0) {
89                 int file_pid;
90                 char *p_filepid;
91                 close(capfd);
92                 addr_filename_buff[len] = 0;
93                 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) {
94                     *p_filepid = '\0';
95                     p_filepid++;
96                     file_pid = atoi(p_filepid);
97                     if (file_pid == (int)getpid()) {
98                         if(unlink(addr_filename) == 0) {
99                             LOG(log_info, logtype_afpd, "removed %s", addr_filename);
100                         } else {
101                             LOG(log_info, logtype_afpd, "error removing %s: %s",
102                                     addr_filename, strerror(errno));
103                         }
104                     } else {
105                         LOG(log_info, logtype_afpd, "%s belongs to another pid %d",
106                              addr_filename, file_pid );
107                     }
108                 } else { /* no pid info */
109                     if (unlink(addr_filename) == 0) {
110                         LOG(log_info, logtype_afpd, "removed %s", addr_filename );
111                     } else {
112                         LOG(log_info, logtype_afpd, "error removing %s: %s",
113                                 addr_filename, strerror(errno));
114                     }
115                 }
116             } else {
117                 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename );
118             }
119         } else {
120             LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename );
121         }
122     } else {
123         LOG(log_info, logtype_afpd, "error stat'ing %s: %s",
124                    addr_filename, strerror(errno));
125     }
126 }
127
128 /* ------------------------
129  * SIGTERM
130 */
131 static void afp_asp_die(const int sig)
132 {
133     ASP asp = child->handle;
134
135     asp_attention(asp, AFPATTN_SHUTDOWN);
136     if ( asp_shutdown( asp ) < 0 ) {
137         LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) );
138     }
139
140     afp_asp_close(child);
141     if (sig == SIGTERM || sig == SIGALRM)
142         exit( 0 );
143     else
144         exit(sig);
145 }
146
147 /* -----------------------------
148  * SIGUSR1
149  */
150 static void afp_asp_timedown(int sig _U_)
151 {
152     struct sigaction    sv;
153     struct itimerval    it;
154
155     /* shutdown and don't reconnect. server going down in 5 minutes. */
156     asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
157                   AFPATTN_TIME(5));
158
159     it.it_interval.tv_sec = 0;
160     it.it_interval.tv_usec = 0;
161     it.it_value.tv_sec = 300;
162     it.it_value.tv_usec = 0;
163     if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
164         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
165         afp_asp_die(EXITERR_SYS);
166     }
167
168     memset(&sv, 0, sizeof(sv));
169     sv.sa_handler = afp_asp_die;
170     sigemptyset( &sv.sa_mask );
171     sigaddset(&sv.sa_mask, SIGHUP);
172     sigaddset(&sv.sa_mask, SIGTERM);
173     sv.sa_flags = SA_RESTART;
174     if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
175         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
176         afp_asp_die(EXITERR_SYS);
177     }
178
179     /* ignore myself */
180     sv.sa_handler = SIG_IGN;
181     sigemptyset( &sv.sa_mask );
182     sv.sa_flags = SA_RESTART;
183     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
184         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGUSR1: %s", strerror(errno) );
185         afp_asp_die(EXITERR_SYS);
186     }
187 }
188
189 /* ---------------------------------
190  * SIGHUP reload configuration file
191 */
192 extern volatile int reload_request;
193
194 static void afp_asp_reload(int sig _U_)
195 {
196     reload_request = 1;
197 }
198
199 /* ---------------------- */
200 #ifdef SERVERTEXT
201 static void afp_asp_getmesg (int sig _U_)
202 {
203     readmessage(child);
204     asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5));
205 }
206 #endif /* SERVERTEXT */
207
208 /* ---------------------- */
209 void afp_over_asp(AFPObj *obj)
210 {
211     ASP asp;
212     struct sigaction  action;
213     int         func,  reply = 0;
214 #ifdef DEBUG1
215     int ccnt = 0;
216 #endif    
217
218     obj->exit = afp_asp_die;
219     obj->reply = (int (*)()) asp_cmdreply;
220     obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
221     child = obj;
222     asp = (ASP) obj->handle;
223
224     /* install signal handlers 
225      * With ASP tickle handler is done in the parent process
226     */
227     memset(&action, 0, sizeof(action));
228
229     /* install SIGHUP */
230     action.sa_handler = afp_asp_reload; 
231     sigemptyset( &action.sa_mask );
232     sigaddset(&action.sa_mask, SIGTERM);
233     sigaddset(&action.sa_mask, SIGUSR1);
234 #ifdef SERVERTEXT
235     sigaddset(&action.sa_mask, SIGUSR2);
236 #endif    
237     action.sa_flags = SA_RESTART;
238     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
239         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
240         afp_asp_die(EXITERR_SYS);
241     }
242
243     /*  install SIGTERM */
244     action.sa_handler = afp_asp_die;
245     sigemptyset( &action.sa_mask );
246     sigaddset(&action.sa_mask, SIGHUP);
247     sigaddset(&action.sa_mask, SIGUSR1);
248 #ifdef SERVERTEXT
249     sigaddset(&action.sa_mask, SIGUSR2);
250 #endif    
251     action.sa_flags = SA_RESTART;
252     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
253         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
254         afp_asp_die(EXITERR_SYS);
255     }
256
257 #ifdef SERVERTEXT
258     /* Added for server message support */
259     action.sa_handler = afp_asp_getmesg;
260     sigemptyset( &action.sa_mask );
261     sigaddset(&action.sa_mask, SIGTERM);
262     sigaddset(&action.sa_mask, SIGUSR1);
263     sigaddset(&action.sa_mask, SIGHUP);
264     action.sa_flags = SA_RESTART;
265     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
266         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
267         afp_asp_die(EXITERR_SYS);
268     }
269 #endif /* SERVERTEXT */
270
271     /*  SIGUSR1 - set down in 5 minutes  */
272     action.sa_handler = afp_asp_timedown; 
273     sigemptyset( &action.sa_mask );
274     sigaddset(&action.sa_mask, SIGHUP);
275     sigaddset(&action.sa_mask, SIGTERM);
276 #ifdef SERVERTEXT
277     sigaddset(&action.sa_mask, SIGUSR2);
278 #endif    
279     action.sa_flags = SA_RESTART;
280     if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) {
281         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
282         afp_asp_die(EXITERR_SYS);
283     }
284
285     LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
286         ntohs( asp->asp_sat.sat_addr.s_net ),
287         asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
288         ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
289         atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
290         atp_sockaddr( asp->asp_atp )->sat_port );
291
292     while ((reply = asp_getrequest(asp))) {
293         if (reload_request) {
294             reload_request = 0;
295             load_volumes(child);
296         }
297         switch (reply) {
298         case ASPFUNC_CLOSE :
299             afp_asp_close(obj);
300             LOG(log_info, logtype_afpd, "done" );
301
302 #ifdef DEBUG1
303             if ( obj->options.flags & OPTION_DEBUG ) {
304                 printf( "done\n" );
305             }
306 #endif
307             return;
308             break;
309
310         case ASPFUNC_CMD :
311 #ifdef AFS
312             if ( writtenfork ) {
313                 if ( flushfork( writtenfork ) < 0 ) {
314                     LOG(log_error, logtype_afpd, "main flushfork: %s",
315                                                 strerror(errno));
316                 }
317                 writtenfork = NULL;
318             }
319 #endif /* AFS */
320             func = (u_char) asp->commands[0];
321 #ifdef DEBUG1
322             if ( obj->options.flags & OPTION_DEBUG ) {
323                 printf("command: %d (%s)\n", func, AfpNum2name(func));
324                 bprint( asp->commands, asp->cmdlen );
325             }
326 #endif            
327             if ( afp_switch[ func ] != NULL ) {
328                 /*
329                  * The function called from afp_switch is expected to
330                  * read its parameters out of buf, put its
331                  * results in replybuf (updating rbuflen), and
332                  * return an error code.
333                 */
334                 asp->datalen = ASP_DATASIZ;
335                 reply = (*afp_switch[ func ])(obj,
336                                               asp->commands, asp->cmdlen,
337                                               asp->data, &asp->datalen);
338 #ifdef FORCE_UIDGID
339                 /* bring everything back to old euid, egid */
340                 if (obj->force_uid)
341                     restore_uidgid ( &obj->uidgid );
342 #endif /* FORCE_UIDGID */
343             } else {
344                 LOG(log_error, logtype_afpd, "bad function %X", func );
345                 asp->datalen = 0;
346                 reply = AFPERR_NOOP;
347             }
348 #ifdef DEBUG1
349             if ( obj->options.flags & OPTION_DEBUG ) {
350                 printf( "reply: %d, %d\n", reply, ccnt++ );
351                 bprint( asp->data, asp->datalen );
352             }
353 #endif
354             if ( asp_cmdreply( asp, reply ) < 0 ) {
355                 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
356                 afp_asp_die(EXITERR_CLNT);
357             }
358             break;
359
360         case ASPFUNC_WRITE :
361             func = (u_char) asp->commands[0];
362 #ifdef DEBUG1
363             if ( obj->options.flags & OPTION_DEBUG ) {
364                 printf( "(write) command: %d\n", func );
365                 bprint( asp->commands, asp->cmdlen );
366             }
367 #endif
368             if ( afp_switch[ func ] != NULL ) {
369                 asp->datalen = ASP_DATASIZ;
370                 reply = (*afp_switch[ func ])(obj,
371                                               asp->commands, asp->cmdlen,
372                                               asp->data, &asp->datalen);
373 #ifdef FORCE_UIDGID
374                 /* bring everything back to old euid, egid */
375                 if (obj->force_uid)
376                     restore_uidgid ( &obj->uidgid );
377 #endif /* FORCE_UIDGID */
378             } else {
379                 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
380                 asp->datalen = 0;
381                 reply = AFPERR_NOOP;
382             }
383 #ifdef DEBUG1
384             if ( obj->options.flags & OPTION_DEBUG ) {
385                 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
386                 bprint( asp->data, asp->datalen );
387             }
388 #endif
389             if ( asp_wrtreply( asp, reply ) < 0 ) {
390                 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
391                 afp_asp_die(EXITERR_CLNT);
392             }
393             break;
394         default:
395             /*
396                * Bad asp packet.  Probably should have asp filter them,
397                * since they are typically things like out-of-order packet.
398                */
399             LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
400             break;
401         }
402 #ifdef DEBUG1
403         if ( obj->options.flags & OPTION_DEBUG ) {
404 #ifdef notdef
405             pdesc( stdout );
406 #endif /* notdef */
407             of_pforkdesc( stdout );
408             fflush( stdout );
409         }
410 #endif
411     }
412 }
413
414 #endif