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 */
154 zdbug((LOG_DEBUG, "internal queue process"));
157 pending = server_dequeue(me_server);
159 status = ZParseNotice(pending->packet, pending->len, &new_notice);
160 if (status != ZERR_NONE) {
161 syslog(LOG_ERR, "bad notice parse (%s): %s",
162 inet_ntoa(pending->who.sin_addr), error_message(status));
164 dispatch(&new_notice, pending->auth, &pending->who, 1);
166 server_pending_free(pending);
171 * nothing in internal queue, go to the external library
174 status = ZReceivePacket(input_packet, &input_len, &whoisit);
175 if (status != ZERR_NONE) {
176 syslog(LOG_ERR, "bad packet receive: %s from %s",
177 error_message(status), inet_ntoa(whoisit.sin_addr));
181 status = ZParseNotice(input_packet, input_len, &new_notice);
182 if (status != ZERR_NONE) {
183 syslog(LOG_ERR, "bad notice parse (%s): %s",
184 inet_ntoa(whoisit.sin_addr), error_message(status));
187 if (server_which_server(&whoisit)) {
188 /* we need to parse twice--once to get
189 the source addr, second to check
191 memset(&input_sin, 0, sizeof(input_sin));
192 input_sin.sin_addr.s_addr = new_notice.z_sender_addr.s_addr;
193 input_sin.sin_port = new_notice.z_port;
194 input_sin.sin_family = AF_INET;
195 /* Should check to see if packet is from another realm's server,
197 /* Clients don't check auth of acks, nor do we make it so they
198 can in general, so this is safe. */
199 if (new_notice.z_kind == SERVACK || new_notice.z_kind == SERVNAK) {
200 authentic = ZAUTH_YES;
202 if (realm = realm_which_realm(&input_sin)) {
203 authentic = ZCheckRealmAuthentication(&new_notice,
207 authentic = ZCheckAuthentication(&new_notice, &input_sin);
212 /* Clients don't check auth of acks, nor do we make it so they
213 can in general, so this is safe. */
214 if (new_notice.z_kind == SERVACK || new_notice.z_kind == SERVNAK) {
215 authentic = ZAUTH_YES;
217 if (realm = realm_which_realm(&whoisit)) {
218 authentic = ZCheckRealmAuthentication(&new_notice,
222 authentic = ZCheckAuthentication(&new_notice, &whoisit);
227 if (whoisit.sin_port != hm_port && whoisit.sin_port != hm_srv_port &&
228 strcasecmp(new_notice.z_class, ZEPHYR_ADMIN_CLASS) != 0 &&
229 whoisit.sin_port != srv_addr.sin_port &&
230 new_notice.z_kind != CLIENTACK) {
231 syslog(LOG_ERR, "bad port %s/%d", inet_ntoa(whoisit.sin_addr),
232 ntohs(whoisit.sin_port));
237 message_notices.val++;
238 dispatch(&new_notice, authentic, &whoisit, from_server);
246 dispatch(ZNotice_t *notice,
248 struct sockaddr_in *who,
252 String *notice_class;
253 struct sockaddr_in who2;
258 char dbg_buf[BUFSIZ];
261 authflag = (auth == ZAUTH_YES);
263 if ((int) notice->z_kind < (int) UNSAFE ||
264 (int) notice->z_kind > (int) CLIENTACK) {
265 syslog(LOG_NOTICE, "bad notice kind 0x%x from %s", notice->z_kind,
266 inet_ntoa(who->sin_addr));
272 "disp:%s '%s' '%s' '%s' notice to '%s' from '%s' %s/%d/%d",
273 ZNoticeKinds[(int) notice->z_kind], notice->z_class,
274 notice->z_class_inst, notice->z_opcode, notice->z_recipient,
275 notice->z_sender, inet_ntoa(who->sin_addr),
276 ntohs(who->sin_port), ntohs(notice->z_port));
280 if (notice->z_kind == CLIENTACK) {
281 nack_cancel(notice, who);
287 if (0 && from_server) {
288 /* incorporate server_dispatch here */
291 notice_class = make_string(notice->z_class,1);
294 interserver_notices.val++;
295 status = server_dispatch(notice, authflag, who);
296 } else if (class_is_hm(notice_class)) {
298 status = hostm_dispatch(notice, authflag, who, me_server);
299 } else if (realm_which_realm(who) && !(class_is_admin(notice_class))) {
301 status = realm_dispatch(notice, authflag, who, me_server);
302 } else if (class_is_control(notice_class)) {
303 control_notices.val++;
304 status = control_dispatch(notice, authflag, who, me_server);
305 } else if (class_is_ulogin(notice_class)) {
307 status = ulogin_dispatch(notice, authflag, who, me_server);
308 } else if (class_is_ulocate(notice_class)) {
309 locate_notices.val++;
310 status = ulocate_dispatch(notice, authflag, who, me_server);
311 } else if (class_is_admin(notice_class)) {
313 status = server_adispatch(notice, authflag, who, me_server);
315 if (!realm_bound_for_realm(ZGetRealm(), notice->z_recipient)) {
316 cp = strchr(notice->z_recipient, '@');
318 !(realm = realm_get_realm_by_name(cp + 1))) {
319 /* Foreign user, local realm */
320 sendit(notice, authflag, who, 0);
322 realm_handoff(notice, authflag, who, realm, 1);
324 if (notice->z_recipient[0] == '@')
325 notice->z_recipient = "";
326 sendit(notice, authflag, who, 1);
328 free_string(notice_class);
332 if (status == ZSRV_REQUEUE)
333 server_self_queue(notice, authflag, who);
334 free_string(notice_class);
338 * Send a notice off to those clients who have subscribed to it.
342 sendit(ZNotice_t *notice,
344 struct sockaddr_in *who,
347 static int send_counter = 0;
348 char recipbuf[ANAME_SZ + INST_SZ + REALM_SZ + 3], *recipp;
354 class = make_string(notice->z_class, 1);
355 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient)) {
358 acl = class_get_acl(class);
360 /* if controlled and not auth, fail */
362 syslog(LOG_WARNING, "sendit unauthentic %s from %s",
363 notice->z_class, notice->z_sender);
364 clt_ack(notice, who, AUTH_FAILED);
368 /* if from foreign realm server, disallow if not realm of sender */
369 rlm = realm_which_realm(who);
371 if (!realm_sender_in_realm(rlm->name, notice->z_sender)) {
372 syslog(LOG_WARNING, "sendit auth not verifiable %s (%s) from %s",
373 notice->z_class, rlm->name, notice->z_sender);
374 clt_ack(notice, who, AUTH_FAILED);
379 /* if not auth to transmit, fail */
380 if (!access_check(notice->z_sender, acl, TRANSMIT)) {
381 syslog(LOG_WARNING, "sendit unauthorized %s from %s",
382 notice->z_class, notice->z_sender);
383 clt_ack(notice, who, AUTH_FAILED);
387 /* sender != inst and not auth to send to others --> fail */
388 if (strcmp(notice->z_sender, notice->z_class_inst) != 0 &&
389 !access_check(notice->z_sender, acl, INSTUID)) {
390 syslog(LOG_WARNING, "sendit unauth uid %s %s.%s", notice->z_sender,
391 notice->z_class, notice->z_class_inst);
392 clt_ack(notice, who, AUTH_FAILED);
398 if (!realm_which_realm(who)) {
399 if (memcmp(¬ice->z_sender_addr.s_addr, &who->sin_addr.s_addr,
400 sizeof(notice->z_sender_addr.s_addr))) {
401 /* someone is playing games... */
402 /* inet_ntoa returns pointer to static area */
403 /* max size is 255.255.255.255 */
405 strcpy(buffer, inet_ntoa(who->sin_addr));
408 "sendit unauthentic fake packet: claimed %s, real %s",
409 inet_ntoa(notice->z_sender_addr), buffer);
410 clt_ack(notice, who, AUTH_FAILED);
414 if (ntohl(notice->z_sender_addr.s_addr) != 0) {
416 "sendit invalid address: claimed %s, real %s",
417 inet_ntoa(notice->z_sender_addr), buffer);
418 clt_ack(notice, who, AUTH_FAILED);
422 syslog(LOG_WARNING, "sendit addr mismatch: claimed %s, real %s",
423 inet_ntoa(notice->z_sender_addr), buffer);
427 /* Increment the send counter, used to prevent duplicate sends to
428 * clients. On the off-chance that we wrap around to 0, skip over
429 * it to prevent missing clients which have never had a packet
432 if (send_counter == 0)
435 /* Send to clients subscribed to the triplet itself. */
436 dest.classname = class;
437 dest.inst = make_string(notice->z_class_inst, 1);
438 if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient) &&
439 *notice->z_recipient == '@')
440 dest.recip = make_string("", 0);
442 strncpy(recipbuf, notice->z_recipient, sizeof(recipbuf));
443 recipp = strrchr(recipbuf, '@');
445 sprintf(recipp + 1, "%s", realm_expand_realm(recipp + 1));
446 dest.recip = make_string(recipbuf, 0);
449 if (send_to_dest(notice, auth, &dest, send_counter, external))
452 /* Send to clients subscribed to the triplet with the instance
453 * substituted with the wildcard instance. */
454 free_string(dest.inst);
455 dest.inst = wildcard_instance;
456 if (send_to_dest(notice, auth, &dest, send_counter, external))
460 free_string(dest.recip);
468 * Send to each client in the list. Avoid duplicates by setting
469 * last_send on each client to send_counter, a nonce which is updated
474 send_to_dest(ZNotice_t *notice,
483 clientp = triplet_lookup(dest);
487 for (; *clientp; clientp++) {
488 if ((*clientp)->last_send == send_counter)
490 (*clientp)->last_send = send_counter;
491 if ((*clientp)->realm) {
493 realm_handoff(notice, auth, &clientp[0]->addr, clientp[0]->realm,
498 xmit(notice, &((*clientp)->addr), auth, *clientp);
507 * Release anything destined for the client in the not-yet-acked table.
511 nack_release(Client *client)
514 Unacked *nacked, *next;
516 for (i = 0; i < NACKTAB_HASHSIZE; i++) {
517 for (nacked = nacktab[i]; nacked; nacked = next) {
519 if (nacked->client == client) {
520 timer_reset(nacked->timer);
522 free(nacked->packet);
530 * Send one packet of a fragmented message to a client. After transmitting,
531 * put it onto the not ack'ed list.
534 /* the arguments must be the same as the arguments to Z_XmitFragment */
537 xmit_frag(ZNotice_t *notice,
542 struct sockaddr_in sin;
546 int hashval, sendfail = 0;
548 retval = ZSendPacket(buf, len, 0);
549 if (retval != ZERR_NONE) {
550 syslog(LOG_WARNING, "xmit_frag send: %s", error_message(retval));
551 if (retval != EAGAIN && retval != ENOBUFS)
556 /* now we've sent it, mark it as not ack'ed */
557 nacked = (Unacked *) malloc(sizeof(Unacked));
559 /* no space: just punt */
560 syslog(LOG_WARNING, "xmit_frag nack malloc");
564 savebuf = (char *) malloc(len);
566 /* no space: just punt */
567 syslog(LOG_WARNING, "xmit_frag pack malloc");
572 memcpy(savebuf, buf, len);
574 sin = ZGetDestAddr();
575 nacked->client = NULL;
576 nacked->rexmits = (sendfail) ? -1 : 0;
577 nacked->packet = savebuf;
578 nacked->dest.addr = sin;
579 nacked->packsz = len;
580 nacked->uid = notice->z_uid;
581 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
582 LIST_INSERT(&nacktab[NACKTAB_HASHVAL(sin, nacked->uid)], nacked);
588 * Send the notice to the client. After transmitting, put it onto the
593 xmit(ZNotice_t *notice,
594 struct sockaddr_in *dest,
600 int packlen, sendfail = 0;
604 zdbug((LOG_DEBUG,"xmit"));
607 noticepack = (char *) malloc(sizeof(ZPacket_t));
609 syslog(LOG_ERR, "xmit malloc");
610 return; /* DON'T put on nack list */
613 packlen = sizeof(ZPacket_t);
615 if (auth && client) { /*
616 we are distributing authentic and
617 we have a pointer to auth info
620 retval = ZFormatAuthenticNoticeV5(notice, noticepack, packlen,
621 &packlen, client->session_keyblock);
622 if (retval != ZERR_NONE) {
623 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
628 #if defined(HAVE_KRB4)
629 retval = ZFormatAuthenticNotice(notice, noticepack, packlen,
630 &packlen, client->session_key);
631 if (retval != ZERR_NONE) {
632 syslog(LOG_ERR, "xmit auth format: %s", error_message(retval));
636 #else /* !HAVE_KRB4 */
638 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
639 if (retval != ZERR_NONE) {
640 syslog(LOG_ERR, "xmit auth/raw format: %s", error_message(retval));
644 #endif /* HAVE_KRB4 */
645 #endif /* HAVE_KRB5 */
648 notice->z_authent_len = 0;
649 notice->z_ascii_authent = (char *)"";
650 retval = ZFormatSmallRawNotice(notice, noticepack, &packlen);
651 /* This code is needed because a Zephyr can "grow" when a remote
652 * realm name is inserted into the Zephyr before being resent out
653 * locally. It essentially matches the code in realm.c to do the
654 * same thing with authentic Zephyrs.
656 if (retval == ZERR_PKTLEN) {
657 ZNotice_t partnotice, newnotice;
660 int buffer_len, hdrlen, offset, fragsize, ret_len, message_len;
661 int origoffset, origlen;
665 retval = ZSetDestAddr(dest);
666 if (retval != ZERR_NONE) {
667 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
671 partnotice = *notice;
673 partnotice.z_auth = 0;
674 partnotice.z_authent_len = 0;
675 partnotice.z_ascii_authent = (char *)"";
677 origoffset = offset = fragsize = 0;
678 origlen = notice->z_message_len;
680 buffer = (char *) malloc(sizeof(ZPacket_t));
682 syslog(LOG_ERR, "xmit unauth refrag malloc");
683 return; /* DON'T put on nack list */
685 buffer_len = sizeof(ZPacket_t);
687 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
688 &hdrlen, NULL, NULL);
689 if (retval != ZERR_NONE) {
690 syslog(LOG_ERR, "xmit unauth refrag fmt: failed");
695 if (notice->z_multinotice && strcmp(notice->z_multinotice, ""))
696 if (sscanf(notice->z_multinotice, "%d/%d", &origoffset, &origlen)
699 syslog(LOG_WARNING, "xmit unauth refrag: parse failed");
704 fragsize = Z_MAXPKTLEN-hdrlen-Z_FRAGFUDGE;
706 while (offset < notice->z_message_len || !notice->z_message_len) {
707 (void) sprintf(multi, "%d/%d", offset+origoffset, origlen);
708 partnotice.z_multinotice = multi;
710 (void) Z_gettimeofday(&partnotice.z_uid.tv, (struct timezone *)0);
711 partnotice.z_uid.tv.tv_sec = htonl((u_long)
712 partnotice.z_uid.tv.tv_sec);
713 partnotice.z_uid.tv.tv_usec = htonl((u_long)
714 partnotice.z_uid.tv.tv_usec);
715 (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
718 partnotice.z_message = notice->z_message+offset;
719 message_len = min(notice->z_message_len-offset, fragsize);
720 partnotice.z_message_len = message_len;
722 retval = Z_FormatRawHeader(&partnotice, buffer, buffer_len,
723 &hdrlen, &ptr, NULL);
724 if (retval != ZERR_NONE) {
725 syslog(LOG_WARNING, "xmit unauth refrag raw: %s",
726 error_message(retval));
733 (void) memcpy(ptr, partnotice.z_message, partnotice.z_message_len);
735 buffer_len = hdrlen+partnotice.z_message_len;
737 xmit_frag(&partnotice, buffer, buffer_len, 0);
741 if (!notice->z_message_len)
747 /* End of refrag code */
749 if (retval != ZERR_NONE) {
750 syslog(LOG_ERR, "xmit format: %s", error_message(retval));
752 return; /* DON'T put on nack list */
756 zdbug((LOG_DEBUG," to %s/%d", inet_ntoa(dest->sin_addr),
757 ntohs(dest->sin_port)));
759 retval = ZSetDestAddr(dest);
760 if (retval != ZERR_NONE) {
761 syslog(LOG_WARNING, "xmit set addr: %s", error_message(retval));
765 retval = ZSendPacket(noticepack, packlen, 0);
766 if (retval != ZERR_NONE) {
767 syslog(LOG_WARNING, "xmit xmit: (%s/%d) %s", inet_ntoa(dest->sin_addr),
768 ntohs(dest->sin_port), error_message(retval));
769 if (retval != EAGAIN && retval != ENOBUFS) {
776 /* now we've sent it, mark it as not ack'ed */
778 nacked = (Unacked *) malloc(sizeof(Unacked));
780 /* no space: just punt */
781 syslog(LOG_WARNING, "xmit nack malloc");
786 nacked->client = client;
787 nacked->rexmits = (sendfail) ? -1 : 0;
788 nacked->packet = noticepack;
789 nacked->dest.addr = *dest;
790 nacked->packsz = packlen;
791 nacked->uid = notice->z_uid;
792 nacked->timer = timer_set_rel(rexmit_times[0], rexmit, nacked);
793 LIST_INSERT(&nacktab[NACKTAB_HASHVAL(*dest, nacked->uid)], nacked);
797 * Retransmit the packet specified. If we have timed out or retransmitted
798 * too many times, punt the packet and initiate the host recovery algorithm
799 * Else, increment the count and re-send the notice packet.
805 Unacked *nacked = (Unacked *) arg;
809 syslog(LOG_DEBUG, "rexmit %s/%d #%d time %d",
810 inet_ntoa(nacked->dest.addr.sin_addr),
811 ntohs(nacked->dest.addr.sin_port), nacked->rexmits + 1, NOW);
815 if (rexmit_times[nacked->rexmits] == -1) {
817 || NOW - nacked->client->last_ack >= CLIENT_GIVEUP_MIN) {
818 /* The client (if there was one) has been unresponsive.
819 * Give up sending this packet, and kill the client if
820 * there was one. (Make sure to remove nacked from the
821 * nack list before calling client_deregister(), which
822 * scans the nack list.)
825 if (nacked->client) {
826 server_kill_clt(nacked->client);
827 client_deregister(nacked->client, 1);
829 free(nacked->packet);
833 /* The client has sent us an ack recently. Retry with the maximum
834 * retransmit time. */
839 /* retransmit the packet */
841 zdbug((LOG_DEBUG," to %s/%d", inet_ntoa(nacked->dest.addr.sin_addr),
842 ntohs(nacked->dest.addr.sin_port)));
844 retval = ZSetDestAddr(&nacked->dest.addr);
845 if (retval != ZERR_NONE) {
846 syslog(LOG_WARNING, "rexmit set addr: %s", error_message(retval));
848 retval = ZSendPacket(nacked->packet, nacked->packsz, 0);
849 if (retval != ZERR_NONE)
850 syslog(LOG_WARNING, "rexmit xmit: %s", error_message(retval));
851 if (retval == EAGAIN || retval == ENOBUFS)
855 /* reset the timer */
856 nacked->timer = timer_set_rel(rexmit_times[nacked->rexmits], rexmit,
862 * Send an acknowledgement to the sending client, by sending back the
863 * header from the original notice with the z_kind field changed to either
864 * SERVACK or SERVNAK, and the contents of the message either SENT or
865 * NOT_SENT, depending on the value of the sent argument.
869 clt_ack(ZNotice_t *notice,
870 struct sockaddr_in *who,
880 if (bdumping) { /* don't ack while dumping */
882 zdbug((LOG_DEBUG,"bdumping, no ack"));
889 acknotice.z_kind = SERVACK;
892 acknotice.z_message = ZSRVACK_SENT;
896 acknotice.z_message = ZSRVACK_FAIL;
897 acknotice.z_kind = SERVNAK;
901 acknotice.z_kind = SERVNAK;
902 acknotice.z_message = ZSRVACK_NOTSENT;
903 sent_name = "nak/not_sent";
906 acknotice.z_message = ZSRVACK_NOTSENT;
907 sent_name = "not_sent";
914 zdbug((LOG_DEBUG,"clt_ack type %s for %d to %s/%d", sent_name,
915 ntohs(notice->z_port), inet_ntoa(who->sin_addr),
916 ntohs(who->sin_port)));
919 acknotice.z_multinotice = "";
921 /* leave room for the trailing null */
922 acknotice.z_message_len = strlen(acknotice.z_message) + 1;
924 packlen = sizeof(ackpack);
926 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
928 if (retval == ZERR_HEADERLEN) {
929 /* Since an ack header can be larger than a message header... (crock) */
930 acknotice.z_opcode = "";
931 acknotice.z_class = "";
932 acknotice.z_class_inst = "";
933 acknotice.z_opcode = "";
934 acknotice.z_default_format = "";
936 retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen);
939 if (retval != ZERR_NONE) {
940 syslog(LOG_ERR, "clt_ack format: %s", error_message(retval));
943 retval = ZSetDestAddr(who);
944 if (retval != ZERR_NONE) {
945 syslog(LOG_WARNING, "clt_ack set addr: %s", error_message(retval));
948 retval = ZSendPacket(ackpack, packlen, 0);
949 if (retval != ZERR_NONE) {
950 syslog(LOG_WARNING, "clt_ack xmit: %s", error_message(retval));
953 zdbug((LOG_DEBUG, "packet sent"));
959 * An ack has arrived.
960 * remove the packet matching this notice from the not-yet-acked queue
964 nack_cancel(ZNotice_t *notice,
965 struct sockaddr_in *who)
970 /* search the not-yet-acked table for this packet, and flush it. */
972 zdbug((LOG_DEBUG, "nack_cancel: %s:%08X,%08X",
973 inet_ntoa(notice->z_uid.zuid_addr),
974 notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));
976 hashval = NACKTAB_HASHVAL(*who, notice->z_uid);
977 for (nacked = nacktab[hashval]; nacked; nacked = nacked->next) {
978 if (nacked->dest.addr.sin_addr.s_addr == who->sin_addr.s_addr
979 && nacked->dest.addr.sin_port == who->sin_port
980 && ZCompareUID(&nacked->uid, ¬ice->z_uid)) {
982 nacked->client->last_ack = NOW;
983 timer_reset(nacked->timer);
984 free(nacked->packet);
992 zdbug((LOG_DEBUG,"nack_cancel: nack not found %s:%08X,%08X",
993 inet_ntoa (notice->z_uid.zuid_addr),
994 notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));
998 /* for compatibility when sending subscription information to old clients */
1000 #define OLD_ZEPHYR_VERSION "ZEPH0.0"
1001 #endif /* OLD_COMPAT */
1003 /* Dispatch an HM_CTL notice. */
1006 hostm_dispatch(ZNotice_t *notice,
1008 struct sockaddr_in *who,
1012 char *opcode = notice->z_opcode;
1014 int i, add = 0, remove = 0;
1017 zdbug((LOG_DEBUG,"hm_disp"));
1020 if (notice->z_kind == HMACK) {
1023 } else if (notice->z_kind != HMCTL) {
1025 zdbug((LOG_DEBUG, "bogus HM packet"));
1027 clt_ack(notice, who, AUTH_FAILED);
1028 } else if (strcmp(opcode, HM_FLUSH) == 0) {
1029 client_flush_host(&who->sin_addr);
1030 if (server == me_server)
1031 server_forward(notice, auth, who);
1032 } else if (strcmp(opcode, HM_BOOT) == 0) {
1033 client_flush_host(&who->sin_addr);
1034 if (server == me_server) {
1035 server_forward(notice, auth, who);
1039 } else if (strcmp(opcode, HM_ATTACH) == 0) {
1040 if (server == me_server) {
1041 server_forward(notice, auth, who);
1047 } else if (strcmp(opcode, HM_DETACH) == 0) {
1050 syslog(LOG_WARNING, "hm_dispatch: unknown opcode %s", opcode);
1054 for (i = 0; i < num_hosts; i++) {
1055 if (hosts[i].s_addr == who->sin_addr.s_addr)
1058 if (i == num_hosts) {
1059 if (hosts_size == 0) {
1060 hosts = (struct in_addr *) malloc(HOSTS_SIZE_INIT *
1061 sizeof(struct in_addr));
1064 hosts_size = HOSTS_SIZE_INIT;
1065 } else if (num_hosts == hosts_size) {
1066 hosts = (struct in_addr *) realloc(hosts, hosts_size * 2 *
1067 sizeof(struct in_addr));
1072 hosts[num_hosts++] = who->sin_addr;
1074 } else if (remove) {
1075 for (i = 0; i < num_hosts; i++) {
1076 if (hosts[i].s_addr == who->sin_addr.s_addr) {
1077 memmove(&hosts[i], &hosts[i + 1], num_hosts - (i + 1));
1087 * Dispatch a ZEPHYR_CTL notice.
1091 control_dispatch(ZNotice_t *notice,
1093 struct sockaddr_in *who,
1096 char *opcode = notice->z_opcode;
1101 struct sockaddr_in newwho;
1104 * ZEPHYR_CTL Opcodes expected are:
1105 * BOOT (inst HM): host has booted; flush data.
1106 * CLIENT_SUBSCRIBE: process with the subscription mananger.
1107 * CLIENT_UNSUBSCRIBE: ""
1108 * CLIENT_CANCELSUB: ""
1111 zdbug((LOG_DEBUG, "ctl_disp: opc=%s", opcode));
1113 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
1114 newwho.sin_port = notice->z_port;
1115 realm = realm_which_realm(&newwho);
1117 return(realm_control_dispatch(notice, auth, who, server, realm));
1119 if (strcasecmp(notice->z_class_inst, ZEPHYR_CTL_HM) == 0) {
1120 return hostm_dispatch(notice, auth, who, server);
1121 } else if (strcmp(opcode, CLIENT_GIMMESUBS) == 0 ||
1122 strcmp(opcode, CLIENT_GIMMEDEFS) == 0) {
1123 /* this special case is before the auth check so that
1124 someone who has no subscriptions does NOT get a SERVNAK
1125 but rather an empty list. Note we must therefore
1126 check authentication inside subscr_sendlist */
1128 /* only acknowledge if *not* old version; the old version
1129 acknowledges the packet with the reply */
1130 if (strcmp(notice->z_version, OLD_ZEPHYR_VERSION) != 0)
1132 #else /* !OLD_COMPAT */
1134 #endif /* OLD_COMPAT */
1135 subscr_sendlist(notice, auth, who);
1139 zdbug((LOG_DEBUG,"unauth ctrl_disp"));
1141 if (server == me_server)
1142 clt_ack(notice, who, AUTH_FAILED);
1146 wantdefs = strcmp(opcode, CLIENT_SUBSCRIBE_NODEFS);
1147 if (!wantdefs || strcmp(opcode, CLIENT_SUBSCRIBE) == 0) {
1148 /* subscription notice */
1149 retval = client_register(notice, &who->sin_addr, &client, wantdefs);
1150 if (retval != ZERR_NONE) {
1151 syslog(LOG_NOTICE, "subscr %s/%s/%d failed: %s",
1152 notice->z_sender, inet_ntoa(who->sin_addr),
1153 ntohs(notice->z_port), error_message(retval));
1154 if (server == me_server) {
1155 if (retval == ZSRV_BADSUBPORT)
1156 clt_ack(notice, who, AUTH_FAILED);
1162 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1163 /* you may only subscribe for your own clients */
1164 if (server == me_server)
1165 clt_ack(notice, who, AUTH_FAILED);
1169 if (client->session_keyblock) {
1170 krb5_free_keyblock_contents(Z_krb5_ctx, client->session_keyblock);
1171 retval = krb5_copy_keyblock_contents(Z_krb5_ctx, ZGetSession(),
1172 client->session_keyblock);
1174 retval = krb5_copy_keyblock(Z_krb5_ctx, ZGetSession(),
1175 &client->session_keyblock);
1178 syslog(LOG_WARNING, "keyblock copy failed in subscr: %s",
1179 error_message(retval));
1180 if (server == me_server)
1186 /* in case it's changed */
1187 memcpy(client->session_key, ZGetSession(), sizeof(C_Block));
1190 retval = subscr_subscribe(client, notice, server);
1191 if (retval != ZERR_NONE) {
1192 syslog(LOG_WARNING, "subscr failed: %s", error_message(retval));
1193 if (server == me_server)
1197 } else if (strcmp(opcode, CLIENT_UNSUBSCRIBE) == 0) {
1198 client = client_find(&who->sin_addr, notice->z_port);
1199 if (client != NULL) {
1200 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1201 /* you may only cancel for your own clients */
1202 if (server == me_server)
1203 clt_ack(notice, who, AUTH_FAILED);
1208 if (server == me_server) {
1209 syslog(LOG_DEBUG, "subscription cancel for %s/%d\n",
1210 inet_ntoa(who->sin_addr), ntohs(who->sin_port));
1213 "subscription cancel for %s/%d from %s\n",
1214 inet_ntoa(who->sin_addr), ntohs(who->sin_port),
1219 subscr_cancel(who, notice);
1224 } else if (strcmp(opcode, CLIENT_CANCELSUB) == 0) {
1225 /* canceling subscriptions implies I can punt info about this client */
1226 client = client_find(&who->sin_addr, notice->z_port);
1227 if (client == NULL) {
1229 zdbug((LOG_DEBUG,"can_sub not found client"));
1231 if (server == me_server)
1235 if (strcmp(client->principal->string, notice->z_sender) != 0) {
1236 /* you may only cancel for your own clients */
1237 if (server == me_server)
1238 clt_ack(notice, who, AUTH_FAILED);
1241 /* don't flush locations here, let him do it explicitly */
1243 zdbug((LOG_DEBUG, "cancelsub clt_dereg %s/%d",
1244 inet_ntoa(who->sin_addr), ntohs(who->sin_port)));
1246 client_deregister(client, 0);
1248 syslog(LOG_WARNING, "unknown ctl opcode %s", opcode);
1249 if (server == me_server) {
1250 if (strcmp(notice->z_class_inst, ZEPHYR_CTL_REALM) != 0)
1256 if (server == me_server) {
1258 server_forward(notice, auth, who);
1264 hostm_shutdown(void)
1266 int i, s, newserver;
1267 struct sockaddr_in sin;
1269 for (i = 0; i < nservers; i++) {
1270 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1273 newserver = (i < nservers);
1274 for (i = 0; i < num_hosts; i++) {
1275 sin.sin_addr = hosts[i];
1276 sin.sin_port = hm_port;
1279 s = (random() % (nservers - 1)) + 1;
1280 if (otherservers[s].state == SERV_UP)
1283 hostm_deathgram(&sin, &otherservers[s]);
1285 hostm_deathgram(&sin, NULL);
1291 realm_shutdown(void)
1293 int i, s, newserver;
1294 struct sockaddr_in sin;
1296 for (i = 0; i < nservers; i++) {
1297 if (i != me_server_idx && otherservers[i].state == SERV_UP)
1300 zdbug((LOG_DEBUG, "rlm_shutdown"));
1302 newserver = (i < nservers);
1305 s = (random() % (nservers - 1)) + 1;
1306 if (otherservers[s].state == SERV_UP)
1309 realm_deathgram(&otherservers[s]);
1311 realm_deathgram(NULL);
1316 hostm_deathgram(struct sockaddr_in *sin,
1321 ZNotice_t shutnotice;
1324 memset (&shutnotice, 0, sizeof(shutnotice));
1326 shutnotice.z_kind = HMCTL;
1327 shutnotice.z_port = sin->sin_port; /* we are sending it */
1328 shutnotice.z_class = HM_CTL_CLASS;
1329 shutnotice.z_class_inst = HM_CTL_SERVER;
1330 shutnotice.z_opcode = SERVER_SHUTDOWN;
1331 shutnotice.z_sender = HM_CTL_SERVER;
1332 shutnotice.z_recipient = hm_recipient();
1333 shutnotice.z_default_format = "";
1334 shutnotice.z_num_other_fields = 0;
1335 shutnotice.z_message = (server) ? server->addr_str : NULL;
1336 shutnotice.z_message_len = (server) ? strlen(server->addr_str) + 1 : 0;
1338 retval = ZFormatNotice(&shutnotice, &shutpack, &shutlen, ZNOAUTH);
1339 if (retval != ZERR_NONE) {
1340 syslog(LOG_ERR, "hm_shut format: %s",error_message(retval));
1343 retval = ZSetDestAddr(sin);
1344 if (retval != ZERR_NONE) {
1345 syslog(LOG_WARNING, "hm_shut set addr: %s", error_message(retval));
1349 retval = ZSendPacket(shutpack, shutlen, 0);
1350 if (retval != ZERR_NONE)
1351 syslog(LOG_WARNING, "hm_shut xmit: %s", error_message(retval));
1358 static char *recipient;
1364 realm = ZGetRealm();
1367 recipient = (char *) malloc(strlen(realm) + 4);
1368 strcpy (recipient, "hm@");
1369 strcat (recipient, realm);