]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
signature parameter: more simple syntax
[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 <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <time.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <sys/socket.h>
21 #include <atalk/logger.h>
22
23 #if 0
24 #ifdef BSD4_4
25 #include <sys/param.h>
26 #ifndef HAVE_GETHOSTID
27 #include <sys/sysctl.h>
28 #endif /* HAVE_GETHOSTID */
29 #endif /* BSD4_4 */
30 #endif
31
32 #include <arpa/inet.h>
33
34 #ifdef HAVE_KERBEROS
35 #ifdef HAVE_KRB5_KRB5_H
36 #include <krb5/krb5.h>
37 #else
38 #include <krb5.h>
39 #endif /* HAVE_KRB5_KRB5_H */
40 #endif /* HAVE_KERBEROS */
41
42 #include <atalk/dsi.h>
43 #include <atalk/unicode.h>
44 #include <atalk/util.h>
45 #include <atalk/globals.h>
46
47 #include "status.h"
48 #include "afp_config.h"
49 #include "icon.h"
50 #include "uam_auth.h"
51
52 static   size_t maxstatuslen = 0;
53
54 static int uam_gss_enabled()
55 {
56     /* XXX: must be a better way to find out if uam_gss is active */
57     return auth_uamfind(UAM_SERVER_LOGIN_EXT,
58                         "Client Krb v2",
59                         sizeof("Client Krb v2")) != NULL;
60 }
61
62 static void status_flags(char *data,
63                          const int notif,
64                          const int ipok,
65                          const unsigned char passwdbits,
66                          const int dirsrvcs,
67                          int flags)
68 {
69     uint16_t           status;
70
71     status = AFPSRVRINFO_COPY
72            | AFPSRVRINFO_SRVSIGNATURE
73            | AFPSRVRINFO_SRVMSGS
74            | AFPSRVRINFO_FASTBOZO
75            | AFPSRVRINFO_SRVUTF8
76            | AFPSRVRINFO_EXTSLEEP;
77
78     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
79         status |= AFPSRVRINFO_PASSWD;
80     if (passwdbits & PASSWD_NOSAVE)
81         status |= AFPSRVRINFO_NOSAVEPASSWD;
82     if (ipok) /* only advertise tcp/ip if we have a valid address */
83         status |= AFPSRVRINFO_TCPIP;
84     if (notif) /* Default is yes */
85         status |= AFPSRVRINFO_SRVNOTIFY;
86     if (dirsrvcs)
87         status |= AFPSRVRINFO_SRVRDIR;
88     if (flags & OPTION_UUID)
89         status |= AFPSRVRINFO_UUID;
90
91     status = htons(status);
92     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
93 }
94
95 static int status_server(char *data, const char *server, const struct afp_options *options)
96 {
97     char                *start = data;
98     char                *Obj, *Type, *Zone;
99     char                buf[32];
100     uint16_t           status;
101     size_t              len;
102
103     /* make room for all offsets before server name */
104     data += AFPSTATUS_PRELEN;
105
106     /* extract the obj part of the server */
107     Obj = (char *) server;
108     if ((size_t)-1 == (len = convert_string( 
109                            options->unixcharset, options->maccharset, 
110                            Obj, -1, buf, sizeof(buf))) ) {
111         len = MIN(strlen(Obj), 31);
112         *data++ = len;
113         memcpy( data, Obj, len );
114         LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
115     } else {
116         *data++ = len;
117         memcpy( data, buf, len );
118     }
119     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
120         len++;
121     data += len;
122
123     /* make room for signature and net address offset. save location of
124      * signature offset. we're also making room for directory names offset
125      * and the utf-8 server name offset.
126      *
127      * NOTE: technically, we don't need to reserve space for the
128      * signature and net address offsets if they're not going to be
129      * used. as there are no offsets after them, it doesn't hurt to
130      * have them specified though. so, we just do that to simplify
131      * things.  
132      *
133      * NOTE2: AFP3.1 Documentation states that the directory names offset
134      * is a required feature, even though it can be set to zero.
135      */
136     len = data - start;
137     status = htons(len + AFPSTATUS_POSTLEN);
138     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
139     return len; /* return the offset to beginning of signature offset */
140 }
141
142 static void status_machine(char *data)
143 {
144     char                *start = data;
145     uint16_t           status;
146     int                 len;
147 #ifdef AFS
148     const char          *machine = "afs";
149 #else /* !AFS */
150     const char          *machine = "Netatalk%s";
151 #endif /* AFS */
152     char buf[AFPSTATUS_MACHLEN+1];
153
154     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
155     data += ntohs( status );
156
157     if ((strlen(machine) + strlen(VERSION)) <= AFPSTATUS_MACHLEN) {
158         len = snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, VERSION);
159     } else {
160         if (strlen(VERSION) > AFPSTATUS_MACHLEN) {
161             len = snprintf(buf, AFPSTATUS_MACHLEN + 1, VERSION);
162         } else {
163             (void)snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, "");
164             (void)snprintf(buf + AFPSTATUS_MACHLEN - strlen(VERSION),
165                            strlen(VERSION) + 1,
166                            VERSION);
167             len = AFPSTATUS_MACHLEN;
168         }
169     }
170
171     *data++ = len;
172     memcpy( data, buf, len );
173     data += len;
174
175     status = htons(data - start);
176     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
177 }
178
179 /* server signature is a 16-byte quantity */
180 static uint16_t status_signature(char *data, int *servoffset,
181                                   const struct afp_options *options)
182 {
183     char                 *status;
184     uint16_t            offset, sigoff;
185
186     status = data;
187
188     /* get server signature offset */
189     memcpy(&offset, data + *servoffset, sizeof(offset));
190     sigoff = offset = ntohs(offset);
191
192     /* jump to server signature offset */
193     data += offset;
194
195     memset(data, 0, 16);
196     memcpy(data, options->signature, 16);
197     data += 16;
198
199     /* calculate net address offset */
200     *servoffset += sizeof(offset);
201     offset = htons(data - status);
202     memcpy(status + *servoffset, &offset, sizeof(offset));
203     return sigoff;
204 }
205
206 static size_t status_netaddress(char *data, int *servoffset,
207                              const DSI *dsi,
208                              const struct afp_options *options)
209 {
210     char               *begin;
211     uint16_t          offset;
212     size_t             addresses_len = 0;
213
214     begin = data;
215
216     /* get net address offset */
217     memcpy(&offset, data + *servoffset, sizeof(offset));
218     data += ntohs(offset);
219
220     /* format:
221        Address count (byte) 
222        len (byte = sizeof(length + address type + address) 
223        address type (byte, ip address = 0x01, ip + port = 0x02,
224                            ddp address = 0x03, fqdn = 0x04) 
225        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
226        */
227
228     /* number of addresses. this currently screws up if we have a dsi
229        connection, but we don't have the ip address. to get around this,
230        we turn off the status flag for tcp/ip. */
231     *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) +
232               (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
233
234     /* ip address */
235     if (dsi) {
236         if (dsi->server.ss_family == AF_INET) { /* IPv4 */
237             const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
238             if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
239                 *data++ = 6; /* length */
240                 *data++ = 0x01; /* basic ip address */
241                 memcpy(data, &inaddr->sin_addr.s_addr,
242                        sizeof(inaddr->sin_addr.s_addr));
243                 data += sizeof(inaddr->sin_addr.s_addr);
244                 addresses_len += 7;
245             } else {
246                 /* ip address + port */
247                 *data++ = 8;
248                 *data++ = 0x02; /* ip address with port */
249                 memcpy(data, &inaddr->sin_addr.s_addr,
250                        sizeof(inaddr->sin_addr.s_addr));
251                 data += sizeof(inaddr->sin_addr.s_addr);
252                 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
253                 data += sizeof(inaddr->sin_port);
254                 addresses_len += 9;
255             }
256         } else { /* IPv6 */
257             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
258             if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
259                 *data++ = 18; /* length */
260                 *data++ = 6; /* type */
261                 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
262                 data += sizeof(sa6->sin6_addr.s6_addr);
263                 addresses_len += 19;
264             } else {
265                 /* ip address + port */
266                 *data++ = 20; /* length */
267                 *data++ = 7; /* type*/
268                 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
269                 data += sizeof(sa6->sin6_addr.s6_addr);
270                 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
271                 data += sizeof(sa6->sin6_port);
272                 addresses_len += 21;
273             }
274
275         }
276     }
277
278     /* handle DNS names */
279     if (options->fqdn && dsi) {
280         size_t len = strlen(options->fqdn);
281         if ( len + 2 + addresses_len < maxstatuslen - offset) {
282             *data++ = len +2;
283             *data++ = 0x04;
284             memcpy(data, options->fqdn, len);
285             data += len;
286             addresses_len += len+2;
287         }
288
289         /* Annouce support for SSH tunneled AFP session, 
290          * this feature is available since 10.3.2.
291          * According to the specs (AFP 3.1 p.225) this should
292          * be an IP+Port style value, but it only works with 
293          * a FQDN. OSX Server uses FQDN as well.
294          */
295         if ( len + 2 + addresses_len < maxstatuslen - offset) {
296             if (options->flags & OPTION_ANNOUNCESSH) {
297                 *data++ = len +2;
298                 *data++ = 0x05;
299                 memcpy(data, options->fqdn, len);
300                 data += len;
301             }
302         }
303     }
304
305     /* calculate/store Directory Services Names offset */
306     offset = htons(data - begin); 
307     *servoffset += sizeof(offset);
308     memcpy(begin + *servoffset, &offset, sizeof(offset));
309
310     /* return length of buffer */
311     return (data - begin);
312 }
313
314 static bool append_directoryname(char **pdata,
315                                  size_t offset,
316                                  size_t *size,
317                                  char* DirectoryNamesCount,
318                                  size_t len,
319                                  char *principal)
320 {
321     if (sizeof(uint8_t) + len  > maxstatuslen - offset - *size) {
322         LOG(log_error, logtype_afpd,
323             "status:DirectoryNames: out of space for principal '%s' (len=%d)",
324             principal, len);
325         return false;
326     } else if (len > 255) {
327         LOG(log_error, logtype_afpd,
328             "status:DirectoryNames: principal '%s' (len=%d) too long (max=255)",
329             principal, len);
330         return false;
331     }
332
333     LOG(log_info, logtype_afpd,
334         "DirectoryNames[%d]=%s",
335         *DirectoryNamesCount, principal);
336
337     *DirectoryNamesCount += 1;
338     char *data = *pdata;
339     *data++ = len;
340     strncpy(data, principal, len);
341
342     *pdata += len + 1;
343     *size += sizeof(uint8_t) + len;
344
345     return true;
346 }
347
348 /**
349  * DirectoryNamesCount offset: uint16_t
350  * ...
351  * DirectoryNamesCount: uint8_t
352  * DirectoryNames: list of UTF-8 Pascal strings (uint8_t + char[1,255])
353  */
354 static size_t status_directorynames(char *data,
355                                     int *diroffset,
356                                     const DSI *dsi _U_,
357                                     const struct afp_options *options _U_)
358 {
359     char *begin = data;
360     uint16_t offset;
361
362     memcpy(&offset, begin + *diroffset, sizeof(offset));
363     offset = ntohs(offset);
364     data += offset;
365
366     char *DirectoryNamesCount = data++, *DirectoryNames = data;
367     *DirectoryNamesCount = 0;
368     size_t size = sizeof(uint8_t);
369
370     if (!uam_gss_enabled())
371         goto offset_calc;
372
373 #ifdef HAVE_KERBEROS
374     krb5_context context;
375     krb5_error_code ret;
376     const char *error_msg;
377
378     if (krb5_init_context(&context)) {
379         LOG(log_error, logtype_afpd,
380             "status:DirectoryNames failed to intialize a krb5_context");
381         goto offset_calc;
382     }
383
384     krb5_keytab keytab;
385     if ((ret = krb5_kt_default(context, &keytab)))
386         goto krb5_error;
387
388     // figure out which service principal to use
389     krb5_keytab_entry entry;
390     char *principal;
391     if (options->k5service && options->fqdn && options->k5realm) {
392         LOG(log_debug, logtype_afpd,
393             "status:DirectoryNames: using service principal specified in options");
394
395         krb5_principal service_principal;
396         if ((ret = krb5_build_principal(context,
397                                         &service_principal,
398                                         strlen(options->k5realm),
399                                         options->k5realm,
400                                         options->k5service,
401                                         options->fqdn,
402                                         NULL)))
403             goto krb5_error;
404
405         // try to get the given service principal from keytab
406         ret = krb5_kt_get_entry(context,
407                                 keytab,
408                                 service_principal,
409                                 0, // kvno - wildcard
410                                 0, // enctype - wildcard
411                                 &entry);
412         if (ret == KRB5_KT_NOTFOUND) {
413             krb5_unparse_name(context, service_principal, &principal);
414             LOG(log_error, logtype_afpd,
415                 "status:DirectoryNames: specified service principal '%s' not found in keytab",
416                 principal);
417             // XXX: should this be krb5_xfree?
418             krb5_free_unparsed_name(context, principal);
419             goto krb5_cleanup;
420         }
421         krb5_free_principal(context, service_principal);
422         if (ret)
423             goto krb5_error;
424     } else {
425         LOG(log_debug, logtype_afpd,
426             "status:DirectoryNames: using first entry from keytab as service principal");
427
428         krb5_kt_cursor cursor;
429         if ((ret = krb5_kt_start_seq_get(context, keytab, &cursor)))
430             goto krb5_error;
431
432         ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
433         krb5_kt_end_seq_get(context, keytab, &cursor);
434         if (ret)
435             goto krb5_error;
436     }
437
438     krb5_unparse_name(context, entry.principal, &principal);
439     krb5_kt_free_entry(context, &entry);
440
441     append_directoryname(&data,
442                          offset,
443                          &size,
444                          DirectoryNamesCount,
445                          strlen(principal),
446                          principal);
447
448     // XXX: should this be krb5_xfree?
449     krb5_free_unparsed_name(context, principal);
450     goto krb5_cleanup;
451
452 krb5_error:
453     if (ret) {
454         error_msg = krb5_get_error_message(context, ret);
455         LOG(log_error, logtype_afpd,
456             "status:DirectoryNames: Kerberos error: %s",
457             (char *) error_msg);
458         krb5_free_error_message(context, error_msg);
459     }
460
461 krb5_cleanup:
462     krb5_kt_close(context, keytab);
463     krb5_free_context(context);
464 #else
465     if (!options->k5service || !options->fqdn || !options->k5realm)
466         goto offset_calc;
467
468     char principal[255];
469     size_t len = snprintf(principal, sizeof(principal), "%s/%s@%s",
470                           options->k5service, options->fqdn, options->k5realm);
471
472     append_directoryname(&data,
473                          offset,
474                          &size,
475                          DirectoryNamesCount,
476                          strlen(principal),
477                          principal);
478 #endif // HAVE_KERBEROS
479
480 offset_calc:
481     /* Calculate and store offset for UTF8ServerName */
482     *diroffset += sizeof(uint16_t);
483     offset = htons(data - begin);
484     memcpy(begin + *diroffset, &offset, sizeof(uint16_t));
485
486     /* return length of buffer */
487     return (data - begin);
488 }
489
490 static size_t status_utf8servername(char *data, int *nameoffset,
491                                  const DSI *dsi _U_,
492                                  const struct afp_options *options)
493 {
494     char *Obj, *Type, *Zone;
495     uint16_t namelen;
496     size_t len;
497     char *begin = data;
498     uint16_t offset, status;
499
500     memcpy(&offset, data + *nameoffset, sizeof(offset));
501     offset = ntohs(offset);
502     data += offset;
503
504     /* FIXME:
505      * What is the valid character range for an nbpname?
506      *
507      * Apple's server likes to use the non-qualified hostname
508      * This obviously won't work very well if multiple servers are running
509      * on the box.
510      */
511
512     /* extract the obj part of the server */
513     Obj = options->hostname;
514     if ((size_t) -1 == (len = convert_string (
515                             options->unixcharset, CH_UTF8_MAC, 
516                             Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
517         LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
518
519         /* set offset to 0 */
520         memset(begin + *nameoffset, 0, sizeof(offset));
521         data = begin + offset;
522     } else {
523         namelen = htons(len);
524         memcpy( data, &namelen, sizeof(namelen));
525         data += sizeof(namelen);
526         data += len;
527         offset = htons(offset);
528         memcpy(begin + *nameoffset, &offset, sizeof(uint16_t));
529     }
530
531     /* return length of buffer */
532     return (data - begin);
533
534 }
535
536 /* returns actual offset to signature */
537 static void status_icon(char *data, const unsigned char *icondata,
538                         const int iconlen, const int sigoffset)
539 {
540     char                *start = data;
541     char                *sigdata = data + sigoffset;
542     uint16_t            ret, status;
543
544     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
545     if ( icondata == NULL ) {
546         ret = status;
547         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
548     } else {
549         data += ntohs( status );
550         memcpy( data, icondata, iconlen);
551         data += iconlen;
552         ret = htons(data - start);
553     }
554
555     /* put in signature offset */
556     if (sigoffset)
557         memcpy(sigdata, &ret, sizeof(ret));
558 }
559
560 /* ---------------------
561  */
562 void status_init(AFPObj *obj, DSI *dsi)
563 {
564     char *status = dsi->status;
565     size_t statuslen;
566     int c, sigoff, ipok = 0;
567     const struct afp_options *options = &obj->options;
568
569     maxstatuslen = sizeof(dsi->status);
570
571     if (dsi->server.ss_family == AF_INET) { /* IPv4 */
572         const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
573         ipok = sa4->sin_addr.s_addr ? 1 : 0;
574     } else { /* IPv6 */
575         const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
576         for (int i=0; i<16; i++) {
577             if (sa6->sin6_addr.s6_addr[i]) {
578                 ipok = 1;
579                 break;
580             }
581         }
582     }
583
584     /*
585      * These routines must be called in order -- earlier calls
586      * set the offsets for later calls.
587      *
588      * using structs is a bad idea, but that's what the original code
589      * does. solaris, in fact, will segfault with them. so, now
590      * we just use the powers of #defines and memcpy.
591      *
592      * reply block layout (offsets are 16-bit quantities):
593      * machine type offset -> AFP version count offset ->
594      * UAM count offset -> vol icon/mask offset -> flags ->
595      *
596      * server name [padded to even boundary] -> signature offset ->
597      * network address offset ->
598      *
599      * at the appropriate offsets:
600      * machine type, afp versions, uams, server signature
601      * (16-bytes), network addresses, volume icon/mask 
602      */
603
604     status_flags(status,
605                  options->flags & OPTION_SERVERNOTIF,
606                  (options->fqdn || ipok),
607                  options->passwdbits, 
608                  uam_gss_enabled(),
609                  options->flags);
610     /* returns offset to signature offset */
611     c = status_server(status, options->hostname, options);
612     status_machine(status);
613     status_versions(status, dsi);
614     status_uams(status, options->uamlist);
615     if (options->flags & OPTION_CUSTOMICON)
616         status_icon(status, icon, sizeof(icon), c);
617     else
618         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
619
620     sigoff = status_signature(status, &c, options);
621     /* c now contains the offset where the netaddress offset lives */
622
623     status_netaddress(status, &c, dsi, options);
624     /* c now contains the offset where the Directory Names Count offset lives */
625
626     statuslen = status_directorynames(status, &c, dsi, options);
627     /* c now contains the offset where the UTF-8 ServerName offset lives */
628
629     if ( statuslen < maxstatuslen) 
630         statuslen = status_utf8servername(status, &c, dsi, options);
631
632     if ((options->flags & OPTION_CUSTOMICON) == 0) {
633         status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
634     }
635
636     dsi->signature = status + sigoff;
637     dsi->statuslen = statuslen;
638 }
639
640 /* set_signature()                                                    */
641 /*                                                                    */
642 /* If found in conf file, use it.                                     */
643 /* If not found in conf file, genarate and append in conf file.       */
644 /* If conf file don't exist, create and genarate.                     */
645 /* If cannot open conf file, use one-time signature.                  */
646 /* If signature = xxxxx, use it.                                      */
647
648 void set_signature(struct afp_options *options) {
649     int fd, i;
650     struct stat tmpstat;
651     char *servername_conf;
652     int header = 0;
653     char buf[1024], *p;
654     FILE *fp = NULL, *randomp;
655     size_t len;
656     char *server_tmp;
657     
658     server_tmp = options->hostname;
659     len = strlen(options->signatureopt);
660     if (len == 0) {
661         goto server_signature_auto;   /* default */
662     } else if (len < 3) {
663         LOG(log_warning, logtype_afpd, "WARNING: signature string %s is very short !", options->signatureopt);
664         goto server_signature_user;
665     } else if (len > 16) {
666         LOG(log_warning, logtype_afpd, "WARNING: signature string %s is very long !", options->signatureopt);
667         len = 16;
668         goto server_signature_user;
669     } else {
670         LOG(log_info, logtype_afpd, "signature string is %s.", options->signatureopt);
671         goto server_signature_user;
672     }
673     
674 server_signature_user:
675     
676     /* Signature is defined in afp.conf */
677     memset(options->signature, 0, 16);
678     memcpy(options->signature, options->signatureopt, len);
679     goto server_signature_done;
680     
681 server_signature_auto:
682     
683     /* Signature type is auto, using afp_signature.conf */
684     if (!stat(options->sigconffile, &tmpstat)) {                /* conf file exists? */
685         if ((fp = fopen(options->sigconffile, "r")) != NULL) {  /* read open? */
686             /* scan in the conf file */
687             while (fgets(buf, sizeof(buf), fp) != NULL) { 
688                 p = buf;
689                 while (p && isblank(*p))
690                     p++;
691                 if (!p || (*p == '#') || (*p == '\n'))
692                     continue;                             /* invalid line */
693                 if (*p == '"') {
694                     p++;
695                     if ((servername_conf = strtok( p, "\"" )) == NULL)
696                         continue;                         /* syntax error: invalid quoted servername */
697                 } else {
698                     if ((servername_conf = strtok( p, " \t" )) == NULL)
699                         continue;                         /* syntax error: invalid servername */
700                 }
701                 p = strchr(p, '\0');
702                 p++;
703                 if (*p == '\0')
704                     continue;                             /* syntax error: missing signature */
705                 
706                 if (strcmp(server_tmp, servername_conf))
707                     continue;                             /* another servername */
708                 
709                 while (p && isblank(*p))
710                     p++;
711                 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
712                                   &options->signature[ 0], &options->signature[ 1],
713                                   &options->signature[ 2], &options->signature[ 3],
714                                   &options->signature[ 4], &options->signature[ 5],
715                                   &options->signature[ 6], &options->signature[ 7],
716                                   &options->signature[ 8], &options->signature[ 9],
717                                   &options->signature[10], &options->signature[11],
718                                   &options->signature[12], &options->signature[13],
719                                   &options->signature[14], &options->signature[15]
720                          )) {
721                     fclose(fp);
722                     goto server_signature_done;                 /* found in conf file */
723                 }
724             }
725             if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
726                 fseek(fp, 0L, SEEK_END);
727                 if(ftell(fp) == 0) {                     /* size = 0 */
728                     header = 1;
729                     goto server_signature_random;
730                 } else {
731                     fseek(fp, -1L, SEEK_END);
732                     if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
733                     goto server_signature_random;
734                 }                    
735             } else {
736                 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
737                     options->sigconffile, strerror(errno));
738                 goto server_signature_random;
739             }
740         } else {
741             LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
742                 options->sigconffile, strerror(errno));
743             goto server_signature_random;
744         }
745     } else {                                                          /* conf file don't exist */
746         if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
747             LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s). Using one-time signature.",
748                 options->sigconffile, strerror(errno));
749             goto server_signature_random;
750         }
751         if (( fp = fdopen( fd, "w" )) == NULL ) {
752             LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s). Using one-time signature.",
753                 options->sigconffile, strerror(errno));
754             close(fd);
755             goto server_signature_random;
756         }
757         header = 1;
758         goto server_signature_random;
759     }
760     
761 server_signature_random:
762     
763     /* generate signature from random number */
764     randombytes(options->signature, 16);
765
766     if (fp && header) { /* conf file is created or size=0 */
767         fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
768         fprintf(fp, "# This file is auto-generated by afpd.\n");
769         fprintf(fp, "# \n");
770         fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
771         fprintf(fp, "# the same server twice.\n");
772         fprintf(fp, "# \n");
773         fprintf(fp, "# If setting \"signature = xxxxx\" in afp.conf, this file is not used.\n\n");
774     }
775     
776     if (fp) {
777         fprintf(fp, "\"%s\"\t", server_tmp);
778         for (i=0 ; i<16 ; i++) {
779             fprintf(fp, "%02X", (options->signature)[i]);
780         }
781         fprintf(fp, "%s", "\n");
782         fclose(fp);
783     }
784     
785 server_signature_done:
786     
787     /* retrun */
788     LOG(log_info, logtype_afpd,
789         "signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
790         (options->signature)[ 0], (options->signature)[ 1],
791         (options->signature)[ 2], (options->signature)[ 3],
792         (options->signature)[ 4], (options->signature)[ 5],
793         (options->signature)[ 6], (options->signature)[ 7],
794         (options->signature)[ 8], (options->signature)[ 9],
795         (options->signature)[10], (options->signature)[11],
796         (options->signature)[12], (options->signature)[13],
797         (options->signature)[14], (options->signature)[15]);
798     
799     return;
800 }
801
802 /* this is the same as asp/dsi_getstatus */
803 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
804 {
805     memcpy(rbuf, obj->dsi->status, obj->dsi->statuslen);
806     *rbuflen = obj->dsi->statuslen;
807
808     return AFP_OK;
809 }