2 * $Id: status.c,v 1.13.6.11.2.1 2005-01-31 19:50:38 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
17 #include <atalk/logger.h>
20 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #endif /* USE_GETHOSTID */
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>
34 #include "globals.h" /* includes <netdb.h> */
36 #include "afp_config.h"
39 static int maxstatuslen = 0;
41 static void status_flags(char *data, const int notif, const int ipok,
42 const unsigned char passwdbits, const int dirsrvcs)
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 */
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
62 status |= AFPSRVRINFO_SRVNOTIFY;
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 ... */
68 status = htons(status);
69 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
72 static int status_server(char *data, const char *server, const struct afp_options *options)
75 char *Obj, *Type, *Zone;
80 /* make room for all offsets before server name */
81 data += AFPSTATUS_PRELEN;
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);
91 memcpy( data, Obj, len );
92 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
95 memcpy( data, buf, len );
97 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
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.
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
111 * NOTE2: AFP3.1 Documentation states that the directory names offset
112 * is a required feature, even though it can be set to zero.
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 */
120 static void status_machine(char *data)
126 const char *machine = "afs";
128 const char *machine = "Netatalk";
131 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
132 data += ntohs( status );
133 len = strlen( machine );
135 memcpy( data, machine, len );
137 status = htons(data - start);
138 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
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.
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)
157 u_int16_t offset, sigoff;
166 /* get server signature offset */
167 memcpy(&offset, data + *servoffset, sizeof(offset));
168 sigoff = offset = ntohs(offset);
170 /* jump to server signature offset */
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.",
178 goto server_signature_hostid;
180 usersign = options->signature + 5;
181 if (strlen(usersign) < 3)
182 LOG(log_warning, logtype_afpd, "Signature %s is very short !",
186 strncpy(data, usersign, 16);
188 goto server_signature_done;
189 } /* signature = user */
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)
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 */
204 hostid = dsi->server.sin_addr.s_addr;
206 struct hostent *host;
208 if ((host = gethostbyname(options->hostname)))
209 hostid = ((struct in_addr *) host->h_addr)->s_addr;
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 */
218 for (i = 0; i < 16; i += sizeof(hostid)) {
219 memcpy(data, &hostid, sizeof(hostid));
220 data += sizeof(hostid);
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));
231 static int status_netaddress(char *data, int *servoffset,
232 const ASP asp, const DSI *dsi,
233 const struct afp_options *options)
237 size_t addresses_len = 0;
241 /* get net address offset */
242 memcpy(&offset, data + *servoffset, sizeof(offset));
243 data += ntohs(offset);
247 len (byte = sizeof(length + address type + address)
248 address type (byte, ip address = 0x01, ip + port = 0x02,
249 ddp address = 0x03, fqdn = 0x04)
250 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
253 /* number of addresses. this currently screws up if we have a dsi
254 connection, but we don't have the ip address. to get around this,
255 we turn off the status flag for tcp/ip. */
256 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
257 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
261 const struct sockaddr_in *inaddr = &dsi->server;
263 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
264 *data++ = 6; /* length */
265 *data++ = 0x01; /* basic ip address */
266 memcpy(data, &inaddr->sin_addr.s_addr,
267 sizeof(inaddr->sin_addr.s_addr));
268 data += sizeof(inaddr->sin_addr.s_addr);
271 /* ip address + port */
273 *data++ = 0x02; /* ip address with port */
274 memcpy(data, &inaddr->sin_addr.s_addr,
275 sizeof(inaddr->sin_addr.s_addr));
276 data += sizeof(inaddr->sin_addr.s_addr);
277 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
278 data += sizeof(inaddr->sin_port);
283 /* handle DNS names */
284 if (options->fqdn && dsi) {
285 int len = strlen(options->fqdn);
286 if ( len + 2 + addresses_len < maxstatuslen - offset) {
289 memcpy(data, options->fqdn, len);
291 addresses_len += len+2;
294 /* Annouce support for SSH tunneled AFP session,
295 * this feature is available since 10.3.2.
296 * According to the specs (AFP 3.1 p.225) this should
297 * be an IP+Port style value, but it only works with
298 * a FQDN. OSX Server uses FQDN as well.
300 if ( len + 2 + addresses_len < maxstatuslen - offset) {
301 if (options->flags & OPTION_ANNOUNCESSH) {
304 memcpy(data, options->fqdn, len);
312 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
316 *data++ = 0x03; /* ddp address */
317 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
318 data += sizeof(ddpaddr->sat_addr.s_net);
319 memcpy(data, &ddpaddr->sat_addr.s_node,
320 sizeof(ddpaddr->sat_addr.s_node));
321 data += sizeof(ddpaddr->sat_addr.s_node);
322 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
323 data += sizeof(ddpaddr->sat_port);
325 #endif /* ! NO_DDP */
327 /* calculate/store Directory Services Names offset */
328 offset = htons(data - begin);
329 *servoffset += sizeof(offset);
330 memcpy(begin + *servoffset, &offset, sizeof(offset));
332 /* return length of buffer */
333 return (data - begin);
336 static int status_directorynames(char *data, int *diroffset,
338 const struct afp_options *options)
342 memcpy(&offset, data + *diroffset, sizeof(offset));
343 offset = ntohs(offset);
346 /* I can not find documentation of any other purpose for the
347 * DirectoryNames field.
350 * Try to synthesize a principal:
351 * service '/' fqdn '@' realm
353 if (options->k5service && options->k5realm && options->fqdn) {
354 /* should k5princ be utf8 encoded? */
356 char *p = strchr( options->fqdn, ':' );
359 len = strlen( options->k5service )
360 + strlen( options->fqdn )
361 + strlen( options->k5realm );
362 len+=2; /* '/' and '@' */
363 if ( len+2 > maxstatuslen - offset) {
365 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
368 *data++ = 1; /* DirectoryNamesCount */
370 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
371 options->fqdn, options->k5realm );
380 /* Calculate and store offset for UTF8ServerName */
381 *diroffset += sizeof(u_int16_t);
382 offset = htons(data - begin);
383 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
385 /* return length of buffer */
386 return (data - begin);
389 static int status_utf8servername(char *data, int *nameoffset,
391 const struct afp_options *options)
393 char *Obj, *Type, *Zone;
397 u_int16_t offset, status;
399 memcpy(&offset, data + *nameoffset, sizeof(offset));
400 offset = ntohs(offset);
404 * What is the valid character range for an nbpname?
406 * Apple's server likes to use the non-qualified hostname
407 * This obviously won't work very well if multiple servers are running
411 /* extract the obj part of the server */
412 Obj = (char *) (options->server ? options->server : options->hostname);
413 nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
415 if ((size_t) -1 == (len = convert_string (
416 options->unixcharset, CH_UTF8_MAC,
417 Obj, strlen(Obj), data+sizeof(namelen), maxstatuslen-offset )) ) {
418 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
420 /* set offset to 0 */
421 memset(begin + *nameoffset, 0, sizeof(offset));
422 data = begin + offset;
425 namelen = htons(len);
426 memcpy( data, &namelen, sizeof(namelen));
427 data += sizeof(namelen);
429 offset = htons(offset);
430 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
432 /* Now set the flag ... */
433 memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
434 status = ntohs(status);
435 status |= AFPSRVRINFO_SRVUTF8;
436 status = htons(status);
437 memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
440 /* return length of buffer */
441 return (data - begin);
445 /* returns actual offset to signature */
446 static void status_icon(char *data, const unsigned char *icondata,
447 const int iconlen, const int sigoffset)
450 char *sigdata = data + sigoffset;
451 u_int16_t ret, status;
453 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
454 if ( icondata == NULL ) {
456 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
458 data += ntohs( status );
459 memcpy( data, icondata, iconlen);
461 ret = htons(data - start);
464 /* put in signature offset */
466 memcpy(sigdata, &ret, sizeof(ret));
469 /* ---------------------
477 /* ---------------------
479 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
480 const struct afp_options *options)
484 u_int8_t *status = NULL;
485 int statuslen, c, sigoff;
487 if (!(aspconfig || dsiconfig) || !options)
491 status = aspconfig->status;
492 maxstatuslen=sizeof(aspconfig->status);
493 asp = aspconfig->obj.handle;
498 status = dsiconfig->status;
499 maxstatuslen=sizeof(dsiconfig->status);
500 dsi = dsiconfig->obj.handle;
505 * These routines must be called in order -- earlier calls
506 * set the offsets for later calls.
508 * using structs is a bad idea, but that's what the original code
509 * does. solaris, in fact, will segfault with them. so, now
510 * we just use the powers of #defines and memcpy.
512 * reply block layout (offsets are 16-bit quantities):
513 * machine type offset -> AFP version count offset ->
514 * UAM count offset -> vol icon/mask offset -> flags ->
516 * server name [padded to even boundary] -> signature offset ->
517 * network address offset ->
519 * at the appropriate offsets:
520 * machine type, afp versions, uams, server signature
521 * (16-bytes), network addresses, volume icon/mask
524 status_flags(status, options->server_notif, options->fqdn ||
525 (dsiconfig && dsi->server.sin_addr.s_addr),
527 (options->k5service && options->k5realm && options->fqdn));
528 /* returns offset to signature offset */
529 c = status_server(status, options->server ? options->server :
530 options->hostname, options);
531 status_machine(status);
532 status_versions(status);
533 status_uams(status, options->uamlist);
534 if (options->flags & OPTION_CUSTOMICON)
535 status_icon(status, icon, sizeof(icon), c);
537 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
539 sigoff = status_signature(status, &c, dsi, options);
540 /* c now contains the offset where the netaddress offset lives */
542 status_netaddress(status, &c, asp, dsi, options);
543 /* c now contains the offset where the Directory Names Count offset lives */
545 statuslen = status_directorynames(status, &c, dsi, options);
546 /* c now contains the offset where the UTF-8 ServerName offset lives */
548 if ( statuslen < maxstatuslen)
549 statuslen = status_utf8servername(status, &c, dsi, options);
553 if (dsiconfig) /* status is dsiconfig->status */
554 memcpy(aspconfig->status, status, statuslen);
555 asp_setstatus(asp, status, statuslen);
556 aspconfig->signature = status + sigoff;
557 aspconfig->statuslen = statuslen;
559 #endif /* ! NO_DDP */
562 if ((options->flags & OPTION_CUSTOMICON) == 0) {
563 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
565 dsi_setstatus(dsi, status, statuslen);
566 dsiconfig->signature = status + sigoff;
567 dsiconfig->statuslen = statuslen;
571 /* this is the same as asp/dsi_getstatus */
572 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
575 int ibuflen, *rbuflen;
577 AFPConfig *config = obj->config;
579 memcpy(rbuf, config->status, config->statuslen);
580 *rbuflen = config->statuslen;