]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_asp.c
Remove all references to CAPDIR. Add a config file option for authprintdir
[netatalk.git] / etc / afpd / afp_asp.c
1 /* 
2  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3  * Copyright (c) 1990,1993 Regents of The University of Michigan.
4  * All Rights Reserved.  See COPYRIGHT.
5  *
6  * modified from main.c. this handles afp over asp. 
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #ifndef NO_DDP
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <signal.h>
19 #include <syslog.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22
23 #include <netatalk/endian.h>
24 #include <atalk/atp.h>
25 #include <atalk/asp.h>
26 #include <atalk/compat.h>
27 #include <atalk/util.h>
28
29 #include "globals.h"
30 #include "switch.h"
31 #include "auth.h"
32 #include "fork.h"
33
34 extern struct oforks    *writtenfork;
35
36 static AFPObj *child;
37
38 static __inline__ void afp_asp_close(AFPObj *obj)
39 {
40     ASP asp = obj->handle;
41     
42     if (obj->logout)
43       (*obj->logout)();
44
45     asp_close( asp );
46     syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
47            asp->read_count / 1024.0, asp->write_count / 1024.0);
48 }
49
50 static void afp_asp_die(const int sig)
51 {
52     ASP asp = child->handle;
53
54     asp_attention(asp, AFPATTN_SHUTDOWN);
55     if ( asp_shutdown( asp ) < 0 ) {
56         syslog( LOG_ERR, "afp_die: asp_shutdown: %m" );
57     }
58
59     afp_asp_close(child);
60     if (sig == SIGTERM || sig == SIGALRM)
61       exit( 0 );
62     else
63       exit(sig);
64 }
65
66 static void afp_asp_timedown()
67 {
68     struct sigaction    sv;
69     struct itimerval    it;
70
71     /* shutdown and don't reconnect. server going down in 5 minutes. */
72     asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
73                   AFPATTN_TIME(5));
74
75     it.it_interval.tv_sec = 0;
76     it.it_interval.tv_usec = 0;
77     it.it_value.tv_sec = 300;
78     it.it_value.tv_usec = 0;
79     if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
80         syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
81         afp_asp_die(1);
82     }
83
84     memset(&sv, 0, sizeof(sv));
85     sv.sa_handler = afp_asp_die;
86     sigemptyset( &sv.sa_mask );
87     sv.sa_flags = SA_RESTART;
88     if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
89         syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
90         afp_asp_die(1);
91     }
92 }
93
94 void afp_over_asp(AFPObj *obj)
95 {
96     ASP asp;
97     struct sigaction  action;
98     int         func, ccnt = 0, reply = 0;
99
100     obj->exit = afp_asp_die;
101     obj->reply = (int (*)()) asp_cmdreply;
102     obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention;
103     child = obj;
104     asp = (ASP) obj->handle;
105
106     /* install signal handlers */
107     memset(&action, 0, sizeof(action));
108     action.sa_handler = afp_asp_timedown;
109     sigemptyset( &action.sa_mask );
110     action.sa_flags = SA_RESTART;
111     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
112         syslog( LOG_ERR, "afp_over_asp: sigaction: %m" );
113         afp_asp_die(1);
114     }
115
116     action.sa_handler = afp_asp_die;
117     sigemptyset( &action.sa_mask );
118     action.sa_flags = SA_RESTART;
119     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
120         syslog( LOG_ERR, "afp_over_asp: sigaction: %m" );
121         afp_asp_die(1);
122     }
123
124     syslog( LOG_INFO, "session from %u.%u:%u on %u.%u:%u",
125             ntohs( asp->asp_sat.sat_addr.s_net ),
126             asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port,
127             ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ),
128             atp_sockaddr( asp->asp_atp )->sat_addr.s_node,
129             atp_sockaddr( asp->asp_atp )->sat_port );
130
131     while ((reply = asp_getrequest(asp))) {
132       switch (reply) {
133       case ASPFUNC_CLOSE :
134         if (obj->options.authprintdir) {
135           char addr_filename[256];
136           struct stat cap_st;
137
138           sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir, 
139                 ntohs( asp->asp_sat.sat_addr.s_net )/256, 
140                 ntohs( asp->asp_sat.sat_addr.s_net )%256, 
141                 asp->asp_sat.sat_addr.s_node );
142
143           if(stat(addr_filename, &cap_st) == 0) {
144                 if(unlink(addr_filename) == 0) {
145                         syslog(LOG_INFO, "removed %s", addr_filename);
146                 } else {
147                         syslog(LOG_INFO, "error removing %s: %m", addr_filename);
148                 }
149           } else {
150                 syslog(LOG_INFO, "error stat'ing %s: %m", addr_filename);
151           }
152         }
153
154         afp_asp_close(obj);
155         syslog( LOG_INFO, "done" );
156
157         if ( obj->options.flags & OPTION_DEBUG ) {
158           printf( "done\n" );
159         }
160         return;
161         break;
162
163       case ASPFUNC_CMD :
164 #ifdef AFS
165         if ( writtenfork ) {
166           if ( flushfork( writtenfork ) < 0 ) {
167             syslog( LOG_ERR, "main flushfork: %m" );
168           }
169           writtenfork = NULL;
170         }
171 #endif AFS
172         func = (u_char) asp->commands[0];
173         if ( obj->options.flags & OPTION_DEBUG ) {
174           printf( "command: %d\n", func );
175           bprint( asp->commands, asp->cmdlen );
176         }
177         if ( afp_switch[ func ] != NULL ) {
178           /*
179            * The function called from afp_switch is expected to
180            * read its parameters out of buf, put its
181            * results in replybuf (updating rbuflen), and
182            * return an error code.
183                  */
184           asp->datalen = ASP_DATASIZ;
185           reply = (*afp_switch[ func ])(obj,
186                                         asp->commands, asp->cmdlen,
187                                         asp->data, &asp->datalen);
188         } else {
189           syslog( LOG_ERR, "bad function %X", func );
190           asp->datalen = 0;
191           reply = AFPERR_NOOP;
192         }
193         if ( obj->options.flags & OPTION_DEBUG ) {
194           printf( "reply: %d, %d\n", reply, ccnt++ );
195           bprint( asp->data, asp->datalen );
196         }
197
198         if ( asp_cmdreply( asp, reply ) < 0 ) {
199           syslog( LOG_ERR, "asp_cmdreply: %m" );
200           afp_asp_die(1);
201         }
202         break;
203
204       case ASPFUNC_WRITE :
205         func = (u_char) asp->commands[0];
206         if ( obj->options.flags & OPTION_DEBUG ) {
207           printf( "(write) command: %d\n", func );
208           bprint( asp->commands, asp->cmdlen );
209         }
210         if ( afp_switch[ func ] != NULL ) {
211           asp->datalen = ASP_DATASIZ;
212           reply = (*afp_switch[ func ])(obj, 
213                                         asp->commands, asp->cmdlen,
214                                         asp->data, &asp->datalen);
215         } else {
216           syslog( LOG_ERR, "(write) bad function %X", func );
217           asp->datalen = 0;
218           reply = AFPERR_NOOP;
219         }
220         if ( obj->options.flags & OPTION_DEBUG ) {
221           printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
222           bprint( asp->data, asp->datalen );
223         }
224         if ( asp_wrtreply( asp, reply ) < 0 ) {
225           syslog( LOG_ERR, "asp_wrtreply: %m" );
226           afp_asp_die(1);
227         }
228         break;
229       default:
230         /*
231            * Bad asp packet.  Probably should have asp filter them,
232            * since they are typically things like out-of-order packet.
233            */
234         syslog( LOG_INFO, "main: asp_getrequest: %d", reply );
235         break;
236       }
237
238       if ( obj->options.flags & OPTION_DEBUG ) {
239 #ifdef notdef
240         pdesc( stdout );
241 #endif notdef
242         of_pforkdesc( stdout );
243         fflush( stdout );
244       }
245     }
246 }
247
248 #endif