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>
39 #include "globals.h" /* includes <netdb.h> */
41 #include "afp_config.h"
44 static size_t maxstatuslen = 0;
46 static void status_flags(char *data, const int notif, const int ipok,
47 const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
51 status = AFPSRVRINFO_COPY
52 | AFPSRVRINFO_SRVSIGNATURE
54 | AFPSRVRINFO_FASTBOZO
57 | AFPSRVRINFO_EXTSLEEP;
59 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
60 status |= AFPSRVRINFO_PASSWD;
61 if (passwdbits & PASSWD_NOSAVE)
62 status |= AFPSRVRINFO_NOSAVEPASSWD;
63 if (ipok) /* only advertise tcp/ip if we have a valid address */
64 status |= AFPSRVRINFO_TCPIP;
65 if (notif) /* Default is yes */
66 status |= AFPSRVRINFO_SRVNOTIFY;
67 if (flags & OPTION_UUID)
68 status |= AFPSRVRINFO_UUID;
70 status = htons(status);
71 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
74 static int status_server(char *data, const char *server, const struct afp_options *options)
77 char *Obj, *Type, *Zone;
82 /* make room for all offsets before server name */
83 data += AFPSTATUS_PRELEN;
85 /* extract the obj part of the server */
86 Obj = (char *) server;
87 nbp_name(server, &Obj, &Type, &Zone);
88 if ((size_t)-1 == (len = convert_string(
89 options->unixcharset, options->maccharset,
90 Obj, -1, buf, sizeof(buf))) ) {
91 len = MIN(strlen(Obj), 31);
93 memcpy( data, Obj, len );
94 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
97 memcpy( data, buf, len );
99 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
103 /* make room for signature and net address offset. save location of
104 * signature offset. we're also making room for directory names offset
105 * and the utf-8 server name offset.
107 * NOTE: technically, we don't need to reserve space for the
108 * signature and net address offsets if they're not going to be
109 * used. as there are no offsets after them, it doesn't hurt to
110 * have them specified though. so, we just do that to simplify
113 * NOTE2: AFP3.1 Documentation states that the directory names offset
114 * is a required feature, even though it can be set to zero.
117 status = htons(len + AFPSTATUS_POSTLEN);
118 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
119 return len; /* return the offset to beginning of signature offset */
122 static void status_machine(char *data)
128 const char *machine = "afs";
130 const char *machine = "Netatalk %s";
134 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
135 data += ntohs( status );
137 // len = strlen( machine );
138 len = snprintf(buf, 64, machine, VERSION);
140 memcpy( data, buf, len );
143 status = htons(data - start);
144 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
147 /* server signature is a 16-byte quantity */
148 static u_int16_t status_signature(char *data, int *servoffset,
149 const struct afp_options *options)
152 u_int16_t offset, sigoff;
156 /* get server signature offset */
157 memcpy(&offset, data + *servoffset, sizeof(offset));
158 sigoff = offset = ntohs(offset);
160 /* jump to server signature offset */
164 memcpy(data, options->signature, 16);
167 /* calculate net address offset */
168 *servoffset += sizeof(offset);
169 offset = htons(data - status);
170 memcpy(status + *servoffset, &offset, sizeof(offset));
174 static size_t status_netaddress(char *data, int *servoffset,
175 const ASP asp, const DSI *dsi,
176 const struct afp_options *options)
180 size_t addresses_len = 0;
184 /* get net address offset */
185 memcpy(&offset, data + *servoffset, sizeof(offset));
186 data += ntohs(offset);
190 len (byte = sizeof(length + address type + address)
191 address type (byte, ip address = 0x01, ip + port = 0x02,
192 ddp address = 0x03, fqdn = 0x04)
193 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
196 /* number of addresses. this currently screws up if we have a dsi
197 connection, but we don't have the ip address. to get around this,
198 we turn off the status flag for tcp/ip. */
199 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
200 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
204 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
205 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
206 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
207 *data++ = 6; /* length */
208 *data++ = 0x01; /* basic ip address */
209 memcpy(data, &inaddr->sin_addr.s_addr,
210 sizeof(inaddr->sin_addr.s_addr));
211 data += sizeof(inaddr->sin_addr.s_addr);
214 /* ip address + port */
216 *data++ = 0x02; /* ip address with port */
217 memcpy(data, &inaddr->sin_addr.s_addr,
218 sizeof(inaddr->sin_addr.s_addr));
219 data += sizeof(inaddr->sin_addr.s_addr);
220 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
221 data += sizeof(inaddr->sin_port);
225 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
226 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
227 *data++ = 18; /* length */
228 *data++ = 6; /* type */
229 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
230 data += sizeof(sa6->sin6_addr.s6_addr);
233 /* ip address + port */
234 *data++ = 20; /* length */
235 *data++ = 7; /* type*/
236 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
237 data += sizeof(sa6->sin6_addr.s6_addr);
238 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
239 data += sizeof(sa6->sin6_port);
246 /* handle DNS names */
247 if (options->fqdn && dsi) {
248 size_t len = strlen(options->fqdn);
249 if ( len + 2 + addresses_len < maxstatuslen - offset) {
252 memcpy(data, options->fqdn, len);
254 addresses_len += len+2;
257 /* Annouce support for SSH tunneled AFP session,
258 * this feature is available since 10.3.2.
259 * According to the specs (AFP 3.1 p.225) this should
260 * be an IP+Port style value, but it only works with
261 * a FQDN. OSX Server uses FQDN as well.
263 if ( len + 2 + addresses_len < maxstatuslen - offset) {
264 if (options->flags & OPTION_ANNOUNCESSH) {
267 memcpy(data, options->fqdn, len);
275 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
279 *data++ = 0x03; /* ddp address */
280 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
281 data += sizeof(ddpaddr->sat_addr.s_net);
282 memcpy(data, &ddpaddr->sat_addr.s_node,
283 sizeof(ddpaddr->sat_addr.s_node));
284 data += sizeof(ddpaddr->sat_addr.s_node);
285 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
286 data += sizeof(ddpaddr->sat_port);
288 #endif /* ! NO_DDP */
290 /* calculate/store Directory Services Names offset */
291 offset = htons(data - begin);
292 *servoffset += sizeof(offset);
293 memcpy(begin + *servoffset, &offset, sizeof(offset));
295 /* return length of buffer */
296 return (data - begin);
299 static size_t status_directorynames(char *data, int *diroffset,
301 const struct afp_options *options)
305 memcpy(&offset, data + *diroffset, sizeof(offset));
306 offset = ntohs(offset);
309 /* I can not find documentation of any other purpose for the
310 * DirectoryNames field.
313 * Try to synthesize a principal:
314 * service '/' fqdn '@' realm
316 if (options->k5service && options->k5realm && options->fqdn) {
317 /* should k5princ be utf8 encoded? */
319 char *p = strchr( options->fqdn, ':' );
322 len = strlen( options->k5service )
323 + strlen( options->fqdn )
324 + strlen( options->k5realm );
325 len+=2; /* '/' and '@' */
326 if ( len > 255 || len+2 > maxstatuslen - offset) {
328 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
331 *data++ = 1; /* DirectoryNamesCount */
333 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
334 options->fqdn, options->k5realm );
343 /* Calculate and store offset for UTF8ServerName */
344 *diroffset += sizeof(u_int16_t);
345 offset = htons(data - begin);
346 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
348 /* return length of buffer */
349 return (data - begin);
352 static size_t status_utf8servername(char *data, int *nameoffset,
354 const struct afp_options *options)
356 char *Obj, *Type, *Zone;
360 u_int16_t offset, status;
362 memcpy(&offset, data + *nameoffset, sizeof(offset));
363 offset = ntohs(offset);
367 * What is the valid character range for an nbpname?
369 * Apple's server likes to use the non-qualified hostname
370 * This obviously won't work very well if multiple servers are running
374 /* extract the obj part of the server */
375 Obj = (char *) (options->server ? options->server : options->hostname);
376 nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
378 if ((size_t) -1 == (len = convert_string (
379 options->unixcharset, CH_UTF8_MAC,
380 Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
381 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
383 /* set offset to 0 */
384 memset(begin + *nameoffset, 0, sizeof(offset));
385 data = begin + offset;
388 namelen = htons(len);
389 memcpy( data, &namelen, sizeof(namelen));
390 data += sizeof(namelen);
392 offset = htons(offset);
393 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
396 /* return length of buffer */
397 return (data - begin);
401 /* returns actual offset to signature */
402 static void status_icon(char *data, const unsigned char *icondata,
403 const int iconlen, const int sigoffset)
406 char *sigdata = data + sigoffset;
407 u_int16_t ret, status;
409 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
410 if ( icondata == NULL ) {
412 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
414 data += ntohs( status );
415 memcpy( data, icondata, iconlen);
417 ret = htons(data - start);
420 /* put in signature offset */
422 memcpy(sigdata, &ret, sizeof(ret));
425 /* ---------------------
427 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
428 const struct afp_options *options)
436 if (!(aspconfig || dsiconfig) || !options)
440 status = aspconfig->status;
441 maxstatuslen=sizeof(aspconfig->status);
442 asp = aspconfig->obj.handle;
448 status = dsiconfig->status;
449 maxstatuslen=sizeof(dsiconfig->status);
450 dsi = dsiconfig->obj.handle;
451 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
452 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
453 ipok = sa4->sin_addr.s_addr ? 1 : 0;
455 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
456 for (int i=0; i<16; i++) {
457 if (sa6->sin6_addr.s6_addr[i]) {
467 * These routines must be called in order -- earlier calls
468 * set the offsets for later calls.
470 * using structs is a bad idea, but that's what the original code
471 * does. solaris, in fact, will segfault with them. so, now
472 * we just use the powers of #defines and memcpy.
474 * reply block layout (offsets are 16-bit quantities):
475 * machine type offset -> AFP version count offset ->
476 * UAM count offset -> vol icon/mask offset -> flags ->
478 * server name [padded to even boundary] -> signature offset ->
479 * network address offset ->
481 * at the appropriate offsets:
482 * machine type, afp versions, uams, server signature
483 * (16-bytes), network addresses, volume icon/mask
487 options->server_notif,
488 (options->fqdn || ipok),
490 (options->k5service && options->k5realm && options->fqdn),
492 /* returns offset to signature offset */
493 c = status_server(status, options->server ? options->server :
494 options->hostname, options);
495 status_machine(status);
496 status_versions(status, asp, dsi);
497 status_uams(status, options->uamlist);
498 if (options->flags & OPTION_CUSTOMICON)
499 status_icon(status, icon, sizeof(icon), c);
501 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
503 sigoff = status_signature(status, &c, options);
504 /* c now contains the offset where the netaddress offset lives */
506 status_netaddress(status, &c, asp, dsi, options);
507 /* c now contains the offset where the Directory Names Count offset lives */
509 statuslen = status_directorynames(status, &c, dsi, options);
510 /* c now contains the offset where the UTF-8 ServerName offset lives */
512 if ( statuslen < maxstatuslen)
513 statuslen = status_utf8servername(status, &c, dsi, options);
517 if (dsiconfig) /* status is dsiconfig->status */
518 memcpy(aspconfig->status, status, statuslen);
519 asp_setstatus(asp, status, statuslen);
520 aspconfig->signature = status + sigoff;
521 aspconfig->statuslen = statuslen;
523 #endif /* ! NO_DDP */
526 if ((options->flags & OPTION_CUSTOMICON) == 0) {
527 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
529 dsi_setstatus(dsi, status, statuslen);
530 dsiconfig->signature = status + sigoff;
531 dsiconfig->statuslen = statuslen;
535 /* set_signature() */
537 /* If found in conf file, use it. */
538 /* If not found in conf file, genarate and append in conf file. */
539 /* If conf file don't exist, create and genarate. */
540 /* If cannot open conf file, use one-time signature. */
541 /* If -signature user:xxxxx, use it. */
543 void set_signature(struct afp_options *options) {
547 char *servername_conf;
550 FILE *fp = NULL, *randomp;
554 server_tmp = (options->server ? options->server : options->hostname);
555 if (strcmp(options->signatureopt, "auto") == 0) {
556 goto server_signature_auto; /* default */
557 } else if (strcmp(options->signatureopt, "host") == 0) {
558 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
559 goto server_signature_auto; /* same as auto */
560 } else if (strncmp(options->signatureopt, "user", 4) == 0) {
561 goto server_signature_user; /* user string */
563 LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
564 goto server_signature_auto; /* switch back to auto*/
567 server_signature_user:
569 /* Signature type is user string */
570 len = strlen(options->signatureopt);
572 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
573 goto server_signature_auto;
575 usersign = options->signatureopt + 5;
578 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !", usersign);
580 } else if (len >= 3) {
581 LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
583 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !", usersign);
585 memset(options->signature, 0, 16);
586 memcpy(options->signature, usersign, len);
587 goto server_signature_done;
589 server_signature_auto:
591 /* Signature type is auto, using afp_signature.conf */
592 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
593 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
594 /* scan in the conf file */
595 while (fgets(buf, sizeof(buf), fp) != NULL) {
597 while (p && isblank(*p))
599 if (!p || (*p == '#') || (*p == '\n'))
600 continue; /* invalid line */
603 if ((servername_conf = strtok( p, "\"" )) == NULL)
604 continue; /* syntax error: invalid quoted servername */
606 if ((servername_conf = strtok( p, " \t" )) == NULL)
607 continue; /* syntax error: invalid servername */
612 continue; /* syntax error: missing signature */
614 if (strcmp(server_tmp, servername_conf))
615 continue; /* another servername */
617 while (p && isblank(*p))
619 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
620 &options->signature[ 0], &options->signature[ 1],
621 &options->signature[ 2], &options->signature[ 3],
622 &options->signature[ 4], &options->signature[ 5],
623 &options->signature[ 6], &options->signature[ 7],
624 &options->signature[ 8], &options->signature[ 9],
625 &options->signature[10], &options->signature[11],
626 &options->signature[12], &options->signature[13],
627 &options->signature[14], &options->signature[15]
630 goto server_signature_done; /* found in conf file */
633 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
634 fseek(fp, 0L, SEEK_END);
635 if(ftell(fp) == 0) { /* size = 0 */
637 goto server_signature_random;
639 fseek(fp, -1L, SEEK_END);
640 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
641 goto server_signature_random;
644 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
645 options->sigconffile, strerror(errno));
646 goto server_signature_random;
649 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
650 options->sigconffile, strerror(errno));
651 goto server_signature_random;
653 } else { /* conf file don't exist */
654 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
655 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s). Using one-time signature.",
656 options->sigconffile, strerror(errno));
657 goto server_signature_random;
659 if (( fp = fdopen( fd, "w" )) == NULL ) {
660 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s). Using one-time signature.",
661 options->sigconffile, strerror(errno));
663 goto server_signature_random;
666 goto server_signature_random;
669 server_signature_random:
671 /* generate signature from random number */
672 randombytes(options->signature, 16);
674 if (fp && header) { /* conf file is created or size=0 */
675 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
676 fprintf(fp, "# This file is auto-generated by afpd.\n");
678 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
679 fprintf(fp, "# the same server twice.\n");
681 fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
685 fprintf(fp, "\"%s\"\t", server_tmp);
686 for (i=0 ; i<16 ; i++) {
687 fprintf(fp, "%02X", (options->signature)[i]);
689 fprintf(fp, "%s", "\n");
693 server_signature_done:
696 LOG(log_info, logtype_afpd,
697 " \"%s\"'s signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
699 (options->signature)[ 0], (options->signature)[ 1],
700 (options->signature)[ 2], (options->signature)[ 3],
701 (options->signature)[ 4], (options->signature)[ 5],
702 (options->signature)[ 6], (options->signature)[ 7],
703 (options->signature)[ 8], (options->signature)[ 9],
704 (options->signature)[10], (options->signature)[11],
705 (options->signature)[12], (options->signature)[13],
706 (options->signature)[14], (options->signature)[15]);
711 /* this is the same as asp/dsi_getstatus */
712 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
714 AFPConfig *config = obj->config;
716 memcpy(rbuf, config->status, config->statuslen);
717 *rbuflen = config->statuslen;