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