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 ZCONST 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;
101 syslog(LOG_INFO, "stats: %s: %d", hm_packets.str, hm_packets.val);
102 syslog(LOG_INFO, "stats: %s: %d", control_notices.str,
103 control_notices.val);
104 syslog(LOG_INFO, "stats: %s: %d", message_notices.str,
105 message_notices.val);
106 syslog(LOG_INFO, "stats: %s: %d", login_notices.str, login_notices.val);
107 syslog(LOG_INFO, "stats: %s: %d", locate_notices.str, locate_notices.val);
108 syslog(LOG_INFO, "stats: %s: %d", admin_notices.str, admin_notices.val);
109 syslog(LOG_INFO, "stats: %s: %d", realm_notices.str, realm_notices.val);
110 syslog(LOG_INFO, "stats: %s: %d", interserver_notices.str,
111 interserver_notices.val);
112 syslog(LOG_INFO, "stats: %s: %d", i_s_ctls.str, i_s_ctls.val);
113 syslog(LOG_INFO, "stats: %s: %d", i_s_logins.str, i_s_logins.val);
114 syslog(LOG_INFO, "stats: %s: %d", i_s_admins.str, i_s_admins.val);
115 syslog(LOG_INFO, "stats: %s: %d", i_s_locates.str, i_s_locates.val);
117 /* log stuff once an hour */
118 timer_set_rel ((long) 6*60*60, dump_stats, arg);
122 * Handle an input packet.
123 * Warning: this function may be called from within a brain dump.
130 ZPacket_t input_packet; /* from the network */
131 ZNotice_t new_notice; /* parsed from input_packet */
132 int input_len; /* len of packet */
133 struct sockaddr_in input_sin; /* Zconstructed for authent */
134 struct sockaddr_in whoisit; /* for holding peer's address */
135 int authentic; /* authentic flag */
136 Pending *pending; /* pending packet */
137 int from_server; /* packet is from another server */
138 ZRealm *realm; /* foreign realm ptr */
140 static int first_time = 1;
144 /* Dump statistics five minutes after startup */
147 timer_set_rel(5*60, dump_stats, NULL);
152 if (otherservers[me_server_idx].queue) {
153 /* something here for me; take care of it */
155 zdbug((LOG_DEBUG, "internal queue process"));
158 pending = server_dequeue(me_server);
160 status = ZParseNotice(pending->packet, pending->len, &new_notice);
161 if (status != ZERR_NONE) {
162 syslog(LOG_ERR, "bad notice parse (%s): %s",
163 inet_ntoa(pending->who.sin_addr), error_message(status));
165 dispatch(&new_notice, pending->auth, &pending->who, 1);
167 server_pending_free(pending);
172 * nothing in internal queue, go to the external library
175 status = ZReceivePacket(input_packet, &input_len, &whoisit);
176 if (status != ZERR_NONE) {
177 syslog(LOG_ERR, "bad packet receive: %s from %s",
178 error_message(status), inet_ntoa(whoisit.sin_addr));
182 status = ZParseNotice(input_packet, input_len, &new_notice);
183 if (status != ZERR_NONE) {
184 syslog(LOG_ERR, "bad notice parse (%s): %s",
185 inet_ntoa(whoisit.sin_addr), error_message(status));
188 if (server_which_server(&whoisit)) {
189 /* we need to parse twice--once to get
190 the source addr, second to check
192 memset(&input_sin, 0, sizeof(input_sin));
193 input_sin.sin_addr.s_addr = new_notice.z_sender_addr.s_addr;
194 input_sin.sin_port = new_notice.z_port;
195 input_sin.sin_family = AF_INET;
196 /* Should check to see if packet is from another realm's server,
198 /* Clients don't check auth of acks, nor do we make it so they
199 can in general, so this is safe. */
200 if (new_notice.z_kind == SERVACK || new_notice.z_kind == SERVNAK) {
201 authentic = ZAUTH_YES;
203 if (realm = realm_which_realm(&input_sin)) {
204 authentic = ZCheckRealmAuthentication(&new_notice,
208 authentic = ZCheckAuthentication(&new_notice, &input_sin);
213 /* Clients don't check auth of acks, nor do we make it so they
214 can in general, so this is safe. */
215 if (new_notice.z_kind == SERVACK || new_notice.z_kind == SERVNAK) {
216 authentic = ZAUTH_YES;
218 if (realm = realm_which_realm(&whoisit)) {
219 authentic = ZCheckRealmAuthentication(&new_notice,
223 authentic = ZCheckAuthentication(&new_notice, &whoisit);
228 if (whoisit.sin_port != hm_port && whoisit.sin_port != hm_srv_port &&
229 strcasecmp(new_notice.z_class, ZEPHYR_ADMIN_CLASS) != 0 &&
230 whoisit.sin_port != srv_addr.sin_port &&
231 new_notice.z_kind != CLIENTACK) {
232 syslog(LOG_ERR, "bad port %s/%d", inet_ntoa(whoisit.sin_addr),
233 ntohs(whoisit.sin_port));
238 message_notices.val++;
239 dispatch(&new_notice, authentic, &whoisit, from_server);
247 dispatch(notice, auth, who, from_server)
250 struct sockaddr_in *who;
254 String *notice_class;
255 struct sockaddr_in who2;
260 char dbg_buf[BUFSIZ];
263 authflag = (auth == ZAUTH_YES);
265 if ((int) notice->z_kind < (int) UNSAFE ||
266 (int) notice->z_kind > (int) CLIENTACK) {
267 syslog(LOG_NOTICE, "bad notice kind 0x%x from %s", notice->z_kind,
268 inet_ntoa(who->sin_addr));
274 "disp:%s '%s' '%s' '%s' notice to '%s' from '%s' %s/%d/%d",
275 ZNoticeKinds[(int) notice->z_kind], notice->z_class,
276 notice->z_class_inst, notice->z_opcode, notice->z_recipient,
277 notice->z_sender, inet_ntoa(who->sin_addr),
278 ntohs(who->sin_port), ntohs(notice->z_port));
282 if (notice->z_kind == CLIENTACK) {
283 nack_cancel(notice, who);
289 if (0 && from_server) {
290 /* incorporate server_dispatch here */
293 notice_class = make_string(notice->z_class,1);
296 interserver_notices.val++;
297 status = server_dispatch(notice, authflag, who);
298 } else if (class_is_hm(notice_class)) {
300 status = hostm_dispatch(notice, authflag, who, me_server);
301 } else if (realm_which_realm(who) && !(class_is_admin(notice_class))) {
303 status = realm_dispatch(notice, authflag, who, me_server);
304 } else if (class_is_control(notice_class)) {
305 control_notices.val++;
306 status = control_dispatch(notice, authflag, who, me_server);
307 } else if (class_is_ulogin(notice_class)) {
309 status = ulogin_dispatch(notice, authflag, who, me_server);
310 } else if (class_is_ulocate(notice_class)) {
311 locate_notices.val++;
312 status = ulocate_dispatch(notice, authflag, who, me_server);
313 } else if (class_is_admin(notice_class)) {
315 status = server_adispatch(notice, authflag, who, me_server);
317 if (!realm_bound_for_realm(ZGetRealm(), notice->z_recipient)) {
318 cp = strchr(notice->z_recipient, '@');
320 !(realm = realm_get_realm_by_name(cp + 1))) {
321 /* Foreign user, local realm */
322 sendit(notice, authflag, who, 0);
324 realm_handoff(notice, authflag, who, realm, 1);
326 if (notice->z_recipient[0] == '@')
327 notice->z_recipient = "";
328 sendit(notice, authflag, who, 1);
330 free_string(notice_class);
334 if (status == ZSRV_REQUEUE)
335 server_self_queue(notice, authflag, who);
336 free_string(notice_class);
340 * Send a notice off to those clients who have subscribed to it.
344 sendit(notice, auth, who, external)
347 struct sockaddr_in *who;
350 static int send_counter = 0;
351 char recipbuf[ANAME_SZ + INST_SZ + REALM_SZ + 3], *recipp;
357 class = make_string(notice->z_class, 1);
358 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient)) {
361 acl = class_get_acl(class);
363 /* if controlled and not auth, fail */
365 syslog(LOG_WARNING, "sendit unauthentic %s from %s",
366 notice->z_class, notice->z_sender);
367 clt_ack(notice, who, AUTH_FAILED);
371 /* if from foreign realm server, disallow if not realm of sender */
372 rlm = realm_which_realm(who);
374 if (!realm_sender_in_realm(rlm->name, notice->z_sender)) {
375 syslog(LOG_WARNING, "sendit auth not verifiable %s (%s) from %s",
376 notice->z_class, rlm->name, notice->z_sender);
377 clt_ack(notice, who, AUTH_FAILED);
382 /* if not auth to transmit, fail */
383 if (!access_check(notice->z_sender, acl, TRANSMIT)) {
384 syslog(LOG_WARNING, "sendit unauthorized %s from %s",
385 notice->z_class, notice->z_sender);
386 clt_ack(notice, who, AUTH_FAILED);
390 /* sender != inst and not auth to send to others --> fail */
391 if (strcmp(notice->z_sender, notice->z_class_inst) != 0 &&
392 !access_check(notice->z_sender, acl, INSTUID)) {
393 syslog(LOG_WARNING, "sendit unauth uid %s %s.%s", notice->z_sender,
394 notice->z_class, notice->z_class_inst);
395 clt_ack(notice, who, AUTH_FAILED);
401 if (!realm_which_realm(who)) {
402 if (memcmp(¬ice->z_sender_addr.s_addr, &who->sin_addr.s_addr,
403 sizeof(notice->z_sender_addr.s_addr))) {
404 /* someone is playing games... */
405 /* inet_ntoa returns pointer to static area */
406 /* max size is 255.255.255.255 */
408 strcpy(buffer, inet_ntoa(who->sin_addr));
411 "sendit unauthentic fake packet: claimed %s, real %s",
412 inet_ntoa(notice->z_sender_addr), buffer);
413 clt_ack(notice, who, AUTH_FAILED);
417 if (ntohl(notice->z_sender_addr.s_addr) != 0) {
419 "sendit invalid address: claimed %s, real %s",
420 inet_ntoa(notice->z_sender_addr), buffer);
421 clt_ack(notice, who, AUTH_FAILED);
425 syslog(LOG_WARNING, "sendit addr mismatch: claimed %s, real %s",
426 inet_ntoa(notice->z_sender_addr), buffer);
430 /* Increment the send counter, used to prevent duplicate sends to
431 * clients. On the off-chance that we wrap around to 0, skip over
432 * it to prevent missing clients which have never had a packet
435 if (send_counter == 0)
438 /* Send to clients subscribed to the triplet itself. */
439 dest.classname = class;
440 dest.inst = make_string(notice->z_class_inst, 1);
441 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient) &&
442 *notice->z_recipient == '@')
443 dest.recip = make_string("", 0);
445 strncpy(recipbuf, notice->z_recipient, sizeof(recipbuf));
446 recipp = strrchr(recipbuf, '@');
448 sprintf(recipp + 1, "%s", realm_expand_realm(recipp + 1));
449 dest.recip = make_string(recipbuf, 0);
452 if (send_to_dest(notice, auth, &dest, send_counter, external))
455 /* Send to clients subscribed to the triplet with the instance
456 * substituted with the wildcard instance. */
457 free_string(dest.inst);
458 dest.inst = wildcard_instance;
459 if (send_to_dest(notice, auth, &dest, send_counter, external))
463 free_string(dest.recip);
471 * Send to each client in the list. Avoid duplicates by setting
472 * last_send on each client to send_counter, a nonce which is updated
477 send_to_dest(notice, auth, dest, send_counter, external)
487 clientp = triplet_lookup(dest);
491 for (; *clientp; clientp++) {
492 if ((*clientp)->last_send == send_counter)
494 (*clientp)->last_send = send_counter;
495 if ((*clientp)->realm) {
497 realm_handoff(notice, auth, &clientp[0]->addr, clientp[0]->realm,
502 xmit(notice, &((*clientp)->addr), auth, *clientp);
511 * Release anything destined for the client in the not-yet-acked table.
519 Unacked *nacked, *next;
521 for (i = 0; i < NACKTAB_HASHSIZE; i++) {
522 for (nacked = nacktab[i]; nacked; nacked = next) {
524 if (nacked->client == client) {
525 timer_reset(nacked->timer);
527 free(nacked->packet);
535 * Send one packet of a fragmented message to a client. After transmitting,
536 * put it onto the not ack'ed list.
539 /* the arguments must be the same as the arguments to Z_XmitFragment */
542 xmit_frag(notice, buf, len, waitforack)
548 struct sockaddr_in sin;
552 int hashval, sendfail = 0;
554 retval = ZSendPacket(buf, len, 0);
555 if (retval != ZERR_NONE) {
556 syslog(LOG_WARNING, "xmit_frag send: %s", error_message(retval));
557 if (retval != EAGAIN && retval != ENOBUFS)
562 /* now we've sent it, mark it as not ack'ed */
563 nacked = (Unacked *) malloc(sizeof(Unacked));
565 /* no space: just punt */
566 syslog(LOG_WARNING, "xmit_frag nack malloc");
570 savebuf = (char *) malloc(len);
572 /* no space: just punt */
573 syslog(LOG_WARNING, "xmit_frag pack malloc");
578 memcpy(savebuf, buf, len);
580 sin = ZGetDestAddr();
581 nacked->client = NULL;
582 nacked->rexmits = (sendfail) ? -1 : 0;
583 nacked->packet = savebuf;
584 nacked->dest.addr = sin;
585 nacked->packsz = len;
586 nacked->uid = notice->z_uid;
587 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
588 LIST_INSERT(&nacktab[NACKTAB_HASHVAL(sin, nacked->uid)], nacked);
594 * Send the notice to the client. After transmitting, put it onto the
599 xmit(notice, dest, auth, client)
601 struct sockaddr_in *dest;
607 int packlen, sendfail = 0;
611 zdbug((LOG_DEBUG,"xmit"));
614 noticepack = (char *) malloc(sizeof(ZPacket_t));
616 syslog(LOG_ERR, "xmit malloc");
617 return; /* DON'T put on nack list */
620 packlen = sizeof(ZPacket_t);
622 if (auth && client) { /*
623 we are distributing authentic and
624 we have a pointer to auth info
627 retval = ZFormatAuthenticNoticeV5(notice, noticepack, packlen,
628 &packlen, client->session_keyblock);
629 if (retval != ZERR_NONE) {
630 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
635 #if defined(HAVE_KRB4)
636 retval = ZFormatAuthenticNotice(notice, noticepack, packlen,
637 &packlen, client->session_key);
638 if (retval != ZERR_NONE) {
639 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
643 #else /* !HAVE_KRB4 */
645 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
646 if (retval != ZERR_NONE) {
647 syslog(LOG_ERR, "xmit auth/raw format: %s", error_message(retval));
651 #endif /* HAVE_KRB4 */
652 #endif /* HAVE_KRB5 */
655 notice->z_authent_len = 0;
656 notice->z_ascii_authent = (char *)"";
657 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
658 /* This code is needed because a Zephyr can "grow" when a remote
659 * realm name is inserted into the Zephyr before being resent out
660 * locally. It essentially matches the code in realm.c to do the
661 * same thing with authentic Zephyrs.
663 if (retval == ZERR_PKTLEN) {
664 ZNotice_t partnotice, newnotice;
667 int buffer_len, hdrlen, offset, fragsize, ret_len, message_len;
668 int origoffset, origlen;
672 retval = ZSetDestAddr(dest);
673 if (retval != ZERR_NONE) {
674 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
678 partnotice = *notice;
680 partnotice.z_auth = 0;
681 partnotice.z_authent_len = 0;
682 partnotice.z_ascii_authent = (char *)"";
684 origoffset = offset = fragsize = 0;
685 origlen = notice->z_message_len;
687 buffer = (char *) malloc(sizeof(ZPacket_t));
689 syslog(LOG_ERR, "xmit unauth refrag malloc");
690 return; /* DON'T put on nack list */
692 buffer_len = sizeof(ZPacket_t);
694 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
695 &hdrlen, NULL, NULL);
696 if (retval != ZERR_NONE) {
697 syslog(LOG_ERR, "xmit unauth refrag fmt: failed");
702 if (notice->z_multinotice && strcmp(notice->z_multinotice, ""))
703 if (sscanf(notice->z_multinotice, "%d/%d", &origoffset, &origlen)
706 syslog(LOG_WARNING, "xmit unauth refrag: parse failed");
711 fragsize = Z_MAXPKTLEN-hdrlen-Z_FRAGFUDGE;
713 while (offset < notice->z_message_len || !notice->z_message_len) {
714 (void) sprintf(multi, "%d/%d", offset+origoffset, origlen);
715 partnotice.z_multinotice = multi;
717 (void) gettimeofday(&partnotice.z_uid.tv, (struct timezone *)0);
718 partnotice.z_uid.tv.tv_sec = htonl((u_long)
719 partnotice.z_uid.tv.tv_sec);
720 partnotice.z_uid.tv.tv_usec = htonl((u_long)
721 partnotice.z_uid.tv.tv_usec);
722 (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
725 partnotice.z_message = notice->z_message+offset;
726 message_len = min(notice->z_message_len-offset, fragsize);
727 partnotice.z_message_len = message_len;
729 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
730 &hdrlen, &ptr, NULL);
731 if (retval != ZERR_NONE) {
732 syslog(LOG_WARNING, "xmit unauth refrag raw: %s",
733 error_message(retval));
740 (void) memcpy(ptr, partnotice.z_message, partnotice.z_message_len);
742 buffer_len = hdrlen+partnotice.z_message_len;
744 xmit_frag(&partnotice, buffer, buffer_len, 0);
748 if (!notice->z_message_len)
754 /* End of refrag code */
756 if (retval != ZERR_NONE) {
757 syslog(LOG_ERR, "xmit format: %s", error_message(retval));
759 return; /* DON'T put on nack list */
763 zdbug((LOG_DEBUG," to %s/%d", inet_ntoa(dest->sin_addr),
764 ntohs(dest->sin_port)));
766 retval = ZSetDestAddr(dest);
767 if (retval != ZERR_NONE) {
768 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
772 retval = ZSendPacket(noticepack, packlen, 0);
773 if (retval != ZERR_NONE) {
774 syslog(LOG_WARNING, "xmit xmit: (%s/%d) %s", inet_ntoa(dest->sin_addr),
775 ntohs(dest->sin_port), error_message(retval));
776 if (retval != EAGAIN && retval != ENOBUFS) {
783 /* now we've sent it, mark it as not ack'ed */
785 nacked = (Unacked *) malloc(sizeof(Unacked));
787 /* no space: just punt */
788 syslog(LOG_WARNING, "xmit nack malloc");
793 nacked->client = client;
794 nacked->rexmits = (sendfail) ? -1 : 0;
795 nacked->packet = noticepack;
796 nacked->dest.addr = *dest;
797 nacked->packsz = packlen;
798 nacked->uid = notice->z_uid;
799 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
800 LIST_INSERT(&nacktab[NACKTAB_HASHVAL(*dest, nacked->uid)], nacked);
804 * Retransmit the packet specified. If we have timed out or retransmitted
805 * too many times, punt the packet and initiate the host recovery algorithm
806 * Else, increment the count and re-send the notice packet.
813 Unacked *nacked = (Unacked *) arg;
817 syslog(LOG_DEBUG, "rexmit %s/%d #%d time %d",
818 inet_ntoa(nacked->dest.addr.sin_addr),
819 ntohs(nacked->dest.addr.sin_port), nacked->rexmits + 1, NOW);
823 if (rexmit_times[nacked->rexmits] == -1) {
825 || NOW - nacked->client->last_ack >= CLIENT_GIVEUP_MIN) {
826 /* The client (if there was one) has been unresponsive.
827 * Give up sending this packet, and kill the client if
828 * there was one. (Make sure to remove nacked from the
829 * nack list before calling client_deregister(), which
830 * scans the nack list.)
833 if (nacked->client) {
834 server_kill_clt(nacked->client);
835 client_deregister(nacked->client, 1);
837 free(nacked->packet);
841 /* The client has sent us an ack recently. Retry with the maximum
842 * retransmit time. */
847 /* retransmit the packet */
849 zdbug((LOG_DEBUG," to %s/%d", inet_ntoa(nacked->dest.addr.sin_addr),
850 ntohs(nacked->dest.addr.sin_port)));
852 retval = ZSetDestAddr(&nacked->dest.addr);
853 if (retval != ZERR_NONE) {
854 syslog(LOG_WARNING, "rexmit set addr: %s", error_message(retval));
856 retval = ZSendPacket(nacked->packet, nacked->packsz, 0);
857 if (retval != ZERR_NONE)
858 syslog(LOG_WARNING, "rexmit xmit: %s", error_message(retval));
859 if (retval == EAGAIN || retval == ENOBUFS)
863 /* reset the timer */
864 nacked->timer = timer_set_rel(rexmit_times[nacked->rexmits], rexmit,
870 * Send an acknowledgement to the sending client, by sending back the
871 * header from the original notice with the z_kind field changed to either
872 * SERVACK or SERVNAK, and the contents of the message either SENT or
873 * NOT_SENT, depending on the value of the sent argument.
877 clt_ack(notice, who, sent)
879 struct sockaddr_in *who;
889 if (bdumping) { /* don't ack while dumping */
891 zdbug((LOG_DEBUG,"bdumping, no ack"));
898 acknotice.z_kind = SERVACK;
901 acknotice.z_message = ZSRVACK_SENT;
905 acknotice.z_message = ZSRVACK_FAIL;
906 acknotice.z_kind = SERVNAK;
910 acknotice.z_kind = SERVNAK;
911 acknotice.z_message = ZSRVACK_NOTSENT;
912 sent_name = "nak/not_sent";
915 acknotice.z_message = ZSRVACK_NOTSENT;
916 sent_name = "not_sent";
923 zdbug((LOG_DEBUG,"clt_ack type %s for %d to %s/%d", sent_name,
924 ntohs(notice->z_port), inet_ntoa(who->sin_addr),
925 ntohs(who->sin_port)));
928 acknotice.z_multinotice = "";
930 /* leave room for the trailing null */
931 acknotice.z_message_len = strlen(acknotice.z_message) + 1;
933 packlen = sizeof(ackpack);
935 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
937 if (retval == ZERR_HEADERLEN) {
938 /* Since an ack header can be larger than a message header... (crock) */
939 acknotice.z_opcode = "";
940 acknotice.z_class = "";
941 acknotice.z_class_inst = "";
942 acknotice.z_opcode = "";
943 acknotice.z_default_format = "";
945 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
948 if (retval != ZERR_NONE) {
949 syslog(LOG_ERR, "clt_ack format: %s", error_message(retval));
952 retval = ZSetDestAddr(who);
953 if (retval != ZERR_NONE) {
954 syslog(LOG_WARNING, "clt_ack set addr: %s", error_message(retval));
957 retval = ZSendPacket(ackpack, packlen, 0);
958 if (retval != ZERR_NONE) {
959 syslog(LOG_WARNING, "clt_ack xmit: %s", error_message(retval));
962 zdbug((LOG_DEBUG, "packet sent"));
968 * An ack has arrived.
969 * remove the packet matching this notice from the not-yet-acked queue
973 nack_cancel(notice, who)
975 struct sockaddr_in *who;
980 /* search the not-yet-acked table for this packet, and flush it. */
982 zdbug((LOG_DEBUG, "nack_cancel: %s:%08X,%08X",
983 inet_ntoa(notice->z_uid.zuid_addr),
984 notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));
986 hashval = NACKTAB_HASHVAL(*who, notice->z_uid);
987 for (nacked = nacktab[hashval]; nacked; nacked = nacked->next) {
988 if (nacked->dest.addr.sin_addr.s_addr == who->sin_addr.s_addr
989 && nacked->dest.addr.sin_port == who->sin_port
990 && ZCompareUID(&nacked->uid, ¬ice->z_uid)) {
992 nacked->client->last_ack = NOW;
993 timer_reset(nacked->timer);
994 free(nacked->packet);
1002 zdbug((LOG_DEBUG,"nack_cancel: nack not found %s:%08X,%08X",
1003 inet_ntoa (notice->z_uid.zuid_addr),
1004 notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));
1008 /* for compatibility when sending subscription information to old clients */
1010 #define OLD_ZEPHYR_VERSION "ZEPH0.0"
1011 #endif /* OLD_COMPAT */
1013 /* Dispatch an HM_CTL notice. */
1016 hostm_dispatch(notice, auth, who, server)
1019 struct sockaddr_in *who;
1023 char *opcode = notice->z_opcode;
1025 int i, add = 0, remove = 0;
1028 zdbug((LOG_DEBUG,"hm_disp"));
1031 if (notice->z_kind == HMACK) {
1034 } else if (notice->z_kind != HMCTL) {
1036 zdbug((LOG_DEBUG, "bogus HM packet"));
1038 clt_ack(notice, who, AUTH_FAILED);
1039 } else if (strcmp(opcode, HM_FLUSH) == 0) {
1040 client_flush_host(&who->sin_addr);
1041 if (server == me_server)
1042 server_forward(notice, auth, who);
1043 } else if (strcmp(opcode, HM_BOOT) == 0) {
1044 client_flush_host(&who->sin_addr);
1045 if (server == me_server) {
1046 server_forward(notice, auth, who);
1050 } else if (strcmp(opcode, HM_ATTACH) == 0) {
1051 if (server == me_server) {
1052 server_forward(notice, auth, who);
1058 } else if (strcmp(opcode, HM_DETACH) == 0) {
1061 syslog(LOG_WARNING, "hm_dispatch: unknown opcode %s", opcode);
1065 for (i = 0; i < num_hosts; i++) {
1066 if (hosts[i].s_addr == who->sin_addr.s_addr)
1069 if (i == num_hosts) {
1070 if (hosts_size == 0) {
1071 hosts = (struct in_addr *) malloc(HOSTS_SIZE_INIT *
1072 sizeof(struct in_addr));
1075 hosts_size = HOSTS_SIZE_INIT;
1076 } else if (num_hosts == hosts_size) {
1077 hosts = (struct in_addr *) realloc(hosts, hosts_size * 2 *
1078 sizeof(struct in_addr));
1083 hosts[num_hosts++] = who->sin_addr;
1085 } else if (remove) {
1086 for (i = 0; i < num_hosts; i++) {
1087 if (hosts[i].s_addr == who->sin_addr.s_addr) {
1088 memmove(&hosts[i], &hosts[i + 1], num_hosts - (i + 1));
1098 * Dispatch a ZEPHYR_CTL notice.
1102 control_dispatch(notice, auth, who, server)
1105 struct sockaddr_in *who;
1108 char *opcode = notice->z_opcode;
1113 struct sockaddr_in newwho;
1116 * ZEPHYR_CTL Opcodes expected are:
1117 * BOOT (inst HM): host has booted; flush data.
1118 * CLIENT_SUBSCRIBE: process with the subscription mananger.
1119 * CLIENT_UNSUBSCRIBE: ""
1120 * CLIENT_CANCELSUB: ""
1123 zdbug((LOG_DEBUG, "ctl_disp: opc=%s", opcode));
1125 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
1126 newwho.sin_port = notice->z_port;
1127 realm = realm_which_realm(&newwho);
1129 return(realm_control_dispatch(notice, auth, who, server, realm));
1131 if (strcasecmp(notice->z_class_inst, ZEPHYR_CTL_HM) == 0) {
1132 return hostm_dispatch(notice, auth, who, server);
1133 } else if (strcmp(opcode, CLIENT_GIMMESUBS) == 0 ||
1134 strcmp(opcode, CLIENT_GIMMEDEFS) == 0) {
1135 /* this special case is before the auth check so that
1136 someone who has no subscriptions does NOT get a SERVNAK
1137 but rather an empty list. Note we must therefore
1138 check authentication inside subscr_sendlist */
1140 /* only acknowledge if *not* old version; the old version
1141 acknowledges the packet with the reply */
1142 if (strcmp(notice->z_version, OLD_ZEPHYR_VERSION) != 0)
1144 #else /* !OLD_COMPAT */
1146 #endif /* OLD_COMPAT */
1147 subscr_sendlist(notice, auth, who);
1151 zdbug((LOG_DEBUG,"unauth ctrl_disp"));
1153 if (server == me_server)
1154 clt_ack(notice, who, AUTH_FAILED);
1158 wantdefs = strcmp(opcode, CLIENT_SUBSCRIBE_NODEFS);
1159 if (!wantdefs || strcmp(opcode, CLIENT_SUBSCRIBE) == 0) {
1160 /* subscription notice */
1161 retval = client_register(notice, &who->sin_addr, &client, wantdefs);
1162 if (retval != ZERR_NONE) {
1163 syslog(LOG_NOTICE, "subscr %s/%s/%d failed: %s",
1164 notice->z_sender, inet_ntoa(who->sin_addr),
1165 ntohs(notice->z_port), error_message(retval));
1166 if (server == me_server) {
1167 if (retval == ZSRV_BADSUBPORT)
1168 clt_ack(notice, who, AUTH_FAILED);
1174 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1175 /* you may only subscribe for your own clients */
1176 if (server == me_server)
1177 clt_ack(notice, who, AUTH_FAILED);
1181 if (client->session_keyblock) {
1182 krb5_free_keyblock_contents(Z_krb5_ctx, client->session_keyblock);
1183 retval = krb5_copy_keyblock_contents(Z_krb5_ctx, ZGetSession(),
1184 client->session_keyblock);
1186 retval = krb5_copy_keyblock(Z_krb5_ctx, ZGetSession(),
1187 &client->session_keyblock);
1190 syslog(LOG_WARNING, "keyblock copy failed in subscr: %s",
1191 error_message(retval));
1192 if (server == me_server)
1198 /* in case it's changed */
1199 memcpy(client->session_key, ZGetSession(), sizeof(C_Block));
1202 retval = subscr_subscribe(client, notice, server);
1203 if (retval != ZERR_NONE) {
1204 syslog(LOG_WARNING, "subscr failed: %s", error_message(retval));
1205 if (server == me_server)
1209 } else if (strcmp(opcode, CLIENT_UNSUBSCRIBE) == 0) {
1210 client = client_find(&who->sin_addr, notice->z_port);
1211 if (client != NULL) {
1212 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1213 /* you may only cancel for your own clients */
1214 if (server == me_server)
1215 clt_ack(notice, who, AUTH_FAILED);
1220 if (server == me_server) {
1221 syslog(LOG_DEBUG, "subscription cancel for %s/%d\n",
1222 inet_ntoa(who->sin_addr), ntohs(who->sin_port));
1225 "subscription cancel for %s/%d from %s\n",
1226 inet_ntoa(who->sin_addr), ntohs(who->sin_port),
1231 subscr_cancel(who, notice);
1236 } else if (strcmp(opcode, CLIENT_CANCELSUB) == 0) {
1237 /* canceling subscriptions implies I can punt info about this client */
1238 client = client_find(&who->sin_addr, notice->z_port);
1239 if (client == NULL) {
1241 zdbug((LOG_DEBUG,"can_sub not found client"));
1243 if (server == me_server)
1247 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1248 /* you may only cancel for your own clients */
1249 if (server == me_server)
1250 clt_ack(notice, who, AUTH_FAILED);
1253 /* don't flush locations here, let him do it explicitly */
1255 zdbug((LOG_DEBUG, "cancelsub clt_dereg %s/%d",
1256 inet_ntoa(who->sin_addr), ntohs(who->sin_port)));
1258 client_deregister(client, 0);
1260 syslog(LOG_WARNING, "unknown ctl opcode %s", opcode);
1261 if (server == me_server) {
1262 if (strcmp(notice->z_class_inst, ZEPHYR_CTL_REALM) != 0)
1268 if (server == me_server) {
1270 server_forward(notice, auth, who);
1278 int i, s, newserver;
1279 struct sockaddr_in sin;
1281 for (i = 0; i < nservers; i++) {
1282 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1285 newserver = (i < nservers);
1286 for (i = 0; i < num_hosts; i++) {
1287 sin.sin_addr = hosts[i];
1288 sin.sin_port = hm_port;
1291 s = (random() % (nservers - 1)) + 1;
1292 if (otherservers[s].state == SERV_UP)
1295 hostm_deathgram(&sin, &otherservers[s]);
1297 hostm_deathgram(&sin, NULL);
1305 int i, s, newserver;
1306 struct sockaddr_in sin;
1308 for (i = 0; i < nservers; i++) {
1309 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1312 zdbug((LOG_DEBUG, "rlm_shutdown"));
1314 newserver = (i < nservers);
1317 s = (random() % (nservers - 1)) + 1;
1318 if (otherservers[s].state == SERV_UP)
1321 realm_deathgram(&otherservers[s]);
1323 realm_deathgram(NULL);
1328 hostm_deathgram(sin, server)
1329 struct sockaddr_in *sin;
1334 ZNotice_t shutnotice;
1337 memset (&shutnotice, 0, sizeof(shutnotice));
1339 shutnotice.z_kind = HMCTL;
1340 shutnotice.z_port = sin->sin_port; /* we are sending it */
1341 shutnotice.z_class = HM_CTL_CLASS;
1342 shutnotice.z_class_inst = HM_CTL_SERVER;
1343 shutnotice.z_opcode = SERVER_SHUTDOWN;
1344 shutnotice.z_sender = HM_CTL_SERVER;
1345 shutnotice.z_recipient = hm_recipient();
1346 shutnotice.z_default_format = "";
1347 shutnotice.z_num_other_fields = 0;
1348 shutnotice.z_message = (server) ? server->addr_str : NULL;
1349 shutnotice.z_message_len = (server) ? strlen(server->addr_str) + 1 : 0;
1351 retval = ZFormatNotice(&shutnotice, &shutpack, &shutlen, ZNOAUTH);
1352 if (retval != ZERR_NONE) {
1353 syslog(LOG_ERR, "hm_shut format: %s",error_message(retval));
1356 retval = ZSetDestAddr(sin);
1357 if (retval != ZERR_NONE) {
1358 syslog(LOG_WARNING, "hm_shut set addr: %s", error_message(retval));
1362 retval = ZSendPacket(shutpack, shutlen, 0);
1363 if (retval != ZERR_NONE)
1364 syslog(LOG_WARNING, "hm_shut xmit: %s", error_message(retval));
1371 static char *recipient;
1377 realm = ZGetRealm();
1380 recipient = (char *) malloc(strlen(realm) + 4);
1381 strcpy (recipient, "hm@");
1382 strcat (recipient, realm);