]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_asp.c
Rewritten TM used size calculation
[netatalk.git] / etc / afpd / afp_asp.c
1 /*
2  * $Id: afp_asp.c,v 1.29 2010-03-09 06:55:12 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 #include <atalk/globals.h>
36
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     AFPobj = obj;
219     obj->exit = afp_asp_die;
220     obj->reply = (int (*)()) asp_cmdreply;
221     obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
222     child = obj;
223     asp = (ASP) obj->handle;
224
225     /* install signal handlers 
226      * With ASP tickle handler is done in the parent process
227     */
228     memset(&action, 0, sizeof(action));
229
230     /* install SIGHUP */
231     action.sa_handler = afp_asp_reload; 
232     sigemptyset( &action.sa_mask );
233     sigaddset(&action.sa_mask, SIGTERM);
234     sigaddset(&action.sa_mask, SIGUSR1);
235 #ifdef SERVERTEXT
236     sigaddset(&action.sa_mask, SIGUSR2);
237 #endif    
238     action.sa_flags = SA_RESTART;
239     if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
240         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
241         afp_asp_die(EXITERR_SYS);
242     }
243
244     /*  install SIGTERM */
245     action.sa_handler = afp_asp_die;
246     sigemptyset( &action.sa_mask );
247     sigaddset(&action.sa_mask, SIGHUP);
248     sigaddset(&action.sa_mask, SIGUSR1);
249 #ifdef SERVERTEXT
250     sigaddset(&action.sa_mask, SIGUSR2);
251 #endif    
252     action.sa_flags = SA_RESTART;
253     if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
254         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
255         afp_asp_die(EXITERR_SYS);
256     }
257
258 #ifdef SERVERTEXT
259     /* Added for server message support */
260     action.sa_handler = afp_asp_getmesg;
261     sigemptyset( &action.sa_mask );
262     sigaddset(&action.sa_mask, SIGTERM);
263     sigaddset(&action.sa_mask, SIGUSR1);
264     sigaddset(&action.sa_mask, SIGHUP);
265     action.sa_flags = SA_RESTART;
266     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
267         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
268         afp_asp_die(EXITERR_SYS);
269     }
270 #endif /* SERVERTEXT */
271
272     /*  SIGUSR1 - set down in 5 minutes  */
273     action.sa_handler = afp_asp_timedown; 
274     sigemptyset( &action.sa_mask );
275     sigaddset(&action.sa_mask, SIGHUP);
276     sigaddset(&action.sa_mask, SIGTERM);
277 #ifdef SERVERTEXT
278     sigaddset(&action.sa_mask, SIGUSR2);
279 #endif    
280     action.sa_flags = SA_RESTART;
281     if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) {
282         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
283         afp_asp_die(EXITERR_SYS);
284     }
285
286     if (dircache_init(obj->options.dircachesize) != 0) {
287         LOG(log_error, logtype_afpd, "afp_over_asp: dircache_init error");
288         afp_asp_die(EXITERR_SYS);
289     }
290
291     LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
292         ntohs( asp->asp_sat.sat_addr.s_net ),
293         asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
294         ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
295         atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
296         atp_sockaddr( asp->asp_atp )->sat_port );
297
298     while ((reply = asp_getrequest(asp))) {
299         if (reload_request) {
300             reload_request = 0;
301             load_volumes(child);
302         }
303         switch (reply) {
304         case ASPFUNC_CLOSE :
305             afp_asp_close(obj);
306             LOG(log_info, logtype_afpd, "done" );
307
308 #ifdef DEBUG1
309             if ( obj->options.flags & OPTION_DEBUG ) {
310                 printf( "done\n" );
311             }
312 #endif
313             return;
314             break;
315
316         case ASPFUNC_CMD :
317 #ifdef AFS
318             if ( writtenfork ) {
319                 if ( flushfork( writtenfork ) < 0 ) {
320                     LOG(log_error, logtype_afpd, "main flushfork: %s",
321                                                 strerror(errno));
322                 }
323                 writtenfork = NULL;
324             }
325 #endif /* AFS */
326             func = (u_char) asp->commands[0];
327 #ifdef DEBUG1
328             if ( obj->options.flags & OPTION_DEBUG ) {
329                 printf("command: %d (%s)\n", func, AfpNum2name(func));
330                 bprint( asp->commands, asp->cmdlen );
331             }
332 #endif            
333             if ( afp_switch[ func ] != NULL ) {
334                 /*
335                  * The function called from afp_switch is expected to
336                  * read its parameters out of buf, put its
337                  * results in replybuf (updating rbuflen), and
338                  * return an error code.
339                 */
340                 asp->datalen = ASP_DATASIZ;
341                 reply = (*afp_switch[ func ])(obj,
342                                               asp->commands, asp->cmdlen,
343                                               asp->data, &asp->datalen);
344 #ifdef FORCE_UIDGID
345                 /* bring everything back to old euid, egid */
346                 if (obj->force_uid)
347                     restore_uidgid ( &obj->uidgid );
348 #endif /* FORCE_UIDGID */
349             } else {
350                 LOG(log_error, logtype_afpd, "bad function %X", func );
351                 asp->datalen = 0;
352                 reply = AFPERR_NOOP;
353             }
354 #ifdef DEBUG1
355             if ( obj->options.flags & OPTION_DEBUG ) {
356                 printf( "reply: %d, %d\n", reply, ccnt++ );
357                 bprint( asp->data, asp->datalen );
358             }
359 #endif
360             if ( asp_cmdreply( asp, reply ) < 0 ) {
361                 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
362                 afp_asp_die(EXITERR_CLNT);
363             }
364             break;
365
366         case ASPFUNC_WRITE :
367             func = (u_char) asp->commands[0];
368 #ifdef DEBUG1
369             if ( obj->options.flags & OPTION_DEBUG ) {
370                 printf( "(write) command: %d\n", func );
371                 bprint( asp->commands, asp->cmdlen );
372             }
373 #endif
374             if ( afp_switch[ func ] != NULL ) {
375                 asp->datalen = ASP_DATASIZ;
376                 reply = (*afp_switch[ func ])(obj,
377                                               asp->commands, asp->cmdlen,
378                                               asp->data, &asp->datalen);
379 #ifdef FORCE_UIDGID
380                 /* bring everything back to old euid, egid */
381                 if (obj->force_uid)
382                     restore_uidgid ( &obj->uidgid );
383 #endif /* FORCE_UIDGID */
384             } else {
385                 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
386                 asp->datalen = 0;
387                 reply = AFPERR_NOOP;
388             }
389 #ifdef DEBUG1
390             if ( obj->options.flags & OPTION_DEBUG ) {
391                 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
392                 bprint( asp->data, asp->datalen );
393             }
394 #endif
395             if ( asp_wrtreply( asp, reply ) < 0 ) {
396                 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
397                 afp_asp_die(EXITERR_CLNT);
398             }
399             break;
400         default:
401             /*
402                * Bad asp packet.  Probably should have asp filter them,
403                * since they are typically things like out-of-order packet.
404                */
405             LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
406             break;
407         }
408 #ifdef DEBUG1
409         if ( obj->options.flags & OPTION_DEBUG ) {
410 #ifdef notdef
411             pdesc( stdout );
412 #endif /* notdef */
413             of_pforkdesc( stdout );
414             fflush( stdout );
415         }
416 #endif
417     }
418 }
419
420 #endif