]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
Merge remote-tracking branch 'remotes/origin/branch-netatalk-2-1'
[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
39 #include "globals.h"  /* includes <netdb.h> */
40 #include "status.h"
41 #include "afp_config.h"
42 #include "icon.h"
43
44 static   size_t maxstatuslen = 0;
45
46 static void status_flags(char *data, const int notif, const int ipok,
47                          const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
48 {
49     uint16_t           status;
50
51     status = AFPSRVRINFO_COPY
52            | AFPSRVRINFO_SRVSIGNATURE
53            | AFPSRVRINFO_SRVMSGS
54            | AFPSRVRINFO_FASTBOZO
55            | AFPSRVRINFO_SRVRDIR
56            | AFPSRVRINFO_SRVUTF8
57            | AFPSRVRINFO_EXTSLEEP;
58
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;
69
70     status = htons(status);
71     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
72 }
73
74 static int status_server(char *data, const char *server, const struct afp_options *options)
75 {
76     char                *start = data;
77     char                *Obj, *Type, *Zone;
78     char                buf[32];
79     u_int16_t           status;
80     size_t              len;
81
82     /* make room for all offsets before server name */
83     data += AFPSTATUS_PRELEN;
84
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);
92         *data++ = len;
93         memcpy( data, Obj, len );
94         LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
95     } else {
96         *data++ = len;
97         memcpy( data, buf, len );
98     }
99     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
100         len++;
101     data += len;
102
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.
106      *
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
111      * things.  
112      *
113      * NOTE2: AFP3.1 Documentation states that the directory names offset
114      * is a required feature, even though it can be set to zero.
115      */
116     len = data - start;
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 */
120 }
121
122 static void status_machine(char *data)
123 {
124     char                *start = data;
125     u_int16_t           status;
126     int                 len;
127 #ifdef AFS
128     const char          *machine = "afs";
129 #else /* !AFS */
130     const char          *machine = "Netatalk %s";
131 #endif /* AFS */
132     char buf[64];
133
134     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
135     data += ntohs( status );
136
137     //    len = strlen( machine );
138     len = snprintf(buf, 64, machine, VERSION);
139     *data++ = len;
140     memcpy( data, buf, len );
141     data += len;
142
143     status = htons(data - start);
144     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
145 }
146
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)
150 {
151     char                 *status;
152     u_int16_t            offset, sigoff;
153
154     status = data;
155
156     /* get server signature offset */
157     memcpy(&offset, data + *servoffset, sizeof(offset));
158     sigoff = offset = ntohs(offset);
159
160     /* jump to server signature offset */
161     data += offset;
162
163     memset(data, 0, 16);
164     memcpy(data, options->signature, 16);
165     data += 16;
166
167     /* calculate net address offset */
168     *servoffset += sizeof(offset);
169     offset = htons(data - status);
170     memcpy(status + *servoffset, &offset, sizeof(offset));
171     return sigoff;
172 }
173
174 static size_t status_netaddress(char *data, int *servoffset,
175                              const ASP asp, const DSI *dsi,
176                              const struct afp_options *options)
177 {
178     char               *begin;
179     u_int16_t          offset;
180     size_t             addresses_len = 0;
181
182     begin = data;
183
184     /* get net address offset */
185     memcpy(&offset, data + *servoffset, sizeof(offset));
186     data += ntohs(offset);
187
188     /* format:
189        Address count (byte) 
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)
194        */
195
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);
201
202     /* ip address */
203     if (dsi) {
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);
212                 addresses_len += 7;
213             } else {
214                 /* ip address + port */
215                 *data++ = 8;
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);
222                 addresses_len += 9;
223             }
224         } else { /* IPv6 */
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);
231                 addresses_len += 19;
232             } else {
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);
240                 addresses_len += 21;
241             }
242
243         }
244     }
245
246     /* handle DNS names */
247     if (options->fqdn && dsi) {
248         size_t len = strlen(options->fqdn);
249         if ( len + 2 + addresses_len < maxstatuslen - offset) {
250             *data++ = len +2;
251             *data++ = 0x04;
252             memcpy(data, options->fqdn, len);
253             data += len;
254             addresses_len += len+2;
255         }
256
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.
262          */
263         if ( len + 2 + addresses_len < maxstatuslen - offset) {
264             if (options->flags & OPTION_ANNOUNCESSH) {
265                 *data++ = len +2;
266                 *data++ = 0x05;
267                 memcpy(data, options->fqdn, len);
268                 data += len;
269             }
270         }
271     }
272
273 #ifndef NO_DDP
274     if (asp) {
275         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
276
277         /* ddp address */
278         *data++ = 6;
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);
287     }
288 #endif /* ! NO_DDP */
289
290     /* calculate/store Directory Services Names offset */
291     offset = htons(data - begin); 
292     *servoffset += sizeof(offset);
293     memcpy(begin + *servoffset, &offset, sizeof(offset));
294
295     /* return length of buffer */
296     return (data - begin);
297 }
298
299 static size_t status_directorynames(char *data, int *diroffset, 
300                                  const DSI *dsi _U_, 
301                                  const struct afp_options *options)
302 {
303     char *begin = data;
304     u_int16_t offset;
305     memcpy(&offset, data + *diroffset, sizeof(offset));
306     offset = ntohs(offset);
307     data += offset;
308
309     /* I can not find documentation of any other purpose for the
310      * DirectoryNames field.
311      */
312     /*
313      * Try to synthesize a principal:
314      * service '/' fqdn '@' realm
315      */
316     if (options->k5service && options->k5realm && options->fqdn) {
317         /* should k5princ be utf8 encoded? */
318         size_t len;
319         char *p = strchr( options->fqdn, ':' );
320         if (p) 
321             *p = '\0';
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) {
327             *data++ = 0;
328             LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
329         }        
330         else {
331             *data++ = 1; /* DirectoryNamesCount */
332             *data++ = len;
333             snprintf( data, len + 1, "%s/%s@%s", options->k5service,
334                                 options->fqdn, options->k5realm );
335             data += len;
336             if (p)
337                 *p = ':';
338        }
339     } else {
340         *data++ = 0;
341     }
342
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));
347
348     /* return length of buffer */
349     return (data - begin);
350 }
351
352 static size_t status_utf8servername(char *data, int *nameoffset,
353                                  const DSI *dsi _U_,
354                                  const struct afp_options *options)
355 {
356     char *Obj, *Type, *Zone;
357     u_int16_t namelen;
358     size_t len;
359     char *begin = data;
360     u_int16_t offset, status;
361
362     memcpy(&offset, data + *nameoffset, sizeof(offset));
363     offset = ntohs(offset);
364     data += offset;
365
366     /* FIXME:
367      * What is the valid character range for an nbpname?
368      *
369      * Apple's server likes to use the non-qualified hostname
370      * This obviously won't work very well if multiple servers are running
371      * on the box.
372      */
373
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);
377
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");
382
383         /* set offset to 0 */
384         memset(begin + *nameoffset, 0, sizeof(offset));
385         data = begin + offset;
386     }
387     else {
388         namelen = htons(len);
389         memcpy( data, &namelen, sizeof(namelen));
390         data += sizeof(namelen);
391         data += len;
392         offset = htons(offset);
393         memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
394     }
395
396     /* return length of buffer */
397     return (data - begin);
398
399 }
400
401 /* returns actual offset to signature */
402 static void status_icon(char *data, const unsigned char *icondata,
403                         const int iconlen, const int sigoffset)
404 {
405     char                *start = data;
406     char                *sigdata = data + sigoffset;
407     u_int16_t           ret, status;
408
409     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
410     if ( icondata == NULL ) {
411         ret = status;
412         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
413     } else {
414         data += ntohs( status );
415         memcpy( data, icondata, iconlen);
416         data += iconlen;
417         ret = htons(data - start);
418     }
419
420     /* put in signature offset */
421     if (sigoffset)
422         memcpy(sigdata, &ret, sizeof(ret));
423 }
424
425 /* ---------------------
426  */
427 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
428                  const struct afp_options *options)
429 {
430     ASP asp;
431     DSI *dsi;
432     char *status = NULL;
433     size_t statuslen;
434     int c, sigoff, ipok;
435
436     if (!(aspconfig || dsiconfig) || !options)
437         return;
438
439     if (aspconfig) {
440         status = aspconfig->status;
441         maxstatuslen=sizeof(aspconfig->status);
442         asp = aspconfig->obj.handle;
443     } else
444         asp = NULL;
445         
446     ipok = 0;
447     if (dsiconfig) {
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;
454         } else { /* IPv6 */
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]) {
458                     ipok = 1;
459                     break;
460                 }
461             }
462         }
463     } else
464         dsi = NULL;
465
466     /*
467      * These routines must be called in order -- earlier calls
468      * set the offsets for later calls.
469      *
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.
473      *
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 ->
477      *
478      * server name [padded to even boundary] -> signature offset ->
479      * network address offset ->
480      *
481      * at the appropriate offsets:
482      * machine type, afp versions, uams, server signature
483      * (16-bytes), network addresses, volume icon/mask 
484      */
485
486     status_flags(status,
487                  options->server_notif,
488                  (options->fqdn || ipok),
489                  options->passwdbits, 
490                  (options->k5service && options->k5realm && options->fqdn),
491                  options->flags);
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);
500     else
501         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
502
503     sigoff = status_signature(status, &c, options);
504     /* c now contains the offset where the netaddress offset lives */
505
506     status_netaddress(status, &c, asp, dsi, options);
507     /* c now contains the offset where the Directory Names Count offset lives */
508
509     statuslen = status_directorynames(status, &c, dsi, options);
510     /* c now contains the offset where the UTF-8 ServerName offset lives */
511
512     if ( statuslen < maxstatuslen) 
513         statuslen = status_utf8servername(status, &c, dsi, options);
514
515 #ifndef NO_DDP
516     if (aspconfig) {
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;
522     }
523 #endif /* ! NO_DDP */
524
525     if (dsiconfig) {
526         if ((options->flags & OPTION_CUSTOMICON) == 0) {
527             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
528         }
529         dsi_setstatus(dsi, status, statuslen);
530         dsiconfig->signature = status + sigoff;
531         dsiconfig->statuslen = statuslen;
532     }
533 }
534
535 /* set_signature()                                                    */
536 /*                                                                    */
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.                                  */
542
543 void set_signature(struct afp_options *options) {
544     char *usersign;
545     int fd, i;
546     struct stat tmpstat;
547     char *servername_conf;
548     int header = 0;
549     char buf[1024], *p;
550     FILE *fp = NULL, *randomp;
551     size_t len;
552     char *server_tmp;
553     
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 */
562     } else {
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*/
565     }
566     
567 server_signature_user:
568     
569     /* Signature type is user string */
570     len = strlen(options->signatureopt);
571     if (len <= 5) {
572         LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
573         goto server_signature_auto;
574     }
575     usersign = options->signatureopt + 5;
576     len = len - 5;
577     if (len > 16) {
578         LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !",  usersign);
579         len = 16;
580     } else if (len >= 3) {
581         LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
582     } else {
583         LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !",  usersign);
584     }
585     memset(options->signature, 0, 16);
586     memcpy(options->signature, usersign, len);
587     goto server_signature_done;
588     
589 server_signature_auto:
590     
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) { 
596                 p = buf;
597                 while (p && isblank(*p))
598                     p++;
599                 if (!p || (*p == '#') || (*p == '\n'))
600                     continue;                             /* invalid line */
601                 if (*p == '"') {
602                     p++;
603                     if ((servername_conf = strtok( p, "\"" )) == NULL)
604                         continue;                         /* syntax error: invalid quoted servername */
605                 } else {
606                     if ((servername_conf = strtok( p, " \t" )) == NULL)
607                         continue;                         /* syntax error: invalid servername */
608                 }
609                 p = strchr(p, '\0');
610                 p++;
611                 if (*p == '\0')
612                     continue;                             /* syntax error: missing signature */
613                 
614                 if (strcmp(server_tmp, servername_conf))
615                     continue;                             /* another servername */
616                 
617                 while (p && isblank(*p))
618                     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]
628                          )) {
629                     fclose(fp);
630                     goto server_signature_done;                 /* found in conf file */
631                 }
632             }
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 */
636                     header = 1;
637                     goto server_signature_random;
638                 } else {
639                     fseek(fp, -1L, SEEK_END);
640                     if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
641                     goto server_signature_random;
642                 }                    
643             } else {
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;
647             }
648         } else {
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;
652         }
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;
658         }
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));
662             close(fd);
663             goto server_signature_random;
664         }
665         header = 1;
666         goto server_signature_random;
667     }
668     
669 server_signature_random:
670     
671     /* generate signature from random number */
672     randombytes(options->signature, 16);
673
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");
677         fprintf(fp, "# \n");
678         fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
679         fprintf(fp, "# the same server twice.\n");
680         fprintf(fp, "# \n");
681         fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
682     }
683     
684     if (fp) {
685         fprintf(fp, "\"%s\"\t", server_tmp);
686         for (i=0 ; i<16 ; i++) {
687             fprintf(fp, "%02X", (options->signature)[i]);
688         }
689         fprintf(fp, "%s", "\n");
690         fclose(fp);
691     }
692     
693 server_signature_done:
694     
695     /* retrun */
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",
698         server_tmp,
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]);
707     
708     return;
709 }
710
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)
713 {
714     AFPConfig *config = obj->config;
715
716     memcpy(rbuf, config->status, config->statuslen);
717     *rbuflen = config->statuslen;
718     return AFP_OK;
719 }