2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <atalk/logger.h>
24 #include <sys/param.h>
25 #ifndef HAVE_GETHOSTID
26 #include <sys/sysctl.h>
27 #endif /* HAVE_GETHOSTID */
31 #include <netatalk/at.h>
32 #include <netatalk/endian.h>
33 #include <atalk/dsi.h>
34 #include <atalk/atp.h>
35 #include <atalk/asp.h>
36 #include <atalk/nbp.h>
37 #include <atalk/unicode.h>
38 #include <atalk/util.h>
40 #include "globals.h" /* includes <netdb.h> */
42 #include "afp_config.h"
45 static size_t maxstatuslen = 0;
47 static void status_flags(char *data, const int notif, const int ipok,
48 const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
52 status = AFPSRVRINFO_COPY;
53 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
54 status |= AFPSRVRINFO_PASSWD;
55 if (passwdbits & PASSWD_NOSAVE)
56 status |= AFPSRVRINFO_NOSAVEPASSWD;
57 status |= AFPSRVRINFO_SRVSIGNATURE;
58 /* only advertise tcp/ip if we have a valid address */
60 status |= AFPSRVRINFO_TCPIP;
61 status |= AFPSRVRINFO_SRVMSGS;
62 /* Allow the user to decide if we should support server notifications.
63 * With this turned off, the clients will poll for directory changes every
64 * 10 seconds. This might be too costly to network resources, so make
65 * this an optional thing. Default will be to _not_ support server
68 status |= AFPSRVRINFO_SRVNOTIFY;
70 status |= AFPSRVRINFO_FASTBOZO;
71 status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
72 /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
74 if (flags & OPTION_UUID) /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
75 status |= AFPSRVRINFO_UUID;
77 status = htons(status);
78 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
81 static int status_server(char *data, const char *server, const struct afp_options *options)
84 char *Obj, *Type, *Zone;
89 /* make room for all offsets before server name */
90 data += AFPSTATUS_PRELEN;
92 /* extract the obj part of the server */
93 Obj = (char *) server;
95 nbp_name(server, &Obj, &Type, &Zone);
97 if ((size_t)-1 == (len = convert_string(
98 options->unixcharset, options->maccharset,
99 Obj, -1, buf, sizeof(buf))) ) {
100 len = MIN(strlen(Obj), 31);
102 memcpy( data, Obj, len );
103 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
106 memcpy( data, buf, len );
108 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
112 /* make room for signature and net address offset. save location of
113 * signature offset. we're also making room for directory names offset
114 * and the utf-8 server name offset.
116 * NOTE: technically, we don't need to reserve space for the
117 * signature and net address offsets if they're not going to be
118 * used. as there are no offsets after them, it doesn't hurt to
119 * have them specified though. so, we just do that to simplify
122 * NOTE2: AFP3.1 Documentation states that the directory names offset
123 * is a required feature, even though it can be set to zero.
126 status = htons(len + AFPSTATUS_POSTLEN);
127 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
128 return len; /* return the offset to beginning of signature offset */
131 static void status_machine(char *data)
137 const char *machine = "afs";
139 const char *machine = "Netatalk";
142 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
143 data += ntohs( status );
144 len = strlen( machine );
146 memcpy( data, machine, len );
148 status = htons(data - start);
149 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
152 /* server signature is a 16-byte quantity */
153 static u_int16_t status_signature(char *data, int *servoffset,
154 const struct afp_options *options)
157 u_int16_t offset, sigoff;
161 /* get server signature offset */
162 memcpy(&offset, data + *servoffset, sizeof(offset));
163 sigoff = offset = ntohs(offset);
165 /* jump to server signature offset */
169 memcpy(data, options->signature, 16);
172 /* calculate net address offset */
173 *servoffset += sizeof(offset);
174 offset = htons(data - status);
175 memcpy(status + *servoffset, &offset, sizeof(offset));
179 static size_t status_netaddress(char *data, int *servoffset,
180 const ASP asp, const DSI *dsi,
181 const struct afp_options *options)
185 size_t addresses_len = 0;
189 /* get net address offset */
190 memcpy(&offset, data + *servoffset, sizeof(offset));
191 data += ntohs(offset);
195 len (byte = sizeof(length + address type + address)
196 address type (byte, ip address = 0x01, ip + port = 0x02,
197 ddp address = 0x03, fqdn = 0x04)
198 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
201 /* number of addresses. this currently screws up if we have a dsi
202 connection, but we don't have the ip address. to get around this,
203 we turn off the status flag for tcp/ip. */
204 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
205 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
209 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
210 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
211 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
212 *data++ = 6; /* length */
213 *data++ = 0x01; /* basic ip address */
214 memcpy(data, &inaddr->sin_addr.s_addr,
215 sizeof(inaddr->sin_addr.s_addr));
216 data += sizeof(inaddr->sin_addr.s_addr);
219 /* ip address + port */
221 *data++ = 0x02; /* ip address with port */
222 memcpy(data, &inaddr->sin_addr.s_addr,
223 sizeof(inaddr->sin_addr.s_addr));
224 data += sizeof(inaddr->sin_addr.s_addr);
225 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
226 data += sizeof(inaddr->sin_port);
230 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
231 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
232 *data++ = 18; /* length */
233 *data++ = 6; /* type */
234 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
235 data += sizeof(sa6->sin6_addr.s6_addr);
238 /* ip address + port */
239 *data++ = 20; /* length */
240 *data++ = 7; /* type*/
241 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
242 data += sizeof(sa6->sin6_addr.s6_addr);
243 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
244 data += sizeof(sa6->sin6_port);
251 /* handle DNS names */
252 if (options->fqdn && dsi) {
253 size_t len = strlen(options->fqdn);
254 if ( len + 2 + addresses_len < maxstatuslen - offset) {
257 memcpy(data, options->fqdn, len);
259 addresses_len += len+2;
262 /* Annouce support for SSH tunneled AFP session,
263 * this feature is available since 10.3.2.
264 * According to the specs (AFP 3.1 p.225) this should
265 * be an IP+Port style value, but it only works with
266 * a FQDN. OSX Server uses FQDN as well.
268 if ( len + 2 + addresses_len < maxstatuslen - offset) {
269 if (options->flags & OPTION_ANNOUNCESSH) {
272 memcpy(data, options->fqdn, len);
280 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
284 *data++ = 0x03; /* ddp address */
285 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
286 data += sizeof(ddpaddr->sat_addr.s_net);
287 memcpy(data, &ddpaddr->sat_addr.s_node,
288 sizeof(ddpaddr->sat_addr.s_node));
289 data += sizeof(ddpaddr->sat_addr.s_node);
290 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
291 data += sizeof(ddpaddr->sat_port);
293 #endif /* ! NO_DDP */
295 /* calculate/store Directory Services Names offset */
296 offset = htons(data - begin);
297 *servoffset += sizeof(offset);
298 memcpy(begin + *servoffset, &offset, sizeof(offset));
300 /* return length of buffer */
301 return (data - begin);
304 static size_t status_directorynames(char *data, int *diroffset,
306 const struct afp_options *options)
310 memcpy(&offset, data + *diroffset, sizeof(offset));
311 offset = ntohs(offset);
314 /* I can not find documentation of any other purpose for the
315 * DirectoryNames field.
318 * Try to synthesize a principal:
319 * service '/' fqdn '@' realm
321 if (options->k5service && options->k5realm && options->fqdn) {
322 /* should k5princ be utf8 encoded? */
324 char *p = strchr( options->fqdn, ':' );
327 len = strlen( options->k5service )
328 + strlen( options->fqdn )
329 + strlen( options->k5realm );
330 len+=2; /* '/' and '@' */
331 if ( len > 255 || len+2 > maxstatuslen - offset) {
333 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
336 *data++ = 1; /* DirectoryNamesCount */
338 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
339 options->fqdn, options->k5realm );
348 /* Calculate and store offset for UTF8ServerName */
349 *diroffset += sizeof(u_int16_t);
350 offset = htons(data - begin);
351 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
353 /* return length of buffer */
354 return (data - begin);
357 static size_t status_utf8servername(char *data, int *nameoffset,
359 const struct afp_options *options)
361 char *Obj, *Type, *Zone;
365 u_int16_t offset, status;
367 memcpy(&offset, data + *nameoffset, sizeof(offset));
368 offset = ntohs(offset);
372 * What is the valid character range for an nbpname?
374 * Apple's server likes to use the non-qualified hostname
375 * This obviously won't work very well if multiple servers are running
379 /* extract the obj part of the server */
380 Obj = (char *) (options->server ? options->server : options->hostname);
382 nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
384 if ((size_t) -1 == (len = convert_string (
385 options->unixcharset, CH_UTF8_MAC,
386 Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
387 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
389 /* set offset to 0 */
390 memset(begin + *nameoffset, 0, sizeof(offset));
391 data = begin + offset;
393 namelen = htons(len);
394 memcpy( data, &namelen, sizeof(namelen));
395 data += sizeof(namelen);
397 offset = htons(offset);
398 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
400 /* Now set the flag ... */
401 memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
402 status = ntohs(status);
403 status |= AFPSRVRINFO_SRVUTF8;
404 status = htons(status);
405 memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
408 /* return length of buffer */
409 return (data - begin);
413 /* returns actual offset to signature */
414 static void status_icon(char *data, const unsigned char *icondata,
415 const int iconlen, const int sigoffset)
418 char *sigdata = data + sigoffset;
419 u_int16_t ret, status;
421 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
422 if ( icondata == NULL ) {
424 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
426 data += ntohs( status );
427 memcpy( data, icondata, iconlen);
429 ret = htons(data - start);
432 /* put in signature offset */
434 memcpy(sigdata, &ret, sizeof(ret));
437 /* ---------------------
439 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
440 const struct afp_options *options)
448 if (!(aspconfig || dsiconfig) || !options)
452 status = aspconfig->status;
453 maxstatuslen=sizeof(aspconfig->status);
454 asp = aspconfig->obj.handle;
460 status = dsiconfig->status;
461 maxstatuslen=sizeof(dsiconfig->status);
462 dsi = dsiconfig->obj.handle;
463 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
464 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
465 ipok = sa4->sin_addr.s_addr ? 1 : 0;
467 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
468 for (int i=0; i<16; i++) {
469 if (sa6->sin6_addr.s6_addr[i]) {
479 * These routines must be called in order -- earlier calls
480 * set the offsets for later calls.
482 * using structs is a bad idea, but that's what the original code
483 * does. solaris, in fact, will segfault with them. so, now
484 * we just use the powers of #defines and memcpy.
486 * reply block layout (offsets are 16-bit quantities):
487 * machine type offset -> AFP version count offset ->
488 * UAM count offset -> vol icon/mask offset -> flags ->
490 * server name [padded to even boundary] -> signature offset ->
491 * network address offset ->
493 * at the appropriate offsets:
494 * machine type, afp versions, uams, server signature
495 * (16-bytes), network addresses, volume icon/mask
499 options->server_notif,
500 (options->fqdn || ipok),
502 (options->k5service && options->k5realm && options->fqdn),
504 /* returns offset to signature offset */
505 c = status_server(status, options->server ? options->server :
506 options->hostname, options);
507 status_machine(status);
508 status_versions(status, asp, dsi);
509 status_uams(status, options->uamlist);
510 if (options->flags & OPTION_CUSTOMICON)
511 status_icon(status, icon, sizeof(icon), c);
513 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
515 sigoff = status_signature(status, &c, options);
516 /* c now contains the offset where the netaddress offset lives */
518 status_netaddress(status, &c, asp, dsi, options);
519 /* c now contains the offset where the Directory Names Count offset lives */
521 statuslen = status_directorynames(status, &c, dsi, options);
522 /* c now contains the offset where the UTF-8 ServerName offset lives */
524 if ( statuslen < maxstatuslen)
525 statuslen = status_utf8servername(status, &c, dsi, options);
529 if (dsiconfig) /* status is dsiconfig->status */
530 memcpy(aspconfig->status, status, statuslen);
531 asp_setstatus(asp, status, statuslen);
532 aspconfig->signature = status + sigoff;
533 aspconfig->statuslen = statuslen;
535 #endif /* ! NO_DDP */
538 if ((options->flags & OPTION_CUSTOMICON) == 0) {
539 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
541 dsi_setstatus(dsi, status, statuslen);
542 dsiconfig->signature = status + sigoff;
543 dsiconfig->statuslen = statuslen;
547 /* set_signature() */
549 /* If found in conf file, use it. */
550 /* If not found in conf file, genarate and append in conf file. */
551 /* If conf file don't exist, create and genarate. */
552 /* If cannot open conf file, use one-time signature. */
553 /* If -signature user:xxxxx, use it. */
555 void set_signature(struct afp_options *options) {
559 char *servername_conf;
562 FILE *fp = NULL, *randomp;
566 server_tmp = (options->server ? options->server : options->hostname);
567 if (strcmp(options->signatureopt, "auto") == 0) {
568 goto server_signature_auto; /* default */
569 } else if (strcmp(options->signatureopt, "host") == 0) {
570 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
571 goto server_signature_auto; /* same as auto */
572 } else if (strncmp(options->signatureopt, "user", 4) == 0) {
573 goto server_signature_user; /* user string */
575 LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
576 goto server_signature_auto; /* switch back to auto*/
579 server_signature_user:
581 /* Signature type is user string */
582 len = strlen(options->signatureopt);
584 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
585 goto server_signature_auto;
587 usersign = options->signatureopt + 5;
590 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !", usersign);
592 } else if (len >= 3) {
593 LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
595 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !", usersign);
597 memset(options->signature, 0, 16);
598 memcpy(options->signature, usersign, len);
599 goto server_signature_done;
601 server_signature_auto:
603 /* Signature type is auto, using afp_signature.conf */
604 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
605 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
606 /* scan in the conf file */
607 while (fgets(buf, sizeof(buf), fp) != NULL) {
609 while (p && isblank(*p))
611 if (!p || (*p == '#') || (*p == '\n'))
612 continue; /* invalid line */
615 if ((servername_conf = strtok( p, "\"" )) == NULL)
616 continue; /* syntax error: invalid quoted servername */
618 if ((servername_conf = strtok( p, " \t" )) == NULL)
619 continue; /* syntax error: invalid servername */
624 continue; /* syntax error: missing signature */
626 if (strcmp(server_tmp, servername_conf))
627 continue; /* another servername */
629 while (p && isblank(*p))
631 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
632 &options->signature[ 0], &options->signature[ 1],
633 &options->signature[ 2], &options->signature[ 3],
634 &options->signature[ 4], &options->signature[ 5],
635 &options->signature[ 6], &options->signature[ 7],
636 &options->signature[ 8], &options->signature[ 9],
637 &options->signature[10], &options->signature[11],
638 &options->signature[12], &options->signature[13],
639 &options->signature[14], &options->signature[15]
642 goto server_signature_done; /* found in conf file */
645 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
646 fseek(fp, 0L, SEEK_END);
647 if(ftell(fp) == 0) { /* size = 0 */
649 goto server_signature_random;
651 fseek(fp, -1L, SEEK_END);
652 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
653 goto server_signature_random;
656 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
657 options->sigconffile, strerror(errno));
658 goto server_signature_random;
661 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
662 options->sigconffile, strerror(errno));
663 goto server_signature_random;
665 } else { /* conf file don't exist */
666 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
667 LOG(log_error, logtype_atalkd, "Cannot create %s (%s). Using one-time signature.",
668 options->sigconffile, strerror(errno));
669 goto server_signature_random;
671 if (( fp = fdopen( fd, "w" )) == NULL ) {
672 LOG(log_error, logtype_atalkd, "Cannot fdopen %s (%s). Using one-time signature.",
673 options->sigconffile, strerror(errno));
675 goto server_signature_random;
678 goto server_signature_random;
681 server_signature_random:
683 /* generate signature from random number */
684 randombytes(options->signature, 16);
686 if (fp && header) { /* conf file is created or size=0 */
687 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
688 fprintf(fp, "# This file is auto-generated by afpd.\n");
690 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
691 fprintf(fp, "# the same server twice.\n");
693 fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
697 fprintf(fp, "\"%s\"\t", server_tmp);
698 for (i=0 ; i<16 ; i++) {
699 fprintf(fp, "%02X", (options->signature)[i]);
701 fprintf(fp, "%s", "\n");
705 server_signature_done:
708 LOG(log_info, logtype_afpd,
709 " \"%s\"'s signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
711 (options->signature)[ 0], (options->signature)[ 1],
712 (options->signature)[ 2], (options->signature)[ 3],
713 (options->signature)[ 4], (options->signature)[ 5],
714 (options->signature)[ 6], (options->signature)[ 7],
715 (options->signature)[ 8], (options->signature)[ 9],
716 (options->signature)[10], (options->signature)[11],
717 (options->signature)[12], (options->signature)[13],
718 (options->signature)[14], (options->signature)[15]);
723 /* this is the same as asp/dsi_getstatus */
724 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
726 AFPConfig *config = obj->config;
728 memcpy(rbuf, config->status, config->statuslen);
729 *rbuflen = config->statuslen;