]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
bugfix: close the right fork in renamefile, copy and paste error...
[netatalk.git] / etc / afpd / status.c
1 /*
2  * $Id: status.c,v 1.9 2002-12-07 02:55:00 rlewczuk Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <atalk/logger.h>
18
19 #ifdef BSD4_4
20 #include <sys/param.h>
21 #ifndef USE_GETHOSTID
22 #include <sys/sysctl.h>
23 #endif /* USE_GETHOSTID */
24 #endif /* BSD4_4 */
25
26 #include <netatalk/at.h>
27 #include <netatalk/endian.h>
28 #include <atalk/dsi.h>
29 #include <atalk/atp.h>
30 #include <atalk/asp.h>
31 #include <atalk/nbp.h>
32
33 #include "globals.h"  /* includes <netdb.h> */
34 #include "status.h"
35 #include "afp_config.h"
36 #include "icon.h"
37
38 static void status_flags(char *data, const int notif, const int ipok,
39                          const unsigned char passwdbits)
40 {
41     u_int16_t           status;
42
43     status = AFPSRVRINFO_COPY;
44     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
45         status |= AFPSRVRINFO_PASSWD;
46     if (passwdbits & PASSWD_NOSAVE)
47         status |= AFPSRVRINFO_NOSAVEPASSWD;
48     status |= AFPSRVRINFO_SRVSIGNATURE;
49     /* only advertise tcp/ip if we have a valid address */
50     if (ipok)
51         status |= AFPSRVRINFO_TCPIP;
52     status |= AFPSRVRINFO_SRVMSGS;
53     /* Allow the user to decide if we should support server notifications.
54      * With this turned off, the clients will poll for directory changes every
55      * 10 seconds.  This might be too costly to network resources, so make
56      * this an optional thing.  Default will be to _not_ support server
57      * notifications. */
58     if (notif) {
59         status |= AFPSRVRINFO_SRVNOTIFY;
60     }
61     status |= AFPSRVRINFO_FASTBOZO;
62     status = htons(status);
63     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
64 }
65
66 static int status_server(char *data, const char *server)
67 {
68     char                *start = data;
69     char                *Obj, *Type, *Zone;
70     u_int16_t           status;
71     int                 len;
72
73     /* make room for all offsets before server name */
74     data += AFPSTATUS_PRELEN;
75
76     /* extract the obj part of the server */
77     Obj = (char *) server;
78     nbp_name(server, &Obj, &Type, &Zone);
79     len = strlen(Obj);
80     *data++ = len;
81     memcpy( data, Obj, len );
82     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
83         len++;
84     data += len;
85
86     /* make room for signature and net address offset. save location of
87      * signature offset. 
88      *
89      * NOTE: technically, we don't need to reserve space for the
90      * signature and net address offsets if they're not going to be
91      * used. as there are no offsets after them, it doesn't hurt to
92      * have them specified though. so, we just do that to simplify
93      * things.  
94      */
95     len = data - start;
96     status = htons(len + AFPSTATUS_POSTLEN);
97     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
98     return len; /* return the offset to beginning of signature offset */
99 }
100
101 static void status_machine(char *data)
102 {
103     char                *start = data;
104     u_int16_t           status;
105     int                 len;
106 #ifdef AFS
107     const char          *machine = "afs";
108 #else /* !AFS */
109     const char          *machine = "unix";
110 #endif /* AFS */
111
112     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
113     data += ntohs( status );
114     len = strlen( machine );
115     *data++ = len;
116     memcpy( data, machine, len );
117     data += len;
118     status = htons(data - start);
119     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
120 }
121
122
123 /* server signature is a 16-byte quantity */
124 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
125                                   const struct afp_options *options)
126 {
127     char                 *status;
128     char                 *usersign, *ifaddr;
129     int                  i;
130     u_int16_t            offset, sigoff;
131     long                 hostid;
132     static int           id = 0;
133 #ifdef BSD4_4
134     int                  mib[2];
135     size_t               len;
136 #endif /* BSD4_4 */
137
138     status = data;
139
140     /* get server signature offset */
141     memcpy(&offset, data + *servoffset, sizeof(offset));
142     sigoff = offset = ntohs(offset);
143
144     /* jump to server signature offset */
145     data += offset;
146
147     /* Signature type is user string */
148     if (strncmp(options->signature, "user", 4) == 0) {
149         if (strlen(options->signature) <= 5) {
150             LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
151                             options->signature);
152             goto server_signature_hostid;
153     }
154     usersign = options->signature + 5;
155         if (strlen(usersign) < 3) 
156             LOG(log_warning, logtype_afpd, "Signature %s is very short !", 
157                             options->signature);
158     
159         memset(data, 0, 16);
160         strncpy(data, usersign, 16);
161         data += 16;
162         goto server_signature_done;
163     } /* signature = user */
164
165     /* If signature type is a standard hostid... */
166 server_signature_hostid:
167     /* 16-byte signature consists of copies of the hostid */
168 #if defined(BSD4_4) && defined(USE_GETHOSTID)
169     mib[0] = CTL_KERN;
170     mib[1] = KERN_HOSTID;
171     len = sizeof(hostid);
172     sysctl(mib, 2, &hostid, &len, NULL, 0);
173 #else /* BSD4_4 && USE_GETHOSTID */
174     hostid = gethostid();
175 #endif /* BSD4_4 && USE_GETHOSTID */
176     if (!hostid) {
177         if (dsi)
178             hostid = dsi->server.sin_addr.s_addr;
179         else {
180             struct hostent *host;
181
182             if ((host = gethostbyname(options->hostname)))
183                 hostid = ((struct in_addr *) host->h_addr)->s_addr;
184         }
185     }
186
187     /* it turns out that a server signature screws up separate
188      * servers running on the same machine. to work around that, 
189      * i add in an increment */
190     hostid += id;
191     id++;
192     for (i = 0; i < 16; i += sizeof(hostid)) {
193         memcpy(data, &hostid, sizeof(hostid));
194         data += sizeof(hostid);
195      }
196
197 server_signature_done:
198     /* calculate net address offset */
199     *servoffset += sizeof(offset);
200     offset = htons(data - status);
201     memcpy(status + *servoffset, &offset, sizeof(offset));
202     return sigoff;
203 }
204
205 static int status_netaddress(char *data, const int servoffset,
206                              const ASP asp, const DSI *dsi,
207                              const char *fqdn)
208 {
209     char               *begin;
210     u_int16_t          offset;
211
212     begin = data;
213
214     /* get net address offset */
215     memcpy(&offset, data + servoffset, sizeof(offset));
216     data += ntohs(offset);
217
218     /* format:
219        Address count (byte) 
220        len (byte = sizeof(length + address type + address) 
221        address type (byte, ip address = 0x01, ip + port = 0x02,
222                            ddp address = 0x03, fqdn = 0x04) 
223        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
224        */
225
226     /* number of addresses. this currently screws up if we have a dsi
227        connection, but we don't have the ip address. to get around this,
228        we turn off the status flag for tcp/ip. */
229     *data++ = (fqdn ? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
230
231     /* handle DNS names */
232     if (fqdn) {
233         int len = strlen(fqdn) + 2;
234         *data++ = len;
235         *data++ = 0x04;
236         memcpy(data, fqdn, len);
237         data += len;
238     }
239
240     /* ip address */
241     if (dsi) {
242         const struct sockaddr_in *inaddr = &dsi->server;
243
244         if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
245             *data++ = 6; /* length */
246             *data++ = 0x01; /* basic ip address */
247             memcpy(data, &inaddr->sin_addr.s_addr,
248                    sizeof(inaddr->sin_addr.s_addr));
249             data += sizeof(inaddr->sin_addr.s_addr);
250         } else {
251             /* ip address + port */
252             *data++ = 8;
253             *data++ = 0x02; /* ip address with port */
254             memcpy(data, &inaddr->sin_addr.s_addr,
255                    sizeof(inaddr->sin_addr.s_addr));
256             data += sizeof(inaddr->sin_addr.s_addr);
257             memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
258             data += sizeof(inaddr->sin_port);
259         }
260     }
261
262 #ifndef NO_DDP
263     if (asp) {
264         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
265
266         /* ddp address */
267         *data++ = 6;
268         *data++ = 0x03; /* ddp address */
269         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
270         data += sizeof(ddpaddr->sat_addr.s_net);
271         memcpy(data, &ddpaddr->sat_addr.s_node,
272                sizeof(ddpaddr->sat_addr.s_node));
273         data += sizeof(ddpaddr->sat_addr.s_node);
274         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
275         data += sizeof(ddpaddr->sat_port);
276     }
277 #endif /* ! NO_DDP */
278
279     /* return length of buffer */
280     return (data - begin);
281 }
282
283 /* returns actual offset to signature */
284 static void status_icon(char *data, const unsigned char *icondata,
285                         const int iconlen, const int sigoffset)
286 {
287     char                *start = data;
288     char                *sigdata = data + sigoffset;
289     u_int16_t           ret, status;
290
291     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
292     if ( icondata == NULL ) {
293         ret = status;
294         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
295     } else {
296         data += ntohs( status );
297         memcpy( data, icondata, iconlen);
298         data += iconlen;
299         ret = htons(data - start);
300     }
301
302     /* put in signature offset */
303     if (sigoffset)
304         memcpy(sigdata, &ret, sizeof(ret));
305 }
306
307 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
308                  const struct afp_options *options)
309 {
310     ASP asp;
311     DSI *dsi;
312     char *status;
313     int c, sigoff;
314
315     if (!(aspconfig || dsiconfig) || !options)
316         return;
317
318     if (aspconfig) {
319         asp = aspconfig->obj.handle;
320         status = aspconfig->status;
321     } else
322         asp = NULL;
323
324     if (dsiconfig) {
325         dsi = dsiconfig->obj.handle;
326         if (!aspconfig)
327             status = dsiconfig->status;
328     } else
329         dsi = NULL;
330
331     /*
332      * These routines must be called in order -- earlier calls
333      * set the offsets for later calls.
334      *
335      * using structs is a bad idea, but that's what the original code
336      * does. solaris, in fact, will segfault with them. so, now
337      * we just use the powers of #defines and memcpy.
338      *
339      * reply block layout (offsets are 16-bit quantities):
340      * machine type offset -> AFP version count offset ->
341      * UAM count offset -> vol icon/mask offset -> flags ->
342      *
343      * server name [padded to even boundary] -> signature offset ->
344      * network address offset ->
345      *
346      * at the appropriate offsets:
347      * machine type, afp versions, uams, server signature
348      * (16-bytes), network addresses, volume icon/mask 
349      */
350
351     status_flags(status, options->server_notif, options->fqdn ||
352                  (dsiconfig && dsi->server.sin_addr.s_addr),
353                  options->passwdbits);
354     /* returns offset to signature offset */
355     c = status_server(status, options->server ? options->server :
356                       options->hostname);
357     status_machine(status);
358     status_versions(status);
359     status_uams(status, options->uamlist);
360     if (options->flags & OPTION_CUSTOMICON)
361         status_icon(status, icon, sizeof(icon), c);
362     else
363         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
364
365     sigoff = status_signature(status, &c, dsi, options);
366
367     /* returns length */
368     c = status_netaddress(status, c, asp, dsi, options->fqdn);
369
370
371 #ifndef NO_DDP
372     if (aspconfig) {
373         asp_setstatus(asp, status, c);
374         aspconfig->signature = status + sigoff;
375         aspconfig->statuslen = c;
376     }
377 #endif /* ! NO_DDP */
378
379     if (dsiconfig) {
380         if (aspconfig) { /* copy to dsiconfig */
381             memcpy(dsiconfig->status, status, ATP_MAXDATA);
382             status = dsiconfig->status;
383         }
384
385         if ((options->flags & OPTION_CUSTOMICON) == 0) {
386             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
387         }
388         dsi_setstatus(dsi, status, c);
389         dsiconfig->signature = status + sigoff;
390         dsiconfig->statuslen = c;
391     }
392 }
393
394 /* this is the same as asp/dsi_getstatus */
395 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
396 AFPObj      *obj;
397 char    *ibuf, *rbuf;
398 int             ibuflen, *rbuflen;
399 {
400     AFPConfig *config = obj->config;
401
402     memcpy(rbuf, config->status, config->statuslen);
403     *rbuflen = config->statuslen;
404     return AFP_OK;
405 }