2 * $Id: status.c,v 1.11 2003-04-16 22:45:10 samnoble 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)
134 char *usersign, *ifaddr;
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 ? 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? */
313 u_int8_t len = strlen( options->k5service )
314 + strlen( options->fqdn )
315 + strlen( options->k5realm );
316 len+=2; /* '/' and '@' */
317 *data++ = 1; /* DirectoryNamesCount */
319 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
320 options->fqdn, options->k5realm );
323 memset(begin + *diroffset, 0, sizeof(offset));
326 /* Calculate and store offset for UTF8ServerName */
327 *diroffset += sizeof(u_int16_t);
328 offset = htons(data - begin);
329 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
331 /* return length of buffer */
332 return (data - begin);
335 static int status_utf8servername(char *data, int *nameoffset,
337 const struct afp_options *options)
339 u_int16_t namelen, len;
342 memcpy(&offset, data + *nameoffset, sizeof(offset));
343 offset = ntohs(offset);
346 /* FIXME: For now we set the UTF-8 ServerName offset to 0
347 * It's irrelevent anyway until we set the appropriate Flags value.
348 * Later this can be set to something meaningful.
350 * What is the valid character range for an nbpname?
352 * Apple's server likes to use the non-qualified hostname
353 * This obviously won't work very well if multiple servers are running
356 memset(begin + *nameoffset, 0, sizeof(offset));
358 /* return length of buffer */
359 return (data - begin);
363 /* returns actual offset to signature */
364 static void status_icon(char *data, const unsigned char *icondata,
365 const int iconlen, const int sigoffset)
368 char *sigdata = data + sigoffset;
369 u_int16_t ret, status;
371 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
372 if ( icondata == NULL ) {
374 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
376 data += ntohs( status );
377 memcpy( data, icondata, iconlen);
379 ret = htons(data - start);
382 /* put in signature offset */
384 memcpy(sigdata, &ret, sizeof(ret));
387 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
388 const struct afp_options *options)
393 int statuslen, c, sigoff;
395 if (!(aspconfig || dsiconfig) || !options)
399 asp = aspconfig->obj.handle;
400 status = aspconfig->status;
405 dsi = dsiconfig->obj.handle;
407 status = dsiconfig->status;
412 * These routines must be called in order -- earlier calls
413 * set the offsets for later calls.
415 * using structs is a bad idea, but that's what the original code
416 * does. solaris, in fact, will segfault with them. so, now
417 * we just use the powers of #defines and memcpy.
419 * reply block layout (offsets are 16-bit quantities):
420 * machine type offset -> AFP version count offset ->
421 * UAM count offset -> vol icon/mask offset -> flags ->
423 * server name [padded to even boundary] -> signature offset ->
424 * network address offset ->
426 * at the appropriate offsets:
427 * machine type, afp versions, uams, server signature
428 * (16-bytes), network addresses, volume icon/mask
431 status_flags(status, options->server_notif, options->fqdn ||
432 (dsiconfig && dsi->server.sin_addr.s_addr),
434 (options->k5service && options->k5realm && options->fqdn));
435 /* returns offset to signature offset */
436 c = status_server(status, options->server ? options->server :
438 status_machine(status);
439 status_versions(status);
440 status_uams(status, options->uamlist);
441 if (options->flags & OPTION_CUSTOMICON)
442 status_icon(status, icon, sizeof(icon), c);
444 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
446 sigoff = status_signature(status, &c, dsi, options);
447 /* c now contains the offset where the netaddress offset lives */
449 status_netaddress(status, &c, asp, dsi, options->fqdn);
450 /* c now contains the offset where the Directory Names Count offset lives */
452 status_directorynames(status, &c, dsi, options);
453 /* c now contains the offset where the UTF-8 ServerName offset lives */
455 statuslen = status_utf8servername(status, &c, dsi, options);
460 asp_setstatus(asp, status, statuslen);
461 aspconfig->signature = status + sigoff;
462 aspconfig->statuslen = statuslen;
464 #endif /* ! NO_DDP */
467 if (aspconfig) { /* copy to dsiconfig */
468 memcpy(dsiconfig->status, status, ATP_MAXDATA);
469 status = dsiconfig->status;
472 if ((options->flags & OPTION_CUSTOMICON) == 0) {
473 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
475 dsi_setstatus(dsi, status, statuslen);
476 dsiconfig->signature = status + sigoff;
477 dsiconfig->statuslen = statuslen;
481 /* this is the same as asp/dsi_getstatus */
482 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
485 int ibuflen, *rbuflen;
487 AFPConfig *config = obj->config;
489 memcpy(rbuf, config->status, config->statuslen);
490 *rbuflen = config->statuslen;