]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
add -advertise_ssh flag to afpd.conf, if set announce SSH tunneling capabilities.
[netatalk.git] / etc / afpd / status.c
1 /*
2  * $Id: status.c,v 1.13.6.6 2004-06-09 02:07:15 bfernhomberg 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 #include <atalk/unicode.h>
33
34 #include "globals.h"  /* includes <netdb.h> */
35 #include "status.h"
36 #include "afp_config.h"
37 #include "icon.h"
38
39 static   int maxstatuslen = 0;
40
41 static void status_flags(char *data, const int notif, const int ipok,
42                          const unsigned char passwdbits, const int dirsrvcs)
43 {
44     u_int16_t           status;
45
46     status = AFPSRVRINFO_COPY;
47     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
48         status |= AFPSRVRINFO_PASSWD;
49     if (passwdbits & PASSWD_NOSAVE)
50         status |= AFPSRVRINFO_NOSAVEPASSWD;
51     status |= AFPSRVRINFO_SRVSIGNATURE;
52     /* only advertise tcp/ip if we have a valid address */
53     if (ipok)
54         status |= AFPSRVRINFO_TCPIP;
55     status |= AFPSRVRINFO_SRVMSGS;
56     /* Allow the user to decide if we should support server notifications.
57      * With this turned off, the clients will poll for directory changes every
58      * 10 seconds.  This might be too costly to network resources, so make
59      * this an optional thing.  Default will be to _not_ support server
60      * notifications. */
61     if (notif) {
62         status |= AFPSRVRINFO_SRVNOTIFY;
63     }
64     status |= AFPSRVRINFO_FASTBOZO;
65     status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
66     /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
67
68     status = htons(status);
69     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
70 }
71
72 static int status_server(char *data, const char *server, const struct afp_options *options)
73 {
74     char                *start = data;
75     char                *Obj, *Type, *Zone;
76     char                buf[32];
77     u_int16_t           status;
78     size_t              len;
79
80     /* make room for all offsets before server name */
81     data += AFPSTATUS_PRELEN;
82
83     /* extract the obj part of the server */
84     Obj = (char *) server;
85     nbp_name(server, &Obj, &Type, &Zone);
86     if ((size_t)-1 == (len = convert_string( 
87                         options->unixcharset, options->maccharset, 
88                         Obj, strlen(Obj), buf, 31)) ) {
89         len = MIN(strlen(Obj), 31);
90         *data++ = len;
91         memcpy( data, Obj, len );
92         LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
93     } else {
94         *data++ = len;
95         memcpy( data, buf, len );
96     }
97     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
98         len++;
99     data += len;
100
101     /* make room for signature and net address offset. save location of
102      * signature offset. we're also making room for directory names offset
103      * and the utf-8 server name offset.
104      *
105      * NOTE: technically, we don't need to reserve space for the
106      * signature and net address offsets if they're not going to be
107      * used. as there are no offsets after them, it doesn't hurt to
108      * have them specified though. so, we just do that to simplify
109      * things.  
110      *
111      * NOTE2: AFP3.1 Documentation states that the directory names offset
112      * is a required feature, even though it can be set to zero.
113      */
114     len = data - start;
115     status = htons(len + AFPSTATUS_POSTLEN);
116     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
117     return len; /* return the offset to beginning of signature offset */
118 }
119
120 static void status_machine(char *data)
121 {
122     char                *start = data;
123     u_int16_t           status;
124     int                 len;
125 #ifdef AFS
126     const char          *machine = "afs";
127 #else /* !AFS */
128     const char          *machine = "Macintosh"; /* shot in the dark, let's see if this changes OSX behaviour, it shouldn't */
129 #endif /* AFS */
130
131     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
132     data += ntohs( status );
133     len = strlen( machine );
134     *data++ = len;
135     memcpy( data, machine, len );
136     data += len;
137     status = htons(data - start);
138     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
139 }
140
141 /* -------------------------------- 
142  * it turns out that a server signature screws up separate
143  * servers running on the same machine. to work around that, 
144  * i add in an increment.
145  * Not great, server signature are config dependent but well.
146  */
147  
148 static int           Id = 0;
149
150 /* server signature is a 16-byte quantity */
151 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
152                                   const struct afp_options *options)
153 {
154     char                 *status;
155     char                 *usersign;
156     int                  i;
157     u_int16_t            offset, sigoff;
158     long                 hostid;
159 #ifdef BSD4_4
160     int                  mib[2];
161     size_t               len;
162 #endif /* BSD4_4 */
163
164     status = data;
165
166     /* get server signature offset */
167     memcpy(&offset, data + *servoffset, sizeof(offset));
168     sigoff = offset = ntohs(offset);
169
170     /* jump to server signature offset */
171     data += offset;
172
173     /* Signature type is user string */
174     if (strncmp(options->signature, "user", 4) == 0) {
175         if (strlen(options->signature) <= 5) {
176             LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
177                             options->signature);
178             goto server_signature_hostid;
179     }
180     usersign = options->signature + 5;
181         if (strlen(usersign) < 3) 
182             LOG(log_warning, logtype_afpd, "Signature %s is very short !", 
183                             options->signature);
184     
185         memset(data, 0, 16);
186         strncpy(data, usersign, 16);
187         data += 16;
188         goto server_signature_done;
189     } /* signature = user */
190
191     /* If signature type is a standard hostid... */
192 server_signature_hostid:
193     /* 16-byte signature consists of copies of the hostid */
194 #if defined(BSD4_4) && defined(USE_GETHOSTID)
195     mib[0] = CTL_KERN;
196     mib[1] = KERN_HOSTID;
197     len = sizeof(hostid);
198     sysctl(mib, 2, &hostid, &len, NULL, 0);
199 #else /* BSD4_4 && USE_GETHOSTID */
200     hostid = gethostid();
201 #endif /* BSD4_4 && USE_GETHOSTID */
202     if (!hostid) {
203         if (dsi)
204             hostid = dsi->server.sin_addr.s_addr;
205         else {
206             struct hostent *host;
207
208             if ((host = gethostbyname(options->hostname)))
209                 hostid = ((struct in_addr *) host->h_addr)->s_addr;
210         }
211     }
212
213     /* it turns out that a server signature screws up separate
214      * servers running on the same machine. to work around that, 
215      * i add in an increment */
216     hostid += Id;
217     Id++;
218     for (i = 0; i < 16; i += sizeof(hostid)) {
219         memcpy(data, &hostid, sizeof(hostid));
220         data += sizeof(hostid);
221      }
222
223 server_signature_done:
224     /* calculate net address offset */
225     *servoffset += sizeof(offset);
226     offset = htons(data - status);
227     memcpy(status + *servoffset, &offset, sizeof(offset));
228     return sigoff;
229 }
230
231 static int status_netaddress(char *data, int *servoffset,
232                              const ASP asp, const DSI *dsi,
233                              const struct afp_options *options)
234 {
235     char               *begin;
236     u_int16_t          offset;
237
238     begin = data;
239
240     /* get net address offset */
241     memcpy(&offset, data + *servoffset, sizeof(offset));
242     data += ntohs(offset);
243
244     /* format:
245        Address count (byte) 
246        len (byte = sizeof(length + address type + address) 
247        address type (byte, ip address = 0x01, ip + port = 0x02,
248                            ddp address = 0x03, fqdn = 0x04) 
249        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
250        */
251
252     /* number of addresses. this currently screws up if we have a dsi
253        connection, but we don't have the ip address. to get around this,
254        we turn off the status flag for tcp/ip. */
255     *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
256               (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
257
258     /* ip address */
259     if (dsi) {
260         const struct sockaddr_in *inaddr = &dsi->server;
261
262         if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
263             *data++ = 6; /* length */
264             *data++ = 0x01; /* basic ip address */
265             memcpy(data, &inaddr->sin_addr.s_addr,
266                    sizeof(inaddr->sin_addr.s_addr));
267             data += sizeof(inaddr->sin_addr.s_addr);
268         } else {
269             /* ip address + port */
270             *data++ = 8;
271             *data++ = 0x02; /* ip address with port */
272             memcpy(data, &inaddr->sin_addr.s_addr,
273                    sizeof(inaddr->sin_addr.s_addr));
274             data += sizeof(inaddr->sin_addr.s_addr);
275             memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
276             data += sizeof(inaddr->sin_port);
277         }
278     }
279
280     /* handle DNS names */
281     if (options->fqdn && dsi) {
282         int len = strlen(options->fqdn);
283         *data++ = len +2;
284         *data++ = 0x04;
285         memcpy(data, options->fqdn, len);
286         data += len;
287
288         /* Annouce support for SSH tunneled AFP session, 
289          * this feature is available since 10.3.2.
290          * According to the specs (AFP 3.1 p.225) this should
291          * be an IP+Port style value, but it only works with 
292          * a FQDN. OSX Server uses FQDN as well.
293          */
294         if (options->flags & OPTION_ANNOUNCESSH) {
295             *data++ = len +2;
296             *data++ = 0x05;
297             memcpy(data, options->fqdn, len);
298             data += len;
299         }
300     }
301
302 #ifndef NO_DDP
303     if (asp) {
304         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
305
306         /* ddp address */
307         *data++ = 6;
308         *data++ = 0x03; /* ddp address */
309         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
310         data += sizeof(ddpaddr->sat_addr.s_net);
311         memcpy(data, &ddpaddr->sat_addr.s_node,
312                sizeof(ddpaddr->sat_addr.s_node));
313         data += sizeof(ddpaddr->sat_addr.s_node);
314         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
315         data += sizeof(ddpaddr->sat_port);
316     }
317 #endif /* ! NO_DDP */
318
319     /* calculate/store Directory Services Names offset */
320     offset = htons(data - begin); 
321     *servoffset += sizeof(offset);
322     memcpy(begin + *servoffset, &offset, sizeof(offset));
323
324     /* return length of buffer */
325     return (data - begin);
326 }
327
328 static int status_directorynames(char *data, int *diroffset, 
329                                  const DSI *dsi, 
330                                  const struct afp_options *options)
331 {
332     char *begin = data;
333     u_int16_t offset;
334     memcpy(&offset, data + *diroffset, sizeof(offset));
335     offset = ntohs(offset);
336     data += offset;
337
338     /* I can not find documentation of any other purpose for the
339      * DirectoryNames field.
340      */
341     /*
342      * Try to synthesize a principal:
343      * service '/' fqdn '@' realm
344      */
345     if (options->k5service && options->k5realm && options->fqdn) {
346         /* should k5princ be utf8 encoded? */
347         u_int8_t len;
348         char *p = strchr( options->fqdn, ':' );
349         if (p) 
350             *p = '\0';
351         len = strlen( options->k5service ) 
352                         + strlen( options->fqdn )
353                         + strlen( options->k5realm );
354         len+=2; /* '/' and '@' */
355         if ( len+2 > maxstatuslen - offset) {
356             *data++ = 0;
357         }        
358         else {
359             *data++ = 1; /* DirectoryNamesCount */
360             *data++ = len;
361             snprintf( data, len + 1, "%s/%s@%s", options->k5service,
362                                 options->fqdn, options->k5realm );
363             data += len;
364             if (p)
365                 *p = ':';
366        }
367     } else {
368         *data++ = 0;
369     }
370
371     /* Calculate and store offset for UTF8ServerName */
372     *diroffset += sizeof(u_int16_t);
373     offset = htons(data - begin);
374     memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
375
376     /* return length of buffer */
377     return (data - begin);
378 }
379
380 static int status_utf8servername(char *data, int *nameoffset,
381                                  const DSI *dsi,
382                                  const struct afp_options *options)
383 {
384     char *Obj, *Type, *Zone;
385     u_int16_t namelen;
386     size_t len;
387     char *begin = data;
388     u_int16_t offset, status;
389
390     memcpy(&offset, data + *nameoffset, sizeof(offset));
391     offset = ntohs(offset);
392     data += offset;
393
394     /* FIXME:
395      * What is the valid character range for an nbpname?
396      *
397      * Apple's server likes to use the non-qualified hostname
398      * This obviously won't work very well if multiple servers are running
399      * on the box.
400      */
401
402     /* extract the obj part of the server */
403     Obj = (char *) (options->server ? options->server : options->hostname);
404     nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
405
406     if ((size_t) -1 == (len = convert_string (
407                                         options->unixcharset, CH_UTF8_MAC, 
408                                         Obj, strlen(Obj), data+sizeof(namelen), maxstatuslen-offset )) ) {
409         LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
410
411         /* set offset to 0 */
412         memset(begin + *nameoffset, 0, sizeof(offset));
413     }
414     else {
415         namelen = htons(len);
416         memcpy( data, &namelen, sizeof(namelen));
417         data += sizeof(namelen);
418         data += len;
419         offset = htons(offset);
420         memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
421         
422         /* Now set the flag ... */
423         memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
424         status = ntohs(status);
425         status |= AFPSRVRINFO_SRVUTF8;
426         status = htons(status);
427         memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
428     }
429
430     /* return length of buffer */
431     return (data - begin);
432
433 }
434
435 /* returns actual offset to signature */
436 static void status_icon(char *data, const unsigned char *icondata,
437                         const int iconlen, const int sigoffset)
438 {
439     char                *start = data;
440     char                *sigdata = data + sigoffset;
441     u_int16_t           ret, status;
442
443     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
444     if ( icondata == NULL ) {
445         ret = status;
446         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
447     } else {
448         data += ntohs( status );
449         memcpy( data, icondata, iconlen);
450         data += iconlen;
451         ret = htons(data - start);
452     }
453
454     /* put in signature offset */
455     if (sigoffset)
456         memcpy(sigdata, &ret, sizeof(ret));
457 }
458
459 /* ---------------------
460 */
461 void status_reset()
462 {
463     Id = 0;
464 }
465
466 /* ---------------------
467 */
468 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
469                  const struct afp_options *options)
470 {
471     ASP asp;
472     DSI *dsi;
473     char *status = NULL;
474     int statuslen, c, sigoff;
475
476     if (!(aspconfig || dsiconfig) || !options)
477         return;
478
479     if (aspconfig) {
480         asp = aspconfig->obj.handle;
481         status = aspconfig->status;
482         maxstatuslen=sizeof(aspconfig->status);
483     } else
484         asp = NULL;
485
486     if (dsiconfig) {
487         dsi = dsiconfig->obj.handle;
488         if (!aspconfig)
489             status = dsiconfig->status;
490             maxstatuslen=sizeof(dsiconfig->status);
491     } else
492         dsi = NULL;
493
494     /*
495      * These routines must be called in order -- earlier calls
496      * set the offsets for later calls.
497      *
498      * using structs is a bad idea, but that's what the original code
499      * does. solaris, in fact, will segfault with them. so, now
500      * we just use the powers of #defines and memcpy.
501      *
502      * reply block layout (offsets are 16-bit quantities):
503      * machine type offset -> AFP version count offset ->
504      * UAM count offset -> vol icon/mask offset -> flags ->
505      *
506      * server name [padded to even boundary] -> signature offset ->
507      * network address offset ->
508      *
509      * at the appropriate offsets:
510      * machine type, afp versions, uams, server signature
511      * (16-bytes), network addresses, volume icon/mask 
512      */
513
514     status_flags(status, options->server_notif, options->fqdn ||
515                  (dsiconfig && dsi->server.sin_addr.s_addr),
516                  options->passwdbits, 
517                  (options->k5service && options->k5realm && options->fqdn));
518     /* returns offset to signature offset */
519     c = status_server(status, options->server ? options->server :
520                       options->hostname, options);
521     status_machine(status);
522     status_versions(status);
523     status_uams(status, options->uamlist);
524     if (options->flags & OPTION_CUSTOMICON)
525         status_icon(status, icon, sizeof(icon), c);
526     else
527         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
528
529     sigoff = status_signature(status, &c, dsi, options);
530     /* c now contains the offset where the netaddress offset lives */
531
532     status_netaddress(status, &c, asp, dsi, options);
533     /* c now contains the offset where the Directory Names Count offset lives */
534
535     statuslen = status_directorynames(status, &c, dsi, options);
536     /* c now contains the offset where the UTF-8 ServerName offset lives */
537
538     if ( statuslen < maxstatuslen) 
539         statuslen = status_utf8servername(status, &c, dsi, options);
540
541 #ifndef NO_DDP
542     if (aspconfig) {
543         asp_setstatus(asp, status, statuslen);
544         aspconfig->signature = status + sigoff;
545         aspconfig->statuslen = statuslen;
546     }
547 #endif /* ! NO_DDP */
548
549     if (dsiconfig) {
550         if (aspconfig) { /* copy to dsiconfig */
551             memcpy(dsiconfig->status, status, ATP_MAXDATA);
552             status = dsiconfig->status;
553         }
554
555         if ((options->flags & OPTION_CUSTOMICON) == 0) {
556             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
557         }
558         dsi_setstatus(dsi, status, statuslen);
559         dsiconfig->signature = status + sigoff;
560         dsiconfig->statuslen = statuslen;
561     }
562 }
563
564 /* this is the same as asp/dsi_getstatus */
565 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
566 AFPObj      *obj;
567 char    *ibuf, *rbuf;
568 int             ibuflen, *rbuflen;
569 {
570     AFPConfig *config = obj->config;
571
572     memcpy(rbuf, config->status, config->statuslen);
573     *rbuflen = config->statuslen;
574     return AFP_OK;
575 }