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[ANAME_SZ + INST_SZ + REALM_SZ + 3], *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);
373 if (!realm_which_realm(who)) {
374 if (memcmp(¬ice->z_sender_addr.s_addr, &who->sin_addr.s_addr,
375 sizeof(notice->z_sender_addr.s_addr))) {
376 /* someone is playing games... */
377 /* inet_ntoa returns pointer to static area */
378 /* max size is 255.255.255.255 */
380 strcpy(buffer, inet_ntoa(who->sin_addr));
383 "sendit unauthentic fake packet: claimed %s, real %s",
384 inet_ntoa(notice->z_sender_addr), buffer);
385 clt_ack(notice, who, AUTH_FAILED);
389 if (ntohl(notice->z_sender_addr.s_addr) != 0) {
391 "sendit invalid address: claimed %s, real %s",
392 inet_ntoa(notice->z_sender_addr), buffer);
393 clt_ack(notice, who, AUTH_FAILED);
397 syslog(LOG_WARNING, "sendit addr mismatch: claimed %s, real %s",
398 inet_ntoa(notice->z_sender_addr), buffer);
402 /* Increment the send counter, used to prevent duplicate sends to
403 * clients. On the off-chance that we wrap around to 0, skip over
404 * it to prevent missing clients which have never had a packet
407 if (send_counter == 0)
410 /* Send to clients subscribed to the triplet itself. */
411 dest.classname = class;
412 dest.inst = make_string(notice->z_class_inst, 1);
413 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient) &&
414 *notice->z_recipient == '@')
415 dest.recip = make_string("", 0);
417 strncpy(recipbuf, notice->z_recipient, sizeof(recipbuf));
418 recipp = strrchr(recipbuf, '@');
420 sprintf(recipp + 1, "%s", realm_expand_realm(recipp + 1));
421 dest.recip = make_string(recipbuf, 0);
424 if (send_to_dest(notice, auth, &dest, send_counter, external))
427 /* Send to clients subscribed to the triplet with the instance
428 * substituted with the wildcard instance. */
429 free_string(dest.inst);
430 dest.inst = wildcard_instance;
431 if (send_to_dest(notice, auth, &dest, send_counter, external))
435 free_string(dest.recip);
443 * Send to each client in the list. Avoid duplicates by setting
444 * last_send on each client to send_counter, a nonce which is updated
449 send_to_dest(ZNotice_t *notice,
458 clientp = triplet_lookup(dest);
462 for (; *clientp; clientp++) {
463 if ((*clientp)->last_send == send_counter)
465 (*clientp)->last_send = send_counter;
466 if ((*clientp)->realm) {
468 realm_handoff(notice, auth, &clientp[0]->addr, clientp[0]->realm,
473 xmit(notice, &((*clientp)->addr), auth, *clientp);
482 * Release anything destined for the client in the not-yet-acked table.
486 nack_release(Client *client)
489 Unacked *nacked, *next;
491 for (i = 0; i < NACKTAB_HASHSIZE; i++) {
492 for (nacked = nacktab[i]; nacked; nacked = next) {
494 if (nacked->client == client) {
495 timer_reset(nacked->timer);
496 Unacked_delete(nacked);
497 free(nacked->packet);
505 * Send one packet of a fragmented message to a client. After transmitting,
506 * put it onto the not ack'ed list.
509 /* the arguments must be the same as the arguments to Z_XmitFragment */
512 xmit_frag(ZNotice_t *notice,
517 struct sockaddr_in sin;
523 retval = ZSendPacket(buf, len, 0);
524 if (retval != ZERR_NONE) {
525 syslog(LOG_WARNING, "xmit_frag send: %s", error_message(retval));
526 if (retval != EAGAIN && retval != ENOBUFS)
531 /* now we've sent it, mark it as not ack'ed */
532 nacked = (Unacked *) malloc(sizeof(Unacked));
534 /* no space: just punt */
535 syslog(LOG_WARNING, "xmit_frag nack malloc");
539 savebuf = (char *) malloc(len);
541 /* no space: just punt */
542 syslog(LOG_WARNING, "xmit_frag pack malloc");
547 memcpy(savebuf, buf, len);
549 sin = ZGetDestAddr();
550 nacked->client = NULL;
551 nacked->rexmits = (sendfail) ? -1 : 0;
552 nacked->packet = savebuf;
553 nacked->dest.addr = sin;
554 nacked->packsz = len;
555 nacked->uid = notice->z_uid;
556 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
557 Unacked_insert(&nacktab[NACKTAB_HASHVAL(sin, nacked->uid)], nacked);
563 * Send the notice to the client. After transmitting, put it onto the
568 xmit(ZNotice_t *notice,
569 struct sockaddr_in *dest,
575 int packlen, sendfail = 0;
578 noticepack = (char *) malloc(sizeof(ZPacket_t));
580 syslog(LOG_ERR, "xmit malloc");
581 return; /* DON'T put on nack list */
584 packlen = sizeof(ZPacket_t);
586 if (auth && client) { /*
587 we are distributing authentic and
588 we have a pointer to auth info
591 retval = ZFormatAuthenticNoticeV5(notice, noticepack, packlen,
592 &packlen, client->session_keyblock);
593 if (retval != ZERR_NONE) {
594 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
599 #if defined(HAVE_KRB4)
600 retval = ZFormatAuthenticNotice(notice, noticepack, packlen,
601 &packlen, client->session_key);
602 if (retval != ZERR_NONE) {
603 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
607 #else /* !HAVE_KRB4 */
609 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
610 if (retval != ZERR_NONE) {
611 syslog(LOG_ERR, "xmit auth/raw format: %s", error_message(retval));
615 #endif /* HAVE_KRB4 */
616 #endif /* HAVE_KRB5 */
619 notice->z_authent_len = 0;
620 notice->z_ascii_authent = (char *)"";
621 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
622 /* This code is needed because a Zephyr can "grow" when a remote
623 * realm name is inserted into the Zephyr before being resent out
624 * locally. It essentially matches the code in realm.c to do the
625 * same thing with authentic Zephyrs.
627 if (retval == ZERR_PKTLEN) {
628 ZNotice_t partnotice;
631 int buffer_len, hdrlen, offset, fragsize, message_len;
632 int origoffset, origlen;
636 retval = ZSetDestAddr(dest);
637 if (retval != ZERR_NONE) {
638 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
642 partnotice = *notice;
644 partnotice.z_auth = 0;
645 partnotice.z_authent_len = 0;
646 partnotice.z_ascii_authent = (char *)"";
648 origoffset = offset = fragsize = 0;
649 origlen = notice->z_message_len;
651 buffer = (char *) malloc(sizeof(ZPacket_t));
653 syslog(LOG_ERR, "xmit unauth refrag malloc");
654 return; /* DON'T put on nack list */
656 buffer_len = sizeof(ZPacket_t);
658 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
659 &hdrlen, NULL, NULL);
660 if (retval != ZERR_NONE) {
661 syslog(LOG_ERR, "xmit unauth refrag fmt: failed");
666 if (notice->z_multinotice && strcmp(notice->z_multinotice, ""))
667 if (sscanf(notice->z_multinotice, "%d/%d", &origoffset, &origlen)
670 syslog(LOG_WARNING, "xmit unauth refrag: parse failed");
675 fragsize = Z_MAXPKTLEN-hdrlen-Z_FRAGFUDGE;
677 while (offset < notice->z_message_len || !notice->z_message_len) {
678 (void) sprintf(multi, "%d/%d", offset+origoffset, origlen);
679 partnotice.z_multinotice = multi;
681 (void) Z_gettimeofday(&partnotice.z_uid.tv, (struct timezone *)0);
682 partnotice.z_uid.tv.tv_sec = htonl((u_long)
683 partnotice.z_uid.tv.tv_sec);
684 partnotice.z_uid.tv.tv_usec = htonl((u_long)
685 partnotice.z_uid.tv.tv_usec);
686 (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
689 partnotice.z_message = notice->z_message+offset;
690 message_len = min(notice->z_message_len-offset, fragsize);
691 partnotice.z_message_len = message_len;
693 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
694 &hdrlen, &ptr, NULL);
695 if (retval != ZERR_NONE) {
696 syslog(LOG_WARNING, "xmit unauth refrag raw: %s",
697 error_message(retval));
704 (void) memcpy(ptr, partnotice.z_message, partnotice.z_message_len);
706 buffer_len = hdrlen+partnotice.z_message_len;
708 xmit_frag(&partnotice, buffer, buffer_len, 0);
712 if (!notice->z_message_len)
718 /* End of refrag code */
720 if (retval != ZERR_NONE) {
721 syslog(LOG_ERR, "xmit format: %s", error_message(retval));
723 return; /* DON'T put on nack list */
726 retval = ZSetDestAddr(dest);
727 if (retval != ZERR_NONE) {
728 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
732 retval = ZSendPacket(noticepack, packlen, 0);
733 if (retval != ZERR_NONE) {
734 syslog(LOG_WARNING, "xmit xmit: (%s/%d) %s", inet_ntoa(dest->sin_addr),
735 ntohs(dest->sin_port), error_message(retval));
736 if (retval != EAGAIN && retval != ENOBUFS) {
743 /* now we've sent it, mark it as not ack'ed */
745 nacked = (Unacked *) malloc(sizeof(Unacked));
747 /* no space: just punt */
748 syslog(LOG_WARNING, "xmit nack malloc");
753 nacked->client = client;
754 nacked->rexmits = (sendfail) ? -1 : 0;
755 nacked->packet = noticepack;
756 nacked->dest.addr = *dest;
757 nacked->packsz = packlen;
758 nacked->uid = notice->z_uid;
759 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
760 Unacked_insert(&nacktab[NACKTAB_HASHVAL(*dest, nacked->uid)], nacked);
764 * Retransmit the packet specified. If we have timed out or retransmitted
765 * too many times, punt the packet and initiate the host recovery algorithm
766 * Else, increment the count and re-send the notice packet.
772 Unacked *nacked = (Unacked *) arg;
775 syslog(LOG_DEBUG, "rexmit %s/%d #%d time %d",
776 inet_ntoa(nacked->dest.addr.sin_addr),
777 ntohs(nacked->dest.addr.sin_port), nacked->rexmits + 1, (int)NOW);
780 if (rexmit_times[nacked->rexmits] == -1) {
782 || NOW - nacked->client->last_ack >= CLIENT_GIVEUP_MIN) {
783 /* The client (if there was one) has been unresponsive.
784 * Give up sending this packet, and kill the client if
785 * there was one. (Make sure to remove nacked from the
786 * nack list before calling client_deregister(), which
787 * scans the nack list.)
789 Unacked_delete(nacked);
790 if (nacked->client) {
791 server_kill_clt(nacked->client);
792 client_deregister(nacked->client, 1);
794 free(nacked->packet);
798 /* The client has sent us an ack recently. Retry with the maximum
799 * retransmit time. */
804 /* retransmit the packet */
805 retval = ZSetDestAddr(&nacked->dest.addr);
806 if (retval != ZERR_NONE) {
807 syslog(LOG_WARNING, "rexmit set addr: %s", error_message(retval));
809 retval = ZSendPacket(nacked->packet, nacked->packsz, 0);
810 if (retval != ZERR_NONE)
811 syslog(LOG_WARNING, "rexmit xmit: %s", error_message(retval));
812 if (retval == EAGAIN || retval == ENOBUFS)
816 /* reset the timer */
817 nacked->timer = timer_set_rel(rexmit_times[nacked->rexmits], rexmit,
823 * Send an acknowledgement to the sending client, by sending back the
824 * header from the original notice with the z_kind field changed to either
825 * SERVACK or SERVNAK, and the contents of the message either SENT or
826 * NOT_SENT, depending on the value of the sent argument.
830 clt_ack(ZNotice_t *notice,
831 struct sockaddr_in *who,
840 if (bdumping) { /* don't ack while dumping */
841 zdbug((LOG_DEBUG,"bdumping, no ack"));
847 acknotice.z_kind = SERVACK;
850 acknotice.z_message = ZSRVACK_SENT;
854 acknotice.z_message = ZSRVACK_FAIL;
855 acknotice.z_kind = SERVNAK;
859 acknotice.z_kind = SERVNAK;
860 acknotice.z_message = ZSRVACK_NOTSENT;
861 sent_name = "nak/not_sent";
864 acknotice.z_message = ZSRVACK_NOTSENT;
865 sent_name = "not_sent";
871 acknotice.z_multinotice = "";
873 /* leave room for the trailing null */
874 acknotice.z_message_len = strlen(acknotice.z_message) + 1;
876 packlen = sizeof(ackpack);
878 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
880 if (retval == ZERR_HEADERLEN) {
881 /* Since an ack header can be larger than a message header... (crock) */
882 acknotice.z_opcode = "";
883 acknotice.z_class = "";
884 acknotice.z_class_inst = "";
885 acknotice.z_opcode = "";
886 acknotice.z_default_format = "";
888 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
891 if (retval != ZERR_NONE) {
892 syslog(LOG_ERR, "clt_ack format: %s", error_message(retval));
895 retval = ZSetDestAddr(who);
896 if (retval != ZERR_NONE) {
897 syslog(LOG_WARNING, "clt_ack set addr: %s", error_message(retval));
900 retval = ZSendPacket(ackpack, packlen, 0);
901 if (retval != ZERR_NONE) {
902 syslog(LOG_WARNING, "clt_ack xmit: %s", error_message(retval));
905 zdbug((LOG_DEBUG, "packet sent"));
911 * An ack has arrived.
912 * remove the packet matching this notice from the not-yet-acked queue
916 nack_cancel(ZNotice_t *notice,
917 struct sockaddr_in *who)
922 /* search the not-yet-acked table for this packet, and flush it. */
923 hashval = NACKTAB_HASHVAL(*who, notice->z_uid);
924 for (nacked = nacktab[hashval]; nacked; nacked = nacked->next) {
925 if (nacked->dest.addr.sin_addr.s_addr == who->sin_addr.s_addr
926 && nacked->dest.addr.sin_port == who->sin_port
927 && ZCompareUID(&nacked->uid, ¬ice->z_uid)) {
929 nacked->client->last_ack = NOW;
930 timer_reset(nacked->timer);
931 free(nacked->packet);
932 Unacked_delete(nacked);
938 zdbug((LOG_DEBUG,"nack_cancel: nack not found %s:%08X,%08X",
939 inet_ntoa (notice->z_uid.zuid_addr),
940 notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));
943 /* for compatibility when sending subscription information to old clients */
945 #define OLD_ZEPHYR_VERSION "ZEPH0.0"
946 #endif /* OLD_COMPAT */
948 /* Dispatch an HM_CTL notice. */
951 hostm_dispatch(ZNotice_t *notice,
953 struct sockaddr_in *who,
956 char *opcode = notice->z_opcode;
957 int i, add = 0, remove = 0;
959 if (notice->z_kind == HMACK) {
962 } else if (notice->z_kind != HMCTL) {
963 clt_ack(notice, who, AUTH_FAILED);
964 } else if (strcmp(opcode, HM_FLUSH) == 0) {
965 client_flush_host(&who->sin_addr);
966 if (server == me_server)
967 server_forward(notice, auth, who);
968 } else if (strcmp(opcode, HM_BOOT) == 0) {
969 client_flush_host(&who->sin_addr);
970 if (server == me_server) {
971 server_forward(notice, auth, who);
975 } else if (strcmp(opcode, HM_ATTACH) == 0) {
976 if (server == me_server) {
977 server_forward(notice, auth, who);
983 } else if (strcmp(opcode, HM_DETACH) == 0) {
986 syslog(LOG_WARNING, "hm_dispatch: unknown opcode %s", opcode);
990 for (i = 0; i < num_hosts; i++) {
991 if (hosts[i].s_addr == who->sin_addr.s_addr)
994 if (i == num_hosts) {
995 if (hosts_size == 0) {
996 hosts = (struct in_addr *) malloc(HOSTS_SIZE_INIT *
997 sizeof(struct in_addr));
1000 hosts_size = HOSTS_SIZE_INIT;
1001 } else if (num_hosts == hosts_size) {
1002 hosts = (struct in_addr *) realloc(hosts, hosts_size * 2 *
1003 sizeof(struct in_addr));
1008 hosts[num_hosts++] = who->sin_addr;
1010 } else if (remove) {
1011 for (i = 0; i < num_hosts; i++) {
1012 if (hosts[i].s_addr == who->sin_addr.s_addr) {
1013 memmove(&hosts[i], &hosts[i + 1], num_hosts - (i + 1));
1023 * Dispatch a ZEPHYR_CTL notice.
1027 control_dispatch(ZNotice_t *notice,
1029 struct sockaddr_in *who,
1032 char *opcode = notice->z_opcode;
1037 struct sockaddr_in newwho;
1040 * ZEPHYR_CTL Opcodes expected are:
1041 * BOOT (inst HM): host has booted; flush data.
1042 * CLIENT_SUBSCRIBE: process with the subscription mananger.
1043 * CLIENT_UNSUBSCRIBE: ""
1044 * CLIENT_CANCELSUB: ""
1047 zdbug((LOG_DEBUG, "ctl_disp: opc=%s", opcode));
1049 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
1050 newwho.sin_port = notice->z_port;
1051 realm = realm_which_realm(&newwho);
1053 return(realm_control_dispatch(notice, auth, who, server, realm));
1055 if (strcasecmp(notice->z_class_inst, ZEPHYR_CTL_HM) == 0) {
1056 return hostm_dispatch(notice, auth, who, server);
1057 } else if (strcmp(opcode, CLIENT_GIMMESUBS) == 0 ||
1058 strcmp(opcode, CLIENT_GIMMEDEFS) == 0) {
1059 /* this special case is before the auth check so that
1060 someone who has no subscriptions does NOT get a SERVNAK
1061 but rather an empty list. Note we must therefore
1062 check authentication inside subscr_sendlist */
1064 /* only acknowledge if *not* old version; the old version
1065 acknowledges the packet with the reply */
1066 if (strcmp(notice->z_version, OLD_ZEPHYR_VERSION) != 0)
1068 #else /* !OLD_COMPAT */
1070 #endif /* OLD_COMPAT */
1071 subscr_sendlist(notice, auth, who);
1074 if (server == me_server)
1075 clt_ack(notice, who, AUTH_FAILED);
1079 wantdefs = strcmp(opcode, CLIENT_SUBSCRIBE_NODEFS);
1080 if (!wantdefs || strcmp(opcode, CLIENT_SUBSCRIBE) == 0) {
1081 /* subscription notice */
1082 retval = client_register(notice, &who->sin_addr, &client, wantdefs);
1083 if (retval != ZERR_NONE) {
1084 syslog(LOG_NOTICE, "subscr %s/%s/%d failed: %s",
1085 notice->z_sender, inet_ntoa(who->sin_addr),
1086 ntohs(notice->z_port), error_message(retval));
1087 if (server == me_server) {
1088 if (retval == ZSRV_BADSUBPORT)
1089 clt_ack(notice, who, AUTH_FAILED);
1095 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1096 /* you may only subscribe for your own clients */
1097 if (server == me_server)
1098 clt_ack(notice, who, AUTH_FAILED);
1102 if (client->session_keyblock) {
1103 krb5_free_keyblock_contents(Z_krb5_ctx, client->session_keyblock);
1104 retval = krb5_copy_keyblock_contents(Z_krb5_ctx, ZGetSession(),
1105 client->session_keyblock);
1107 retval = krb5_copy_keyblock(Z_krb5_ctx, ZGetSession(),
1108 &client->session_keyblock);
1111 syslog(LOG_WARNING, "keyblock copy failed in subscr: %s",
1112 error_message(retval));
1113 if (server == me_server)
1119 /* in case it's changed */
1120 memcpy(client->session_key, ZGetSession(), sizeof(C_Block));
1123 retval = subscr_subscribe(client, notice, server);
1124 if (retval != ZERR_NONE) {
1125 syslog(LOG_WARNING, "subscr failed: %s", error_message(retval));
1126 if (server == me_server)
1130 } else if (strcmp(opcode, CLIENT_UNSUBSCRIBE) == 0) {
1131 client = client_find(&who->sin_addr, notice->z_port);
1132 if (client != NULL) {
1133 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1134 /* you may only cancel for your own clients */
1135 if (server == me_server)
1136 clt_ack(notice, who, AUTH_FAILED);
1139 subscr_cancel(who, notice);
1144 } else if (strcmp(opcode, CLIENT_CANCELSUB) == 0) {
1145 /* canceling subscriptions implies I can punt info about this client */
1146 client = client_find(&who->sin_addr, notice->z_port);
1147 if (client == NULL) {
1148 if (server == me_server)
1152 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1153 /* you may only cancel for your own clients */
1154 if (server == me_server)
1155 clt_ack(notice, who, AUTH_FAILED);
1158 /* don't flush locations here, let him do it explicitly */
1159 client_deregister(client, 0);
1161 syslog(LOG_WARNING, "unknown ctl opcode %s", opcode);
1162 if (server == me_server) {
1163 if (strcmp(notice->z_class_inst, ZEPHYR_CTL_REALM) != 0)
1169 if (server == me_server) {
1171 server_forward(notice, auth, who);
1177 hostm_shutdown(void)
1179 int i, s, newserver;
1180 struct sockaddr_in sin;
1182 for (i = 0; i < nservers; i++) {
1183 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1186 newserver = (i < nservers);
1187 for (i = 0; i < num_hosts; i++) {
1188 sin.sin_addr = hosts[i];
1189 sin.sin_port = hm_port;
1192 s = (random() % (nservers - 1)) + 1;
1193 if (otherservers[s].state == SERV_UP)
1196 hostm_deathgram(&sin, &otherservers[s]);
1198 hostm_deathgram(&sin, NULL);
1204 realm_shutdown(void)
1206 int i, s, newserver;
1208 for (i = 0; i < nservers; i++) {
1209 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1212 zdbug((LOG_DEBUG, "rlm_shutdown"));
1214 newserver = (i < nservers);
1217 s = (random() % (nservers - 1)) + 1;
1218 if (otherservers[s].state == SERV_UP)
1221 realm_deathgram(&otherservers[s]);
1223 realm_deathgram(NULL);
1228 hostm_deathgram(struct sockaddr_in *sin,
1233 ZNotice_t shutnotice;
1236 memset (&shutnotice, 0, sizeof(shutnotice));
1238 shutnotice.z_kind = HMCTL;
1239 shutnotice.z_port = sin->sin_port; /* we are sending it */
1240 shutnotice.z_class = HM_CTL_CLASS;
1241 shutnotice.z_class_inst = HM_CTL_SERVER;
1242 shutnotice.z_opcode = SERVER_SHUTDOWN;
1243 shutnotice.z_sender = HM_CTL_SERVER;
1244 shutnotice.z_recipient = hm_recipient();
1245 shutnotice.z_default_format = "";
1246 shutnotice.z_num_other_fields = 0;
1247 shutnotice.z_message = (server) ? server->addr_str : NULL;
1248 shutnotice.z_message_len = (server) ? strlen(server->addr_str) + 1 : 0;
1250 retval = ZFormatNotice(&shutnotice, &shutpack, &shutlen, ZNOAUTH);
1251 if (retval != ZERR_NONE) {
1252 syslog(LOG_ERR, "hm_shut format: %s",error_message(retval));
1255 retval = ZSetDestAddr(sin);
1256 if (retval != ZERR_NONE) {
1257 syslog(LOG_WARNING, "hm_shut set addr: %s", error_message(retval));
1261 retval = ZSendPacket(shutpack, shutlen, 0);
1262 if (retval != ZERR_NONE)
1263 syslog(LOG_WARNING, "hm_shut xmit: %s", error_message(retval));
1270 static char *recipient;
1276 realm = ZGetRealm();
1279 recipient = (char *) malloc(strlen(realm) + 4);
1280 strcpy (recipient, "hm@");
1281 strcat (recipient, realm);