1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the internal Zephyr routines.
4 * Created by: Robert French
6 * $Id: Zinternal.c,v 1.42 2002/09/10 16:04:31 ghudson Exp $
8 * Copyright (c) 1987,1988,1991 by the Massachusetts Institute of
10 * For copying and distribution information, see the file
15 #include <arpa/inet.h>
16 #include <sys/socket.h>
20 static const char rcsid_Zinternal_c[] =
21 "$Id: Zinternal.c,v 1.42 2002/09/10 16:04:31 ghudson Exp $";
22 static const char copyright[] =
23 "Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology.";
26 extern char *inet_ntoa ();
30 int __Zephyr_port = -1;
31 struct in_addr __My_addr;
32 int __Q_CompleteLength;
34 struct _Z_InputQ *__Q_Head, *__Q_Tail;
35 struct sockaddr_in __HM_addr;
36 struct sockaddr_in __HM_addr_real;
39 ZLocations_t *__locate_list;
42 ZSubscription_t *__subscriptions_list;
43 int __subscriptions_num;
44 int __subscriptions_next;
45 int Z_discarded_packets = 0;
48 /* This context is used throughout */
49 krb5_context Z_krb5_ctx;
51 static struct cksum_map_s {
55 /* per RFC1510 and draft-ietf-krb-wg-crypto-02.txt */
56 { ENCTYPE_NULL, CKSUMTYPE_RSA_MD5 },
57 { ENCTYPE_DES_CBC_CRC, CKSUMTYPE_RSA_MD5_DES },
58 { ENCTYPE_DES_CBC_MD4, CKSUMTYPE_RSA_MD4_DES },
59 { ENCTYPE_DES_CBC_MD5, CKSUMTYPE_RSA_MD5_DES },
62 * The implementors hate us, and are inconsistent with names for
63 * most things defined after RFC1510. Note that des3-cbc-sha1
64 * and des3-cbc-sha1-kd are listed by number to avoid confusion
65 * caused by inconsistency between the names used in the specs
66 * and those used by implementations.
67 * -- jhutz, 30-Nov-2002
70 /* source lost in history (an expired internet-draft) */
71 { 5 /* des3-cbc-md5 */, 9 /* rsa-md5-des3 */ },
72 { 7 /* des3-cbc-sha1 */, 12 /* hmac-sha1-des3 */ },
74 /* per draft-ietf-krb-wg-crypto-02.txt */
75 { 16 /* des3-cbc-sha1-kd */, 12 /* hmac-sha1-des3-kd */ },
77 /* per draft-raeburn-krb-rijndael-krb-02.txt */
78 { 17 /* aes128-cts-hmac-sha1-96 */, 10 /* hmac-sha1-96-aes128 */ },
79 { 18 /* aes256-cts-hmac-sha1-96 */, 11 /* hmac-sha1-96-aes256 */ },
81 /* per draft-brezak-win2k-krb-rc4-hmac-04.txt */
82 { 23 /* rc4-hmac */, -138 /* hmac-md5 */ },
83 { 24 /* rc4-hmac-exp */, -138 /* hmac-md5 */ },
85 #define N_CKSUM_MAP (sizeof(cksum_map) / sizeof(struct cksum_map_s))
87 Code_t Z_krb5_lookup_cksumtype(krb5_enctype e, krb5_cksumtype *c)
91 for (i = 0; i < N_CKSUM_MAP; i++) {
92 if (cksum_map[i].e == e) {
97 return KRB5_PROG_ETYPE_NOSUPP;
99 #endif /* HAVE_KRB5 */
102 C_Block __Zephyr_session;
104 char __Zephyr_realm[REALM_SZ];
107 void (*__Z_debug_print) __P((const char *fmt, va_list args, void *closure));
108 void *__Z_debug_print_closure;
111 #define min(a,b) ((a)<(b)?(a):(b))
113 static int Z_AddField __P((char **ptr, char *field, char *end));
114 static int find_or_insert_uid __P((ZUnique_Id_t *uid, ZNotice_Kind_t kind));
116 /* Find or insert uid in the old uids buffer. The buffer is a sorted
117 * circular queue. We make the assumption that most packets arrive in
118 * order, so we can usually search for a uid or insert it into the buffer
119 * by looking back just a few entries from the end. Since this code is
120 * only executed by the client, the implementation isn't microoptimized. */
121 static int find_or_insert_uid(uid, kind)
125 static struct _filter {
139 /* Initialize the uid buffer if it hasn't been done already. */
141 size = Z_INITFILTERSIZE;
142 buffer = (struct _filter *) malloc(size * sizeof(*buffer));
147 /* Age the uid buffer, discarding any uids older than the clock skew. */
149 while (num && (now - buffer[start % size].t) > CLOCK_SKEW)
153 /* Make room for a new uid, since we'll probably have to insert one. */
155 new_size = size * 2 + 2;
156 new = (struct _filter *) malloc(new_size * sizeof(*new));
159 for (i = 0; i < num; i++)
160 new[i] = buffer[(start + i) % size];
167 /* Search for this uid in the buffer, starting from the end. */
168 for (i = start + num - 1; i >= start; i--) {
169 result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid));
170 if (result == 0 && buffer[i % size].kind == kind)
176 /* We didn't find it; insert the uid into the buffer after i. */
178 for (j = start + num; j > i; j--)
179 buffer[j % size] = buffer[(j - 1) % size];
180 buffer[i % size].uid = *uid;
181 buffer[i % size].kind = kind;
182 buffer[i % size].t = now;
189 /* Return 1 if there is a packet waiting, 0 otherwise */
191 int Z_PacketWaiting()
196 tv.tv_sec = tv.tv_usec = 0;
198 FD_SET(ZGetFD(), &read);
199 return (select(ZGetFD() + 1, &read, NULL, NULL, &tv));
203 /* Wait for a complete notice to become available */
205 Code_t Z_WaitForComplete()
209 if (__Q_CompleteLength)
210 return (Z_ReadEnqueue());
212 while (!__Q_CompleteLength)
213 if ((retval = Z_ReadWait()) != ZERR_NONE)
220 /* Read any available packets and enqueue them */
222 Code_t Z_ReadEnqueue()
227 return (ZERR_NOPORT);
229 while (Z_PacketWaiting())
230 if ((retval = Z_ReadWait()) != ZERR_NONE)
238 * Search the queue for a notice with the proper multiuid - remove any
239 * notices that haven't been touched in a while
242 struct _Z_InputQ *Z_SearchQueue(uid, kind)
246 register struct _Z_InputQ *qptr;
247 struct _Z_InputQ *next;
250 (void) gettimeofday(&tv, (struct timezone *)0);
255 if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind)
258 if (qptr->timep && (qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec))
266 * Now we delve into really convoluted queue handling and
267 * fragmentation reassembly algorithms and other stuff you probably
268 * don't want to look at...
270 * This routine does NOT guarantee a complete packet will be ready when it
276 register struct _Z_InputQ *qptr;
279 struct sockaddr_in olddest, from;
280 int from_len, packet_len, zvlen, part, partof;
287 return (ZERR_NOPORT);
290 FD_SET(ZGetFD(), &fds);
294 if (select(ZGetFD() + 1, &fds, NULL, NULL, &tv) < 0)
296 if (!FD_ISSET(ZGetFD(), &fds))
299 from_len = sizeof(struct sockaddr_in);
301 packet_len = recvfrom(ZGetFD(), packet, sizeof(packet), 0,
302 (struct sockaddr *)&from, &from_len);
310 /* Ignore obviously non-Zephyr packets. */
311 zvlen = sizeof(ZVERSIONHDR) - 1;
312 if (packet_len < zvlen || memcmp(packet, ZVERSIONHDR, zvlen) != 0) {
313 Z_discarded_packets++;
317 /* Parse the notice */
318 if ((retval = ZParseNotice(packet, packet_len, ¬ice)) != ZERR_NONE)
322 * If we're not a server and the notice is of an appropriate kind,
323 * send back a CLIENTACK to whoever sent it to say we got it.
325 if (!__Zephyr_server) {
326 if (notice.z_kind != HMACK && notice.z_kind != SERVACK &&
327 notice.z_kind != SERVNAK && notice.z_kind != CLIENTACK) {
333 tmpnotice.z_kind = CLIENTACK;
334 tmpnotice.z_message_len = 0;
337 if ((retval = ZFormatSmallRawNotice(&tmpnotice, pkt, &len))
340 if ((retval = ZSendPacket(pkt, len, 0)) != ZERR_NONE)
344 if (find_or_insert_uid(¬ice.z_uid, notice.z_kind))
347 /* Check authentication on the notice. */
348 notice.z_checked_auth = ZCheckAuthentication(¬ice, &from);
353 * Parse apart the z_multinotice field - if the field is blank for
354 * some reason, assume this packet stands by itself.
356 slash = strchr(notice.z_multinotice, '/');
358 part = atoi(notice.z_multinotice);
359 partof = atoi(slash+1);
360 if (part > partof || partof == 0) {
362 partof = notice.z_message_len;
367 partof = notice.z_message_len;
370 /* Too big a packet...just ignore it! */
371 if (partof > Z_MAXNOTICESIZE)
375 * If we aren't a server and we can find a notice in the queue
376 * with the same multiuid field, insert the current fragment as
379 switch (notice.z_kind) {
382 /* The SERVACK and SERVNAK replies shouldn't be reassembled
383 (they have no parts). Instead, we should hold on to the reply
384 ONLY if it's the first part of a fragmented message, i.e.
385 multi_uid == uid. This allows programs to wait for the uid
386 of the first packet, and get a response when that notice
387 arrives. Acknowledgements of the other fragments are discarded
388 (XXX we assume here that they all carry the same information
389 regarding failure/success)
391 if (!__Zephyr_server &&
392 !ZCompareUID(¬ice.z_multiuid, ¬ice.z_uid))
393 /* they're not the same... throw away this packet. */
395 /* fall thru & process it */
397 /* for HMACK types, we assume no packet loss (local loopback
398 connections). The other types can be fragmented and MUST
399 run through this code. */
400 if (!__Zephyr_server && (qptr = Z_SearchQueue(¬ice.z_multiuid,
403 * If this is the first fragment, and we haven't already
404 * gotten a first fragment, grab the header from it.
406 if (part == 0 && !qptr->header) {
407 qptr->header_len = packet_len-notice.z_message_len;
408 qptr->header = (char *) malloc((unsigned) qptr->header_len);
411 (void) memcpy(qptr->header, packet, qptr->header_len);
413 return (Z_AddNoticeToEntry(qptr, ¬ice, part));
418 * We'll have to create a new entry...make sure the queue isn't
419 * going to get too big.
421 if (__Q_Size+(__Zephyr_server ? notice.z_message_len : partof) > Z_MAXQUEUESIZE)
425 * This is a notice we haven't heard of, so create a new queue
426 * entry for it and zero it out.
428 qptr = (struct _Z_InputQ *)malloc(sizeof(struct _Z_InputQ));
431 (void) memset((char *)qptr, 0, sizeof(struct _Z_InputQ));
433 /* Insert the entry at the end of the queue */
435 qptr->prev = __Q_Tail;
437 __Q_Tail->next = qptr;
444 /* Copy the from field, multiuid, kind, and checked authentication. */
446 qptr->uid = notice.z_multiuid;
447 qptr->kind = notice.z_kind;
448 qptr->auth = notice.z_checked_auth;
451 * If this is the first part of the notice, we take the header
452 * from it. We only take it if this is the first fragment so that
453 * the Unique ID's will be predictable.
455 * If a Zephyr Server, we always take the header.
457 if (__Zephyr_server || part == 0) {
458 qptr->header_len = packet_len-notice.z_message_len;
459 qptr->header = (char *) malloc((unsigned) qptr->header_len);
462 (void) memcpy(qptr->header, packet, qptr->header_len);
466 * If this is not a fragmented notice, then don't bother with a
468 * If we are a Zephyr server, all notices are treated as complete.
470 if (__Zephyr_server || (part == 0 && notice.z_message_len == partof)) {
471 __Q_CompleteLength++;
472 qptr->holelist = (struct _Z_Hole *) 0;
474 /* allocate a msg buf for this piece */
475 if (notice.z_message_len == 0)
477 else if (!(qptr->msg = (char *) malloc((unsigned) notice.z_message_len)))
480 (void) memcpy(qptr->msg, notice.z_message, notice.z_message_len);
481 qptr->msg_len = notice.z_message_len;
482 __Q_Size += notice.z_message_len;
483 qptr->packet_len = qptr->header_len+qptr->msg_len;
484 if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
486 (void) memcpy(qptr->packet, qptr->header, qptr->header_len);
488 (void) memcpy(qptr->packet+qptr->header_len, qptr->msg,
494 * We know how long the message is going to be (this is better
495 * than IP fragmentation...), so go ahead and allocate it all.
497 if (!(qptr->msg = (char *) malloc((unsigned) partof)) && partof)
499 qptr->msg_len = partof;
503 * Well, it's a fragmented notice...allocate a hole list and
504 * initialize it to the full packet size. Then insert the
507 if (!(qptr->holelist = (struct _Z_Hole *)
508 malloc(sizeof(struct _Z_Hole))))
510 qptr->holelist->next = (struct _Z_Hole *) 0;
511 qptr->holelist->first = 0;
512 qptr->holelist->last = partof-1;
513 return (Z_AddNoticeToEntry(qptr, ¬ice, part));
517 /* Fragment management routines - compliments, more or less, of RFC815 */
519 Code_t Z_AddNoticeToEntry(qptr, notice, part)
520 struct _Z_InputQ *qptr;
524 int last, oldfirst, oldlast;
525 struct _Z_Hole *hole, *lasthole;
529 if (part < 0 || notice->z_message_len < 0 || part > qptr->msg_len
530 || notice->z_message_len > qptr->msg_len - part)
533 /* Incorporate this notice's checked authentication. */
534 if (notice->z_checked_auth == ZAUTH_FAILED)
535 qptr->auth = ZAUTH_FAILED;
536 else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED)
537 qptr->auth = ZAUTH_NO;
539 (void) gettimeofday(&tv, (struct timezone *)0);
540 qptr->timep = tv.tv_sec;
542 last = part+notice->z_message_len-1;
544 hole = qptr->holelist;
545 lasthole = (struct _Z_Hole *) 0;
547 /* copy in the message body */
548 (void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len);
550 /* Search for a hole that overlaps with the current fragment */
552 if (part <= hole->last && last >= hole->first)
558 /* If we found one, delete it and reconstruct a new hole */
560 oldfirst = hole->first;
561 oldlast = hole->last;
563 lasthole->next = hole->next;
565 qptr->holelist = hole->next;
568 * Now create a new hole that is the original hole without the
571 if (part > oldfirst) {
572 /* Search for the end of the hole list */
573 hole = qptr->holelist;
574 lasthole = (struct _Z_Hole *) 0;
580 if (!(lasthole->next = (struct _Z_Hole *)
581 malloc(sizeof(struct _Z_InputQ))))
583 hole = lasthole->next;
586 if (!(qptr->holelist = (struct _Z_Hole *)
587 malloc(sizeof(struct _Z_InputQ))))
589 hole = qptr->holelist;
592 hole->first = oldfirst;
595 if (last < oldlast) {
596 /* Search for the end of the hole list */
597 hole = qptr->holelist;
598 lasthole = (struct _Z_Hole *) 0;
604 if (!(lasthole->next = (struct _Z_Hole *)
605 malloc(sizeof(struct _Z_InputQ))))
607 hole = lasthole->next;
610 if (!(qptr->holelist = (struct _Z_Hole *)
611 malloc(sizeof(struct _Z_InputQ))))
613 hole = qptr->holelist;
615 hole->next = (struct _Z_Hole *) 0;
616 hole->first = last+1;
617 hole->last = oldlast;
621 if (!qptr->holelist) {
623 __Q_CompleteLength++;
625 qptr->timep = 0; /* don't time out anymore */
626 qptr->packet_len = qptr->header_len+qptr->msg_len;
627 if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
629 (void) memcpy(qptr->packet, qptr->header, qptr->header_len);
630 (void) memcpy(qptr->packet+qptr->header_len, qptr->msg,
637 void Z_gettimeofday(struct _ZTimeval *ztv, struct timezone *tz)
640 (void) gettimeofday(&tv, tz); /* yeah, yeah, I know */
641 ztv->tv_sec=tv.tv_sec;
642 ztv->tv_usec=tv.tv_usec;
645 Code_t Z_FormatHeader(notice, buffer, buffer_len, len, cert_routine)
650 Z_AuthProc cert_routine;
653 static char version[BUFSIZ]; /* default init should be all \0 */
654 struct sockaddr_in name;
655 int namelen = sizeof(name);
657 if (!notice->z_sender)
658 notice->z_sender = ZGetSender();
660 if (notice->z_port == 0) {
662 retval = ZOpenPort((u_short *)0);
663 if (retval != ZERR_NONE)
666 retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen);
669 notice->z_port = name.sin_port;
672 notice->z_multinotice = "";
674 (void) Z_gettimeofday(¬ice->z_uid.tv, (struct timezone *)0);
675 notice->z_uid.tv.tv_sec = htonl((u_long) notice->z_uid.tv.tv_sec);
676 notice->z_uid.tv.tv_usec = htonl((u_long) notice->z_uid.tv.tv_usec);
678 (void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
680 notice->z_multiuid = notice->z_uid;
683 (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
685 notice->z_version = version;
687 return Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);
690 Code_t Z_NewFormatHeader(notice, buffer, buffer_len, len, cert_routine)
695 Z_AuthProc cert_routine;
698 static char version[BUFSIZ]; /* default init should be all \0 */
699 struct sockaddr_in name;
700 int namelen = sizeof(name);
702 if (!notice->z_sender)
703 notice->z_sender = ZGetSender();
705 if (notice->z_port == 0) {
707 retval = ZOpenPort((u_short *)0);
708 if (retval != ZERR_NONE)
711 retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen);
714 notice->z_port = name.sin_port;
717 notice->z_multinotice = "";
719 (void) gettimeofday(¬ice->z_uid.tv, (struct timezone *)0);
720 notice->z_uid.tv.tv_sec = htonl((u_long) notice->z_uid.tv.tv_sec);
721 notice->z_uid.tv.tv_usec = htonl((u_long) notice->z_uid.tv.tv_usec);
723 (void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
725 notice->z_multiuid = notice->z_uid;
728 (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
730 notice->z_version = version;
732 return Z_NewFormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);
735 Code_t Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine)
740 Z_AuthProc cert_routine;
744 notice->z_authent_len = 0;
745 notice->z_ascii_authent = "";
746 notice->z_checksum = 0;
747 return (Z_FormatRawHeader(notice, buffer, buffer_len,
751 return ((*cert_routine)(notice, buffer, buffer_len, len));
754 Code_t Z_NewFormatAuthHeader(notice, buffer, buffer_len, len, cert_routine)
759 Z_AuthProc cert_routine;
763 notice->z_authent_len = 0;
764 notice->z_ascii_authent = "";
765 notice->z_checksum = 0;
766 return (Z_FormatRawHeader(notice, buffer, buffer_len,
770 return ((*cert_routine)(notice, buffer, buffer_len, len));
773 Code_t Z_NewFormatRawHeader(notice, buffer, buffer_len, hdr_len,
774 cksum_start, cksum_len, cstart, cend)
781 char **cstart, **cend;
783 return(Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len,
784 cksum_start, cksum_len, cstart, cend, 0));
787 Code_t Z_AsciiFormatRawHeader(notice, buffer, buffer_len, hdr_len,
788 cksum_start, cksum_len, cstart, cend)
795 char **cstart, **cend;
797 return(Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len,
798 cksum_start, cksum_len, cstart, cend, 1));
801 Code_t Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len, cksum_start,
802 cksum_len, cstart, cend, cksumstyle)
809 char **cstart, **cend;
812 static char version_nogalaxy[BUFSIZ]; /* default init should be all \0 */
813 char newrecip[BUFSIZ];
817 if (!notice->z_class)
818 notice->z_class = "";
820 if (!notice->z_class_inst)
821 notice->z_class_inst = "";
823 if (!notice->z_opcode)
824 notice->z_opcode = "";
826 if (!notice->z_recipient)
827 notice->z_recipient = "";
829 if (!notice->z_default_format)
830 notice->z_default_format = "";
833 end = buffer+buffer_len;
838 (void) sprintf(version_nogalaxy, "%s%d.%d", ZVERSIONHDR,
839 ZVERSIONMAJOR, ZVERSIONMINOR);
841 notice->z_version = version_nogalaxy;
843 if (Z_AddField(&ptr, version_nogalaxy, end))
844 return (ZERR_HEADERLEN);
846 if (ZMakeAscii32(ptr, end-ptr,
847 Z_NUMFIELDS + notice->z_num_other_fields)
849 return (ZERR_HEADERLEN);
850 ptr += strlen(ptr)+1;
852 if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN)
853 return (ZERR_HEADERLEN);
854 ptr += strlen(ptr)+1;
856 if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid,
857 sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
858 return (ZERR_HEADERLEN);
859 ptr += strlen(ptr)+1;
861 if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN)
862 return (ZERR_HEADERLEN);
863 ptr += strlen(ptr)+1;
865 if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN)
866 return (ZERR_HEADERLEN);
867 ptr += strlen(ptr)+1;
869 if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN)
870 return (ZERR_HEADERLEN);
871 ptr += strlen(ptr)+1;
873 if (Z_AddField(&ptr, notice->z_ascii_authent, end))
874 return (ZERR_HEADERLEN);
875 if (Z_AddField(&ptr, notice->z_class, end))
876 return (ZERR_HEADERLEN);
877 if (Z_AddField(&ptr, notice->z_class_inst, end))
878 return (ZERR_HEADERLEN);
879 if (Z_AddField(&ptr, notice->z_opcode, end))
880 return (ZERR_HEADERLEN);
881 if (Z_AddField(&ptr, notice->z_sender, end))
882 return (ZERR_HEADERLEN);
883 if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) {
884 if (Z_AddField(&ptr, notice->z_recipient, end))
885 return (ZERR_HEADERLEN);
888 if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 >
890 return (ZERR_HEADERLEN);
891 (void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm);
892 if (Z_AddField(&ptr, newrecip, end))
893 return (ZERR_HEADERLEN);
895 if (Z_AddField(&ptr, notice->z_default_format, end))
896 return (ZERR_HEADERLEN);
898 /* copy back the end pointer location for crypto checksum */
901 if (cksumstyle == 1) {
902 if (Z_AddField(&ptr, notice->z_ascii_checksum, end))
903 return (ZERR_HEADERLEN);
906 if (ZMakeZcode32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
907 return ZERR_HEADERLEN;
909 if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
910 return (ZERR_HEADERLEN);
912 ptr += strlen(ptr)+1;
917 if (Z_AddField(&ptr, notice->z_multinotice, end))
918 return (ZERR_HEADERLEN);
920 if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid,
921 sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
922 return (ZERR_HEADERLEN);
923 ptr += strlen(ptr)+1;
925 for (i=0;i<notice->z_num_other_fields;i++)
926 if (Z_AddField(&ptr, notice->z_other_fields[i], end))
927 return (ZERR_HEADERLEN);
930 *cksum_len = ptr-*cksum_start;
932 *hdr_len = ptr-buffer;
936 printf("Z_FormatRawHeader output:\n");
937 for (i = 0; i < *hdr_len; i += 16) {
940 for (i2 = i; i2 < i+16 && i2 < *hdr_len; i2++)
941 printf(" %02x", buffer[i2] & 0xff);
942 for (; i2 < i+16; i2++)
945 for (i2 = i; i2 < i+16 && i2 < *hdr_len; i2++)
947 ((buffer[i2] > 0 && buffer[i2] < 127 && isprint(buffer[i2]))
958 Code_t Z_FormatRawHeader(notice, buffer, buffer_len, len, cstart, cend)
963 char **cstart, **cend;
965 char newrecip[BUFSIZ];
969 if (!notice->z_class)
970 notice->z_class = "";
972 if (!notice->z_class_inst)
973 notice->z_class_inst = "";
975 if (!notice->z_opcode)
976 notice->z_opcode = "";
978 if (!notice->z_recipient)
979 notice->z_recipient = "";
981 if (!notice->z_default_format)
982 notice->z_default_format = "";
985 end = buffer+buffer_len;
987 if (buffer_len < strlen(notice->z_version)+1)
988 return (ZERR_HEADERLEN);
990 (void) strcpy(ptr, notice->z_version);
991 ptr += strlen(ptr)+1;
993 if (ZMakeAscii32(ptr, end-ptr, Z_NUMFIELDS + notice->z_num_other_fields)
995 return (ZERR_HEADERLEN);
996 ptr += strlen(ptr)+1;
998 if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN)
999 return (ZERR_HEADERLEN);
1000 ptr += strlen(ptr)+1;
1002 if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid,
1003 sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
1004 return (ZERR_HEADERLEN);
1005 ptr += strlen(ptr)+1;
1007 if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN)
1008 return (ZERR_HEADERLEN);
1009 ptr += strlen(ptr)+1;
1011 if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN)
1012 return (ZERR_HEADERLEN);
1013 ptr += strlen(ptr)+1;
1015 if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN)
1016 return (ZERR_HEADERLEN);
1017 ptr += strlen(ptr)+1;
1019 if (Z_AddField(&ptr, notice->z_ascii_authent, end))
1020 return (ZERR_HEADERLEN);
1021 if (Z_AddField(&ptr, notice->z_class, end))
1022 return (ZERR_HEADERLEN);
1023 if (Z_AddField(&ptr, notice->z_class_inst, end))
1024 return (ZERR_HEADERLEN);
1025 if (Z_AddField(&ptr, notice->z_opcode, end))
1026 return (ZERR_HEADERLEN);
1027 if (Z_AddField(&ptr, notice->z_sender, end))
1028 return (ZERR_HEADERLEN);
1029 if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) {
1030 if (Z_AddField(&ptr, notice->z_recipient, end))
1031 return (ZERR_HEADERLEN);
1034 if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 >
1036 return (ZERR_HEADERLEN);
1037 (void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm);
1038 if (Z_AddField(&ptr, newrecip, end))
1039 return (ZERR_HEADERLEN);
1041 if (Z_AddField(&ptr, notice->z_default_format, end))
1042 return (ZERR_HEADERLEN);
1044 /* copy back the end pointer location for crypto checksum */
1047 if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
1048 return (ZERR_HEADERLEN);
1049 ptr += strlen(ptr)+1;
1053 if (Z_AddField(&ptr, notice->z_multinotice, end))
1054 return (ZERR_HEADERLEN);
1056 if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid,
1057 sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
1058 return (ZERR_HEADERLEN);
1059 ptr += strlen(ptr)+1;
1061 for (i=0;i<notice->z_num_other_fields;i++)
1062 if (Z_AddField(&ptr, notice->z_other_fields[i], end))
1063 return (ZERR_HEADERLEN);
1071 Z_AddField(ptr, field, end)
1072 char **ptr, *field, *end;
1076 len = field ? strlen (field) + 1 : 1;
1081 (void) strcpy(*ptr, field);
1089 struct _Z_InputQ *Z_GetFirstComplete()
1091 struct _Z_InputQ *qptr;
1101 return ((struct _Z_InputQ *)0);
1104 struct _Z_InputQ *Z_GetNextComplete(qptr)
1105 struct _Z_InputQ *qptr;
1114 return ((struct _Z_InputQ *)0);
1117 void Z_RemQueue(qptr)
1118 struct _Z_InputQ *qptr;
1120 struct _Z_Hole *hole, *nexthole;
1123 __Q_CompleteLength--;
1125 __Q_Size -= qptr->msg_len;
1134 hole = qptr->holelist;
1136 nexthole = hole->next;
1141 if (qptr == __Q_Head && __Q_Head == __Q_Tail) {
1142 free ((char *)qptr);
1143 __Q_Head = (struct _Z_InputQ *)0;
1144 __Q_Tail = (struct _Z_InputQ *)0;
1148 if (qptr == __Q_Head) {
1149 __Q_Head = qptr->next;
1150 __Q_Head->prev = (struct _Z_InputQ *)0;
1151 free ((char *)qptr);
1154 if (qptr == __Q_Tail) {
1155 __Q_Tail = qptr->prev;
1156 __Q_Tail->next = (struct _Z_InputQ *)0;
1157 free ((char *)qptr);
1160 qptr->prev->next = qptr->next;
1161 qptr->next->prev = qptr->prev;
1162 free ((char *)qptr);
1166 Code_t Z_SendFragmentedNotice(notice, len, cert_func, send_func)
1169 Z_AuthProc cert_func;
1170 Z_SendProc send_func;
1172 ZNotice_t partnotice;
1175 int offset, hdrsize, fragsize, ret_len, message_len, waitforack;
1178 hdrsize = len-notice->z_message_len;
1179 fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE;
1183 waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED)
1184 && !__Zephyr_server);
1186 partnotice = *notice;
1188 while (offset < notice->z_message_len || !notice->z_message_len) {
1189 (void) sprintf(multi, "%d/%d", offset, notice->z_message_len);
1190 partnotice.z_multinotice = multi;
1192 (void) Z_gettimeofday(&partnotice.z_uid.tv,
1193 (struct timezone *)0);
1194 partnotice.z_uid.tv.tv_sec =
1195 htonl((u_long) partnotice.z_uid.tv.tv_sec);
1196 partnotice.z_uid.tv.tv_usec =
1197 htonl((u_long) partnotice.z_uid.tv.tv_usec);
1198 (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
1201 message_len = min(notice->z_message_len-offset, fragsize);
1202 partnotice.z_message = notice->z_message+offset;
1203 partnotice.z_message_len = message_len;
1204 if ((retval = Z_FormatAuthHeader(&partnotice, buffer, Z_MAXHEADERLEN,
1205 &ret_len, cert_func)) != ZERR_NONE) {
1208 memcpy(buffer + ret_len, partnotice.z_message, message_len);
1209 if ((retval = (*send_func)(&partnotice, buffer, ret_len+message_len,
1210 waitforack)) != ZERR_NONE) {
1214 if (!notice->z_message_len)
1222 Code_t Z_XmitFragment(notice, buf, len, wait)
1228 return(ZSendPacket(buf, len, wait));
1232 /* For debugging printing */
1233 const char *const ZNoticeKinds[] = {
1234 "UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK",
1242 #ifdef HAVE_STDARG_H
1243 void Z_debug (const char *format, ...)
1246 if (!__Z_debug_print)
1248 va_start (pvar, format);
1249 (*__Z_debug_print) (format, pvar, __Z_debug_print_closure);
1253 void Z_debug (va_alist) va_dcl
1257 if (!__Z_debug_print)
1260 format = va_arg (pvar, char *);
1261 (*__Z_debug_print) (format, pvar, __Z_debug_print_closure);
1266 void Z_debug_stderr (format, args, closure)
1272 vfprintf (stderr, format, args);
1274 _doprnt (format, args, stderr);
1276 putc ('\n', stderr);
1280 int ZGetFD () { return __Zephyr_fd; }
1283 int ZQLength () { return __Q_CompleteLength; }
1286 struct sockaddr_in ZGetDestAddr () { return __HM_addr; }
1289 Zconst char * ZGetRealm () { return __Zephyr_realm; }
1292 void ZSetDebug(proc, arg)
1293 void (*proc) __P((const char *, va_list, void *));
1296 __Z_debug_print = proc;
1297 __Z_debug_print_closure = arg;
1299 #endif /* Z_DEBUG */