]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
Merge master
[netatalk.git] / etc / afpd / status.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <time.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <sys/socket.h>
20 #include <atalk/logger.h>
21
22 #if 0
23 #ifdef BSD4_4
24 #include <sys/param.h>
25 #ifndef HAVE_GETHOSTID
26 #include <sys/sysctl.h>
27 #endif /* HAVE_GETHOSTID */
28 #endif /* BSD4_4 */
29 #endif
30
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>
39
40 #include "globals.h"  /* includes <netdb.h> */
41 #include "status.h"
42 #include "afp_config.h"
43 #include "icon.h"
44
45 static   size_t maxstatuslen = 0;
46
47 static void status_flags(char *data, const int notif, const int ipok,
48                          const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
49 {
50     u_int16_t           status;
51
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 */
59     if (ipok)
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
66      * notifications. */
67     if (notif) {
68         status |= AFPSRVRINFO_SRVNOTIFY;
69     }
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 ... */
73
74     if (flags & OPTION_UUID)    /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
75         status |= AFPSRVRINFO_UUID;
76
77     status = htons(status);
78     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
79 }
80
81 static int status_server(char *data, const char *server, const struct afp_options *options)
82 {
83     char                *start = data;
84     char                *Obj, *Type, *Zone;
85     char                buf[32];
86     u_int16_t           status;
87     size_t              len;
88
89     /* make room for all offsets before server name */
90     data += AFPSTATUS_PRELEN;
91
92     /* extract the obj part of the server */
93     Obj = (char *) server;
94 #ifndef NO_DDP
95     nbp_name(server, &Obj, &Type, &Zone);
96 #endif
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);
101         *data++ = len;
102         memcpy( data, Obj, len );
103         LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
104     } else {
105         *data++ = len;
106         memcpy( data, buf, len );
107     }
108     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
109         len++;
110     data += len;
111
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.
115      *
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
120      * things.  
121      *
122      * NOTE2: AFP3.1 Documentation states that the directory names offset
123      * is a required feature, even though it can be set to zero.
124      */
125     len = data - start;
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 */
129 }
130
131 static void status_machine(char *data)
132 {
133     char                *start = data;
134     u_int16_t           status;
135     int                 len;
136 #ifdef AFS
137     const char          *machine = "afs";
138 #else /* !AFS */
139     const char          *machine = "Netatalk %s";
140 #endif /* AFS */
141     char buf[64];
142
143     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
144     data += ntohs( status );
145
146     //    len = strlen( machine );
147     len = snprintf(buf, 64, machine, VERSION);
148     *data++ = len;
149     memcpy( data, buf, len );
150     data += len;
151
152     status = htons(data - start);
153     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
154 }
155
156 /* server signature is a 16-byte quantity */
157 static u_int16_t status_signature(char *data, int *servoffset,
158                                   const struct afp_options *options)
159 {
160     char                 *status;
161     u_int16_t            offset, sigoff;
162
163     status = data;
164
165     /* get server signature offset */
166     memcpy(&offset, data + *servoffset, sizeof(offset));
167     sigoff = offset = ntohs(offset);
168
169     /* jump to server signature offset */
170     data += offset;
171
172     memset(data, 0, 16);
173     memcpy(data, options->signature, 16);
174     data += 16;
175
176     /* calculate net address offset */
177     *servoffset += sizeof(offset);
178     offset = htons(data - status);
179     memcpy(status + *servoffset, &offset, sizeof(offset));
180     return sigoff;
181 }
182
183 static size_t status_netaddress(char *data, int *servoffset,
184                              const ASP asp, const DSI *dsi,
185                              const struct afp_options *options)
186 {
187     char               *begin;
188     u_int16_t          offset;
189     size_t             addresses_len = 0;
190
191     begin = data;
192
193     /* get net address offset */
194     memcpy(&offset, data + *servoffset, sizeof(offset));
195     data += ntohs(offset);
196
197     /* format:
198        Address count (byte) 
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)
203        */
204
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) + (asp ? 1 : 0) +
209               (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
210
211     /* ip address */
212     if (dsi) {
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);
221                 addresses_len += 7;
222             } else {
223                 /* ip address + port */
224                 *data++ = 8;
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);
231                 addresses_len += 9;
232             }
233         } else { /* IPv6 */
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);
240                 addresses_len += 19;
241             } else {
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);
249                 addresses_len += 21;
250             }
251
252         }
253     }
254
255     /* handle DNS names */
256     if (options->fqdn && dsi) {
257         size_t len = strlen(options->fqdn);
258         if ( len + 2 + addresses_len < maxstatuslen - offset) {
259             *data++ = len +2;
260             *data++ = 0x04;
261             memcpy(data, options->fqdn, len);
262             data += len;
263             addresses_len += len+2;
264         }
265
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.
271          */
272         if ( len + 2 + addresses_len < maxstatuslen - offset) {
273             if (options->flags & OPTION_ANNOUNCESSH) {
274                 *data++ = len +2;
275                 *data++ = 0x05;
276                 memcpy(data, options->fqdn, len);
277                 data += len;
278             }
279         }
280     }
281
282 #ifndef NO_DDP
283     if (asp) {
284         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
285
286         /* ddp address */
287         *data++ = 6;
288         *data++ = 0x03; /* ddp address */
289         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
290         data += sizeof(ddpaddr->sat_addr.s_net);
291         memcpy(data, &ddpaddr->sat_addr.s_node,
292                sizeof(ddpaddr->sat_addr.s_node));
293         data += sizeof(ddpaddr->sat_addr.s_node);
294         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
295         data += sizeof(ddpaddr->sat_port);
296     }
297 #endif /* ! NO_DDP */
298
299     /* calculate/store Directory Services Names offset */
300     offset = htons(data - begin); 
301     *servoffset += sizeof(offset);
302     memcpy(begin + *servoffset, &offset, sizeof(offset));
303
304     /* return length of buffer */
305     return (data - begin);
306 }
307
308 static size_t status_directorynames(char *data, int *diroffset, 
309                                  const DSI *dsi _U_, 
310                                  const struct afp_options *options)
311 {
312     char *begin = data;
313     u_int16_t offset;
314     memcpy(&offset, data + *diroffset, sizeof(offset));
315     offset = ntohs(offset);
316     data += offset;
317
318     /* I can not find documentation of any other purpose for the
319      * DirectoryNames field.
320      */
321     /*
322      * Try to synthesize a principal:
323      * service '/' fqdn '@' realm
324      */
325     if (options->k5service && options->k5realm && options->fqdn) {
326         /* should k5princ be utf8 encoded? */
327         size_t len;
328         char *p = strchr( options->fqdn, ':' );
329         if (p) 
330             *p = '\0';
331         len = strlen( options->k5service ) 
332                         + strlen( options->fqdn )
333                         + strlen( options->k5realm );
334         len+=2; /* '/' and '@' */
335         if ( len > 255 || len+2 > maxstatuslen - offset) {
336             *data++ = 0;
337             LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
338         }        
339         else {
340             *data++ = 1; /* DirectoryNamesCount */
341             *data++ = len;
342             snprintf( data, len + 1, "%s/%s@%s", options->k5service,
343                                 options->fqdn, options->k5realm );
344             data += len;
345             if (p)
346                 *p = ':';
347        }
348     } else {
349         *data++ = 0;
350     }
351
352     /* Calculate and store offset for UTF8ServerName */
353     *diroffset += sizeof(u_int16_t);
354     offset = htons(data - begin);
355     memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
356
357     /* return length of buffer */
358     return (data - begin);
359 }
360
361 static size_t status_utf8servername(char *data, int *nameoffset,
362                                  const DSI *dsi _U_,
363                                  const struct afp_options *options)
364 {
365     char *Obj, *Type, *Zone;
366     u_int16_t namelen;
367     size_t len;
368     char *begin = data;
369     u_int16_t offset, status;
370
371     memcpy(&offset, data + *nameoffset, sizeof(offset));
372     offset = ntohs(offset);
373     data += offset;
374
375     /* FIXME:
376      * What is the valid character range for an nbpname?
377      *
378      * Apple's server likes to use the non-qualified hostname
379      * This obviously won't work very well if multiple servers are running
380      * on the box.
381      */
382
383     /* extract the obj part of the server */
384     Obj = (char *) (options->server ? options->server : options->hostname);
385 #ifndef NO_DDP
386     nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
387 #endif
388     if ((size_t) -1 == (len = convert_string (
389                             options->unixcharset, CH_UTF8_MAC, 
390                             Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
391         LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
392
393         /* set offset to 0 */
394         memset(begin + *nameoffset, 0, sizeof(offset));
395         data = begin + offset;
396     } else {
397         namelen = htons(len);
398         memcpy( data, &namelen, sizeof(namelen));
399         data += sizeof(namelen);
400         data += len;
401         offset = htons(offset);
402         memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
403         
404         /* Now set the flag ... */
405         memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
406         status = ntohs(status);
407         status |= AFPSRVRINFO_SRVUTF8;
408         status = htons(status);
409         memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
410     }
411
412     /* return length of buffer */
413     return (data - begin);
414
415 }
416
417 /* returns actual offset to signature */
418 static void status_icon(char *data, const unsigned char *icondata,
419                         const int iconlen, const int sigoffset)
420 {
421     char                *start = data;
422     char                *sigdata = data + sigoffset;
423     u_int16_t           ret, status;
424
425     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
426     if ( icondata == NULL ) {
427         ret = status;
428         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
429     } else {
430         data += ntohs( status );
431         memcpy( data, icondata, iconlen);
432         data += iconlen;
433         ret = htons(data - start);
434     }
435
436     /* put in signature offset */
437     if (sigoffset)
438         memcpy(sigdata, &ret, sizeof(ret));
439 }
440
441 /* ---------------------
442  */
443 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
444                  const struct afp_options *options)
445 {
446     ASP asp;
447     DSI *dsi;
448     char *status = NULL;
449     size_t statuslen;
450     int c, sigoff, ipok;
451
452     if (!(aspconfig || dsiconfig) || !options)
453         return;
454
455     if (aspconfig) {
456         status = aspconfig->status;
457         maxstatuslen=sizeof(aspconfig->status);
458         asp = aspconfig->obj.handle;
459     } else
460         asp = NULL;
461         
462     ipok = 0;
463     if (dsiconfig) {
464         status = dsiconfig->status;
465         maxstatuslen=sizeof(dsiconfig->status);
466         dsi = dsiconfig->obj.handle;
467         if (dsi->server.ss_family == AF_INET) { /* IPv4 */
468             const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
469             ipok = sa4->sin_addr.s_addr ? 1 : 0;
470         } else { /* IPv6 */
471             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
472             for (int i=0; i<16; i++) {
473                 if (sa6->sin6_addr.s6_addr[i]) {
474                     ipok = 1;
475                     break;
476                 }
477             }
478         }
479     } else
480         dsi = NULL;
481
482     /*
483      * These routines must be called in order -- earlier calls
484      * set the offsets for later calls.
485      *
486      * using structs is a bad idea, but that's what the original code
487      * does. solaris, in fact, will segfault with them. so, now
488      * we just use the powers of #defines and memcpy.
489      *
490      * reply block layout (offsets are 16-bit quantities):
491      * machine type offset -> AFP version count offset ->
492      * UAM count offset -> vol icon/mask offset -> flags ->
493      *
494      * server name [padded to even boundary] -> signature offset ->
495      * network address offset ->
496      *
497      * at the appropriate offsets:
498      * machine type, afp versions, uams, server signature
499      * (16-bytes), network addresses, volume icon/mask 
500      */
501
502     status_flags(status,
503                  options->server_notif,
504                  (options->fqdn || ipok),
505                  options->passwdbits, 
506                  (options->k5service && options->k5realm && options->fqdn),
507                  options->flags);
508     /* returns offset to signature offset */
509     c = status_server(status, options->server ? options->server :
510                       options->hostname, options);
511     status_machine(status);
512     status_versions(status, asp, dsi);
513     status_uams(status, options->uamlist);
514     if (options->flags & OPTION_CUSTOMICON)
515         status_icon(status, icon, sizeof(icon), c);
516     else
517         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
518
519     sigoff = status_signature(status, &c, options);
520     /* c now contains the offset where the netaddress offset lives */
521
522     status_netaddress(status, &c, asp, dsi, options);
523     /* c now contains the offset where the Directory Names Count offset lives */
524
525     statuslen = status_directorynames(status, &c, dsi, options);
526     /* c now contains the offset where the UTF-8 ServerName offset lives */
527
528     if ( statuslen < maxstatuslen) 
529         statuslen = status_utf8servername(status, &c, dsi, options);
530
531 #ifndef NO_DDP
532     if (aspconfig) {
533         if (dsiconfig) /* status is dsiconfig->status */
534             memcpy(aspconfig->status, status, statuslen);
535         asp_setstatus(asp, status, statuslen);
536         aspconfig->signature = status + sigoff;
537         aspconfig->statuslen = statuslen;
538     }
539 #endif /* ! NO_DDP */
540
541     if (dsiconfig) {
542         if ((options->flags & OPTION_CUSTOMICON) == 0) {
543             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
544         }
545         dsi_setstatus(dsi, status, statuslen);
546         dsiconfig->signature = status + sigoff;
547         dsiconfig->statuslen = statuslen;
548     }
549 }
550
551 /* set_signature()                                                    */
552 /*                                                                    */
553 /* If found in conf file, use it.                                     */
554 /* If not found in conf file, genarate and append in conf file.       */
555 /* If conf file don't exist, create and genarate.                     */
556 /* If cannot open conf file, use one-time signature.                  */
557 /* If -signature user:xxxxx, use it.                                  */
558
559 void set_signature(struct afp_options *options) {
560     char *usersign;
561     int fd, i;
562     struct stat tmpstat;
563     char *servername_conf;
564     int header = 0;
565     char buf[1024], *p;
566     FILE *fp = NULL, *randomp;
567     size_t len;
568     char *server_tmp;
569     
570     server_tmp = (options->server ? options->server : options->hostname);
571     if (strcmp(options->signatureopt, "auto") == 0) {
572         goto server_signature_auto;   /* default */
573     } else if (strcmp(options->signatureopt, "host") == 0) {
574         LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
575         goto server_signature_auto;   /* same as auto */
576     } else if (strncmp(options->signatureopt, "user", 4) == 0) {
577         goto server_signature_user;   /*  user string */
578     } else {
579         LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
580         goto server_signature_auto;   /* switch back to auto*/
581     }
582     
583 server_signature_user:
584     
585     /* Signature type is user string */
586     len = strlen(options->signatureopt);
587     if (len <= 5) {
588         LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
589         goto server_signature_auto;
590     }
591     usersign = options->signatureopt + 5;
592     len = len - 5;
593     if (len > 16) {
594         LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !",  usersign);
595         len = 16;
596     } else if (len >= 3) {
597         LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
598     } else {
599         LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !",  usersign);
600     }
601     memset(options->signature, 0, 16);
602     memcpy(options->signature, usersign, len);
603     goto server_signature_done;
604     
605 server_signature_auto:
606     
607     /* Signature type is auto, using afp_signature.conf */
608     if (!stat(options->sigconffile, &tmpstat)) {                /* conf file exists? */
609         if ((fp = fopen(options->sigconffile, "r")) != NULL) {  /* read open? */
610             /* scan in the conf file */
611             while (fgets(buf, sizeof(buf), fp) != NULL) { 
612                 p = buf;
613                 while (p && isblank(*p))
614                     p++;
615                 if (!p || (*p == '#') || (*p == '\n'))
616                     continue;                             /* invalid line */
617                 if (*p == '"') {
618                     p++;
619                     if ((servername_conf = strtok( p, "\"" )) == NULL)
620                         continue;                         /* syntax error: invalid quoted servername */
621                 } else {
622                     if ((servername_conf = strtok( p, " \t" )) == NULL)
623                         continue;                         /* syntax error: invalid servername */
624                 }
625                 p = strchr(p, '\0');
626                 p++;
627                 if (*p == '\0')
628                     continue;                             /* syntax error: missing signature */
629                 
630                 if (strcmp(server_tmp, servername_conf))
631                     continue;                             /* another servername */
632                 
633                 while (p && isblank(*p))
634                     p++;
635                 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
636                                   &options->signature[ 0], &options->signature[ 1],
637                                   &options->signature[ 2], &options->signature[ 3],
638                                   &options->signature[ 4], &options->signature[ 5],
639                                   &options->signature[ 6], &options->signature[ 7],
640                                   &options->signature[ 8], &options->signature[ 9],
641                                   &options->signature[10], &options->signature[11],
642                                   &options->signature[12], &options->signature[13],
643                                   &options->signature[14], &options->signature[15]
644                          )) {
645                     fclose(fp);
646                     goto server_signature_done;                 /* found in conf file */
647                 }
648             }
649             if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
650                 fseek(fp, 0L, SEEK_END);
651                 if(ftell(fp) == 0) {                     /* size = 0 */
652                     header = 1;
653                     goto server_signature_random;
654                 } else {
655                     fseek(fp, -1L, SEEK_END);
656                     if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
657                     goto server_signature_random;
658                 }                    
659             } else {
660                 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
661                     options->sigconffile, strerror(errno));
662                 goto server_signature_random;
663             }
664         } else {
665             LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
666                 options->sigconffile, strerror(errno));
667             goto server_signature_random;
668         }
669     } else { /* conf file don't exist */
670         if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
671             LOG(log_error, logtype_atalkd, "Cannot create %s (%s). Using one-time signature.",
672                 options->sigconffile, strerror(errno));
673             goto server_signature_random;
674         }
675         if (( fp = fdopen( fd, "w" )) == NULL ) {
676             LOG(log_error, logtype_atalkd, "Cannot fdopen %s (%s). Using one-time signature.",
677                 options->sigconffile, strerror(errno));
678             close(fd);
679             goto server_signature_random;
680         }
681         header = 1;
682         goto server_signature_random;
683     }
684     
685 server_signature_random:
686     
687     /* generate signature from random number */
688     randombytes(options->signature, 16);
689
690     if (fp && header) { /* conf file is created or size=0 */
691         fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
692         fprintf(fp, "# This file is auto-generated by afpd.\n");
693         fprintf(fp, "# \n");
694         fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
695         fprintf(fp, "# the same server twice.\n");
696         fprintf(fp, "# \n");
697         fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
698     }
699     
700     if (fp) {
701         fprintf(fp, "\"%s\"\t", server_tmp);
702         for (i=0 ; i<16 ; i++) {
703             fprintf(fp, "%02X", (options->signature)[i]);
704         }
705         fprintf(fp, "%s", "\n");
706         fclose(fp);
707     }
708     
709 server_signature_done:
710     
711     /* retrun */
712     LOG(log_info, logtype_afpd,
713         " \"%s\"'s signature is  %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
714         server_tmp,
715         (options->signature)[ 0], (options->signature)[ 1],
716         (options->signature)[ 2], (options->signature)[ 3],
717         (options->signature)[ 4], (options->signature)[ 5],
718         (options->signature)[ 6], (options->signature)[ 7],
719         (options->signature)[ 8], (options->signature)[ 9],
720         (options->signature)[10], (options->signature)[11],
721         (options->signature)[12], (options->signature)[13],
722         (options->signature)[14], (options->signature)[15]);
723     
724     return;
725 }
726
727 /* this is the same as asp/dsi_getstatus */
728 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
729 {
730     AFPConfig *config = obj->config;
731
732     memcpy(rbuf, config->status, config->statuslen);
733     *rbuflen = config->statuslen;
734     return AFP_OK;
735 }