]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/Zinternal.c
I am committing the unforgivable sin of committing big glop of code with a
[1ts-debian.git] / zephyr / lib / Zinternal.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the internal Zephyr routines.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id: Zinternal.c,v 1.42 2002/09/10 16:04:31 ghudson Exp $
7  *
8  *      Copyright (c) 1987,1988,1991 by the Massachusetts Institute of
9  *      Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h". 
12  */
13
14 #include <internal.h>
15 #include <arpa/inet.h>
16 #include <sys/socket.h>
17 #include <utmp.h>
18
19 #ifndef lint
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.";
24 #endif
25
26 extern char *inet_ntoa ();
27
28 int __Zephyr_fd = -1;
29 int __Zephyr_open;
30 int __Zephyr_port = -1;
31 struct in_addr __My_addr;
32 int __Q_CompleteLength;
33 int __Q_Size;
34 struct _Z_InputQ *__Q_Head, *__Q_Tail;
35 struct sockaddr_in __HM_addr;
36 struct sockaddr_in __HM_addr_real;
37 int __HM_set;
38 int __Zephyr_server;
39 ZLocations_t *__locate_list;
40 int __locate_num;
41 int __locate_next;
42 ZSubscription_t *__subscriptions_list;
43 int __subscriptions_num;
44 int __subscriptions_next;
45 int Z_discarded_packets = 0;
46
47 #ifdef HAVE_KRB5
48 /* This context is used throughout */
49 krb5_context Z_krb5_ctx;
50
51 static struct cksum_map_s {
52   krb5_enctype e;
53   krb5_cksumtype c;
54 } cksum_map[] = {
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 },
60
61   /* 
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
68    */
69
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 */ },
73
74   /* per draft-ietf-krb-wg-crypto-02.txt */
75   { 16 /* des3-cbc-sha1-kd */,        12 /* hmac-sha1-des3-kd */ },
76
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 */ },
80
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 */ },
84 };
85 #define N_CKSUM_MAP (sizeof(cksum_map) / sizeof(struct cksum_map_s))
86
87 Code_t Z_krb5_lookup_cksumtype(krb5_enctype e, krb5_cksumtype *c)
88 {
89   int i;
90
91   for (i = 0; i < N_CKSUM_MAP; i++) {
92     if (cksum_map[i].e == e) {
93       *c = cksum_map[i].c;
94       return ZERR_NONE;
95     }
96   }
97   return KRB5_PROG_ETYPE_NOSUPP;
98 }
99 #endif /* HAVE_KRB5 */
100
101 char __Zephyr_realm[REALM_SZ];
102
103 #ifdef Z_DEBUG
104 void (*__Z_debug_print) __P((const char *fmt, va_list args, void *closure));
105 void *__Z_debug_print_closure;
106 #endif
107
108 #define min(a,b) ((a)<(b)?(a):(b))
109
110 static int Z_AddField __P((char **ptr, char *field, char *end));
111 static int find_or_insert_uid __P((ZUnique_Id_t *uid, ZNotice_Kind_t kind));
112 static Code_t Z_ZcodeFormatRawHeader __P((ZNotice_t *, char *, int, int *, char **, 
113                                  int *, char **, char **, int cksumtype));
114
115 /* Find or insert uid in the old uids buffer.  The buffer is a sorted
116  * circular queue.  We make the assumption that most packets arrive in
117  * order, so we can usually search for a uid or insert it into the buffer
118  * by looking back just a few entries from the end.  Since this code is
119  * only executed by the client, the implementation isn't microoptimized. */
120 static int find_or_insert_uid(uid, kind)
121     ZUnique_Id_t *uid;
122     ZNotice_Kind_t kind;
123 {
124     static struct _filter {
125         ZUnique_Id_t    uid;
126         ZNotice_Kind_t  kind;
127         time_t          t;
128     } *buffer;
129     static long size;
130     static long start;
131     static long num;
132
133     time_t now;
134     struct _filter *new;
135     long i, j, new_size;
136     int result;
137
138     /* Initialize the uid buffer if it hasn't been done already. */
139     if (!buffer) {
140         size = Z_INITFILTERSIZE;
141         buffer = (struct _filter *) malloc(size * sizeof(*buffer));
142         if (!buffer)
143             return 0;
144     }
145
146     /* Age the uid buffer, discarding any uids older than the clock skew. */
147     time(&now);
148     while (num && (now - buffer[start % size].t) > CLOCK_SKEW)
149         start++, num--;
150     start %= size;
151
152     /* Make room for a new uid, since we'll probably have to insert one. */
153     if (num == size) {
154         new_size = size * 2 + 2;
155         new = (struct _filter *) malloc(new_size * sizeof(*new));
156         if (!new)
157             return 0;
158         for (i = 0; i < num; i++)
159             new[i] = buffer[(start + i) % size];
160         free(buffer);
161         buffer = new;
162         size = new_size;
163         start = 0;
164     }
165
166     /* Search for this uid in the buffer, starting from the end. */
167     for (i = start + num - 1; i >= start; i--) {
168         result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid));
169         if (result == 0 && buffer[i % size].kind == kind)
170             return 1;
171         if (result > 0)
172             break;
173     }
174
175     /* We didn't find it; insert the uid into the buffer after i. */
176     i++;
177     for (j = start + num; j > i; j--)
178         buffer[j % size] = buffer[(j - 1) % size];
179     buffer[i % size].uid = *uid;
180     buffer[i % size].kind = kind;
181     buffer[i % size].t = now;
182     num++;
183
184     return 0;
185 }
186
187
188 /* Return 1 if there is a packet waiting, 0 otherwise */
189
190 int Z_PacketWaiting()
191 {
192     struct timeval tv;
193     fd_set read;
194
195     tv.tv_sec = tv.tv_usec = 0;
196     FD_ZERO(&read);
197     FD_SET(ZGetFD(), &read);
198     return (select(ZGetFD() + 1, &read, NULL, NULL, &tv));
199
200
201
202 /* Wait for a complete notice to become available */
203
204 Code_t Z_WaitForComplete()
205 {
206     Code_t retval;
207
208     if (__Q_CompleteLength)
209         return (Z_ReadEnqueue());
210
211     while (!__Q_CompleteLength)
212         if ((retval = Z_ReadWait()) != ZERR_NONE)
213             return (retval);
214
215     return (ZERR_NONE);
216 }
217
218
219 /* Read any available packets and enqueue them */
220
221 Code_t Z_ReadEnqueue()
222 {
223     Code_t retval;
224
225     if (ZGetFD() < 0)
226         return (ZERR_NOPORT);
227     
228     while (Z_PacketWaiting())
229         if ((retval = Z_ReadWait()) != ZERR_NONE)
230             return (retval);
231
232     return (ZERR_NONE);
233 }
234
235
236 /*
237  * Search the queue for a notice with the proper multiuid - remove any
238  * notices that haven't been touched in a while
239  */
240
241 struct _Z_InputQ *Z_SearchQueue(uid, kind)
242     ZUnique_Id_t *uid;
243     ZNotice_Kind_t kind;
244 {
245     register struct _Z_InputQ *qptr;
246     struct _Z_InputQ *next;
247     struct timeval tv;
248
249     (void) gettimeofday(&tv, (struct timezone *)0);
250
251     qptr = __Q_Head;
252
253     while (qptr) {
254         if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind)
255             return (qptr);
256         next = qptr->next;
257         if (qptr->timep && (qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec))
258             Z_RemQueue(qptr);
259         qptr = next;
260     }
261     return (NULL);
262 }
263
264 /*
265  * Now we delve into really convoluted queue handling and
266  * fragmentation reassembly algorithms and other stuff you probably
267  * don't want to look at...
268  *
269  * This routine does NOT guarantee a complete packet will be ready when it
270  * returns.
271  */
272
273 Code_t Z_ReadWait()
274 {
275     register struct _Z_InputQ *qptr;
276     ZNotice_t notice;
277     ZPacket_t packet;
278     struct sockaddr_in olddest, from;
279     int from_len, packet_len, zvlen, part, partof;
280     char *slash;
281     Code_t retval;
282     fd_set fds;
283     struct timeval tv;
284
285     if (ZGetFD() < 0)
286         return (ZERR_NOPORT);
287         
288     FD_ZERO(&fds);
289     FD_SET(ZGetFD(), &fds);
290     tv.tv_sec = 60;
291     tv.tv_usec = 0;
292
293     if (select(ZGetFD() + 1, &fds, NULL, NULL, &tv) < 0)
294       return (errno);
295     if (!FD_ISSET(ZGetFD(), &fds))
296       return ETIMEDOUT;
297
298     from_len = sizeof(struct sockaddr_in);
299
300     packet_len = recvfrom(ZGetFD(), packet, sizeof(packet), 0, 
301                           (struct sockaddr *)&from, &from_len);
302
303     if (packet_len < 0)
304         return (errno);
305
306     if (!packet_len)
307         return (ZERR_EOF);
308
309     /* Ignore obviously non-Zephyr packets. */
310     zvlen = sizeof(ZVERSIONHDR) - 1;
311     if (packet_len < zvlen || memcmp(packet, ZVERSIONHDR, zvlen) != 0) {
312         Z_discarded_packets++;
313         return (ZERR_NONE);
314     }   
315
316     /* Parse the notice */
317     if ((retval = ZParseNotice(packet, packet_len, &notice)) != ZERR_NONE)
318         return (retval);
319
320     /*
321      * If we're not a server and the notice is of an appropriate kind,
322      * send back a CLIENTACK to whoever sent it to say we got it.
323      */
324     if (!__Zephyr_server) {
325         if (notice.z_kind != HMACK && notice.z_kind != SERVACK &&
326             notice.z_kind != SERVNAK && notice.z_kind != CLIENTACK) {
327             ZNotice_t tmpnotice;
328             ZPacket_t pkt;
329             int len;
330
331             tmpnotice = notice;
332             tmpnotice.z_kind = CLIENTACK;
333             tmpnotice.z_message_len = 0;
334             olddest = __HM_addr;
335             __HM_addr = from;
336             if ((retval = ZFormatSmallRawNotice(&tmpnotice, pkt, &len))
337                 != ZERR_NONE)
338                 return(retval);
339             if ((retval = ZSendPacket(pkt, len, 0)) != ZERR_NONE)
340                 return (retval);
341             __HM_addr = olddest;
342         }
343         if (find_or_insert_uid(&notice.z_uid, notice.z_kind))
344             return(ZERR_NONE);
345
346         /* Check authentication on the notice. */
347         notice.z_checked_auth = ZCheckAuthentication(&notice, &from);
348     }
349
350
351     /*
352      * Parse apart the z_multinotice field - if the field is blank for
353      * some reason, assume this packet stands by itself.
354      */
355     slash = strchr(notice.z_multinotice, '/');
356     if (slash) {
357         part = atoi(notice.z_multinotice);
358         partof = atoi(slash+1);
359         if (part > partof || partof == 0) {
360             part = 0;
361             partof = notice.z_message_len;
362         }
363     }
364     else {
365         part = 0;
366         partof = notice.z_message_len;
367     }
368
369     /* Too big a packet...just ignore it! */
370     if (partof > Z_MAXNOTICESIZE)
371         return (ZERR_NONE);
372
373     /*
374      * If we aren't a server and we can find a notice in the queue
375      * with the same multiuid field, insert the current fragment as
376      * appropriate.
377      */
378     switch (notice.z_kind) {
379     case SERVACK:
380     case SERVNAK:
381         /* The SERVACK and SERVNAK replies shouldn't be reassembled
382            (they have no parts).  Instead, we should hold on to the reply
383            ONLY if it's the first part of a fragmented message, i.e.
384            multi_uid == uid.  This allows programs to wait for the uid
385            of the first packet, and get a response when that notice
386            arrives.  Acknowledgements of the other fragments are discarded
387            (XXX we assume here that they all carry the same information
388            regarding failure/success)
389          */
390         if (!__Zephyr_server &&
391             !ZCompareUID(&notice.z_multiuid, &notice.z_uid))
392             /* they're not the same... throw away this packet. */
393             return(ZERR_NONE);
394         /* fall thru & process it */
395     default:
396         /* for HMACK types, we assume no packet loss (local loopback
397            connections).  The other types can be fragmented and MUST
398            run through this code. */
399         if (!__Zephyr_server && (qptr = Z_SearchQueue(&notice.z_multiuid,
400                                                       notice.z_kind))) {
401             /*
402              * If this is the first fragment, and we haven't already
403              * gotten a first fragment, grab the header from it.
404              */
405             if (part == 0 && !qptr->header) {
406                 qptr->header_len = packet_len-notice.z_message_len;
407                 qptr->header = (char *) malloc((unsigned) qptr->header_len);
408                 if (!qptr->header)
409                     return (ENOMEM);
410                 (void) memcpy(qptr->header, packet, qptr->header_len);
411             }
412             return (Z_AddNoticeToEntry(qptr, &notice, part));
413         }
414     }
415
416     /*
417      * We'll have to create a new entry...make sure the queue isn't
418      * going to get too big.
419      */
420     if (__Q_Size+(__Zephyr_server ? notice.z_message_len : partof) > Z_MAXQUEUESIZE)
421         return (ZERR_NONE);
422
423     /*
424      * This is a notice we haven't heard of, so create a new queue
425      * entry for it and zero it out.
426      */
427     qptr = (struct _Z_InputQ *)malloc(sizeof(struct _Z_InputQ));
428     if (!qptr)
429         return (ENOMEM);
430     (void) memset((char *)qptr, 0, sizeof(struct _Z_InputQ));
431
432     /* Insert the entry at the end of the queue */
433     qptr->next = NULL;
434     qptr->prev = __Q_Tail;
435     if (__Q_Tail)
436         __Q_Tail->next = qptr;
437     __Q_Tail = qptr;
438
439     if (!__Q_Head)
440         __Q_Head = qptr;
441
442     
443     /* Copy the from field, multiuid, kind, and checked authentication. */
444     qptr->from = from;
445     qptr->uid = notice.z_multiuid;
446     qptr->kind = notice.z_kind;
447     qptr->auth = notice.z_checked_auth;
448     
449     /*
450      * If this is the first part of the notice, we take the header
451      * from it.  We only take it if this is the first fragment so that
452      * the Unique ID's will be predictable.
453      *
454      * If a Zephyr Server, we always take the header.
455      */
456     if (__Zephyr_server || part == 0) {
457         qptr->header_len = packet_len-notice.z_message_len;
458         qptr->header = (char *) malloc((unsigned) qptr->header_len);
459         if (!qptr->header)
460             return ENOMEM;
461         (void) memcpy(qptr->header, packet, qptr->header_len);
462     }
463
464     /*
465      * If this is not a fragmented notice, then don't bother with a
466      * hole list.
467      * If we are a Zephyr server, all notices are treated as complete.
468      */
469     if (__Zephyr_server || (part == 0 && notice.z_message_len == partof)) {
470         __Q_CompleteLength++;
471         qptr->holelist = (struct _Z_Hole *) 0;
472         qptr->complete = 1;
473         /* allocate a msg buf for this piece */
474         if (notice.z_message_len == 0)
475             qptr->msg = 0;
476         else if (!(qptr->msg = (char *) malloc((unsigned) notice.z_message_len)))
477             return(ENOMEM);
478         else
479             (void) memcpy(qptr->msg, notice.z_message, notice.z_message_len);
480         qptr->msg_len = notice.z_message_len;
481         __Q_Size += notice.z_message_len;
482         qptr->packet_len = qptr->header_len+qptr->msg_len;
483         if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
484             return (ENOMEM);
485         (void) memcpy(qptr->packet, qptr->header, qptr->header_len);
486         if(qptr->msg)
487             (void) memcpy(qptr->packet+qptr->header_len, qptr->msg,
488                            qptr->msg_len);
489         return (ZERR_NONE);
490     }
491
492     /*
493      * We know how long the message is going to be (this is better
494      * than IP fragmentation...), so go ahead and allocate it all.
495      */
496     if (!(qptr->msg = (char *) malloc((unsigned) partof)) && partof)
497         return (ENOMEM);
498     qptr->msg_len = partof;
499     __Q_Size += partof;
500
501     /*
502      * Well, it's a fragmented notice...allocate a hole list and
503      * initialize it to the full packet size.  Then insert the
504      * current fragment.
505      */
506     if (!(qptr->holelist = (struct _Z_Hole *)
507           malloc(sizeof(struct _Z_Hole))))
508         return (ENOMEM);
509     qptr->holelist->next = (struct _Z_Hole *) 0;
510     qptr->holelist->first = 0;
511     qptr->holelist->last = partof-1;
512     return (Z_AddNoticeToEntry(qptr, &notice, part));
513 }
514
515
516 /* Fragment management routines - compliments, more or less, of RFC815 */
517
518 Code_t Z_AddNoticeToEntry(qptr, notice, part)
519     struct _Z_InputQ *qptr;
520     ZNotice_t *notice;
521     int part;
522 {
523     int last, oldfirst, oldlast;
524     struct _Z_Hole *hole, *lasthole;
525     struct timeval tv;
526
527     /* Bounds check. */
528     if (part < 0 || notice->z_message_len < 0 || part > qptr->msg_len
529         || notice->z_message_len > qptr->msg_len - part)
530       return (ZERR_NONE);
531
532     /* Incorporate this notice's checked authentication. */
533     if (notice->z_checked_auth == ZAUTH_FAILED)
534         qptr->auth = ZAUTH_FAILED;
535     else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED)
536         qptr->auth = ZAUTH_NO;
537
538     (void) gettimeofday(&tv, (struct timezone *)0);
539     qptr->timep = tv.tv_sec;
540     
541     last = part+notice->z_message_len-1;
542
543     hole = qptr->holelist;
544     lasthole = (struct _Z_Hole *) 0;
545
546     /* copy in the message body */
547     (void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len);
548
549     /* Search for a hole that overlaps with the current fragment */
550     while (hole) {
551         if (part <= hole->last && last >= hole->first)
552             break;
553         lasthole = hole;
554         hole = hole->next;
555     }
556
557     /* If we found one, delete it and reconstruct a new hole */
558     if (hole) {
559         oldfirst = hole->first;
560         oldlast = hole->last;
561         if (lasthole)
562             lasthole->next = hole->next;
563         else
564             qptr->holelist = hole->next;
565         free((char *)hole);
566         /*
567          * Now create a new hole that is the original hole without the
568          * current fragment.
569          */
570         if (part > oldfirst) {
571             /* Search for the end of the hole list */
572             hole = qptr->holelist;
573             lasthole = (struct _Z_Hole *) 0;
574             while (hole) {
575                 lasthole = hole;
576                 hole = hole->next;
577             }
578             if (lasthole) {
579                 if (!(lasthole->next = (struct _Z_Hole *)
580                       malloc(sizeof(struct _Z_InputQ))))
581                     return (ENOMEM);
582                 hole = lasthole->next;
583             }
584             else {
585                 if (!(qptr->holelist = (struct _Z_Hole *)
586                       malloc(sizeof(struct _Z_InputQ))))
587                     return (ENOMEM);
588                 hole = qptr->holelist;
589             }
590             hole->next = NULL;
591             hole->first = oldfirst;
592             hole->last = part-1;
593         }
594         if (last < oldlast) {
595             /* Search for the end of the hole list */
596             hole = qptr->holelist;
597             lasthole = (struct _Z_Hole *) 0;
598             while (hole) {
599                 lasthole = hole;
600                 hole = hole->next;
601             }
602             if (lasthole) {
603                 if (!(lasthole->next = (struct _Z_Hole *)
604                       malloc(sizeof(struct _Z_InputQ))))
605                     return (ENOMEM);
606                 hole = lasthole->next;
607             }
608             else {
609                 if (!(qptr->holelist = (struct _Z_Hole *)
610                       malloc(sizeof(struct _Z_InputQ))))
611                     return (ENOMEM);
612                 hole = qptr->holelist;
613             }
614             hole->next = (struct _Z_Hole *) 0;
615             hole->first = last+1;
616             hole->last = oldlast;
617         }
618     }
619
620     if (!qptr->holelist) {
621         if (!qptr->complete)
622             __Q_CompleteLength++;
623         qptr->complete = 1;
624         qptr->timep = 0;                /* don't time out anymore */
625         qptr->packet_len = qptr->header_len+qptr->msg_len;
626         if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
627             return (ENOMEM);
628         (void) memcpy(qptr->packet, qptr->header, qptr->header_len);
629         (void) memcpy(qptr->packet+qptr->header_len, qptr->msg,
630                        qptr->msg_len);
631     }
632     
633     return (ZERR_NONE);
634 }
635
636 void Z_gettimeofday(struct _ZTimeval *ztv, struct timezone *tz)
637 {
638         struct timeval tv;
639         (void) gettimeofday(&tv, tz); /* yeah, yeah, I know */
640         ztv->tv_sec=tv.tv_sec;
641         ztv->tv_usec=tv.tv_usec;
642 }
643
644 Code_t Z_FormatHeader(notice, buffer, buffer_len, len, cert_routine)
645     ZNotice_t *notice;
646     char *buffer;
647     int buffer_len;
648     int *len;
649     Z_AuthProc cert_routine;
650 {
651     Code_t retval;
652     static char version[BUFSIZ]; /* default init should be all \0 */
653     struct sockaddr_in name;
654     int namelen = sizeof(name);
655
656     if (!notice->z_sender)
657         notice->z_sender = ZGetSender();
658
659     if (notice->z_port == 0) {
660         if (ZGetFD() < 0) {
661             retval = ZOpenPort((u_short *)0);
662             if (retval != ZERR_NONE)
663                 return (retval);
664         }
665         retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen);
666         if (retval != 0)
667             return (retval);
668         notice->z_port = name.sin_port;
669     }
670
671     notice->z_multinotice = "";
672     
673     (void) Z_gettimeofday(&notice->z_uid.tv, (struct timezone *)0);
674     notice->z_uid.tv.tv_sec = htonl((u_long) notice->z_uid.tv.tv_sec);
675     notice->z_uid.tv.tv_usec = htonl((u_long) notice->z_uid.tv.tv_usec);
676     
677     (void) memcpy(&notice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
678
679     notice->z_multiuid = notice->z_uid;
680
681     if (!version[0])
682             (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
683                            ZVERSIONMINOR);
684     notice->z_version = version;
685
686     return Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);
687 }
688
689 Code_t Z_NewFormatHeader(notice, buffer, buffer_len, len, cert_routine)
690     ZNotice_t *notice;
691     char *buffer;
692     int buffer_len;
693     int *len;
694     Z_AuthProc cert_routine;
695 {
696     Code_t retval;
697     static char version[BUFSIZ]; /* default init should be all \0 */
698     struct sockaddr_in name;
699     struct timeval tv;
700     int namelen = sizeof(name);
701
702     if (!notice->z_sender)
703         notice->z_sender = ZGetSender();
704
705     if (notice->z_port == 0) {
706         if (ZGetFD() < 0) {
707             retval = ZOpenPort((u_short *)0);
708             if (retval != ZERR_NONE)
709                 return (retval);
710         }
711         retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen);
712         if (retval != 0)
713             return (retval);
714         notice->z_port = name.sin_port;
715     }
716
717     notice->z_multinotice = "";
718     
719     (void) gettimeofday(&tv, (struct timezone *)0);
720     notice->z_uid.tv.tv_sec = htonl((u_long) tv.tv_sec);
721     notice->z_uid.tv.tv_usec = htonl((u_long) tv.tv_usec);
722     
723     (void) memcpy(&notice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
724
725     notice->z_multiuid = notice->z_uid;
726
727     if (!version[0])
728             (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
729                            ZVERSIONMINOR);
730     notice->z_version = version;
731
732     return Z_NewFormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);
733 }
734
735 Code_t Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine)
736     ZNotice_t *notice;
737     char *buffer;
738     int buffer_len;
739     int *len;
740     Z_AuthProc cert_routine;
741 {
742     if (!cert_routine) {
743         notice->z_auth = 0;
744         notice->z_authent_len = 0;
745         notice->z_ascii_authent = "";
746         notice->z_checksum = 0;
747         return (Z_FormatRawHeader(notice, buffer, buffer_len,
748                                   len, NULL, NULL));
749     }
750     
751     return ((*cert_routine)(notice, buffer, buffer_len, len));
752
753         
754 Code_t Z_NewFormatAuthHeader(notice, buffer, buffer_len, len, cert_routine)
755     ZNotice_t *notice;
756     char *buffer;
757     int buffer_len;
758     int *len;
759     Z_AuthProc cert_routine;
760 {
761     if (!cert_routine) {
762         notice->z_auth = 0;
763         notice->z_authent_len = 0;
764         notice->z_ascii_authent = "";
765         notice->z_checksum = 0;
766         return (Z_FormatRawHeader(notice, buffer, buffer_len,
767                                      len, NULL, NULL));
768     }
769     
770     return ((*cert_routine)(notice, buffer, buffer_len, len));
771
772         
773 Code_t Z_NewFormatRawHeader(notice, buffer, buffer_len, hdr_len,
774                          cksum_start, cksum_len, cstart, cend)
775     ZNotice_t *notice;
776     char *buffer;
777     int buffer_len;
778     int *hdr_len;
779     char **cksum_start;
780     int *cksum_len;
781     char **cstart, **cend;
782 {
783    return(Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len,
784                                  cksum_start, cksum_len, cstart, cend, 0));
785 }
786
787 Code_t Z_AsciiFormatRawHeader(notice, buffer, buffer_len, hdr_len,
788                               cksum_start, cksum_len, cstart, cend)
789     ZNotice_t *notice;
790     char *buffer;
791     int buffer_len;
792     int *hdr_len;
793     char **cksum_start;
794     int *cksum_len;
795     char **cstart, **cend;
796 {
797    return(Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len,
798                                  cksum_start, cksum_len, cstart, cend, 1));
799 }
800
801 static Code_t Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len, cksum_start,
802                               cksum_len, cstart, cend, cksumstyle)
803     ZNotice_t *notice;
804     char *buffer;
805     int buffer_len;
806     int *hdr_len;
807     char **cksum_start;
808     int *cksum_len;
809     char **cstart, **cend;
810     int cksumstyle;
811 {
812     static char version_nogalaxy[BUFSIZ]; /* default init should be all \0 */
813     char newrecip[BUFSIZ];
814     char *ptr, *end;
815     int i;
816
817     if (!notice->z_class)
818             notice->z_class = "";
819
820     if (!notice->z_class_inst)
821             notice->z_class_inst = "";
822
823     if (!notice->z_opcode)
824             notice->z_opcode = "";
825
826     if (!notice->z_recipient)
827             notice->z_recipient = "";
828
829     if (!notice->z_default_format)
830             notice->z_default_format = "";
831
832     ptr = buffer;
833     end = buffer+buffer_len;
834
835     if (cksum_start)
836         *cksum_start = ptr;
837
838     (void) sprintf(version_nogalaxy, "%s%d.%d", ZVERSIONHDR,
839                    ZVERSIONMAJOR, ZVERSIONMINOR);
840
841     notice->z_version = version_nogalaxy;
842
843     if (Z_AddField(&ptr, version_nogalaxy, end))
844         return (ZERR_HEADERLEN);
845
846     if (ZMakeAscii32(ptr, end-ptr,
847                      Z_NUMFIELDS + notice->z_num_other_fields)
848         == ZERR_FIELDLEN)
849         return (ZERR_HEADERLEN);
850     ptr += strlen(ptr)+1;
851
852     if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN)
853         return (ZERR_HEADERLEN);
854     ptr += strlen(ptr)+1;
855
856     if (ZMakeAscii(ptr, end-ptr, (unsigned char *)&notice->z_uid, 
857                    sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
858         return (ZERR_HEADERLEN);
859     ptr += strlen(ptr)+1;
860
861     if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN)
862         return (ZERR_HEADERLEN);
863     ptr += strlen(ptr)+1;
864
865     if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN)
866         return (ZERR_HEADERLEN);
867     ptr += strlen(ptr)+1;
868
869     if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN)
870         return (ZERR_HEADERLEN);
871     ptr += strlen(ptr)+1;
872
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);
886     }
887     else {
888         if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 >
889             sizeof(newrecip))
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);
894     }           
895     if (Z_AddField(&ptr, notice->z_default_format, end))
896         return (ZERR_HEADERLEN);
897
898     /* copy back the end pointer location for crypto checksum */
899     if (cstart)
900         *cstart = ptr;
901     if (cksumstyle == 1) {
902       if (Z_AddField(&ptr, notice->z_ascii_checksum, end))
903          return (ZERR_HEADERLEN);
904     } else {
905 #ifdef xZCODE_K4SUM
906     if (ZMakeZcode32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
907         return ZERR_HEADERLEN;
908 #else
909     if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
910         return (ZERR_HEADERLEN);
911 #endif
912     ptr += strlen(ptr)+1;
913     }
914     if (cend)
915         *cend = ptr;
916
917     if (Z_AddField(&ptr, notice->z_multinotice, end))
918         return (ZERR_HEADERLEN);
919
920     if (ZMakeAscii(ptr, end-ptr, (unsigned char *)&notice->z_multiuid, 
921                    sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
922         return (ZERR_HEADERLEN);
923     ptr += strlen(ptr)+1;
924         
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);
928     
929     if (cksum_len)
930         *cksum_len = ptr-*cksum_start;
931
932     *hdr_len = ptr-buffer;
933
934 #if 0
935     {
936         printf("Z_FormatRawHeader output:\n");
937         for (i = 0; i < *hdr_len; i += 16) {
938             int i2;
939             printf("%03d:", i);
940             for (i2 = i; i2 < i+16 && i2 < *hdr_len; i2++)
941                 printf(" %02x", buffer[i2] & 0xff);
942             for (; i2 < i+16; i2++)
943                 printf("   ");
944             printf("  ");
945             for (i2 = i; i2 < i+16 && i2 < *hdr_len; i2++)
946                 printf("%c",
947                        ((buffer[i2] > 0 && buffer[i2] < 127 && isprint(buffer[i2]))
948                         ? buffer[i2]
949                         : '.'));
950             printf("\n");
951         }
952     }
953 #endif
954
955     return (ZERR_NONE);
956 }
957
958 Code_t Z_FormatRawHeader(notice, buffer, buffer_len, len, cstart, cend)
959     ZNotice_t *notice;
960     char *buffer;
961     int buffer_len;
962     int *len;
963     char **cstart, **cend;
964 {
965     char newrecip[BUFSIZ];
966     char *ptr, *end;
967     int i;
968
969     if (!notice->z_class)
970             notice->z_class = "";
971
972     if (!notice->z_class_inst)
973             notice->z_class_inst = "";
974
975     if (!notice->z_opcode)
976             notice->z_opcode = "";
977
978     if (!notice->z_recipient)
979             notice->z_recipient = "";
980
981     if (!notice->z_default_format)
982             notice->z_default_format = "";
983
984     ptr = buffer;
985     end = buffer+buffer_len;
986
987     if (buffer_len < strlen(notice->z_version)+1)
988         return (ZERR_HEADERLEN);
989
990     (void) strcpy(ptr, notice->z_version);
991     ptr += strlen(ptr)+1;
992
993     if (ZMakeAscii32(ptr, end-ptr, Z_NUMFIELDS + notice->z_num_other_fields)
994         == ZERR_FIELDLEN)
995         return (ZERR_HEADERLEN);
996     ptr += strlen(ptr)+1;
997
998     if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN)
999         return (ZERR_HEADERLEN);
1000     ptr += strlen(ptr)+1;
1001
1002     if (ZMakeAscii(ptr, end-ptr, (unsigned char *)&notice->z_uid, 
1003                    sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
1004         return (ZERR_HEADERLEN);
1005     ptr += strlen(ptr)+1;
1006
1007     if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN)
1008         return (ZERR_HEADERLEN);
1009     ptr += strlen(ptr)+1;
1010
1011     if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN)
1012         return (ZERR_HEADERLEN);
1013     ptr += strlen(ptr)+1;
1014
1015     if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN)
1016         return (ZERR_HEADERLEN);
1017     ptr += strlen(ptr)+1;
1018
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);
1032     }
1033     else {
1034         if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 >
1035             sizeof(newrecip))
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);
1040     }           
1041     if (Z_AddField(&ptr, notice->z_default_format, end))
1042         return (ZERR_HEADERLEN);
1043
1044     /* copy back the end pointer location for crypto checksum */
1045     if (cstart)
1046         *cstart = ptr;
1047     if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
1048         return (ZERR_HEADERLEN);
1049     ptr += strlen(ptr)+1;
1050     if (cend)
1051         *cend = ptr;
1052
1053     if (Z_AddField(&ptr, notice->z_multinotice, end))
1054         return (ZERR_HEADERLEN);
1055
1056     if (ZMakeAscii(ptr, end-ptr, (unsigned char *)&notice->z_multiuid, 
1057                    sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
1058         return (ZERR_HEADERLEN);
1059     ptr += strlen(ptr)+1;
1060         
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);
1064     
1065     *len = ptr-buffer;
1066         
1067     return (ZERR_NONE);
1068 }
1069
1070 static int
1071 Z_AddField(ptr, field, end)
1072     char **ptr, *field, *end;
1073 {
1074     register int len;
1075
1076     len = field ? strlen (field) + 1 : 1;
1077
1078     if (*ptr+len > end)
1079         return 1;
1080     if (field)
1081         (void) strcpy(*ptr, field);
1082     else
1083         **ptr = '\0';
1084     *ptr += len;
1085
1086     return 0;
1087 }
1088
1089 struct _Z_InputQ *Z_GetFirstComplete()
1090 {
1091     struct _Z_InputQ *qptr;
1092
1093     qptr = __Q_Head;
1094
1095     while (qptr) {
1096         if (qptr->complete)
1097             return (qptr);
1098         qptr = qptr->next;
1099     }
1100
1101     return ((struct _Z_InputQ *)0);
1102 }
1103
1104 struct _Z_InputQ *Z_GetNextComplete(qptr)
1105     struct _Z_InputQ *qptr;
1106 {
1107     qptr = qptr->next;
1108     while (qptr) {
1109         if (qptr->complete)
1110             return (qptr);
1111         qptr = qptr->next;
1112     }
1113
1114     return ((struct _Z_InputQ *)0);
1115 }
1116
1117 void Z_RemQueue(qptr)
1118     struct _Z_InputQ *qptr;
1119 {
1120     struct _Z_Hole *hole, *nexthole;
1121     
1122     if (qptr->complete)
1123         __Q_CompleteLength--;
1124
1125     __Q_Size -= qptr->msg_len;
1126     
1127     if (qptr->header)
1128         free(qptr->header);
1129     if (qptr->msg)
1130         free(qptr->msg);
1131     if (qptr->packet)
1132         free(qptr->packet);
1133     
1134     hole = qptr->holelist;
1135     while (hole) {
1136         nexthole = hole->next;
1137         free((char *)hole);
1138         hole = nexthole;
1139     }
1140     
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;
1145         return;
1146     }
1147     
1148     if (qptr == __Q_Head) {
1149         __Q_Head = qptr->next;
1150         __Q_Head->prev = (struct _Z_InputQ *)0;
1151         free ((char *)qptr);
1152         return;
1153     } 
1154     if (qptr == __Q_Tail) {
1155         __Q_Tail = qptr->prev;
1156         __Q_Tail->next = (struct _Z_InputQ *)0;
1157         free ((char *)qptr);
1158         return;
1159     }
1160     qptr->prev->next = qptr->next;
1161     qptr->next->prev = qptr->prev;
1162     free ((char *)qptr);
1163     return;
1164 }
1165
1166 Code_t Z_SendFragmentedNotice(notice, len, cert_func, send_func)
1167     ZNotice_t *notice;
1168     int len;
1169     Z_AuthProc cert_func;
1170     Z_SendProc send_func;
1171 {
1172     ZNotice_t partnotice;
1173     ZPacket_t buffer;
1174     char multi[64];
1175     int offset, hdrsize, fragsize, ret_len, message_len, waitforack;
1176     Code_t retval;
1177     
1178     hdrsize = len-notice->z_message_len;
1179     fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE;
1180     
1181     offset = 0;
1182
1183     waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED)
1184                   && !__Zephyr_server);
1185     
1186     partnotice = *notice;
1187
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;
1191         if (offset > 0) {
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, 
1199                           sizeof(__My_addr));
1200         }
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) {
1206             return (retval);
1207         }
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) {
1211             return (retval);
1212         }
1213         offset += fragsize;
1214         if (!notice->z_message_len)
1215             break;
1216     }
1217
1218     return (ZERR_NONE);
1219 }
1220
1221 /*ARGSUSED*/
1222 Code_t Z_XmitFragment(notice, buf, len, wait)
1223 ZNotice_t *notice;
1224 char *buf;
1225 int len;
1226 int wait;
1227 {
1228         return(ZSendPacket(buf, len, wait));
1229 }
1230
1231 #ifdef Z_DEBUG
1232 /* For debugging printing */
1233 const char *const ZNoticeKinds[] = {
1234     "UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK",
1235     "CLIENTACK", "STAT"
1236 };
1237 #endif
1238
1239 #ifdef Z_DEBUG
1240
1241 #undef Z_debug
1242 #ifdef HAVE_STDARG_H
1243 void Z_debug (const char *format, ...)
1244 {
1245     va_list pvar;
1246     if (!__Z_debug_print)
1247       return;
1248     va_start (pvar, format);
1249     (*__Z_debug_print) (format, pvar, __Z_debug_print_closure);
1250     va_end (pvar);
1251 }
1252 #else /* stdarg */
1253 void Z_debug (va_alist) va_dcl
1254 {
1255     va_list pvar;
1256     char *format;
1257     if (!__Z_debug_print)
1258       return;
1259     va_start (pvar);
1260     format = va_arg (pvar, char *);
1261     (*__Z_debug_print) (format, pvar, __Z_debug_print_closure);
1262     va_end (pvar);
1263 }
1264 #endif
1265
1266 void Z_debug_stderr (format, args, closure)
1267      const char *format;
1268      va_list args;
1269      void *closure;
1270 {
1271 #ifdef HAVE_VPRINTF
1272     vfprintf (stderr, format, args);
1273 #else
1274     _doprnt (format, args, stderr);
1275 #endif
1276     putc ('\n', stderr);
1277 }
1278
1279 #undef ZGetFD
1280 int ZGetFD () { return __Zephyr_fd; }
1281
1282 #undef ZQLength
1283 int ZQLength () { return __Q_CompleteLength; }
1284
1285 #undef ZGetDestAddr
1286 struct sockaddr_in ZGetDestAddr () { return __HM_addr; }
1287
1288 #undef ZGetRealm
1289 Zconst char * ZGetRealm () { return __Zephyr_realm; }
1290
1291 #undef ZSetDebug
1292 void ZSetDebug(proc, arg)
1293     void (*proc) __P((const char *, va_list, void *));
1294     char *arg;
1295 {
1296     __Z_debug_print = proc;
1297     __Z_debug_print_closure = arg;
1298 }
1299 #endif /* Z_DEBUG */
1300
1301 #ifdef HAVE_KRB5
1302 Code_t Z_Checksum(krb5_data *cksumbuf, krb5_keyblock *keyblock, 
1303                   krb5_cksumtype cksumtype, 
1304                   char **asn1_data, int *asn1_len) {
1305     krb5_error_code result;
1306     char *data;
1307     int len;
1308 #if HAVE_KRB5_C_MAKE_CHECKSUM
1309     krb5_checksum checksum;
1310 #else
1311     Checksum checksum;
1312     krb5_crypto cryptctx;
1313 #endif
1314     
1315 #if HAVE_KRB5_C_MAKE_CHECKSUM
1316     /* Create the checksum -- MIT crypto API */
1317     result = krb5_c_make_checksum(Z_krb5_ctx, cksumtype,
1318                                   keyblock, Z_KEYUSAGE_CLT_CKSUM,
1319                                   cksumbuf, &checksum);
1320     if (result)
1321         return result;
1322     /* HOLDING: checksum */
1323
1324     data = checksum.contents;
1325     len = checksum.length;
1326 #else
1327     /* Create the checksum -- heimdal crypto API */
1328     result = krb5_crypto_init(Z_krb5_ctx, keyblock, keyblock->keytype, 
1329                               &cryptctx);
1330     if (result)
1331         return result;
1332
1333     /* HOLDING: cryptctx */
1334     result = krb5_create_checksum(Z_krb5_ctx, cryptctx,
1335                                   Z_KEYUSAGE_CLT_CKSUM, cksumtype,
1336                                   cksumbuf->data, cksumbuf->length,
1337                                   &checksum);
1338     krb5_crypto_destroy(Z_krb5_ctx, cryptctx);
1339     if (result)
1340         return result;
1341
1342     len = checksum.checksum.length;
1343     data = checksum.checksum.data;
1344     /* HOLDING: checksum */
1345 #endif
1346
1347     *asn1_data = malloc(len);
1348     if (*asn1_data == NULL)
1349         return errno;
1350     memcpy(*asn1_data, data, len);
1351     *asn1_len = len;
1352
1353 #if HAVE_KRB5_C_MAKE_CHECKSUM
1354     krb5_free_checksum_contents(Z_krb5_ctx, &checksum);
1355 #else
1356     free_Checksum(&checksum);
1357 #endif
1358
1359     return 0;
1360 }
1361
1362 Code_t
1363 Z_InsertZcodeChecksum(krb5_keyblock *keyblock, ZNotice_t *notice, 
1364                       char *buffer, char *cksum_start, int cksum_len, 
1365                       char *cstart, char *cend, int buffer_len, 
1366                       int *length_adjust)
1367 {
1368      int plain_len;   /* length of part not to be checksummed */
1369      int cksum0_len;  /* length of part before checksum */
1370      int cksum1_len;  /* length of part after checksum */
1371      krb5_data cksumbuf;
1372      krb5_data cksum;
1373      char *key_data;
1374      int key_len;
1375      krb5_enctype enctype;
1376      krb5_cksumtype cksumtype;
1377      Code_t result;
1378      
1379      key_data = Z_keydata(keyblock);
1380      key_len = Z_keylen(keyblock);
1381      result = Z_ExtractEncCksum(keyblock, &enctype, &cksumtype);
1382      if (result)
1383           return (ZAUTH_FAILED);
1384      
1385      /* Assemble the things to be checksummed */
1386      plain_len  = cksum_start - buffer;
1387      cksum0_len = cstart - cksum_start;
1388      cksum1_len = (cksum_start + cksum_len) - cend;
1389      memset(&cksumbuf, 0, sizeof(cksumbuf));
1390      cksumbuf.length = cksum0_len + cksum1_len + notice->z_message_len;
1391      cksumbuf.data = malloc(cksumbuf.length);
1392      if (!cksumbuf.data)
1393           return ENOMEM;
1394      memcpy(cksumbuf.data, cksum_start, cksum0_len);
1395      memcpy(cksumbuf.data + cksum0_len, cend, cksum1_len);
1396      memcpy(cksumbuf.data + cksum0_len + cksum1_len,
1397             notice->z_message, notice->z_message_len);
1398      /* compute the checksum */
1399      result = Z_Checksum(&cksumbuf, keyblock, cksumtype, 
1400                         (char **)&cksum.data, &cksum.length);
1401      if (result) {
1402           free(cksumbuf.data);
1403           return result;
1404      }
1405      
1406      /*
1407       * OK....  we can zcode to a space starting at 'cstart',
1408       * with a length of buffer_len - (plain_len + cksum_len).
1409       * Then we tack on the end part, which is located at
1410       * cksumbuf.data + cksum0_len and has length cksum1_len
1411       */
1412      
1413      result = ZMakeZcode(cstart, buffer_len - (plain_len + cksum_len),
1414                          cksum.data, cksum.length);
1415      free(cksum.data);
1416      if (!result) {
1417           int zcode_len = strlen(cstart) + 1;
1418           memcpy(cstart + zcode_len, cksumbuf.data + cksum0_len, cksum1_len);
1419           *length_adjust = zcode_len - cksum_len + (cksum0_len + cksum1_len);
1420      }
1421      free(cksumbuf.data);
1422      return result;
1423 }
1424
1425 Code_t
1426 Z_ExtractEncCksum(krb5_keyblock *keyblock, krb5_enctype *enctype, 
1427                   krb5_cksumtype *cksumtype) {
1428 #if HAVE_KRB5_CREDS_KEYBLOCK_ENCTYPE
1429     *enctype  = keyblock->enctype; 
1430     return Z_krb5_lookup_cksumtype(*enctype, cksumtype); 
1431 #else 
1432     unsigned int len; 
1433     ENCTYPE *val; 
1434     int i = 0; 
1435     Code_t result;
1436  
1437     result = krb5_keytype_to_enctypes(Z_krb5_ctx, keyblock->keytype, 
1438                                       &len, &val); 
1439     if (result)
1440       return result;
1441     
1442     do { 
1443       if (i == len) break;
1444       result = Z_krb5_lookup_cksumtype(val[i], cksumtype); 
1445       i++;
1446     } while (result != 0); 
1447     
1448     if (result)
1449       return result;
1450
1451     *enctype = val[i-1]; 
1452 #endif
1453     return 0;
1454 }
1455 #endif
1456
1457 #ifdef HAVE_KRB5
1458 /* returns 0 if invalid or losing, 1 if valid, *sigh* */
1459 int
1460 Z_krb5_verify_cksum(krb5_keyblock *keyblock, krb5_data *cksumbuf, 
1461                     krb5_cksumtype cksumtype, char *asn1_data, 
1462                     int asn1_len) {
1463     krb5_error_code result;
1464 #if HAVE_KRB5_C_MAKE_CHECKSUM
1465     krb5_checksum checksum;
1466     krb5_boolean valid;
1467 #else
1468     krb5_crypto cryptctx;
1469     Checksum checksum;
1470     size_t xlen;
1471 #endif
1472
1473     memset(&checksum, 0, sizeof(checksum));
1474 #if HAVE_KRB5_C_MAKE_CHECKSUM
1475     /* Verify the checksum -- MIT crypto API */
1476     checksum.length = asn1_len;
1477     checksum.contents = asn1_data;
1478     checksum.checksum_type = cksumtype;
1479     result = krb5_c_verify_checksum(Z_krb5_ctx,
1480                                     keyblock, Z_KEYUSAGE_SRV_CKSUM,
1481                                     cksumbuf, &checksum, &valid);
1482     if (!result && valid)
1483         return 1;
1484     else
1485         return 0;
1486 #else
1487     checksum.checksum.length = asn1_len;
1488     checksum.checksum.data = asn1_data;
1489     checksum.cksumtype = cksumtype;
1490
1491     result = krb5_crypto_init(Z_krb5_ctx, keyblock, keyblock->keytype, &cryptctx);
1492     if (result)
1493         return result;
1494     
1495     /* HOLDING: cryptctx */
1496     result = krb5_verify_checksum(Z_krb5_ctx, cryptctx,
1497                                   Z_KEYUSAGE_SRV_CKSUM,
1498                                   cksumbuf->data, cksumbuf->length,
1499                                   &checksum);
1500     krb5_crypto_destroy(Z_krb5_ctx, cryptctx);
1501     if (result)
1502         return 0;
1503     else
1504         return 1;
1505 #endif
1506 }
1507 #endif