]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
b66c17dafb2313985c4dca9a1d3f3b45d1a97602
[netatalk.git] / etc / afpd / status.c
1 /*
2  * $Id: status.c,v 1.11 2003-04-16 22:45:10 samnoble 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, const int dirsrvcs)
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     if (dirsrvcs)
63         status |= AFPSRVRINFO_SRVRDIR;
64     status = htons(status);
65     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
66 }
67
68 static int status_server(char *data, const char *server)
69 {
70     char                *start = data;
71     char                *Obj, *Type, *Zone;
72     u_int16_t           status;
73     int                 len;
74
75     /* make room for all offsets before server name */
76     data += AFPSTATUS_PRELEN;
77
78     /* extract the obj part of the server */
79     Obj = (char *) server;
80     nbp_name(server, &Obj, &Type, &Zone);
81     len = strlen(Obj);
82     *data++ = len;
83     memcpy( data, Obj, len );
84     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
85         len++;
86     data += len;
87
88     /* make room for signature and net address offset. save location of
89      * signature offset. we're also making room for directory names offset
90      * and the utf-8 server name offset.
91      *
92      * NOTE: technically, we don't need to reserve space for the
93      * signature and net address offsets if they're not going to be
94      * used. as there are no offsets after them, it doesn't hurt to
95      * have them specified though. so, we just do that to simplify
96      * things.  
97      *
98      * NOTE2: AFP3.1 Documentation states that the directory names offset
99      * is a required feature, even though it can be set to zero.
100      */
101     len = data - start;
102     status = htons(len + AFPSTATUS_POSTLEN);
103     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
104     return len; /* return the offset to beginning of signature offset */
105 }
106
107 static void status_machine(char *data)
108 {
109     char                *start = data;
110     u_int16_t           status;
111     int                 len;
112 #ifdef AFS
113     const char          *machine = "afs";
114 #else /* !AFS */
115     const char          *machine = "unix";
116 #endif /* AFS */
117
118     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
119     data += ntohs( status );
120     len = strlen( machine );
121     *data++ = len;
122     memcpy( data, machine, len );
123     data += len;
124     status = htons(data - start);
125     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
126 }
127
128
129 /* server signature is a 16-byte quantity */
130 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
131                                   const struct afp_options *options)
132 {
133     char                 *status;
134     char                 *usersign, *ifaddr;
135     int                  i;
136     u_int16_t            offset, sigoff;
137     long                 hostid;
138     static int           id = 0;
139 #ifdef BSD4_4
140     int                  mib[2];
141     size_t               len;
142 #endif /* BSD4_4 */
143
144     status = data;
145
146     /* get server signature offset */
147     memcpy(&offset, data + *servoffset, sizeof(offset));
148     sigoff = offset = ntohs(offset);
149
150     /* jump to server signature offset */
151     data += offset;
152
153     /* Signature type is user string */
154     if (strncmp(options->signature, "user", 4) == 0) {
155         if (strlen(options->signature) <= 5) {
156             LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
157                             options->signature);
158             goto server_signature_hostid;
159     }
160     usersign = options->signature + 5;
161         if (strlen(usersign) < 3) 
162             LOG(log_warning, logtype_afpd, "Signature %s is very short !", 
163                             options->signature);
164     
165         memset(data, 0, 16);
166         strncpy(data, usersign, 16);
167         data += 16;
168         goto server_signature_done;
169     } /* signature = user */
170
171     /* If signature type is a standard hostid... */
172 server_signature_hostid:
173     /* 16-byte signature consists of copies of the hostid */
174 #if defined(BSD4_4) && defined(USE_GETHOSTID)
175     mib[0] = CTL_KERN;
176     mib[1] = KERN_HOSTID;
177     len = sizeof(hostid);
178     sysctl(mib, 2, &hostid, &len, NULL, 0);
179 #else /* BSD4_4 && USE_GETHOSTID */
180     hostid = gethostid();
181 #endif /* BSD4_4 && USE_GETHOSTID */
182     if (!hostid) {
183         if (dsi)
184             hostid = dsi->server.sin_addr.s_addr;
185         else {
186             struct hostent *host;
187
188             if ((host = gethostbyname(options->hostname)))
189                 hostid = ((struct in_addr *) host->h_addr)->s_addr;
190         }
191     }
192
193     /* it turns out that a server signature screws up separate
194      * servers running on the same machine. to work around that, 
195      * i add in an increment */
196     hostid += id;
197     id++;
198     for (i = 0; i < 16; i += sizeof(hostid)) {
199         memcpy(data, &hostid, sizeof(hostid));
200         data += sizeof(hostid);
201      }
202
203 server_signature_done:
204     /* calculate net address offset */
205     *servoffset += sizeof(offset);
206     offset = htons(data - status);
207     memcpy(status + *servoffset, &offset, sizeof(offset));
208     return sigoff;
209 }
210
211 static int status_netaddress(char *data, int *servoffset,
212                              const ASP asp, const DSI *dsi,
213                              const char *fqdn)
214 {
215     char               *begin;
216     u_int16_t          offset;
217
218     begin = data;
219
220     /* get net address offset */
221     memcpy(&offset, data + *servoffset, sizeof(offset));
222     data += ntohs(offset);
223
224     /* format:
225        Address count (byte) 
226        len (byte = sizeof(length + address type + address) 
227        address type (byte, ip address = 0x01, ip + port = 0x02,
228                            ddp address = 0x03, fqdn = 0x04) 
229        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
230        */
231
232     /* number of addresses. this currently screws up if we have a dsi
233        connection, but we don't have the ip address. to get around this,
234        we turn off the status flag for tcp/ip. */
235     *data++ = (fqdn ? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
236
237     /* handle DNS names */
238     if (fqdn) {
239         int len = strlen(fqdn);
240         *data++ = len +2;
241         *data++ = 0x04;
242         memcpy(data, fqdn, len);
243         data += len;
244     }
245
246     /* ip address */
247     if (dsi) {
248         const struct sockaddr_in *inaddr = &dsi->server;
249
250         if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
251             *data++ = 6; /* length */
252             *data++ = 0x01; /* basic ip address */
253             memcpy(data, &inaddr->sin_addr.s_addr,
254                    sizeof(inaddr->sin_addr.s_addr));
255             data += sizeof(inaddr->sin_addr.s_addr);
256         } else {
257             /* ip address + port */
258             *data++ = 8;
259             *data++ = 0x02; /* ip address with port */
260             memcpy(data, &inaddr->sin_addr.s_addr,
261                    sizeof(inaddr->sin_addr.s_addr));
262             data += sizeof(inaddr->sin_addr.s_addr);
263             memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
264             data += sizeof(inaddr->sin_port);
265         }
266     }
267
268 #ifndef NO_DDP
269     if (asp) {
270         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
271
272         /* ddp address */
273         *data++ = 6;
274         *data++ = 0x03; /* ddp address */
275         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
276         data += sizeof(ddpaddr->sat_addr.s_net);
277         memcpy(data, &ddpaddr->sat_addr.s_node,
278                sizeof(ddpaddr->sat_addr.s_node));
279         data += sizeof(ddpaddr->sat_addr.s_node);
280         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
281         data += sizeof(ddpaddr->sat_port);
282     }
283 #endif /* ! NO_DDP */
284
285     /* calculate/store Directory Services Names offset */
286     offset = htons(data - begin); 
287     *servoffset += sizeof(offset);
288     memcpy(begin + *servoffset, &offset, sizeof(offset));
289
290     /* return length of buffer */
291     return (data - begin);
292 }
293
294 static int status_directorynames(char *data, int *diroffset, 
295                                  const DSI *dsi, 
296                                  const struct afp_options *options)
297 {
298     char *begin = data;
299     u_int16_t offset;
300     memcpy(&offset, data + *diroffset, sizeof(offset));
301     offset = ntohs(offset);
302     data += offset;
303
304     /* I can not find documentation of any other purpose for the
305      * DirectoryNames field.
306      */
307     /*
308      * Try to synthesize a principal:
309      * service '/' fqdn '@' realm
310      */
311     if (options->k5service && options->k5realm && options->fqdn) {
312         /* should k5princ be utf8 encoded? */
313         u_int8_t len = strlen( options->k5service ) 
314                         + strlen( options->fqdn )
315                         + strlen( options->k5realm );
316         len+=2; /* '/' and '@' */
317         *data++ = 1; /* DirectoryNamesCount */
318         *data++ = len;
319         snprintf( data, len + 1, "%s/%s@%s", options->k5service,
320                                 options->fqdn, options->k5realm );
321         data += len;
322     } else {
323         memset(begin + *diroffset, 0, sizeof(offset));
324     }
325
326     /* Calculate and store offset for UTF8ServerName */
327     *diroffset += sizeof(u_int16_t);
328     offset = htons(data - begin);
329     memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
330
331     /* return length of buffer */
332     return (data - begin);
333 }
334
335 static int status_utf8servername(char *data, int *nameoffset,
336                                  const DSI *dsi,
337                                  const struct afp_options *options)
338 {
339     u_int16_t namelen, len;
340     char *begin = data;
341     u_int16_t offset;
342     memcpy(&offset, data + *nameoffset, sizeof(offset));
343     offset = ntohs(offset);
344     data += offset;
345
346     /* FIXME: For now we set the UTF-8 ServerName offset to 0
347      * It's irrelevent anyway until we set the appropriate Flags value.
348      * Later this can be set to something meaningful.
349      *
350      * What is the valid character range for an nbpname?
351      *
352      * Apple's server likes to use the non-qualified hostname
353      * This obviously won't work very well if multiple servers are running
354      * on the box.
355      */
356     memset(begin + *nameoffset, 0, sizeof(offset));
357
358     /* return length of buffer */
359     return (data - begin);
360
361 }
362
363 /* returns actual offset to signature */
364 static void status_icon(char *data, const unsigned char *icondata,
365                         const int iconlen, const int sigoffset)
366 {
367     char                *start = data;
368     char                *sigdata = data + sigoffset;
369     u_int16_t           ret, status;
370
371     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
372     if ( icondata == NULL ) {
373         ret = status;
374         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
375     } else {
376         data += ntohs( status );
377         memcpy( data, icondata, iconlen);
378         data += iconlen;
379         ret = htons(data - start);
380     }
381
382     /* put in signature offset */
383     if (sigoffset)
384         memcpy(sigdata, &ret, sizeof(ret));
385 }
386
387 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
388                  const struct afp_options *options)
389 {
390     ASP asp;
391     DSI *dsi;
392     char *status;
393     int statuslen, c, sigoff;
394
395     if (!(aspconfig || dsiconfig) || !options)
396         return;
397
398     if (aspconfig) {
399         asp = aspconfig->obj.handle;
400         status = aspconfig->status;
401     } else
402         asp = NULL;
403
404     if (dsiconfig) {
405         dsi = dsiconfig->obj.handle;
406         if (!aspconfig)
407             status = dsiconfig->status;
408     } else
409         dsi = NULL;
410
411     /*
412      * These routines must be called in order -- earlier calls
413      * set the offsets for later calls.
414      *
415      * using structs is a bad idea, but that's what the original code
416      * does. solaris, in fact, will segfault with them. so, now
417      * we just use the powers of #defines and memcpy.
418      *
419      * reply block layout (offsets are 16-bit quantities):
420      * machine type offset -> AFP version count offset ->
421      * UAM count offset -> vol icon/mask offset -> flags ->
422      *
423      * server name [padded to even boundary] -> signature offset ->
424      * network address offset ->
425      *
426      * at the appropriate offsets:
427      * machine type, afp versions, uams, server signature
428      * (16-bytes), network addresses, volume icon/mask 
429      */
430
431     status_flags(status, options->server_notif, options->fqdn ||
432                  (dsiconfig && dsi->server.sin_addr.s_addr),
433                  options->passwdbits, 
434                  (options->k5service && options->k5realm && options->fqdn));
435     /* returns offset to signature offset */
436     c = status_server(status, options->server ? options->server :
437                       options->hostname);
438     status_machine(status);
439     status_versions(status);
440     status_uams(status, options->uamlist);
441     if (options->flags & OPTION_CUSTOMICON)
442         status_icon(status, icon, sizeof(icon), c);
443     else
444         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
445
446     sigoff = status_signature(status, &c, dsi, options);
447     /* c now contains the offset where the netaddress offset lives */
448
449     status_netaddress(status, &c, asp, dsi, options->fqdn);
450     /* c now contains the offset where the Directory Names Count offset lives */
451
452     status_directorynames(status, &c, dsi, options);
453     /* c now contains the offset where the UTF-8 ServerName offset lives */
454
455     statuslen = status_utf8servername(status, &c, dsi, options);
456
457
458 #ifndef NO_DDP
459     if (aspconfig) {
460         asp_setstatus(asp, status, statuslen);
461         aspconfig->signature = status + sigoff;
462         aspconfig->statuslen = statuslen;
463     }
464 #endif /* ! NO_DDP */
465
466     if (dsiconfig) {
467         if (aspconfig) { /* copy to dsiconfig */
468             memcpy(dsiconfig->status, status, ATP_MAXDATA);
469             status = dsiconfig->status;
470         }
471
472         if ((options->flags & OPTION_CUSTOMICON) == 0) {
473             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
474         }
475         dsi_setstatus(dsi, status, statuslen);
476         dsiconfig->signature = status + sigoff;
477         dsiconfig->statuslen = statuslen;
478     }
479 }
480
481 /* this is the same as asp/dsi_getstatus */
482 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
483 AFPObj      *obj;
484 char    *ibuf, *rbuf;
485 int             ibuflen, *rbuflen;
486 {
487     AFPConfig *config = obj->config;
488
489     memcpy(rbuf, config->status, config->statuslen);
490     *rbuflen = config->statuslen;
491     return AFP_OK;
492 }