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 <arpa/inet.h>
33 #include <atalk/dsi.h>
34 #include <atalk/unicode.h>
35 #include <atalk/util.h>
36 #include <atalk/globals.h>
39 #include "afp_config.h"
42 static size_t maxstatuslen = 0;
44 static void status_flags(char *data, const int notif, const int ipok,
45 const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
49 status = AFPSRVRINFO_COPY
50 | AFPSRVRINFO_SRVSIGNATURE
52 | AFPSRVRINFO_FASTBOZO
55 | AFPSRVRINFO_EXTSLEEP;
57 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
58 status |= AFPSRVRINFO_PASSWD;
59 if (passwdbits & PASSWD_NOSAVE)
60 status |= AFPSRVRINFO_NOSAVEPASSWD;
61 if (ipok) /* only advertise tcp/ip if we have a valid address */
62 status |= AFPSRVRINFO_TCPIP;
63 if (notif) /* Default is yes */
64 status |= AFPSRVRINFO_SRVNOTIFY;
65 if (flags & OPTION_UUID)
66 status |= AFPSRVRINFO_UUID;
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 if ((size_t)-1 == (len = convert_string(
86 options->unixcharset, options->maccharset,
87 Obj, -1, buf, sizeof(buf))) ) {
88 len = MIN(strlen(Obj), 31);
90 memcpy( data, Obj, len );
91 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
94 memcpy( data, buf, len );
96 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
100 /* make room for signature and net address offset. save location of
101 * signature offset. we're also making room for directory names offset
102 * and the utf-8 server name offset.
104 * NOTE: technically, we don't need to reserve space for the
105 * signature and net address offsets if they're not going to be
106 * used. as there are no offsets after them, it doesn't hurt to
107 * have them specified though. so, we just do that to simplify
110 * NOTE2: AFP3.1 Documentation states that the directory names offset
111 * is a required feature, even though it can be set to zero.
114 status = htons(len + AFPSTATUS_POSTLEN);
115 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
116 return len; /* return the offset to beginning of signature offset */
119 static void status_machine(char *data)
125 const char *machine = "afs";
127 const char *machine = "Netatalk%s";
129 char buf[AFPSTATUS_MACHLEN+1];
131 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
132 data += ntohs( status );
134 if ((strlen(machine) + strlen(VERSION)) <= AFPSTATUS_MACHLEN) {
135 len = snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, VERSION);
137 if (strlen(VERSION) > AFPSTATUS_MACHLEN) {
138 len = snprintf(buf, AFPSTATUS_MACHLEN + 1, VERSION);
140 (void)snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, "");
141 (void)snprintf(buf + AFPSTATUS_MACHLEN - strlen(VERSION),
144 len = AFPSTATUS_MACHLEN;
149 memcpy( data, buf, len );
152 status = htons(data - start);
153 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
156 /* server signature is a 16-byte quantity */
157 static uint16_t status_signature(char *data, int *servoffset,
158 const struct afp_options *options)
161 uint16_t offset, sigoff;
165 /* get server signature offset */
166 memcpy(&offset, data + *servoffset, sizeof(offset));
167 sigoff = offset = ntohs(offset);
169 /* jump to server signature offset */
173 memcpy(data, options->signature, 16);
176 /* calculate net address offset */
177 *servoffset += sizeof(offset);
178 offset = htons(data - status);
179 memcpy(status + *servoffset, &offset, sizeof(offset));
183 static size_t status_netaddress(char *data, int *servoffset,
185 const struct afp_options *options)
189 size_t addresses_len = 0;
193 /* get net address offset */
194 memcpy(&offset, data + *servoffset, sizeof(offset));
195 data += ntohs(offset);
199 len (byte = sizeof(length + address type + address)
200 address type (byte, ip address = 0x01, ip + port = 0x02,
201 ddp address = 0x03, fqdn = 0x04)
202 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
205 /* number of addresses. this currently screws up if we have a dsi
206 connection, but we don't have the ip address. to get around this,
207 we turn off the status flag for tcp/ip. */
208 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) +
209 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
213 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
214 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
215 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
216 *data++ = 6; /* length */
217 *data++ = 0x01; /* basic ip address */
218 memcpy(data, &inaddr->sin_addr.s_addr,
219 sizeof(inaddr->sin_addr.s_addr));
220 data += sizeof(inaddr->sin_addr.s_addr);
223 /* ip address + port */
225 *data++ = 0x02; /* ip address with port */
226 memcpy(data, &inaddr->sin_addr.s_addr,
227 sizeof(inaddr->sin_addr.s_addr));
228 data += sizeof(inaddr->sin_addr.s_addr);
229 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
230 data += sizeof(inaddr->sin_port);
234 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
235 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
236 *data++ = 18; /* length */
237 *data++ = 6; /* type */
238 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
239 data += sizeof(sa6->sin6_addr.s6_addr);
242 /* ip address + port */
243 *data++ = 20; /* length */
244 *data++ = 7; /* type*/
245 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
246 data += sizeof(sa6->sin6_addr.s6_addr);
247 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
248 data += sizeof(sa6->sin6_port);
255 /* handle DNS names */
256 if (options->fqdn && dsi) {
257 size_t len = strlen(options->fqdn);
258 if ( len + 2 + addresses_len < maxstatuslen - offset) {
261 memcpy(data, options->fqdn, len);
263 addresses_len += len+2;
266 /* Annouce support for SSH tunneled AFP session,
267 * this feature is available since 10.3.2.
268 * According to the specs (AFP 3.1 p.225) this should
269 * be an IP+Port style value, but it only works with
270 * a FQDN. OSX Server uses FQDN as well.
272 if ( len + 2 + addresses_len < maxstatuslen - offset) {
273 if (options->flags & OPTION_ANNOUNCESSH) {
276 memcpy(data, options->fqdn, len);
282 /* calculate/store Directory Services Names offset */
283 offset = htons(data - begin);
284 *servoffset += sizeof(offset);
285 memcpy(begin + *servoffset, &offset, sizeof(offset));
287 /* return length of buffer */
288 return (data - begin);
291 static size_t status_directorynames(char *data, int *diroffset,
293 const struct afp_options *options)
297 memcpy(&offset, data + *diroffset, sizeof(offset));
298 offset = ntohs(offset);
301 /* I can not find documentation of any other purpose for the
302 * DirectoryNames field.
305 * Try to synthesize a principal:
306 * service '/' fqdn '@' realm
308 if (options->k5service && options->k5realm && options->fqdn) {
309 /* should k5princ be utf8 encoded? */
311 char *p = strchr( options->fqdn, ':' );
314 len = strlen( options->k5service )
315 + strlen( options->fqdn )
316 + strlen( options->k5realm );
317 len+=2; /* '/' and '@' */
318 if ( len > 255 || len+2 > maxstatuslen - offset) {
320 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
323 *data++ = 1; /* DirectoryNamesCount */
325 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
326 options->fqdn, options->k5realm );
335 /* Calculate and store offset for UTF8ServerName */
336 *diroffset += sizeof(uint16_t);
337 offset = htons(data - begin);
338 memcpy(begin + *diroffset, &offset, sizeof(uint16_t));
340 /* return length of buffer */
341 return (data - begin);
344 static size_t status_utf8servername(char *data, int *nameoffset,
346 const struct afp_options *options)
348 char *Obj, *Type, *Zone;
352 uint16_t offset, status;
354 memcpy(&offset, data + *nameoffset, sizeof(offset));
355 offset = ntohs(offset);
359 * What is the valid character range for an nbpname?
361 * Apple's server likes to use the non-qualified hostname
362 * This obviously won't work very well if multiple servers are running
366 /* extract the obj part of the server */
367 Obj = (char *) (options->server ? options->server : options->hostname);
368 if ((size_t) -1 == (len = convert_string (
369 options->unixcharset, CH_UTF8_MAC,
370 Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
371 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
373 /* set offset to 0 */
374 memset(begin + *nameoffset, 0, sizeof(offset));
375 data = begin + offset;
377 namelen = htons(len);
378 memcpy( data, &namelen, sizeof(namelen));
379 data += sizeof(namelen);
381 offset = htons(offset);
382 memcpy(begin + *nameoffset, &offset, sizeof(uint16_t));
385 /* return length of buffer */
386 return (data - begin);
390 /* returns actual offset to signature */
391 static void status_icon(char *data, const unsigned char *icondata,
392 const int iconlen, const int sigoffset)
395 char *sigdata = data + sigoffset;
396 uint16_t ret, status;
398 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
399 if ( icondata == NULL ) {
401 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
403 data += ntohs( status );
404 memcpy( data, icondata, iconlen);
406 ret = htons(data - start);
409 /* put in signature offset */
411 memcpy(sigdata, &ret, sizeof(ret));
414 /* ---------------------
416 void status_init(AFPConfig *dsiconfig,
417 const struct afp_options *options)
424 if (!dsiconfig || !options)
429 status = dsiconfig->status;
430 maxstatuslen=sizeof(dsiconfig->status);
431 dsi = dsiconfig->obj.handle;
432 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
433 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
434 ipok = sa4->sin_addr.s_addr ? 1 : 0;
436 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
437 for (int i=0; i<16; i++) {
438 if (sa6->sin6_addr.s6_addr[i]) {
448 * These routines must be called in order -- earlier calls
449 * set the offsets for later calls.
451 * using structs is a bad idea, but that's what the original code
452 * does. solaris, in fact, will segfault with them. so, now
453 * we just use the powers of #defines and memcpy.
455 * reply block layout (offsets are 16-bit quantities):
456 * machine type offset -> AFP version count offset ->
457 * UAM count offset -> vol icon/mask offset -> flags ->
459 * server name [padded to even boundary] -> signature offset ->
460 * network address offset ->
462 * at the appropriate offsets:
463 * machine type, afp versions, uams, server signature
464 * (16-bytes), network addresses, volume icon/mask
468 options->server_notif,
469 (options->fqdn || ipok),
471 (options->k5service && options->k5realm && options->fqdn),
473 /* returns offset to signature offset */
474 c = status_server(status, options->server ? options->server :
475 options->hostname, options);
476 status_machine(status);
477 status_versions(status, dsi);
478 status_uams(status, options->uamlist);
479 if (options->flags & OPTION_CUSTOMICON)
480 status_icon(status, icon, sizeof(icon), c);
482 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
484 sigoff = status_signature(status, &c, options);
485 /* c now contains the offset where the netaddress offset lives */
487 status_netaddress(status, &c, dsi, options);
488 /* c now contains the offset where the Directory Names Count offset lives */
490 statuslen = status_directorynames(status, &c, dsi, options);
491 /* c now contains the offset where the UTF-8 ServerName offset lives */
493 if ( statuslen < maxstatuslen)
494 statuslen = status_utf8servername(status, &c, dsi, options);
497 if ((options->flags & OPTION_CUSTOMICON) == 0) {
498 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
500 dsi_setstatus(dsi, status, statuslen);
501 dsiconfig->signature = status + sigoff;
502 dsiconfig->statuslen = statuslen;
506 /* set_signature() */
508 /* If found in conf file, use it. */
509 /* If not found in conf file, genarate and append in conf file. */
510 /* If conf file don't exist, create and genarate. */
511 /* If cannot open conf file, use one-time signature. */
512 /* If -signature user:xxxxx, use it. */
514 void set_signature(struct afp_options *options) {
518 char *servername_conf;
521 FILE *fp = NULL, *randomp;
525 server_tmp = (options->server ? options->server : options->hostname);
526 if (strcmp(options->signatureopt, "auto") == 0) {
527 goto server_signature_auto; /* default */
528 } else if (strcmp(options->signatureopt, "host") == 0) {
529 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
530 goto server_signature_auto; /* same as auto */
531 } else if (strncmp(options->signatureopt, "user", 4) == 0) {
532 goto server_signature_user; /* user string */
534 LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
535 goto server_signature_auto; /* switch back to auto*/
538 server_signature_user:
540 /* Signature type is user string */
541 len = strlen(options->signatureopt);
543 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
544 goto server_signature_auto;
546 usersign = options->signatureopt + 5;
549 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !", usersign);
551 } else if (len >= 3) {
552 LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
554 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !", usersign);
556 memset(options->signature, 0, 16);
557 memcpy(options->signature, usersign, len);
558 goto server_signature_done;
560 server_signature_auto:
562 /* Signature type is auto, using afp_signature.conf */
563 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
564 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
565 /* scan in the conf file */
566 while (fgets(buf, sizeof(buf), fp) != NULL) {
568 while (p && isblank(*p))
570 if (!p || (*p == '#') || (*p == '\n'))
571 continue; /* invalid line */
574 if ((servername_conf = strtok( p, "\"" )) == NULL)
575 continue; /* syntax error: invalid quoted servername */
577 if ((servername_conf = strtok( p, " \t" )) == NULL)
578 continue; /* syntax error: invalid servername */
583 continue; /* syntax error: missing signature */
585 if (strcmp(server_tmp, servername_conf))
586 continue; /* another servername */
588 while (p && isblank(*p))
590 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
591 &options->signature[ 0], &options->signature[ 1],
592 &options->signature[ 2], &options->signature[ 3],
593 &options->signature[ 4], &options->signature[ 5],
594 &options->signature[ 6], &options->signature[ 7],
595 &options->signature[ 8], &options->signature[ 9],
596 &options->signature[10], &options->signature[11],
597 &options->signature[12], &options->signature[13],
598 &options->signature[14], &options->signature[15]
601 goto server_signature_done; /* found in conf file */
604 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
605 fseek(fp, 0L, SEEK_END);
606 if(ftell(fp) == 0) { /* size = 0 */
608 goto server_signature_random;
610 fseek(fp, -1L, SEEK_END);
611 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
612 goto server_signature_random;
615 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
616 options->sigconffile, strerror(errno));
617 goto server_signature_random;
620 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
621 options->sigconffile, strerror(errno));
622 goto server_signature_random;
624 } else { /* conf file don't exist */
625 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
626 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s). Using one-time signature.",
627 options->sigconffile, strerror(errno));
628 goto server_signature_random;
630 if (( fp = fdopen( fd, "w" )) == NULL ) {
631 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s). Using one-time signature.",
632 options->sigconffile, strerror(errno));
634 goto server_signature_random;
637 goto server_signature_random;
640 server_signature_random:
642 /* generate signature from random number */
643 randombytes(options->signature, 16);
645 if (fp && header) { /* conf file is created or size=0 */
646 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
647 fprintf(fp, "# This file is auto-generated by afpd.\n");
649 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
650 fprintf(fp, "# the same server twice.\n");
652 fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
656 fprintf(fp, "\"%s\"\t", server_tmp);
657 for (i=0 ; i<16 ; i++) {
658 fprintf(fp, "%02X", (options->signature)[i]);
660 fprintf(fp, "%s", "\n");
664 server_signature_done:
667 LOG(log_info, logtype_afpd,
668 " \"%s\"'s signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
670 (options->signature)[ 0], (options->signature)[ 1],
671 (options->signature)[ 2], (options->signature)[ 3],
672 (options->signature)[ 4], (options->signature)[ 5],
673 (options->signature)[ 6], (options->signature)[ 7],
674 (options->signature)[ 8], (options->signature)[ 9],
675 (options->signature)[10], (options->signature)[11],
676 (options->signature)[12], (options->signature)[13],
677 (options->signature)[14], (options->signature)[15]);
682 /* this is the same as asp/dsi_getstatus */
683 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
685 AFPConfig *config = obj->config;
687 memcpy(rbuf, config->status, config->statuslen);
688 *rbuflen = config->statuslen;