1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/* This file is part of the Project Athena Zephyr Notification System.
* It contains source for the ZParseNotice function.
*
* Created by: Robert French
*
* $Id: 48311b5383bac8c645e627e350ab886eceacbcc8 $
*
* Copyright (c) 1987,1991 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#ifndef lint
static const char rcsid_ZParseNotice_c[] =
"$Id: 48311b5383bac8c645e627e350ab886eceacbcc8 $";
#endif
#include <internal.h>
#include <syslog.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
inline static int
_bad_packet(int line, char *where, ZNotice_t *notice, char *what) {
if (__Zephyr_server) {
syslog(LOG_ERR, "ZParseNotice: bad packet (%s) from %s.%d at line %d",
what, inet_ntoa(notice->z_uid.zuid_addr), notice->z_port, line);
} else {
#ifdef Z_DEBUG
Z_debug("ZParseNotice: bad packet (%s) from %s.%d at line %d",
what, inet_ntoa(notice->z_uid.zuid_addr), notice->z_port, line);
#endif
}
return ZERR_BADPKT;
}
/* Skip to the next NUL-terminated field in the packet. */
inline static char *
next_field(char *ptr,
char *end)
{
while (ptr < end && *ptr != '\0')
ptr++;
if (ptr < end)
ptr++;
return (ptr);
}
Code_t
ZParseNotice(char *buffer,
int len,
ZNotice_t *notice)
{
char *ptr, *end;
unsigned long temp;
int maj, numfields, i;
#ifndef __LINE__
#define __LINE__ -1
#endif
#define BAD_PACKET(what) return _bad_packet(__LINE__, ptr, notice, what)
(void) memset((char *)notice, 0, sizeof(ZNotice_t));
ptr = buffer;
end = buffer+len;
notice->z_packet = buffer;
notice->z_version = ptr;
if (strncmp(ptr, ZVERSIONHDR, sizeof(ZVERSIONHDR) - 1))
return (ZERR_VERS);
ptr += sizeof(ZVERSIONHDR) - 1;
if (!*ptr)
BAD_PACKET("null version string");
maj = atoi(ptr);
if (maj != ZVERSIONMAJOR)
return (ZERR_VERS);
ptr = next_field(ptr, end);
if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
BAD_PACKET("parsing num_hdr_fields");
numfields = temp;
notice->z_num_hdr_fields = numfields;
ptr = next_field(ptr, end);
/*XXX 3 */
numfields -= 2; /* numfields, version, and checksum */
if (numfields < 0)
BAD_PACKET("no header fields");
if (numfields && ptr < end) {
if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
BAD_PACKET("parsing kind");
notice->z_kind = (ZNotice_Kind_t)temp;
numfields--;
ptr = next_field(ptr, end);
}
else
BAD_PACKET("missing kind");
if (numfields && ptr < end) {
if (ZReadAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid,
sizeof(ZUnique_Id_t)) == ZERR_BADFIELD)
BAD_PACKET("parsing uid");
notice->z_time.tv_sec = ntohl((u_long) notice->z_uid.tv.tv_sec);
notice->z_time.tv_usec = ntohl((u_long) notice->z_uid.tv.tv_usec);
numfields--;
ptr = next_field(ptr, end);
}
else
BAD_PACKET("missing uid");
if (numfields && ptr < end) {
if (ZReadAscii16(ptr, end-ptr, ¬ice->z_port) == ZERR_BADFIELD)
BAD_PACKET("parsing port");
notice->z_port = htons(notice->z_port);
numfields--;
ptr = next_field(ptr, end);
}
else
BAD_PACKET("missing port");
if (numfields && ptr < end) {
if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
BAD_PACKET("parsing auth");
notice->z_auth = temp;
numfields--;
ptr = next_field(ptr, end);
}
else
BAD_PACKET("missing auth");
notice->z_checked_auth = ZAUTH_UNSET;
if (numfields && ptr < end) {
if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
BAD_PACKET("parsing authenticator length");
notice->z_authent_len = temp;
numfields--;
ptr = next_field(ptr, end);
}
else
BAD_PACKET("missing authenticator length");
if (numfields && ptr < end) {
notice->z_ascii_authent = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
BAD_PACKET("missing authenticator field");
if (numfields && ptr < end) {
notice->z_class = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_class = "";
if (numfields && ptr < end) {
notice->z_class_inst = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_class_inst = "";
if (numfields && ptr < end) {
notice->z_opcode = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_opcode = "";
if (numfields && ptr < end) {
notice->z_sender = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_sender = "";
if (numfields && ptr < end) {
notice->z_recipient = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_recipient = "";
if (numfields && ptr < end) {
notice->z_default_format = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_default_format = "";
if (numfields && ptr < end) {
notice->z_ascii_checksum = ptr;
if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
notice->z_checksum = 0;
else
notice->z_checksum = temp;
numfields--;
ptr = next_field (ptr, end);
}
else
{
notice->z_ascii_checksum = "";
notice->z_checksum = 0;
}
if (numfields && ptr < end) {
notice->z_multinotice = ptr;
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_multinotice = "";
if (numfields && ptr < end) {
if (ZReadAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid,
sizeof(ZUnique_Id_t)) == ZERR_BADFIELD)
BAD_PACKET("parsing multiuid");
notice->z_time.tv_sec = ntohl((u_long) notice->z_multiuid.tv.tv_sec);
notice->z_time.tv_usec = ntohl((u_long) notice->z_multiuid.tv.tv_usec);
numfields--;
ptr = next_field(ptr, end);
}
else
notice->z_multiuid = notice->z_uid;
if (numfields && ptr < end) {
/* we will take it on faith that ipv6 addresses are longer than ipv4
addresses */
unsigned char addrbuf[sizeof(notice->z_sender_sockaddr.ip6.sin6_addr)];
int alen;
/* because we're paranoid about naughtily misformatted packets */
if (memchr(ptr, '\0', end - ptr) == NULL)
BAD_PACKET("unterminated address field");
if (*ptr == 'Z') {
if (ZReadZcode((unsigned char *)ptr, addrbuf,
sizeof(addrbuf), &alen) == ZERR_BADFIELD)
BAD_PACKET("parsing Zcode address");
} else {
alen = sizeof(notice->z_sender_sockaddr.ip4.sin_addr);
if (ZReadAscii(ptr, end - ptr, (unsigned char *)addrbuf,
alen) == ZERR_BADFIELD)
BAD_PACKET("parsing NetASCII address");
}
if (alen == sizeof(notice->z_sender_sockaddr.ip6.sin6_addr)) {
notice->z_sender_sockaddr.ip6.sin6_family = AF_INET6;
memcpy(¬ice->z_sender_sockaddr.ip6.sin6_addr, addrbuf, alen);
#ifdef HAVE_SOCKADDR_IN6_SIN6_LEN
notice->z_sender_sockaddr.ip6.sin6_len = sizeof(notice->z_sender_sockaddr.ip6);
#endif
} else if (alen == sizeof(notice->z_sender_sockaddr.ip4.sin_addr)) {
notice->z_sender_sockaddr.ip4.sin_family = AF_INET;
memcpy(¬ice->z_sender_sockaddr.ip4.sin_addr, addrbuf, alen);
#ifdef HAVE_SOCKADDR_IN_SIN_LEN
notice->z_sender_sockaddr.ip4.sin_len = sizeof(notice->z_sender_sockaddr.ip4);
#endif
} else
BAD_PACKET("address claims to be neither IPv4 or IPv6");
numfields--;
ptr = next_field(ptr, end);
} else {
memset(¬ice->z_sender_sockaddr, 0,
sizeof notice->z_sender_sockaddr);
notice->z_sender_sockaddr.ip4.sin_family = AF_INET;
notice->z_sender_sockaddr.ip4.sin_addr = notice->z_uid.zuid_addr;
#ifdef HAVE_SOCKADDR_IN_SIN_LEN
notice->z_sender_sockaddr.ip4.sin_len = sizeof(notice->z_sender_sockaddr.ip4);
#endif
}
if (numfields && ptr < end) {
if (ZReadAscii16(ptr, end-ptr, ¬ice->z_charset) == ZERR_BADFIELD)
BAD_PACKET("parsing charset");
notice->z_charset = htons(notice->z_charset);
numfields--;
ptr = next_field(ptr, end);
} else
notice->z_charset = ZCHARSET_UNKNOWN;
for (i=0;ptr < end && i<Z_MAXOTHERFIELDS && numfields;i++,numfields--) {
notice->z_other_fields[i] = ptr;
ptr = next_field(ptr, end);
}
notice->z_num_other_fields = i;
for (i=0;ptr < end && numfields;numfields--)
ptr = next_field(ptr, end);
if (numfields || *(ptr - 1) != '\0')
BAD_PACKET("end of headers");
notice->z_message = (void *)ptr;
notice->z_message_len = len-(ptr-buffer);
return (ZERR_NONE);
}
|