1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains functions for dispatching a notice.
4 * Created by: John T. Kohl
6 * $Source: /afs/dev.mit.edu/source/repository/athena/lib/zephyr/server/dispatch.c,v $
9 * Copyright (c) 1987, 1991 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
14 #include <zephyr/mit-copyright.h>
16 #include <sys/socket.h>
21 static const char rcsid_dispatch_c[] =
26 #define NACKTAB_HASHSIZE 1023
27 #define NACKTAB_HASHVAL(sockaddr, uid) (((sockaddr).sin_addr.s_addr ^ \
28 (sockaddr).sin_port ^ \
29 (uid).zuid_addr.s_addr ^ \
31 (uid).tv.tv_usec) % NACKTAB_HASHSIZE)
32 #define HOSTS_SIZE_INIT 256
35 const char *ZNoticeKinds[9] = {"UNSAFE", "UNACKED", "ACKED", "HMACK",
36 "HMCTL", "SERVACK", "SERVNAK", "CLIENTACK",
43 * void dispatch(notice, auth, who)
46 * struct sockaddr_in *who;
48 * void clt_ack(notice, who, sent)
50 * struct sockaddr_in *who;
53 * void nack_release(client)
56 * void sendit(notice, auth, who, external)
59 * struct sockaddr_in *who;
62 * void xmit(notice, dest, auth, client)
64 * struct sockaddr_in *dest;
70 String *class_control, *class_admin, *class_hm, *class_ulogin, *class_ulocate;
72 int rexmit_times[] = REXMIT_TIMES;
74 static void nack_cancel __P((ZNotice_t *, struct sockaddr_in *));
75 static void dispatch __P((ZNotice_t *, int, struct sockaddr_in *, int));
76 static int send_to_dest __P((ZNotice_t *, int, Destination *dest, int, int));
77 static void hostm_deathgram __P((struct sockaddr_in *, Server *));
78 static char *hm_recipient __P((void));
80 Statistic realm_notices = {0, "inter-realm notices"};
81 Statistic interserver_notices = {0, "inter-server notices"};
82 Statistic hm_packets = {0, "hostmanager packets"};
83 Statistic control_notices = {0, "client control notices"};
84 Statistic message_notices = {0, "message notices"};
85 Statistic login_notices = {0, "login notices"};
86 Statistic i_s_ctls = {0, "inter-server control notices"};
87 Statistic i_s_logins = {0, "inter-server login notices"};
88 Statistic i_s_admins = {0, "inter-server admin notices"};
89 Statistic i_s_locates = {0, "inter-server locate notices"};
90 Statistic locate_notices = {0, "locate notices"};
91 Statistic admin_notices = {0, "admin notices"};
93 static Unacked *nacktab[NACKTAB_HASHSIZE];
94 static struct in_addr *hosts;
95 static int hosts_size = 0, num_hosts = 0;
98 dump_stats (void *arg)
100 syslog(LOG_INFO, "stats: %s: %d", hm_packets.str, hm_packets.val);
101 syslog(LOG_INFO, "stats: %s: %d", control_notices.str,
102 control_notices.val);
103 syslog(LOG_INFO, "stats: %s: %d", message_notices.str,
104 message_notices.val);
105 syslog(LOG_INFO, "stats: %s: %d", login_notices.str, login_notices.val);
106 syslog(LOG_INFO, "stats: %s: %d", locate_notices.str, locate_notices.val);
107 syslog(LOG_INFO, "stats: %s: %d", admin_notices.str, admin_notices.val);
108 syslog(LOG_INFO, "stats: %s: %d", realm_notices.str, realm_notices.val);
109 syslog(LOG_INFO, "stats: %s: %d", interserver_notices.str,
110 interserver_notices.val);
111 syslog(LOG_INFO, "stats: %s: %d", i_s_ctls.str, i_s_ctls.val);
112 syslog(LOG_INFO, "stats: %s: %d", i_s_logins.str, i_s_logins.val);
113 syslog(LOG_INFO, "stats: %s: %d", i_s_admins.str, i_s_admins.val);
114 syslog(LOG_INFO, "stats: %s: %d", i_s_locates.str, i_s_locates.val);
116 /* log stuff once an hour */
117 timer_set_rel ((long) 6*60*60, dump_stats, arg);
121 * Handle an input packet.
122 * Warning: this function may be called from within a brain dump.
129 ZPacket_t input_packet; /* from the network */
130 ZNotice_t new_notice; /* parsed from input_packet */
131 int input_len; /* len of packet */
132 struct sockaddr_in input_sin; /* Zconstructed for authent */
133 struct sockaddr_in whoisit; /* for holding peer's address */
134 int authentic; /* authentic flag */
135 Pending *pending; /* pending packet */
136 int from_server; /* packet is from another server */
137 ZRealm *realm; /* foreign realm ptr */
139 static int first_time = 1;
143 /* Dump statistics five minutes after startup */
146 timer_set_rel(5*60, dump_stats, NULL);
151 if (otherservers[me_server_idx].queue) {
152 /* something here for me; take care of it */
153 zdbug((LOG_DEBUG, "internal queue process"));
155 pending = server_dequeue(me_server);
157 status = ZParseNotice(pending->packet, pending->len, &new_notice);
158 if (status != ZERR_NONE) {
159 syslog(LOG_ERR, "bad notice parse (%s): %s",
160 inet_ntoa(pending->who.sin_addr), error_message(status));
162 dispatch(&new_notice, pending->auth, &pending->who, 1);
164 server_pending_free(pending);
169 * nothing in internal queue, go to the external library
172 status = ZReceivePacket(input_packet, &input_len, &whoisit);
173 if (status != ZERR_NONE) {
174 syslog(LOG_ERR, "bad packet receive: %s from %s",
175 error_message(status), inet_ntoa(whoisit.sin_addr));
179 status = ZParseNotice(input_packet, input_len, &new_notice);
180 if (status != ZERR_NONE) {
181 syslog(LOG_ERR, "bad notice parse (%s): %s",
182 inet_ntoa(whoisit.sin_addr), error_message(status));
185 if (server_which_server(&whoisit)) {
186 /* we need to parse twice--once to get
187 the source addr, second to check
189 memset(&input_sin, 0, sizeof(input_sin));
190 input_sin.sin_addr.s_addr = new_notice.z_sender_addr.s_addr;
191 input_sin.sin_port = new_notice.z_port;
192 input_sin.sin_family = AF_INET;
193 /* Should check to see if packet is from another realm's server,
195 /* Clients don't check auth of acks, nor do we make it so they
196 can in general, so this is safe. */
197 if (new_notice.z_kind == SERVACK || new_notice.z_kind == SERVNAK) {
198 authentic = ZAUTH_YES;
200 realm = realm_which_realm(&input_sin);
202 authentic = ZCheckRealmAuthentication(&new_notice,
206 authentic = ZCheckAuthentication(&new_notice, &input_sin);
211 /* Clients don't check auth of acks, nor do we make it so they
212 can in general, so this is safe. */
213 if (new_notice.z_kind == SERVACK || new_notice.z_kind == SERVNAK) {
214 authentic = ZAUTH_YES;
216 realm = realm_which_realm(&whoisit);
218 authentic = ZCheckRealmAuthentication(&new_notice,
222 authentic = ZCheckAuthentication(&new_notice, &whoisit);
226 message_notices.val++;
227 dispatch(&new_notice, authentic, &whoisit, from_server);
235 dispatch(ZNotice_t *notice,
237 struct sockaddr_in *who,
241 String *notice_class;
242 struct sockaddr_in who2;
247 char dbg_buf[BUFSIZ];
250 authflag = (auth == ZAUTH_YES);
252 if ((int) notice->z_kind < (int) UNSAFE ||
253 (int) notice->z_kind > (int) CLIENTACK) {
254 syslog(LOG_NOTICE, "bad notice kind 0x%x from %s", notice->z_kind,
255 inet_ntoa(who->sin_addr));
259 if (notice->z_kind == CLIENTACK) {
260 nack_cancel(notice, who);
266 notice_class = make_string(notice->z_class,1);
269 interserver_notices.val++;
270 status = server_dispatch(notice, authflag, who);
271 } else if (class_is_hm(notice_class)) {
273 status = hostm_dispatch(notice, authflag, who, me_server);
274 } else if (realm_which_realm(who) && !(class_is_admin(notice_class))) {
276 status = realm_dispatch(notice, authflag, who, me_server);
277 } else if (class_is_control(notice_class)) {
278 control_notices.val++;
279 status = control_dispatch(notice, authflag, who, me_server);
280 } else if (class_is_ulogin(notice_class)) {
282 status = ulogin_dispatch(notice, authflag, who, me_server);
283 } else if (class_is_ulocate(notice_class)) {
284 locate_notices.val++;
285 status = ulocate_dispatch(notice, authflag, who, me_server);
286 } else if (class_is_admin(notice_class)) {
288 status = server_adispatch(notice, authflag, who, me_server);
290 if (!realm_bound_for_realm(ZGetRealm(), notice->z_recipient)) {
291 cp = strchr(notice->z_recipient, '@');
293 !(realm = realm_get_realm_by_name(cp + 1))) {
294 /* Foreign user, local realm */
295 sendit(notice, authflag, who, 0);
297 realm_handoff(notice, authflag, who, realm, 1);
299 if (notice->z_recipient[0] == '@')
300 notice->z_recipient = "";
301 sendit(notice, authflag, who, 1);
303 free_string(notice_class);
307 if (status == ZSRV_REQUEUE)
308 server_self_queue(notice, authflag, who);
309 free_string(notice_class);
313 * Send a notice off to those clients who have subscribed to it.
317 sendit(ZNotice_t *notice,
319 struct sockaddr_in *who,
322 static int send_counter = 0;
323 char recipbuf[MAX_PRINCIPAL_SIZE], *recipp;
329 class = make_string(notice->z_class, 1);
330 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient)) {
333 acl = class_get_acl(class);
335 /* if controlled and not auth, fail */
337 syslog(LOG_WARNING, "sendit unauthentic %s from %s",
338 notice->z_class, notice->z_sender);
339 clt_ack(notice, who, AUTH_FAILED);
343 /* if from foreign realm server, disallow if not realm of sender */
344 rlm = realm_which_realm(who);
346 if (!realm_sender_in_realm(rlm->name, notice->z_sender)) {
347 syslog(LOG_WARNING, "sendit auth not verifiable %s (%s) from %s",
348 notice->z_class, rlm->name, notice->z_sender);
349 clt_ack(notice, who, AUTH_FAILED);
354 /* if not auth to transmit, fail */
355 if (!access_check(notice->z_sender, acl, TRANSMIT)) {
356 syslog(LOG_WARNING, "sendit unauthorized %s from %s",
357 notice->z_class, notice->z_sender);
358 clt_ack(notice, who, AUTH_FAILED);
362 /* sender != inst and not auth to send to others --> fail */
363 if (strcmp(notice->z_sender, notice->z_class_inst) != 0 &&
364 !access_check(notice->z_sender, acl, INSTUID)) {
365 syslog(LOG_WARNING, "sendit unauth uid %s %s.%s", notice->z_sender,
366 notice->z_class, notice->z_class_inst);
367 clt_ack(notice, who, AUTH_FAILED);
374 /* Increment the send counter, used to prevent duplicate sends to
375 * clients. On the off-chance that we wrap around to 0, skip over
376 * it to prevent missing clients which have never had a packet
379 if (send_counter == 0)
382 /* Send to clients subscribed to the triplet itself. */
383 dest.classname = class;
384 dest.inst = make_string(notice->z_class_inst, 1);
385 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient) &&
386 *notice->z_recipient == '@')
387 dest.recip = make_string("", 0);
389 strncpy(recipbuf, notice->z_recipient, sizeof(recipbuf));
390 recipp = strrchr(recipbuf, '@');
392 /* XXX if realm_expand_realm doesn't find a match
393 * it returns what's passed into it, causing an overlapping
394 * copy, the results of which are undefined.
396 strncpy(recipp + 1, realm_expand_realm(recipp + 1),
397 sizeof(recipbuf) - (recipp - recipbuf) - 1);
398 dest.recip = make_string(recipbuf, 0);
401 if (send_to_dest(notice, auth, &dest, send_counter, external))
404 /* Send to clients subscribed to the triplet with the instance
405 * substituted with the wildcard instance. */
406 free_string(dest.inst);
407 dest.inst = wildcard_instance;
408 if (send_to_dest(notice, auth, &dest, send_counter, external))
412 free_string(dest.recip);
420 * Send to each client in the list. Avoid duplicates by setting
421 * last_send on each client to send_counter, a nonce which is updated
426 send_to_dest(ZNotice_t *notice,
435 clientp = triplet_lookup(dest);
439 for (; *clientp; clientp++) {
440 if ((*clientp)->last_send == send_counter)
442 (*clientp)->last_send = send_counter;
443 if ((*clientp)->realm) {
445 realm_handoff(notice, auth, &clientp[0]->addr, clientp[0]->realm,
450 xmit(notice, &((*clientp)->addr), auth, *clientp);
459 * Release anything destined for the client in the not-yet-acked table.
463 nack_release(Client *client)
466 Unacked *nacked, *next;
468 for (i = 0; i < NACKTAB_HASHSIZE; i++) {
469 for (nacked = nacktab[i]; nacked; nacked = next) {
471 if (nacked->client == client) {
472 timer_reset(nacked->timer);
473 Unacked_delete(nacked);
474 free(nacked->packet);
482 * Send one packet of a fragmented message to a client. After transmitting,
483 * put it onto the not ack'ed list.
486 /* the arguments must be the same as the arguments to Z_XmitFragment */
489 xmit_frag(ZNotice_t *notice,
494 struct sockaddr_in sin;
500 retval = ZSendPacket(buf, len, 0);
501 if (retval != ZERR_NONE) {
502 syslog(LOG_WARNING, "xmit_frag send: %s", error_message(retval));
503 if (retval != EAGAIN && retval != ENOBUFS)
508 /* now we've sent it, mark it as not ack'ed */
509 nacked = (Unacked *) malloc(sizeof(Unacked));
511 /* no space: just punt */
512 syslog(LOG_WARNING, "xmit_frag nack malloc");
516 savebuf = (char *) malloc(len);
518 /* no space: just punt */
519 syslog(LOG_WARNING, "xmit_frag pack malloc");
524 memcpy(savebuf, buf, len);
526 sin = ZGetDestAddr();
527 nacked->client = NULL;
528 nacked->rexmits = (sendfail) ? -1 : 0;
529 nacked->packet = savebuf;
530 nacked->dest.addr = sin;
531 nacked->packsz = len;
532 nacked->uid = notice->z_uid;
533 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
534 Unacked_insert(&nacktab[NACKTAB_HASHVAL(sin, nacked->uid)], nacked);
540 * Send the notice to the client. After transmitting, put it onto the
545 xmit(ZNotice_t *notice,
546 struct sockaddr_in *dest,
552 int packlen, sendfail = 0;
555 noticepack = (char *) malloc(sizeof(ZPacket_t));
557 syslog(LOG_ERR, "xmit malloc");
558 return; /* DON'T put on nack list */
561 packlen = sizeof(ZPacket_t);
563 if (auth && client) { /*
564 we are distributing authentic and
565 we have a pointer to auth info
568 retval = ZFormatAuthenticNoticeV5(notice, noticepack, packlen,
569 &packlen, client->session_keyblock);
570 if (retval != ZERR_NONE) {
571 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
576 #if defined(HAVE_KRB4)
577 retval = ZFormatAuthenticNotice(notice, noticepack, packlen,
578 &packlen, client->session_key);
579 if (retval != ZERR_NONE) {
580 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
584 #else /* !HAVE_KRB4 */
586 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
587 if (retval != ZERR_NONE) {
588 syslog(LOG_ERR, "xmit auth/raw format: %s", error_message(retval));
592 #endif /* HAVE_KRB4 */
593 #endif /* HAVE_KRB5 */
596 notice->z_authent_len = 0;
597 notice->z_ascii_authent = (char *)"";
598 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
599 /* This code is needed because a Zephyr can "grow" when a remote
600 * realm name is inserted into the Zephyr before being resent out
601 * locally. It essentially matches the code in realm.c to do the
602 * same thing with authentic Zephyrs.
604 if (retval == ZERR_PKTLEN) {
605 ZNotice_t partnotice;
608 int buffer_len, hdrlen, offset, fragsize, message_len;
609 int origoffset, origlen;
613 retval = ZSetDestAddr(dest);
614 if (retval != ZERR_NONE) {
615 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
619 partnotice = *notice;
621 partnotice.z_auth = 0;
622 partnotice.z_authent_len = 0;
623 partnotice.z_ascii_authent = (char *)"";
625 origoffset = offset = fragsize = 0;
626 origlen = notice->z_message_len;
628 buffer = (char *) malloc(sizeof(ZPacket_t));
630 syslog(LOG_ERR, "xmit unauth refrag malloc");
631 return; /* DON'T put on nack list */
633 buffer_len = sizeof(ZPacket_t);
635 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
636 &hdrlen, NULL, NULL);
637 if (retval != ZERR_NONE) {
638 syslog(LOG_ERR, "xmit unauth refrag fmt: failed");
643 if (notice->z_multinotice && strcmp(notice->z_multinotice, ""))
644 if (sscanf(notice->z_multinotice, "%d/%d", &origoffset, &origlen)
647 syslog(LOG_WARNING, "xmit unauth refrag: parse failed");
652 fragsize = Z_MAXPKTLEN-hdrlen-Z_FRAGFUDGE;
654 while (offset < notice->z_message_len || !notice->z_message_len) {
655 (void) sprintf(multi, "%d/%d", offset+origoffset, origlen);
656 partnotice.z_multinotice = multi;
658 (void) Z_gettimeofday(&partnotice.z_uid.tv, (struct timezone *)0);
659 partnotice.z_uid.tv.tv_sec = htonl((u_long)
660 partnotice.z_uid.tv.tv_sec);
661 partnotice.z_uid.tv.tv_usec = htonl((u_long)
662 partnotice.z_uid.tv.tv_usec);
663 (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
666 partnotice.z_message = notice->z_message+offset;
667 message_len = min(notice->z_message_len-offset, fragsize);
668 partnotice.z_message_len = message_len;
670 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
671 &hdrlen, &ptr, NULL);
672 if (retval != ZERR_NONE) {
673 syslog(LOG_WARNING, "xmit unauth refrag raw: %s",
674 error_message(retval));
681 (void) memcpy(ptr, partnotice.z_message, partnotice.z_message_len);
683 buffer_len = hdrlen+partnotice.z_message_len;
685 xmit_frag(&partnotice, buffer, buffer_len, 0);
689 if (!notice->z_message_len)
695 /* End of refrag code */
697 if (retval != ZERR_NONE) {
698 syslog(LOG_ERR, "xmit format: %s", error_message(retval));
700 return; /* DON'T put on nack list */
703 retval = ZSetDestAddr(dest);
704 if (retval != ZERR_NONE) {
705 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
709 retval = ZSendPacket(noticepack, packlen, 0);
710 if (retval != ZERR_NONE) {
711 syslog(LOG_WARNING, "xmit xmit: (%s/%d) %s", inet_ntoa(dest->sin_addr),
712 ntohs(dest->sin_port), error_message(retval));
713 if (retval != EAGAIN && retval != ENOBUFS) {
720 /* now we've sent it, mark it as not ack'ed */
722 nacked = (Unacked *) malloc(sizeof(Unacked));
724 /* no space: just punt */
725 syslog(LOG_WARNING, "xmit nack malloc");
730 nacked->client = client;
731 nacked->rexmits = (sendfail) ? -1 : 0;
732 nacked->packet = noticepack;
733 nacked->dest.addr = *dest;
734 nacked->packsz = packlen;
735 nacked->uid = notice->z_uid;
736 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
737 Unacked_insert(&nacktab[NACKTAB_HASHVAL(*dest, nacked->uid)], nacked);
741 * Retransmit the packet specified. If we have timed out or retransmitted
742 * too many times, punt the packet and initiate the host recovery algorithm
743 * Else, increment the count and re-send the notice packet.
749 Unacked *nacked = (Unacked *) arg;
752 syslog(LOG_DEBUG, "rexmit %s/%d #%d time %d",
753 inet_ntoa(nacked->dest.addr.sin_addr),
754 ntohs(nacked->dest.addr.sin_port), nacked->rexmits + 1, (int)NOW);
757 if (rexmit_times[nacked->rexmits] == -1) {
759 || NOW - nacked->client->last_ack >= CLIENT_GIVEUP_MIN) {
760 /* The client (if there was one) has been unresponsive.
761 * Give up sending this packet, and kill the client if
762 * there was one. (Make sure to remove nacked from the
763 * nack list before calling client_deregister(), which
764 * scans the nack list.)
766 Unacked_delete(nacked);
767 if (nacked->client) {
768 server_kill_clt(nacked->client);
769 client_deregister(nacked->client, 1);
771 free(nacked->packet);
775 /* The client has sent us an ack recently. Retry with the maximum
776 * retransmit time. */
781 /* retransmit the packet */
782 retval = ZSetDestAddr(&nacked->dest.addr);
783 if (retval != ZERR_NONE) {
784 syslog(LOG_WARNING, "rexmit set addr: %s", error_message(retval));
786 retval = ZSendPacket(nacked->packet, nacked->packsz, 0);
787 if (retval != ZERR_NONE)
788 syslog(LOG_WARNING, "rexmit xmit: %s", error_message(retval));
789 if (retval == EAGAIN || retval == ENOBUFS)
793 /* reset the timer */
794 nacked->timer = timer_set_rel(rexmit_times[nacked->rexmits], rexmit,
800 * Send an acknowledgement to the sending client, by sending back the
801 * header from the original notice with the z_kind field changed to either
802 * SERVACK or SERVNAK, and the contents of the message either SENT or
803 * NOT_SENT, depending on the value of the sent argument.
807 clt_ack(ZNotice_t *notice,
808 struct sockaddr_in *who,
817 if (bdumping) { /* don't ack while dumping */
818 zdbug((LOG_DEBUG,"bdumping, no ack"));
824 acknotice.z_kind = SERVACK;
827 acknotice.z_message = ZSRVACK_SENT;
831 acknotice.z_message = ZSRVACK_FAIL;
832 acknotice.z_kind = SERVNAK;
836 acknotice.z_kind = SERVNAK;
837 acknotice.z_message = ZSRVACK_NOTSENT;
838 sent_name = "nak/not_sent";
841 acknotice.z_message = ZSRVACK_NOTSENT;
842 sent_name = "not_sent";
848 acknotice.z_multinotice = "";
850 /* leave room for the trailing null */
851 acknotice.z_message_len = strlen(acknotice.z_message) + 1;
853 packlen = sizeof(ackpack);
855 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
857 if (retval == ZERR_HEADERLEN) {
858 /* Since an ack header can be larger than a message header... (crock) */
859 acknotice.z_opcode = "";
860 acknotice.z_class = "";
861 acknotice.z_class_inst = "";
862 acknotice.z_opcode = "";
863 acknotice.z_default_format = "";
865 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
868 if (retval != ZERR_NONE) {
869 syslog(LOG_ERR, "clt_ack format: %s", error_message(retval));
872 retval = ZSetDestAddr(who);
873 if (retval != ZERR_NONE) {
874 syslog(LOG_WARNING, "clt_ack set addr: %s", error_message(retval));
877 retval = ZSendPacket(ackpack, packlen, 0);
878 if (retval != ZERR_NONE) {
879 syslog(LOG_WARNING, "clt_ack xmit: %s", error_message(retval));
882 zdbug((LOG_DEBUG, "packet sent"));
888 * An ack has arrived.
889 * remove the packet matching this notice from the not-yet-acked queue
893 nack_cancel(ZNotice_t *notice,
894 struct sockaddr_in *who)
899 /* search the not-yet-acked table for this packet, and flush it. */
900 hashval = NACKTAB_HASHVAL(*who, notice->z_uid);
901 for (nacked = nacktab[hashval]; nacked; nacked = nacked->next) {
902 if (nacked->dest.addr.sin_addr.s_addr == who->sin_addr.s_addr
903 && nacked->dest.addr.sin_port == who->sin_port
904 && ZCompareUID(&nacked->uid, ¬ice->z_uid)) {
906 nacked->client->last_ack = NOW;
907 timer_reset(nacked->timer);
908 free(nacked->packet);
909 Unacked_delete(nacked);
915 zdbug((LOG_DEBUG,"nack_cancel: nack not found %s:%08X,%08X",
916 inet_ntoa (notice->z_uid.zuid_addr),
917 notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));
920 /* for compatibility when sending subscription information to old clients */
922 #define OLD_ZEPHYR_VERSION "ZEPH0.0"
923 #endif /* OLD_COMPAT */
925 /* Dispatch an HM_CTL notice. */
928 hostm_dispatch(ZNotice_t *notice,
930 struct sockaddr_in *who,
933 char *opcode = notice->z_opcode;
934 int i, add = 0, remove = 0;
936 if (notice->z_kind == HMACK) {
939 } else if (notice->z_kind != HMCTL) {
940 clt_ack(notice, who, AUTH_FAILED);
941 } else if (strcmp(opcode, HM_FLUSH) == 0) {
942 client_flush_host(&who->sin_addr);
943 if (server == me_server)
944 server_forward(notice, auth, who);
945 } else if (strcmp(opcode, HM_BOOT) == 0) {
946 client_flush_host(&who->sin_addr);
947 if (server == me_server) {
948 server_forward(notice, auth, who);
952 } else if (strcmp(opcode, HM_ATTACH) == 0) {
953 if (server == me_server) {
954 server_forward(notice, auth, who);
960 } else if (strcmp(opcode, HM_DETACH) == 0) {
963 syslog(LOG_WARNING, "hm_dispatch: unknown opcode %s", opcode);
967 for (i = 0; i < num_hosts; i++) {
968 if (hosts[i].s_addr == who->sin_addr.s_addr)
971 if (i == num_hosts) {
972 if (hosts_size == 0) {
973 hosts = (struct in_addr *) malloc(HOSTS_SIZE_INIT *
974 sizeof(struct in_addr));
977 hosts_size = HOSTS_SIZE_INIT;
978 } else if (num_hosts == hosts_size) {
979 hosts = (struct in_addr *) realloc(hosts, hosts_size * 2 *
980 sizeof(struct in_addr));
985 hosts[num_hosts++] = who->sin_addr;
988 for (i = 0; i < num_hosts; i++) {
989 if (hosts[i].s_addr == who->sin_addr.s_addr) {
990 memmove(&hosts[i], &hosts[i + 1], num_hosts - (i + 1));
1000 * Dispatch a ZEPHYR_CTL notice.
1004 control_dispatch(ZNotice_t *notice,
1006 struct sockaddr_in *who,
1009 char *opcode = notice->z_opcode;
1014 struct sockaddr_in newwho;
1017 * ZEPHYR_CTL Opcodes expected are:
1018 * BOOT (inst HM): host has booted; flush data.
1019 * CLIENT_SUBSCRIBE: process with the subscription mananger.
1020 * CLIENT_UNSUBSCRIBE: ""
1021 * CLIENT_CANCELSUB: ""
1024 zdbug((LOG_DEBUG, "ctl_disp: opc=%s", opcode));
1026 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
1027 newwho.sin_port = notice->z_port;
1028 realm = realm_which_realm(&newwho);
1030 return(realm_control_dispatch(notice, auth, who, server, realm));
1032 if (strcasecmp(notice->z_class_inst, ZEPHYR_CTL_HM) == 0) {
1033 return hostm_dispatch(notice, auth, who, server);
1034 } else if (strcmp(opcode, CLIENT_GIMMESUBS) == 0 ||
1035 strcmp(opcode, CLIENT_GIMMEDEFS) == 0) {
1036 /* this special case is before the auth check so that
1037 someone who has no subscriptions does NOT get a SERVNAK
1038 but rather an empty list. Note we must therefore
1039 check authentication inside subscr_sendlist */
1041 /* only acknowledge if *not* old version; the old version
1042 acknowledges the packet with the reply */
1043 if (strcmp(notice->z_version, OLD_ZEPHYR_VERSION) != 0)
1045 #else /* !OLD_COMPAT */
1047 #endif /* OLD_COMPAT */
1048 subscr_sendlist(notice, auth, who);
1051 if (server == me_server)
1052 clt_ack(notice, who, AUTH_FAILED);
1056 wantdefs = strcmp(opcode, CLIENT_SUBSCRIBE_NODEFS);
1057 if (!wantdefs || strcmp(opcode, CLIENT_SUBSCRIBE) == 0) {
1058 /* subscription notice */
1059 retval = client_register(notice, &who->sin_addr, &client, wantdefs);
1060 if (retval != ZERR_NONE) {
1061 syslog(LOG_NOTICE, "subscr %s/%s/%d failed: %s",
1062 notice->z_sender, inet_ntoa(who->sin_addr),
1063 ntohs(notice->z_port), error_message(retval));
1064 if (server == me_server) {
1065 if (retval == ZSRV_BADSUBPORT)
1066 clt_ack(notice, who, AUTH_FAILED);
1072 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1073 /* you may only subscribe for your own clients */
1074 if (server == me_server)
1075 clt_ack(notice, who, AUTH_FAILED);
1079 if (client->session_keyblock) {
1080 krb5_free_keyblock_contents(Z_krb5_ctx, client->session_keyblock);
1081 retval = krb5_copy_keyblock_contents(Z_krb5_ctx, ZGetSession(),
1082 client->session_keyblock);
1084 retval = krb5_copy_keyblock(Z_krb5_ctx, ZGetSession(),
1085 &client->session_keyblock);
1088 syslog(LOG_WARNING, "keyblock copy failed in subscr: %s",
1089 error_message(retval));
1090 if (server == me_server)
1096 /* in case it's changed */
1097 memcpy(client->session_key, ZGetSession(), sizeof(C_Block));
1100 retval = subscr_subscribe(client, notice, server);
1101 if (retval != ZERR_NONE) {
1102 syslog(LOG_WARNING, "subscr failed: %s", error_message(retval));
1103 if (server == me_server)
1107 } else if (strcmp(opcode, CLIENT_UNSUBSCRIBE) == 0) {
1108 client = client_find(&who->sin_addr, notice->z_port);
1109 if (client != NULL) {
1110 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1111 /* you may only cancel for your own clients */
1112 if (server == me_server)
1113 clt_ack(notice, who, AUTH_FAILED);
1116 subscr_cancel(who, notice);
1121 } else if (strcmp(opcode, CLIENT_CANCELSUB) == 0) {
1122 /* canceling subscriptions implies I can punt info about this client */
1123 client = client_find(&who->sin_addr, notice->z_port);
1124 if (client == NULL) {
1125 if (server == me_server)
1129 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1130 /* you may only cancel for your own clients */
1131 if (server == me_server)
1132 clt_ack(notice, who, AUTH_FAILED);
1135 /* don't flush locations here, let him do it explicitly */
1136 client_deregister(client, 0);
1138 syslog(LOG_WARNING, "unknown ctl opcode %s", opcode);
1139 if (server == me_server) {
1140 if (strcmp(notice->z_class_inst, ZEPHYR_CTL_REALM) != 0)
1146 if (server == me_server) {
1148 server_forward(notice, auth, who);
1154 hostm_shutdown(void)
1156 int i, s, newserver;
1157 struct sockaddr_in sin;
1159 for (i = 0; i < nservers; i++) {
1160 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1163 newserver = (i < nservers);
1164 for (i = 0; i < num_hosts; i++) {
1165 sin.sin_addr = hosts[i];
1166 sin.sin_port = hm_port;
1169 s = (random() % (nservers - 1)) + 1;
1170 if (otherservers[s].state == SERV_UP)
1173 hostm_deathgram(&sin, &otherservers[s]);
1175 hostm_deathgram(&sin, NULL);
1181 realm_shutdown(void)
1183 int i, s, newserver;
1185 for (i = 0; i < nservers; i++) {
1186 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1189 zdbug((LOG_DEBUG, "rlm_shutdown"));
1191 newserver = (i < nservers);
1194 s = (random() % (nservers - 1)) + 1;
1195 if (otherservers[s].state == SERV_UP)
1198 realm_deathgram(&otherservers[s]);
1200 realm_deathgram(NULL);
1205 hostm_deathgram(struct sockaddr_in *sin,
1210 ZNotice_t shutnotice;
1213 memset (&shutnotice, 0, sizeof(shutnotice));
1215 shutnotice.z_kind = HMCTL;
1216 shutnotice.z_port = sin->sin_port; /* we are sending it */
1217 shutnotice.z_class = HM_CTL_CLASS;
1218 shutnotice.z_class_inst = HM_CTL_SERVER;
1219 shutnotice.z_opcode = SERVER_SHUTDOWN;
1220 shutnotice.z_sender = HM_CTL_SERVER;
1221 shutnotice.z_recipient = hm_recipient();
1222 shutnotice.z_default_format = "";
1223 shutnotice.z_num_other_fields = 0;
1224 shutnotice.z_message = (server) ? server->addr_str : NULL;
1225 shutnotice.z_message_len = (server) ? strlen(server->addr_str) + 1 : 0;
1227 retval = ZFormatNotice(&shutnotice, &shutpack, &shutlen, ZNOAUTH);
1228 if (retval != ZERR_NONE) {
1229 syslog(LOG_ERR, "hm_shut format: %s",error_message(retval));
1232 retval = ZSetDestAddr(sin);
1233 if (retval != ZERR_NONE) {
1234 syslog(LOG_WARNING, "hm_shut set addr: %s", error_message(retval));
1238 retval = ZSendPacket(shutpack, shutlen, 0);
1239 if (retval != ZERR_NONE)
1240 syslog(LOG_WARNING, "hm_shut xmit: %s", error_message(retval));
1247 static char *recipient;
1253 realm = ZGetRealm();
1256 recipient = (char *) malloc(strlen(realm) + 4);
1257 strcpy (recipient, "hm@");
1258 strcat (recipient, realm);