2 * $Id: status.c,v 1.15 2003-06-09 14:42:40 srittau Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
17 #include <atalk/logger.h>
20 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #endif /* USE_GETHOSTID */
26 #include <netatalk/at.h>
27 #include <netatalk/endian.h>
28 #include <atalk/dsi.h>
29 #include <atalk/atp.h>
30 #include <atalk/asp.h>
31 #include <atalk/nbp.h>
33 #include "globals.h" /* includes <netdb.h> */
35 #include "afp_config.h"
38 static void status_flags(char *data, const int notif, const int ipok,
39 const unsigned char passwdbits, const int dirsrvcs)
43 status = AFPSRVRINFO_COPY;
44 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
45 status |= AFPSRVRINFO_PASSWD;
46 if (passwdbits & PASSWD_NOSAVE)
47 status |= AFPSRVRINFO_NOSAVEPASSWD;
48 status |= AFPSRVRINFO_SRVSIGNATURE;
49 /* only advertise tcp/ip if we have a valid address */
51 status |= AFPSRVRINFO_TCPIP;
52 status |= AFPSRVRINFO_SRVMSGS;
53 /* Allow the user to decide if we should support server notifications.
54 * With this turned off, the clients will poll for directory changes every
55 * 10 seconds. This might be too costly to network resources, so make
56 * this an optional thing. Default will be to _not_ support server
59 status |= AFPSRVRINFO_SRVNOTIFY;
61 status |= AFPSRVRINFO_FASTBOZO;
63 status |= AFPSRVRINFO_SRVRDIR;
64 status = htons(status);
65 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
68 static int status_server(char *data, const char *server)
71 char *Obj, *Type, *Zone;
75 /* make room for all offsets before server name */
76 data += AFPSTATUS_PRELEN;
78 /* extract the obj part of the server */
79 Obj = (char *) server;
80 nbp_name(server, &Obj, &Type, &Zone);
83 memcpy( data, Obj, len );
84 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
88 /* Make room for signature and net address offset. Save location of
89 * signature offset. We're also making room for directory names offset
90 * and the utf-8 server name offset.
92 * NOTE: Technically, we don't need to reserve space for the
93 * signature and net address offsets if they're not going to be
94 * used. As there are no offsets after them, it doesn't hurt to
95 * have them specified though. So, we just do that to simplify
98 * NOTE2: AFP3.1 Documentation states that the directory names offset
99 * is a required feature, even though it can be set to zero.
102 status = htons(len + AFPSTATUS_POSTLEN);
103 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
104 return len; /* return the offset to beginning of signature offset */
107 static void status_machine(char *data)
113 const char *machine = "afs";
115 const char *machine = "unix";
118 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
119 data += ntohs( status );
120 len = strlen( machine );
122 memcpy( data, machine, len );
124 status = htons(data - start);
125 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
129 /* server signature is a 16-byte quantity */
130 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
131 const struct afp_options *options)
136 u_int16_t offset, sigoff;
146 /* get server signature offset */
147 memcpy(&offset, data + *servoffset, sizeof(offset));
148 sigoff = offset = ntohs(offset);
150 /* jump to server signature offset */
153 /* Signature type is user string */
154 if (strncmp(options->signature, "user", 4) == 0) {
155 if (strlen(options->signature) <= 5) {
156 LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
158 goto server_signature_hostid;
160 usersign = options->signature + 5;
161 if (strlen(usersign) < 3)
162 LOG(log_warning, logtype_afpd, "Signature %s is very short !",
166 strncpy(data, usersign, 16);
168 goto server_signature_done;
169 } /* signature = user */
171 /* If signature type is a standard hostid... */
172 server_signature_hostid:
173 /* 16-byte signature consists of copies of the hostid */
174 #if defined(BSD4_4) && defined(USE_GETHOSTID)
176 mib[1] = KERN_HOSTID;
177 len = sizeof(hostid);
178 sysctl(mib, 2, &hostid, &len, NULL, 0);
179 #else /* BSD4_4 && USE_GETHOSTID */
180 hostid = gethostid();
181 #endif /* BSD4_4 && USE_GETHOSTID */
184 hostid = dsi->server.sin_addr.s_addr;
186 struct hostent *host;
188 if ((host = gethostbyname(options->hostname)))
189 hostid = ((struct in_addr *) host->h_addr)->s_addr;
193 /* it turns out that a server signature screws up separate
194 * servers running on the same machine. to work around that,
195 * i add in an increment */
198 for (i = 0; i < 16; i += sizeof(hostid)) {
199 memcpy(data, &hostid, sizeof(hostid));
200 data += sizeof(hostid);
203 server_signature_done:
204 /* calculate net address offset */
205 *servoffset += sizeof(offset);
206 offset = htons(data - status);
207 memcpy(status + *servoffset, &offset, sizeof(offset));
211 static int status_netaddress(char *data, int *servoffset,
212 const ASP asp, const DSI *dsi,
220 /* get net address offset */
221 memcpy(&offset, data + *servoffset, sizeof(offset));
222 data += ntohs(offset);
226 len (byte = sizeof(length + address type + address)
227 address type (byte, ip address = 0x01, ip + port = 0x02,
228 ddp address = 0x03, fqdn = 0x04)
229 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
232 /* number of addresses. this currently screws up if we have a dsi
233 connection, but we don't have the ip address. to get around this,
234 we turn off the status flag for tcp/ip. */
235 *data++ = ((fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
237 /* handle DNS names */
239 int len = strlen(fqdn);
242 memcpy(data, fqdn, len);
248 const struct sockaddr_in *inaddr = &dsi->server;
250 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
251 *data++ = 6; /* length */
252 *data++ = 0x01; /* basic ip address */
253 memcpy(data, &inaddr->sin_addr.s_addr,
254 sizeof(inaddr->sin_addr.s_addr));
255 data += sizeof(inaddr->sin_addr.s_addr);
257 /* ip address + port */
259 *data++ = 0x02; /* ip address with port */
260 memcpy(data, &inaddr->sin_addr.s_addr,
261 sizeof(inaddr->sin_addr.s_addr));
262 data += sizeof(inaddr->sin_addr.s_addr);
263 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
264 data += sizeof(inaddr->sin_port);
270 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
274 *data++ = 0x03; /* ddp address */
275 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
276 data += sizeof(ddpaddr->sat_addr.s_net);
277 memcpy(data, &ddpaddr->sat_addr.s_node,
278 sizeof(ddpaddr->sat_addr.s_node));
279 data += sizeof(ddpaddr->sat_addr.s_node);
280 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
281 data += sizeof(ddpaddr->sat_port);
283 #endif /* ! NO_DDP */
285 /* calculate/store Directory Services Names offset */
286 offset = htons(data - begin);
287 *servoffset += sizeof(offset);
288 memcpy(begin + *servoffset, &offset, sizeof(offset));
290 /* return length of buffer */
291 return (data - begin);
294 static int status_directorynames(char *data, int *diroffset,
296 const struct afp_options *options)
300 memcpy(&offset, data + *diroffset, sizeof(offset));
301 offset = ntohs(offset);
304 /* I can not find documentation of any other purpose for the
305 * DirectoryNames field.
308 * Try to synthesize a principal:
309 * service '/' fqdn '@' realm
311 if (options->k5service && options->k5realm && options->fqdn) {
312 /* should k5princ be utf8 encoded? */
314 char *p = strchr( options->fqdn, ':' );
317 len = strlen( options->k5service )
318 + strlen( options->fqdn )
319 + strlen( options->k5realm );
320 len+=2; /* '/' and '@' */
321 *data++ = 1; /* DirectoryNamesCount */
323 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
324 options->fqdn, options->k5realm );
329 memset(begin + *diroffset, 0, sizeof(offset));
332 /* Calculate and store offset for UTF8ServerName */
333 *diroffset += sizeof(u_int16_t);
334 offset = htons(data - begin);
335 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
337 /* return length of buffer */
338 return (data - begin);
341 static int status_utf8servername(char *data, int *nameoffset,
343 const struct afp_options *options)
348 memcpy(&offset, data + *nameoffset, sizeof(offset));
349 offset = ntohs(offset);
352 /* FIXME: For now we set the UTF-8 ServerName offset to 0
353 * It's irrelevent anyway until we set the appropriate Flags value.
354 * Later this can be set to something meaningful.
356 * What is the valid character range for an nbpname?
358 * Apple's server likes to use the non-qualified hostname
359 * This obviously won't work very well if multiple servers are running
362 memset(begin + *nameoffset, 0, sizeof(offset));
364 /* return length of buffer */
365 return (data - begin);
369 /* returns actual offset to signature */
370 static void status_icon(char *data, const unsigned char *icondata,
371 const int iconlen, const int sigoffset)
374 char *sigdata = data + sigoffset;
375 u_int16_t ret, status;
377 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
378 if ( icondata == NULL ) {
380 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
382 data += ntohs( status );
383 memcpy( data, icondata, iconlen);
385 ret = htons(data - start);
388 /* put in signature offset */
390 memcpy(sigdata, &ret, sizeof(ret));
393 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
394 const struct afp_options *options)
399 int statuslen, c, sigoff;
401 if (!(aspconfig || dsiconfig) || !options)
405 asp = aspconfig->obj.handle;
406 status = aspconfig->status;
411 dsi = dsiconfig->obj.handle;
413 status = dsiconfig->status;
418 * These routines must be called in order -- earlier calls
419 * set the offsets for later calls.
421 * using structs is a bad idea, but that's what the original code
422 * does. solaris, in fact, will segfault with them. so, now
423 * we just use the powers of #defines and memcpy.
425 * reply block layout (offsets are 16-bit quantities):
426 * machine type offset -> AFP version count offset ->
427 * UAM count offset -> vol icon/mask offset -> flags ->
429 * server name [padded to even boundary] -> signature offset ->
430 * network address offset ->
432 * at the appropriate offsets:
433 * machine type, afp versions, uams, server signature
434 * (16-bytes), network addresses, volume icon/mask
437 status_flags(status, options->server_notif, options->fqdn ||
438 (dsiconfig && dsi->server.sin_addr.s_addr),
440 (options->k5service && options->k5realm && options->fqdn));
441 /* returns offset to signature offset */
442 c = status_server(status, options->server ? options->server :
444 status_machine(status);
445 status_versions(status);
446 status_uams(status, options->uamlist);
447 if (options->flags & OPTION_CUSTOMICON)
448 status_icon(status, icon, sizeof(icon), c);
450 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
452 sigoff = status_signature(status, &c, dsi, options);
453 /* c now contains the offset where the netaddress offset lives */
455 status_netaddress(status, &c, asp, dsi, options->fqdn);
456 /* c now contains the offset where the Directory Names Count offset lives */
458 status_directorynames(status, &c, dsi, options);
459 /* c now contains the offset where the UTF-8 ServerName offset lives */
461 statuslen = status_utf8servername(status, &c, dsi, options);
466 asp_setstatus(asp, status, statuslen);
467 aspconfig->signature = status + sigoff;
468 aspconfig->statuslen = statuslen;
470 #endif /* ! NO_DDP */
473 if (aspconfig) { /* copy to dsiconfig */
474 memcpy(dsiconfig->status, status, ATP_MAXDATA);
475 status = dsiconfig->status;
478 if ((options->flags & OPTION_CUSTOMICON) == 0) {
479 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
481 dsi_setstatus(dsi, status, statuslen);
482 dsiconfig->signature = status + sigoff;
483 dsiconfig->statuslen = statuslen;
487 /* this is the same as asp/dsi_getstatus */
488 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
491 int ibuflen, *rbuflen;
493 AFPConfig *config = obj->config;
495 memcpy(rbuf, config->status, config->statuslen);
496 *rbuflen = config->statuslen;