/* This file is part of the Project Athena Zephyr Notification System. * It contains source for the internal Zephyr routines. * Created by: Robert French * Copyright (c) 1987,1988,1991 by the Massachusetts Institute of * For copying and distribution information, see the file #ifndef ZEPHYR_USES_KERBEROS int gettimeofday(struct timeval* p, struct timezone* tz ){ long long ns100; /*time since 1 Jan 1601 in 100ns units */ GetSystemTimeAsFileTime( &(_now.ft) ); p->tv_usec=(long)((_now.ns100 / 10LL) % 1000000LL ); p->tv_sec= (long)((_now.ns100-(116444736000000000LL))/10000000LL); struct in_addr __My_addr; struct _Z_InputQ *__Q_Head, *__Q_Tail; struct sockaddr_in __HM_addr; struct sockaddr_in __HM_addr_real; ZLocations_t *__locate_list; ZSubscription_t *__subscriptions_list; int __subscriptions_next; int Z_discarded_packets = 0; #ifdef ZEPHYR_USES_KERBEROS C_Block __Zephyr_session; char __Zephyr_realm[REALM_SZ]; void (*__Z_debug_print) __P((const char *fmt, va_list args, void *closure)); void *__Z_debug_print_closure; #define min(a,b) ((a)<(b)?(a):(b)) static int Z_AddField __P((char **ptr, const char *field, char *end)); static int find_or_insert_uid __P((ZUnique_Id_t *uid, ZNotice_Kind_t kind)); /* Find or insert uid in the old uids buffer. The buffer is a sorted * circular queue. We make the assumption that most packets arrive in * order, so we can usually search for a uid or insert it into the buffer * by looking back just a few entries from the end. Since this code is * only executed by the client, the implementation isn't microoptimized. */ static int find_or_insert_uid(uid, kind) /* Initialize the uid buffer if it hasn't been done already. */ buffer = (struct _filter *) malloc(size * sizeof(*buffer)); /* Age the uid buffer, discarding any uids older than the clock skew. */ while (num && (now - buffer[start % size].t) > CLOCK_SKEW) /* Make room for a new uid, since we'll probably have to insert one. */ new = (struct _filter *) malloc(new_size * sizeof(*new)); for (i = 0; i < num; i++) new[i] = buffer[(start + i) % size]; /* Search for this uid in the buffer, starting from the end. */ for (i = start + num - 1; i >= start; i--) { result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid)); if (result == 0 && buffer[i % size].kind == kind) /* We didn't find it; insert the uid into the buffer after i. */ for (j = start + num; j > i; j--) buffer[j % size] = buffer[(j - 1) % size]; buffer[i % size].uid = *uid; buffer[i % size].kind = kind; buffer[i % size].t = now; /* Return 1 if there is a packet waiting, 0 otherwise */ static int Z_PacketWaiting(void) tv.tv_sec = tv.tv_usec = 0; return (select(ZGetFD() + 1, &read, NULL, NULL, &tv)); /* Wait for a complete notice to become available */ Code_t Z_WaitForComplete(void) return (Z_ReadEnqueue()); while (!__Q_CompleteLength) if ((retval = Z_ReadWait()) != ZERR_NONE) /* Read any available packets and enqueue them */ while (Z_PacketWaiting()) if ((retval = Z_ReadWait()) != ZERR_NONE) * Search the queue for a notice with the proper multiuid - remove any * notices that haven't been touched in a while static struct _Z_InputQ *Z_SearchQueue(ZUnique_Id_t *uid, ZNotice_Kind_t kind) register struct _Z_InputQ *qptr; (void) gettimeofday(&tv, (struct timezone *)0); if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind) if (qptr->timep && (qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec)) * Now we delve into really convoluted queue handling and * fragmentation reassembly algorithms and other stuff you probably * don't want to look at... * This routine does NOT guarantee a complete packet will be ready when it register struct _Z_InputQ *qptr; struct sockaddr_in olddest, from; int packet_len, zvlen, part, partof; if (select(ZGetFD() + 1, &fds, NULL, NULL, &tv) < 0) if (!FD_ISSET(ZGetFD(), &fds)) from_len = sizeof(struct sockaddr_in); packet_len = recvfrom(ZGetFD(), packet, sizeof(packet), 0, (struct sockaddr *)&from, &from_len); /* Ignore obviously non-Zephyr packets. */ zvlen = sizeof(ZVERSIONHDR) - 1; if (packet_len < zvlen || memcmp(packet, ZVERSIONHDR, zvlen) != 0) { if ((retval = ZParseNotice(packet, packet_len, ¬ice)) != ZERR_NONE) * If we're not a server and the notice is of an appropriate kind, * send back a CLIENTACK to whoever sent it to say we got it. if (notice.z_kind != HMACK && notice.z_kind != SERVACK && notice.z_kind != SERVNAK && notice.z_kind != CLIENTACK) { tmpnotice.z_kind = CLIENTACK; tmpnotice.z_message_len = 0; if ((retval = ZFormatSmallRawNotice(&tmpnotice, pkt, &len)) if ((retval = ZSendPacket(pkt, len, 0)) != ZERR_NONE) if (find_or_insert_uid(¬ice.z_uid, notice.z_kind)) /* Check authentication on the notice. */ notice.z_checked_auth = ZCheckAuthentication(¬ice, &from); * Parse apart the z_multinotice field - if the field is blank for * some reason, assume this packet stands by itself. slash = strchr(notice.z_multinotice, '/'); part = atoi(notice.z_multinotice); if (part > partof || partof == 0) { partof = notice.z_message_len; partof = notice.z_message_len; /* Too big a packet...just ignore it! */ if (partof > Z_MAXNOTICESIZE) * If we aren't a server and we can find a notice in the queue * with the same multiuid field, insert the current fragment as /* The SERVACK and SERVNAK replies shouldn't be reassembled (they have no parts). Instead, we should hold on to the reply ONLY if it's the first part of a fragmented message, i.e. multi_uid == uid. This allows programs to wait for the uid of the first packet, and get a response when that notice arrives. Acknowledgements of the other fragments are discarded (XXX we assume here that they all carry the same information regarding failure/success) !ZCompareUID(¬ice.z_multiuid, ¬ice.z_uid)) /* they're not the same... throw away this packet. */ /* fall thru & process it */ /* for HMACK types, we assume no packet loss (local loopback connections). The other types can be fragmented and MUST run through this code. */ if (!__Zephyr_server && (qptr = Z_SearchQueue(¬ice.z_multiuid, * If this is the first fragment, and we haven't already * gotten a first fragment, grab the header from it. if (part == 0 && !qptr->header) { qptr->header_len = packet_len-notice.z_message_len; qptr->header = (char *) malloc((unsigned) qptr->header_len); (void) memcpy(qptr->header, packet, qptr->header_len); return (Z_AddNoticeToEntry(qptr, ¬ice, part)); * We'll have to create a new entry...make sure the queue isn't if (__Q_Size+(__Zephyr_server ? notice.z_message_len : partof) > Z_MAXQUEUESIZE) * This is a notice we haven't heard of, so create a new queue * entry for it and zero it out. qptr = (struct _Z_InputQ *)malloc(sizeof(struct _Z_InputQ)); (void) memset((char *)qptr, 0, sizeof(struct _Z_InputQ)); /* Insert the entry at the end of the queue */ /* Copy the from field, multiuid, kind, and checked authentication. */ qptr->uid = notice.z_multiuid; qptr->kind = notice.z_kind; qptr->auth = notice.z_checked_auth; * If this is the first part of the notice, we take the header * from it. We only take it if this is the first fragment so that * the Unique ID's will be predictable. * If a Zephyr Server, we always take the header. if (__Zephyr_server || part == 0) { qptr->header_len = packet_len-notice.z_message_len; qptr->header = (char *) malloc((unsigned) qptr->header_len); (void) memcpy(qptr->header, packet, qptr->header_len); * If this is not a fragmented notice, then don't bother with a * If we are a Zephyr server, all notices are treated as complete. if (__Zephyr_server || (part == 0 && notice.z_message_len == partof)) { qptr->holelist = (struct _Z_Hole *) 0; /* allocate a msg buf for this piece */ if (notice.z_message_len == 0) else if (!(qptr->msg = (char *) malloc((unsigned) notice.z_message_len))) (void) memcpy(qptr->msg, notice.z_message, notice.z_message_len); qptr->msg_len = notice.z_message_len; __Q_Size += notice.z_message_len; qptr->packet_len = qptr->header_len+qptr->msg_len; if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len))) (void) memcpy(qptr->packet, qptr->header, qptr->header_len); (void) memcpy(qptr->packet+qptr->header_len, qptr->msg, * We know how long the message is going to be (this is better * than IP fragmentation...), so go ahead and allocate it all. if (!(qptr->msg = (char *) malloc((unsigned) partof)) && partof) * Well, it's a fragmented notice...allocate a hole list and * initialize it to the full packet size. Then insert the if (!(qptr->holelist = (struct _Z_Hole *) malloc(sizeof(struct _Z_Hole)))) qptr->holelist->next = (struct _Z_Hole *) 0; qptr->holelist->first = 0; qptr->holelist->last = partof-1; return (Z_AddNoticeToEntry(qptr, ¬ice, part)); /* Fragment management routines - compliments, more or less, of RFC815 */ Code_t Z_AddNoticeToEntry(qptr, notice, part) int last, oldfirst, oldlast; struct _Z_Hole *hole, *lasthole; /* Incorporate this notice's checked authentication. */ if (notice->z_checked_auth == ZAUTH_FAILED) qptr->auth = ZAUTH_FAILED; else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED) (void) gettimeofday(&tv, (struct timezone *)0); last = part+notice->z_message_len-1; lasthole = (struct _Z_Hole *) 0; /* copy in the message body */ (void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len); /* Search for a hole that overlaps with the current fragment */ if (part <= hole->last && last >= hole->first) /* If we found one, delete it and reconstruct a new hole */ lasthole->next = hole->next; qptr->holelist = hole->next; * Now create a new hole that is the original hole without the /* Search for the end of the hole list */ lasthole = (struct _Z_Hole *) 0; if (!(lasthole->next = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) if (!(qptr->holelist = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) /* Search for the end of the hole list */ lasthole = (struct _Z_Hole *) 0; if (!(lasthole->next = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) if (!(qptr->holelist = (struct _Z_Hole *) malloc(sizeof(struct _Z_InputQ)))) hole->next = (struct _Z_Hole *) 0; qptr->timep = 0; /* don't time out anymore */ qptr->packet_len = qptr->header_len+qptr->msg_len; if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len))) (void) memcpy(qptr->packet, qptr->header, qptr->header_len); (void) memcpy(qptr->packet+qptr->header_len, qptr->msg, Code_t Z_FormatHeader(notice, buffer, buffer_len, len, cert_routine) static char version[BUFSIZ]; /* default init should be all \0 */ socklen_t namelen = sizeof(name); notice->z_sender = ZGetSender(); if (notice->z_port == 0) { retval = ZOpenPort((unsigned short *)0); retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen); notice->z_port = name.sin_port; notice->z_multinotice = ""; (void) gettimeofday(¬ice->z_uid.tv, (struct timezone *)0); notice->z_uid.tv.tv_sec = htonl((unsigned long) notice->z_uid.tv.tv_sec); notice->z_uid.tv.tv_usec = htonl((unsigned long) notice->z_uid.tv.tv_usec); (void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr)); notice->z_multiuid = notice->z_uid; (void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR, notice->z_version = version; return Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine); Code_t Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine) notice->z_authent_len = 0; notice->z_ascii_authent = ""; return (Z_FormatRawHeader(notice, buffer, buffer_len, return ((*cert_routine)(notice, buffer, buffer_len, len)); Code_t Z_FormatRawHeader(notice, buffer, buffer_len, len, cstart, cend) if (!notice->z_class_inst) notice->z_class_inst = ""; if (!notice->z_recipient) notice->z_recipient = ""; if (!notice->z_default_format) notice->z_default_format = ""; if (buffer_len < strlen(notice->z_version)+1) g_strlcpy(ptr, notice->z_version, buffer_len); if (ZMakeAscii32(ptr, end-ptr, Z_NUMFIELDS + notice->z_num_other_fields) if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN) if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid, sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN) if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN) if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN) if (Z_AddField(&ptr, notice->z_ascii_authent, end)) if (Z_AddField(&ptr, notice->z_class, end)) if (Z_AddField(&ptr, notice->z_class_inst, end)) if (Z_AddField(&ptr, notice->z_opcode, end)) if (Z_AddField(&ptr, notice->z_sender, end)) if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) { if (Z_AddField(&ptr, notice->z_recipient, end)) if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 > (void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm); if (Z_AddField(&ptr, newrecip, end)) if (Z_AddField(&ptr, notice->z_default_format, end)) /* copy back the end pointer location for crypto checksum */ if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN) if (Z_AddField(&ptr, notice->z_multinotice, end)) if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid, sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN) for (i=0;i<notice->z_num_other_fields;i++) if (Z_AddField(&ptr, notice->z_other_fields[i], end)) Z_AddField(char **ptr, const char *field, char *end) len = field ? strlen (field) + 1 : 1; struct _Z_InputQ *Z_GetFirstComplete() return ((struct _Z_InputQ *)0); struct _Z_InputQ *Z_GetNextComplete(qptr) return ((struct _Z_InputQ *)0); struct _Z_Hole *hole, *nexthole; __Q_Size -= qptr->msg_len; if (qptr == __Q_Head && __Q_Head == __Q_Tail) { __Q_Head = (struct _Z_InputQ *)0; __Q_Tail = (struct _Z_InputQ *)0; __Q_Head->prev = (struct _Z_InputQ *)0; __Q_Tail->next = (struct _Z_InputQ *)0; qptr->prev->next = qptr->next; qptr->next->prev = qptr->prev; Code_t Z_SendFragmentedNotice(notice, len, cert_func, send_func) int offset, hdrsize, fragsize, ret_len, message_len, waitforack; hdrsize = len-notice->z_message_len; fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE; waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED) while (offset < notice->z_message_len || !notice->z_message_len) { (void) sprintf(multi, "%d/%d", offset, notice->z_message_len); partnotice.z_multinotice = multi; (void) gettimeofday(&partnotice.z_uid.tv, partnotice.z_uid.tv.tv_sec = htonl((unsigned long) partnotice.z_uid.tv.tv_sec); partnotice.z_uid.tv.tv_usec = htonl((unsigned long) partnotice.z_uid.tv.tv_usec); (void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr, message_len = min(notice->z_message_len-offset, fragsize); partnotice.z_message = (char*)notice->z_message+offset; partnotice.z_message_len = message_len; if ((retval = Z_FormatAuthHeader(&partnotice, buffer, Z_MAXHEADERLEN, &ret_len, cert_func)) != ZERR_NONE) { memcpy(buffer + ret_len, partnotice.z_message, message_len); if ((retval = (*send_func)(&partnotice, buffer, ret_len+message_len, waitforack)) != ZERR_NONE) { if (!notice->z_message_len) Code_t Z_XmitFragment(notice, buf, len, wait) return(ZSendPacket(buf, len, wait)); /* For debugging printing */ const char *const ZNoticeKinds[] = { "UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK", void Z_debug (const char *format, ...) (*__Z_debug_print) (format, pvar, __Z_debug_print_closure); void Z_debug (va_alist) va_dcl format = va_arg (pvar, char *); (*__Z_debug_print) (format, pvar, __Z_debug_print_closure); void Z_debug_stderr (format, args, closure) vfprintf (stderr, format, args); _doprnt (format, args, stderr); int ZGetFD () { return __Zephyr_fd; } int ZQLength () { return __Q_CompleteLength; } struct sockaddr_in ZGetDestAddr () { return __HM_addr; } Zconst char * ZGetRealm () { return __Zephyr_realm; } void ZSetDebug(proc, arg) void (*proc) __P((const char *, va_list, void *)); __Z_debug_print_closure = arg;