2 * $Id: status.c,v 1.8 2002-12-07 02:39:57 rlewczuk 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>
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)
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;
62 status = htons(status);
63 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
66 static int status_server(char *data, const char *server)
69 char *Obj, *Type, *Zone;
73 /* make room for all offsets before server name */
74 data += AFPSTATUS_PRELEN;
76 /* extract the obj part of the server */
77 Obj = (char *) server;
78 nbp_name(server, &Obj, &Type, &Zone);
81 memcpy( data, Obj, len );
82 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
86 /* make room for signature and net address offset. save location of
89 * NOTE: technically, we don't need to reserve space for the
90 * signature and net address offsets if they're not going to be
91 * used. as there are no offsets after them, it doesn't hurt to
92 * have them specified though. so, we just do that to simplify
96 status = htons(len + AFPSTATUS_POSTLEN);
97 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
98 return len; /* return the offset to beginning of signature offset */
101 static void status_machine(char *data)
107 const char *machine = "afs";
109 const char *machine = "unix";
112 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
113 data += ntohs( status );
114 len = strlen( machine );
116 memcpy( data, machine, len );
118 status = htons(data - start);
119 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
123 /* server signature is a 16-byte quantity */
124 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
125 const char *hostname, const struct afp_options *options)
128 char *usersign, *ifaddr;
130 u_int16_t offset, sigoff;
140 /* get server signature offset */
141 memcpy(&offset, data + *servoffset, sizeof(offset));
142 sigoff = offset = ntohs(offset);
144 /* jump to server signature offset */
147 /* Signature type is user string */
148 if (strncmp(options->signature, "user", 4) == 0) {
149 if (strlen(options->signature) <= 5) {
150 syslog( LOG_ERR, "Signature %s id not valid. Switching back to hostid.",
152 goto server_signature_hostid;
154 usersign = options->signature + 5;
155 if (strlen(usersign) < 3)
156 syslog( LOG_WARNING, "Signature %s is very short !", options->signature);
159 strncpy(data, usersign, 16);
161 goto server_signature_done;
162 } /* signature = user */
164 /* If signature type is a standard hostid... */
165 server_signature_hostid:
166 /* 16-byte signature consists of copies of the hostid */
167 #if defined(BSD4_4) && defined(USE_GETHOSTID)
169 mib[1] = KERN_HOSTID;
170 len = sizeof(hostid);
171 sysctl(mib, 2, &hostid, &len, NULL, 0);
172 #else /* BSD4_4 && USE_GETHOSTID */
173 hostid = gethostid();
174 #endif /* BSD4_4 && USE_GETHOSTID */
177 hostid = dsi->server.sin_addr.s_addr;
179 struct hostent *host;
181 if ((host = gethostbyname(hostname)))
182 hostid = ((struct in_addr *) host->h_addr)->s_addr;
186 /* it turns out that a server signature screws up separate
187 * servers running on the same machine. to work around that,
188 * i add in an increment */
191 for (i = 0; i < 16; i += sizeof(hostid)) {
192 memcpy(data, &hostid, sizeof(hostid));
193 data += sizeof(hostid);
196 server_signature_done:
197 /* calculate net address offset */
198 *servoffset += sizeof(offset);
199 offset = htons(data - status);
200 memcpy(status + *servoffset, &offset, sizeof(offset));
204 static int status_netaddress(char *data, const int servoffset,
205 const ASP asp, const DSI *dsi,
213 /* get net address offset */
214 memcpy(&offset, data + servoffset, sizeof(offset));
215 data += ntohs(offset);
219 len (byte = sizeof(length + address type + address)
220 address type (byte, ip address = 0x01, ip + port = 0x02,
221 ddp address = 0x03, fqdn = 0x04)
222 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
225 /* number of addresses. this currently screws up if we have a dsi
226 connection, but we don't have the ip address. to get around this,
227 we turn off the status flag for tcp/ip. */
228 *data++ = (fqdn ? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
230 /* handle DNS names */
232 int len = strlen(fqdn) + 2;
235 memcpy(data, fqdn, len);
241 const struct sockaddr_in *inaddr = &dsi->server;
243 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
244 *data++ = 6; /* length */
245 *data++ = 0x01; /* basic ip address */
246 memcpy(data, &inaddr->sin_addr.s_addr,
247 sizeof(inaddr->sin_addr.s_addr));
248 data += sizeof(inaddr->sin_addr.s_addr);
250 /* ip address + port */
252 *data++ = 0x02; /* ip address with port */
253 memcpy(data, &inaddr->sin_addr.s_addr,
254 sizeof(inaddr->sin_addr.s_addr));
255 data += sizeof(inaddr->sin_addr.s_addr);
256 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
257 data += sizeof(inaddr->sin_port);
263 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
267 *data++ = 0x03; /* ddp address */
268 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
269 data += sizeof(ddpaddr->sat_addr.s_net);
270 memcpy(data, &ddpaddr->sat_addr.s_node,
271 sizeof(ddpaddr->sat_addr.s_node));
272 data += sizeof(ddpaddr->sat_addr.s_node);
273 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
274 data += sizeof(ddpaddr->sat_port);
276 #endif /* ! NO_DDP */
278 /* return length of buffer */
279 return (data - begin);
282 /* returns actual offset to signature */
283 static void status_icon(char *data, const unsigned char *icondata,
284 const int iconlen, const int sigoffset)
287 char *sigdata = data + sigoffset;
288 u_int16_t ret, status;
290 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
291 if ( icondata == NULL ) {
293 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
295 data += ntohs( status );
296 memcpy( data, icondata, iconlen);
298 ret = htons(data - start);
301 /* put in signature offset */
303 memcpy(sigdata, &ret, sizeof(ret));
306 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
307 const struct afp_options *options)
314 if (!(aspconfig || dsiconfig) || !options)
318 asp = aspconfig->obj.handle;
319 status = aspconfig->status;
324 dsi = dsiconfig->obj.handle;
326 status = dsiconfig->status;
331 * These routines must be called in order -- earlier calls
332 * set the offsets for later calls.
334 * using structs is a bad idea, but that's what the original code
335 * does. solaris, in fact, will segfault with them. so, now
336 * we just use the powers of #defines and memcpy.
338 * reply block layout (offsets are 16-bit quantities):
339 * machine type offset -> AFP version count offset ->
340 * UAM count offset -> vol icon/mask offset -> flags ->
342 * server name [padded to even boundary] -> signature offset ->
343 * network address offset ->
345 * at the appropriate offsets:
346 * machine type, afp versions, uams, server signature
347 * (16-bytes), network addresses, volume icon/mask
350 status_flags(status, options->server_notif, options->fqdn ||
351 (dsiconfig && dsi->server.sin_addr.s_addr),
352 options->passwdbits);
353 /* returns offset to signature offset */
354 c = status_server(status, options->server ? options->server :
356 status_machine(status);
357 status_versions(status);
358 status_uams(status, options->uamlist);
359 if (options->flags & OPTION_CUSTOMICON)
360 status_icon(status, icon, sizeof(icon), c);
362 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
364 sigoff = status_signature(status, &c, dsi, options->hostname, options);
367 c = status_netaddress(status, c, asp, dsi, options->fqdn);
372 asp_setstatus(asp, status, c);
373 aspconfig->signature = status + sigoff;
374 aspconfig->statuslen = c;
376 #endif /* ! NO_DDP */
379 if (aspconfig) { /* copy to dsiconfig */
380 memcpy(dsiconfig->status, status, ATP_MAXDATA);
381 status = dsiconfig->status;
384 if ((options->flags & OPTION_CUSTOMICON) == 0) {
385 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
387 dsi_setstatus(dsi, status, c);
388 dsiconfig->signature = status + sigoff;
389 dsiconfig->statuslen = c;
393 /* this is the same as asp/dsi_getstatus */
394 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
397 int ibuflen, *rbuflen;
399 AFPConfig *config = obj->config;
401 memcpy(rbuf, config->status, config->statuslen);
402 *rbuflen = config->statuslen;