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