| 1 | /* |
| 2 | * Copyright (c) 1983 Eric P. Allman |
| 3 | * Copyright (c) 1988, 1993 |
| 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
| 6 | * %sccs.include.redist.c% |
| 7 | */ |
| 8 | |
| 9 | #ifndef lint |
| 10 | static char sccsid[] = "@(#)parseaddr.c 8.29 (Berkeley) %G%"; |
| 11 | #endif /* not lint */ |
| 12 | |
| 13 | #include "sendmail.h" |
| 14 | |
| 15 | #ifdef CC_WONT_PROMOTE |
| 16 | static int toktype __P((char)); |
| 17 | #else /* !CC_WONT_PROMOTE */ |
| 18 | static int toktype __P((int)); /* char -> int */ |
| 19 | #endif /* CC_WONT_PROMOTE */ |
| 20 | static void _rewrite __P((char **, int)); |
| 21 | static void callsubr __P((char **)); |
| 22 | static ADDRESS * buildaddr __P((char **, ADDRESS *)); |
| 23 | static void uurelativize __P((const char *, const char *, char **)); |
| 24 | |
| 25 | char *DelimChar; /* set to point to the delimiter */ |
| 26 | |
| 27 | /* |
| 28 | ** PARSEADDR -- Parse an address |
| 29 | ** |
| 30 | ** Parses an address and breaks it up into three parts: a |
| 31 | ** net to transmit the message on, the host to transmit it |
| 32 | ** to, and a user on that host. These are loaded into an |
| 33 | ** ADDRESS header with the values squirreled away if necessary. |
| 34 | ** The "user" part may not be a real user; the process may |
| 35 | ** just reoccur on that machine. For example, on a machine |
| 36 | ** with an arpanet connection, the address |
| 37 | ** csvax.bill@berkeley |
| 38 | ** will break up to a "user" of 'csvax.bill' and a host |
| 39 | ** of 'berkeley' -- to be transmitted over the arpanet. |
| 40 | ** |
| 41 | ** Parameters: |
| 42 | ** addr -- the address to parse. |
| 43 | ** a -- a pointer to the address descriptor buffer. |
| 44 | ** If NULL, a header will be created. |
| 45 | ** flags -- describe detail for parsing. See RF_ definitions |
| 46 | ** in sendmail.h. |
| 47 | ** delim -- the character to terminate the address, passed |
| 48 | ** to prescan. |
| 49 | ** delimptr -- if non-NULL, set to the location of the |
| 50 | ** delim character that was found. |
| 51 | ** e -- the envelope that will contain this address. |
| 52 | ** |
| 53 | ** Returns: |
| 54 | ** A pointer to the address descriptor header (`a' if |
| 55 | ** `a' is non-NULL). |
| 56 | ** NULL on error. |
| 57 | ** |
| 58 | ** Side Effects: |
| 59 | ** none |
| 60 | */ |
| 61 | |
| 62 | /* following delimiters are inherent to the internal algorithms */ |
| 63 | # define DELIMCHARS "()<>,;\r\n" /* default word delimiters */ |
| 64 | |
| 65 | ADDRESS * |
| 66 | parseaddr(addr, a, flags, delim, delimptr, e) |
| 67 | char *addr; |
| 68 | register ADDRESS *a; |
| 69 | int flags; |
| 70 | int delim; |
| 71 | char **delimptr; |
| 72 | register ENVELOPE *e; |
| 73 | { |
| 74 | register char **pvp; |
| 75 | auto char *delimptrbuf; |
| 76 | bool queueup; |
| 77 | char pvpbuf[PSBUFSIZE]; |
| 78 | |
| 79 | /* |
| 80 | ** Initialize and prescan address. |
| 81 | */ |
| 82 | |
| 83 | e->e_to = addr; |
| 84 | if (tTd(20, 1)) |
| 85 | printf("\n--parseaddr(%s)\n", addr); |
| 86 | |
| 87 | { |
| 88 | extern char *DelimChar; /* parseaddr.c */ |
| 89 | char savec; |
| 90 | bool invalid; |
| 91 | extern char *finddelim(); |
| 92 | extern bool invalidaddr(); |
| 93 | |
| 94 | DelimChar = finddelim(addr, delim); |
| 95 | savec = *DelimChar; |
| 96 | *DelimChar = '\0'; |
| 97 | invalid = invalidaddr(addr); |
| 98 | *DelimChar = savec; |
| 99 | if (invalid) |
| 100 | return (NULL); |
| 101 | } |
| 102 | |
| 103 | if (delimptr == NULL) |
| 104 | delimptr = &delimptrbuf; |
| 105 | |
| 106 | pvp = prescan(addr, delim, pvpbuf, sizeof pvpbuf, delimptr); |
| 107 | if (pvp == NULL) |
| 108 | { |
| 109 | if (tTd(20, 1)) |
| 110 | printf("parseaddr-->NULL\n"); |
| 111 | return (NULL); |
| 112 | } |
| 113 | |
| 114 | if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr)) |
| 115 | { |
| 116 | if (tTd(20, 1)) |
| 117 | printf("parseaddr-->bad address\n"); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | ** Save addr if we are going to have to. |
| 123 | ** |
| 124 | ** We have to do this early because there is a chance that |
| 125 | ** the map lookups in the rewriting rules could clobber |
| 126 | ** static memory somewhere. |
| 127 | */ |
| 128 | |
| 129 | if (bitset(RF_COPYPADDR, flags) && addr != NULL) |
| 130 | { |
| 131 | char savec = **delimptr; |
| 132 | |
| 133 | if (savec != '\0') |
| 134 | **delimptr = '\0'; |
| 135 | addr = newstr(addr); |
| 136 | if (savec != '\0') |
| 137 | **delimptr = savec; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | ** Apply rewriting rules. |
| 142 | ** Ruleset 0 does basic parsing. It must resolve. |
| 143 | */ |
| 144 | |
| 145 | queueup = FALSE; |
| 146 | if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) |
| 147 | queueup = TRUE; |
| 148 | if (rewrite(pvp, 0, 0, e) == EX_TEMPFAIL) |
| 149 | queueup = TRUE; |
| 150 | |
| 151 | |
| 152 | /* |
| 153 | ** Build canonical address from pvp. |
| 154 | */ |
| 155 | |
| 156 | a = buildaddr(pvp, a, flags, e); |
| 157 | |
| 158 | /* |
| 159 | ** Make local copies of the host & user and then |
| 160 | ** transport them out. |
| 161 | */ |
| 162 | |
| 163 | allocaddr(a, flags, addr); |
| 164 | if (bitset(QBADADDR, a->q_flags)) |
| 165 | return a; |
| 166 | |
| 167 | /* |
| 168 | ** If there was a parsing failure, mark it for queueing. |
| 169 | */ |
| 170 | |
| 171 | if (queueup) |
| 172 | { |
| 173 | char *msg = "Transient parse error -- message queued for future delivery"; |
| 174 | |
| 175 | if (tTd(20, 1)) |
| 176 | printf("parseaddr: queuing message\n"); |
| 177 | message(msg); |
| 178 | if (e->e_message == NULL) |
| 179 | e->e_message = newstr(msg); |
| 180 | a->q_flags |= QQUEUEUP; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | ** Compute return value. |
| 185 | */ |
| 186 | |
| 187 | if (tTd(20, 1)) |
| 188 | { |
| 189 | printf("parseaddr-->"); |
| 190 | printaddr(a, FALSE); |
| 191 | } |
| 192 | |
| 193 | return (a); |
| 194 | } |
| 195 | \f/* |
| 196 | ** INVALIDADDR -- check for address containing meta-characters |
| 197 | ** |
| 198 | ** Parameters: |
| 199 | ** addr -- the address to check. |
| 200 | ** |
| 201 | ** Returns: |
| 202 | ** TRUE -- if the address has any "wierd" characters |
| 203 | ** FALSE -- otherwise. |
| 204 | */ |
| 205 | |
| 206 | bool |
| 207 | invalidaddr(addr, delimptr) |
| 208 | register char *addr; |
| 209 | char *delimptr; |
| 210 | { |
| 211 | char savedelim; |
| 212 | |
| 213 | if (delimptr != NULL) |
| 214 | { |
| 215 | savedelim = *delimptr; |
| 216 | if (savedelim != '\0') |
| 217 | *delimptr = '\0'; |
| 218 | } |
| 219 | #if 0 |
| 220 | /* for testing.... */ |
| 221 | if (strcmp(addr, "INvalidADDR") == 0) |
| 222 | { |
| 223 | usrerr("553 INvalid ADDRess"); |
| 224 | goto addrfailure; |
| 225 | } |
| 226 | #endif |
| 227 | for (; *addr != '\0'; addr++) |
| 228 | { |
| 229 | if ((*addr & 0340) == 0200) |
| 230 | break; |
| 231 | } |
| 232 | if (*addr == '\0') |
| 233 | { |
| 234 | if (savedelim != '\0' && delimptr != NULL) |
| 235 | *delimptr = savedelim; |
| 236 | return FALSE; |
| 237 | } |
| 238 | setstat(EX_USAGE); |
| 239 | usrerr("553 Address contained invalid control characters"); |
| 240 | addrfailure: |
| 241 | if (savedelim != '\0' && delimptr != NULL) |
| 242 | *delimptr = savedelim; |
| 243 | return TRUE; |
| 244 | } |
| 245 | \f/* |
| 246 | ** ALLOCADDR -- do local allocations of address on demand. |
| 247 | ** |
| 248 | ** Also lowercases the host name if requested. |
| 249 | ** |
| 250 | ** Parameters: |
| 251 | ** a -- the address to reallocate. |
| 252 | ** flags -- the copy flag (see RF_ definitions in sendmail.h |
| 253 | ** for a description). |
| 254 | ** paddr -- the printname of the address. |
| 255 | ** |
| 256 | ** Returns: |
| 257 | ** none. |
| 258 | ** |
| 259 | ** Side Effects: |
| 260 | ** Copies portions of a into local buffers as requested. |
| 261 | */ |
| 262 | |
| 263 | allocaddr(a, flags, paddr) |
| 264 | register ADDRESS *a; |
| 265 | int flags; |
| 266 | char *paddr; |
| 267 | { |
| 268 | if (tTd(24, 4)) |
| 269 | printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr); |
| 270 | |
| 271 | a->q_paddr = paddr; |
| 272 | |
| 273 | if (a->q_user == NULL) |
| 274 | a->q_user = ""; |
| 275 | if (a->q_host == NULL) |
| 276 | a->q_host = ""; |
| 277 | |
| 278 | if (bitset(RF_COPYPARSE, flags)) |
| 279 | { |
| 280 | a->q_host = newstr(a->q_host); |
| 281 | if (a->q_user != a->q_paddr) |
| 282 | a->q_user = newstr(a->q_user); |
| 283 | } |
| 284 | |
| 285 | if (a->q_paddr == NULL) |
| 286 | a->q_paddr = a->q_user; |
| 287 | } |
| 288 | \f/* |
| 289 | ** INVALIDADDR -- check an address string for invalid control characters. |
| 290 | ** |
| 291 | ** Parameters: |
| 292 | ** addr -- address string to be checked. |
| 293 | ** |
| 294 | ** Returns: |
| 295 | ** TRUE if address string could cause problems, FALSE o/w. |
| 296 | ** |
| 297 | ** Side Effects: |
| 298 | ** ExitStat may be changed and an error message generated. |
| 299 | */ |
| 300 | |
| 301 | bool |
| 302 | invalidaddr(addr) |
| 303 | const char *addr; |
| 304 | { |
| 305 | register const char *cp; |
| 306 | |
| 307 | /* make sure error messages don't have garbage on them */ |
| 308 | errno = 0; |
| 309 | |
| 310 | /* |
| 311 | ** Sendmail reserves characters 020 - 036 for rewriting rules |
| 312 | ** which can cause havoc (e.g. infinite rewriting loops) if |
| 313 | ** one shows up at the wrong time. If any of these characters |
| 314 | ** appear in an address, the address is deemed "invalid" and |
| 315 | ** an error message is generated. |
| 316 | */ |
| 317 | |
| 318 | for (cp = addr; *cp; cp++) |
| 319 | if ((*cp >= MATCHZANY && *cp <= HOSTEND) || *cp == '\001') |
| 320 | { |
| 321 | setstat(EX_USAGE); |
| 322 | usrerr("address contained invalid control char(s)"); |
| 323 | return (TRUE); |
| 324 | } |
| 325 | return (FALSE); |
| 326 | } |
| 327 | \f/* |
| 328 | ** PRESCAN -- Prescan name and make it canonical |
| 329 | ** |
| 330 | ** Scans a name and turns it into a set of tokens. This process |
| 331 | ** deletes blanks and comments (in parentheses). |
| 332 | ** |
| 333 | ** This routine knows about quoted strings and angle brackets. |
| 334 | ** |
| 335 | ** There are certain subtleties to this routine. The one that |
| 336 | ** comes to mind now is that backslashes on the ends of names |
| 337 | ** are silently stripped off; this is intentional. The problem |
| 338 | ** is that some versions of sndmsg (like at LBL) set the kill |
| 339 | ** character to something other than @ when reading addresses; |
| 340 | ** so people type "csvax.eric\@berkeley" -- which screws up the |
| 341 | ** berknet mailer. |
| 342 | ** |
| 343 | ** Parameters: |
| 344 | ** addr -- the name to chomp. |
| 345 | ** delim -- the delimiter for the address, normally |
| 346 | ** '\0' or ','; \0 is accepted in any case. |
| 347 | ** If '\t' then we are reading the .cf file. |
| 348 | ** pvpbuf -- place to put the saved text -- note that |
| 349 | ** the pointers are static. |
| 350 | ** pvpbsize -- size of pvpbuf. |
| 351 | ** delimptr -- if non-NULL, set to the location of the |
| 352 | ** terminating delimiter. |
| 353 | ** |
| 354 | ** Returns: |
| 355 | ** A pointer to a vector of tokens. |
| 356 | ** NULL on error. |
| 357 | */ |
| 358 | |
| 359 | /* states and character types */ |
| 360 | # define OPR 0 /* operator */ |
| 361 | # define ATM 1 /* atom */ |
| 362 | # define QST 2 /* in quoted string */ |
| 363 | # define SPC 3 /* chewing up spaces */ |
| 364 | # define ONE 4 /* pick up one character */ |
| 365 | |
| 366 | # define NSTATES 5 /* number of states */ |
| 367 | # define TYPE 017 /* mask to select state type */ |
| 368 | |
| 369 | /* meta bits for table */ |
| 370 | # define M 020 /* meta character; don't pass through */ |
| 371 | # define B 040 /* cause a break */ |
| 372 | # define MB M|B /* meta-break */ |
| 373 | |
| 374 | static short StateTab[NSTATES][NSTATES] = |
| 375 | { |
| 376 | /* oldst chtype> OPR ATM QST SPC ONE */ |
| 377 | /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, |
| 378 | /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, |
| 379 | /*QST*/ QST, QST, OPR, QST, QST, |
| 380 | /*SPC*/ OPR, ATM, QST, SPC|M, ONE, |
| 381 | /*ONE*/ OPR, OPR, OPR, OPR, OPR, |
| 382 | }; |
| 383 | |
| 384 | /* token type table -- it gets modified with $o characters */ |
| 385 | static TokTypeTab[256] = |
| 386 | { |
| 387 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM, |
| 388 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 389 | SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM,ATM,SPC,ATM,ATM,ATM,ATM,ATM,ATM, |
| 390 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 391 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 392 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 393 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 394 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 395 | OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR, |
| 396 | OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR, |
| 397 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 398 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 399 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 400 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 401 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 402 | ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, |
| 403 | }; |
| 404 | |
| 405 | #define toktype(c) ((int) TokTypeTab[(c) & 0xff]) |
| 406 | |
| 407 | |
| 408 | # define NOCHAR -1 /* signal nothing in lookahead token */ |
| 409 | |
| 410 | char ** |
| 411 | prescan(addr, delim, pvpbuf, pvpbsize, delimptr) |
| 412 | char *addr; |
| 413 | char delim; |
| 414 | char pvpbuf[]; |
| 415 | char **delimptr; |
| 416 | { |
| 417 | register char *p; |
| 418 | register char *q; |
| 419 | register int c; |
| 420 | char **avp; |
| 421 | bool bslashmode; |
| 422 | int cmntcnt; |
| 423 | int anglecnt; |
| 424 | char *tok; |
| 425 | int state; |
| 426 | int newstate; |
| 427 | char *saveto = CurEnv->e_to; |
| 428 | static char *av[MAXATOM+1]; |
| 429 | static char firsttime = TRUE; |
| 430 | |
| 431 | if (firsttime) |
| 432 | { |
| 433 | /* initialize the token type table */ |
| 434 | char obuf[50]; |
| 435 | |
| 436 | firsttime = FALSE; |
| 437 | expand("\201o", obuf, &obuf[sizeof obuf - sizeof DELIMCHARS], CurEnv); |
| 438 | strcat(obuf, DELIMCHARS); |
| 439 | for (p = obuf; *p != '\0'; p++) |
| 440 | { |
| 441 | if (TokTypeTab[*p & 0xff] == ATM) |
| 442 | TokTypeTab[*p & 0xff] = OPR; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* make sure error messages don't have garbage on them */ |
| 447 | errno = 0; |
| 448 | |
| 449 | q = pvpbuf; |
| 450 | bslashmode = FALSE; |
| 451 | cmntcnt = 0; |
| 452 | anglecnt = 0; |
| 453 | avp = av; |
| 454 | state = ATM; |
| 455 | c = NOCHAR; |
| 456 | p = addr; |
| 457 | CurEnv->e_to = p; |
| 458 | if (tTd(22, 11)) |
| 459 | { |
| 460 | printf("prescan: "); |
| 461 | xputs(p); |
| 462 | (void) putchar('\n'); |
| 463 | } |
| 464 | |
| 465 | do |
| 466 | { |
| 467 | /* read a token */ |
| 468 | tok = q; |
| 469 | for (;;) |
| 470 | { |
| 471 | /* store away any old lookahead character */ |
| 472 | if (c != NOCHAR && !bslashmode) |
| 473 | { |
| 474 | /* see if there is room */ |
| 475 | if (q >= &pvpbuf[pvpbsize - 5]) |
| 476 | { |
| 477 | usrerr("553 Address too long"); |
| 478 | returnnull: |
| 479 | if (delimptr != NULL) |
| 480 | *delimptr = p; |
| 481 | CurEnv->e_to = saveto; |
| 482 | return (NULL); |
| 483 | } |
| 484 | |
| 485 | /* squirrel it away */ |
| 486 | *q++ = c; |
| 487 | } |
| 488 | |
| 489 | /* read a new input character */ |
| 490 | c = *p++; |
| 491 | if (c == '\0') |
| 492 | { |
| 493 | /* diagnose and patch up bad syntax */ |
| 494 | if (state == QST) |
| 495 | { |
| 496 | usrerr("653 Unbalanced '\"'"); |
| 497 | c = '"'; |
| 498 | } |
| 499 | else if (cmntcnt > 0) |
| 500 | { |
| 501 | usrerr("653 Unbalanced '('"); |
| 502 | c = ')'; |
| 503 | } |
| 504 | else if (anglecnt > 0) |
| 505 | { |
| 506 | c = '>'; |
| 507 | usrerr("653 Unbalanced '<'"); |
| 508 | } |
| 509 | else |
| 510 | break; |
| 511 | |
| 512 | p--; |
| 513 | } |
| 514 | else if (c == delim && anglecnt <= 0 && |
| 515 | cmntcnt <= 0 && state != QST) |
| 516 | break; |
| 517 | |
| 518 | if (tTd(22, 101)) |
| 519 | printf("c=%c, s=%d; ", c, state); |
| 520 | |
| 521 | /* chew up special characters */ |
| 522 | *q = '\0'; |
| 523 | if (bslashmode) |
| 524 | { |
| 525 | bslashmode = FALSE; |
| 526 | |
| 527 | /* kludge \! for naive users */ |
| 528 | if (cmntcnt > 0) |
| 529 | { |
| 530 | c = NOCHAR; |
| 531 | continue; |
| 532 | } |
| 533 | else if (c != '!' || state == QST) |
| 534 | { |
| 535 | *q++ = '\\'; |
| 536 | continue; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (c == '\\') |
| 541 | { |
| 542 | bslashmode = TRUE; |
| 543 | } |
| 544 | if (state == QST) |
| 545 | { |
| 546 | /* do nothing, just avoid next clauses */ |
| 547 | } |
| 548 | else if (c == '(') |
| 549 | { |
| 550 | cmntcnt++; |
| 551 | c = NOCHAR; |
| 552 | } |
| 553 | else if (c == ')') |
| 554 | { |
| 555 | if (cmntcnt <= 0) |
| 556 | { |
| 557 | usrerr("653 Unbalanced ')'"); |
| 558 | c = NOCHAR; |
| 559 | } |
| 560 | else |
| 561 | cmntcnt--; |
| 562 | } |
| 563 | else if (cmntcnt > 0) |
| 564 | c = NOCHAR; |
| 565 | else if (c == '<') |
| 566 | anglecnt++; |
| 567 | else if (c == '>') |
| 568 | { |
| 569 | if (anglecnt <= 0) |
| 570 | { |
| 571 | usrerr("653 Unbalanced '>'"); |
| 572 | c = NOCHAR; |
| 573 | } |
| 574 | else |
| 575 | anglecnt--; |
| 576 | } |
| 577 | else if (delim == ' ' && isascii(c) && isspace(c)) |
| 578 | c = ' '; |
| 579 | else if (c == ':' && !CurEnv->e_oldstyle) |
| 580 | { |
| 581 | /* consume characters until a semicolon */ |
| 582 | while (*p != '\0' && *p != ';') |
| 583 | p++; |
| 584 | if (*p == '\0') |
| 585 | usrerr("Unbalanced ':...;' group spec"); |
| 586 | else |
| 587 | p++; |
| 588 | c = ' '; |
| 589 | } |
| 590 | |
| 591 | else if (c == ';') /* semicolons are not tokens */ |
| 592 | c = NOCHAR; |
| 593 | |
| 594 | if (c == NOCHAR) |
| 595 | continue; |
| 596 | |
| 597 | /* see if this is end of input */ |
| 598 | if (c == delim && anglecnt <= 0 && state != QST) |
| 599 | break; |
| 600 | |
| 601 | newstate = StateTab[state][toktype(c)]; |
| 602 | if (tTd(22, 101)) |
| 603 | printf("ns=%02o\n", newstate); |
| 604 | state = newstate & TYPE; |
| 605 | if (bitset(M, newstate)) |
| 606 | c = NOCHAR; |
| 607 | if (bitset(B, newstate)) |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | /* new token */ |
| 612 | if (tok != q) |
| 613 | { |
| 614 | *q++ = '\0'; |
| 615 | if (tTd(22, 36)) |
| 616 | { |
| 617 | printf("tok="); |
| 618 | xputs(tok); |
| 619 | (void) putchar('\n'); |
| 620 | } |
| 621 | if (avp >= &av[MAXATOM]) |
| 622 | { |
| 623 | syserr("553 prescan: too many tokens"); |
| 624 | goto returnnull; |
| 625 | } |
| 626 | if (q - tok > MAXNAME) |
| 627 | { |
| 628 | syserr("553 prescan: token too long"); |
| 629 | goto returnnull; |
| 630 | } |
| 631 | *avp++ = tok; |
| 632 | } |
| 633 | } while (c != '\0' && (c != delim || anglecnt > 0)); |
| 634 | *avp = NULL; |
| 635 | p--; |
| 636 | if (delimptr != NULL) |
| 637 | *delimptr = p; |
| 638 | if (tTd(22, 12)) |
| 639 | { |
| 640 | printf("prescan==>"); |
| 641 | printav(av); |
| 642 | } |
| 643 | CurEnv->e_to = saveto; |
| 644 | if (av[0] == NULL) |
| 645 | { |
| 646 | if (tTd(22, 1)) |
| 647 | printf("prescan: null leading token\n"); |
| 648 | return (NULL); |
| 649 | } |
| 650 | return (av); |
| 651 | } |
| 652 | \f/* |
| 653 | ** REWRITE -- apply rewrite rules to token vector. |
| 654 | ** |
| 655 | ** This routine is an ordered production system. Each rewrite |
| 656 | ** rule has a LHS (called the pattern) and a RHS (called the |
| 657 | ** rewrite); 'rwr' points the the current rewrite rule. |
| 658 | ** |
| 659 | ** For each rewrite rule, 'avp' points the address vector we |
| 660 | ** are trying to match against, and 'pvp' points to the pattern. |
| 661 | ** If pvp points to a special match value (MATCHZANY, MATCHANY, |
| 662 | ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp |
| 663 | ** matched is saved away in the match vector (pointed to by 'mvp'). |
| 664 | ** |
| 665 | ** When a match between avp & pvp does not match, we try to |
| 666 | ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS |
| 667 | ** we must also back out the match in mvp. If we reach a |
| 668 | ** MATCHANY or MATCHZANY we just extend the match and start |
| 669 | ** over again. |
| 670 | ** |
| 671 | ** When we finally match, we rewrite the address vector |
| 672 | ** and try over again. |
| 673 | ** |
| 674 | ** Parameters: |
| 675 | ** pvp -- pointer to token vector. |
| 676 | ** ruleset -- the ruleset to use for rewriting. |
| 677 | ** reclevel -- recursion level (to catch loops). |
| 678 | ** e -- the current envelope. |
| 679 | ** |
| 680 | ** Returns: |
| 681 | ** A status code. If EX_TEMPFAIL, higher level code should |
| 682 | ** attempt recovery. |
| 683 | ** |
| 684 | ** Side Effects: |
| 685 | ** pvp is modified. |
| 686 | */ |
| 687 | |
| 688 | # define OP_NONZLEN 00001 |
| 689 | # define OP_VARLEN 00002 |
| 690 | # define OP_CLASS 00004 |
| 691 | # define OP_EXACT 00010 |
| 692 | |
| 693 | struct match |
| 694 | { |
| 695 | char **first; /* first token matched */ |
| 696 | char **last; /* last token matched */ |
| 697 | char **pattern; /* pointer to pattern */ |
| 698 | char **source; /* left hand source operand */ |
| 699 | char flags; /* attributes of this operator */ |
| 700 | }; |
| 701 | |
| 702 | # define MAXMATCH 9 /* max params per rewrite */ |
| 703 | # define MAX_CONTROL ' ' |
| 704 | |
| 705 | # ifndef MAXRULERECURSION |
| 706 | # define MAXRULERECURSION 50 /* max recursion depth */ |
| 707 | # endif |
| 708 | static char control_opts[MAX_CONTROL]; |
| 709 | |
| 710 | |
| 711 | int |
| 712 | static char control_init_data[] = { |
| 713 | MATCHZANY, OP_VARLEN, |
| 714 | MATCHONE, OP_NONZLEN, |
| 715 | MATCHANY, OP_VARLEN|OP_NONZLEN, |
| 716 | #ifdef MACVALUE |
| 717 | MACVALUE, OP_EXACT, |
| 718 | #endif /* MACVALUE */ |
| 719 | MATCHNCLASS, OP_NONZLEN, |
| 720 | MATCHCLASS, OP_NONZLEN|OP_VARLEN|OP_CLASS, |
| 721 | }; |
| 722 | |
| 723 | static int nrw; |
| 724 | |
| 725 | void |
| 726 | rewrite(pvp, ruleset, reclevel, e) |
| 727 | char **pvp; |
| 728 | int ruleset; |
| 729 | int reclevel; |
| 730 | register ENVELOPE *e; |
| 731 | { |
| 732 | nrw = 0; |
| 733 | _rewrite(pvp, ruleset); |
| 734 | } |
| 735 | |
| 736 | static void |
| 737 | _rewrite(pvp, ruleset) |
| 738 | char **pvp; |
| 739 | int ruleset; |
| 740 | { |
| 741 | register char *ap; /* address pointer */ |
| 742 | register char *rp; /* rewrite pointer */ |
| 743 | register char **avp; /* address vector pointer */ |
| 744 | register char **rvp; /* rewrite vector pointer */ |
| 745 | register struct match *mlp; /* cur ptr into mlist */ |
| 746 | register struct rewrite *rwr; /* pointer to current rewrite rule */ |
| 747 | int ruleno; /* current rule number */ |
| 748 | int rstat = EX_OK; /* return status */ |
| 749 | int loopcount; |
| 750 | int subr; /* subroutine number if >= 0 */ |
| 751 | bool dolookup; /* do host aliasing */ |
| 752 | char *npvp[MAXATOM+1]; /* temporary space for rebuild */ |
| 753 | char tokbuf[MAXNAME+1]; /* for concatenated class tokens */ |
| 754 | int nloops, nmatches = 0; /* for looping rule checks */ |
| 755 | struct rewrite *prev_rwr; /* pointer to previous rewrite rule */ |
| 756 | struct match mlist[MAXMATCH+1]; /* stores match on LHS */ |
| 757 | struct match *old_mlp; /* to save our place */ |
| 758 | bool extend_match; /* extend existing match during backup */ |
| 759 | |
| 760 | if (OpMode == MD_TEST || tTd(21, 2)) |
| 761 | { |
| 762 | printf("rewrite: ruleset %2d input:", ruleset); |
| 763 | printcav(pvp); |
| 764 | } |
| 765 | if (ruleset < 0 || ruleset >= MAXRWSETS) |
| 766 | { |
| 767 | syserr("554 rewrite: illegal ruleset number %d", ruleset); |
| 768 | return EX_CONFIG; |
| 769 | } |
| 770 | if (reclevel++ > MAXRULERECURSION) |
| 771 | { |
| 772 | syserr("rewrite: infinite recursion, ruleset %d", ruleset); |
| 773 | return EX_CONFIG; |
| 774 | } |
| 775 | if (pvp == NULL) |
| 776 | return EX_USAGE; |
| 777 | |
| 778 | if (++nrw > 100) |
| 779 | { |
| 780 | char buf[MAXLINE]; |
| 781 | |
| 782 | buf[0] = buf[MAXLINE-1] = 0; |
| 783 | while (*pvp) |
| 784 | (void) strncat(buf, *pvp++, sizeof buf); |
| 785 | syserr("address causes rewrite loop: <%s>", buf); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | /* Be sure to recognize first rule as new */ |
| 790 | prev_rwr = NULL; |
| 791 | |
| 792 | /* |
| 793 | ** Run through the list of rewrite rules, applying any that match. |
| 794 | */ |
| 795 | |
| 796 | ruleno = 1; |
| 797 | loopcount = 0; |
| 798 | for (rwr = RewriteRules[ruleset]; rwr != NULL; ) |
| 799 | { |
| 800 | if (tTd(21, 12)) |
| 801 | { |
| 802 | printf("-----trying rule:"); |
| 803 | printcav(rwr->r_lhs); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | ** Set up the match list. This is done once for each |
| 808 | ** rule. If a rule is used repeatedly, the list need not |
| 809 | ** be set up the next time. |
| 810 | */ |
| 811 | |
| 812 | if (rwr != prev_rwr) |
| 813 | { |
| 814 | prev_rwr = rwr; |
| 815 | for (rvp = rwr->r_lhs, mlp = mlist; |
| 816 | *rvp && (mlp < &mlist[MAXMATCH]); rvp++) |
| 817 | { |
| 818 | mlp->flags = ((unsigned char) **rvp >= MAX_CONTROL) ? |
| 819 | 0 : control_opts[**rvp] ; |
| 820 | if (mlp->flags) |
| 821 | { |
| 822 | mlp->source = rvp; |
| 823 | mlp++; |
| 824 | } |
| 825 | } |
| 826 | if (*rvp) |
| 827 | { |
| 828 | syserr("Too many variables on LHS in ruleset %d", ruleset); |
| 829 | return; |
| 830 | } |
| 831 | mlp->source = rvp; |
| 832 | |
| 833 | /* Make sure end marker is initialized */ |
| 834 | mlp->flags = 0; |
| 835 | } |
| 836 | |
| 837 | /* try to match on this rule */ |
| 838 | mlp = mlist; |
| 839 | |
| 840 | rvp = rwr->r_lhs; |
| 841 | avp = pvp; |
| 842 | nloops = 0; |
| 843 | extend_match = FALSE; |
| 844 | |
| 845 | if (++loopcount > 100) |
| 846 | { |
| 847 | if (nloops++ > 400) |
| 848 | { |
| 849 | syserr("Looping on ruleset %d, rule %d", |
| 850 | ruleset, rwr-RewriteRules[ruleset]); |
| 851 | mlp = mlist - 1; /* force rule failure */ |
| 852 | break; |
| 853 | } |
| 854 | syserr("554 Infinite loop in ruleset %d, rule %d", |
| 855 | ruleset, ruleno); |
| 856 | if (tTd(21, 1)) |
| 857 | { |
| 858 | printf("workspace: "); |
| 859 | printav(pvp); |
| 860 | } |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | while ((ap = *avp) != NULL || *rvp != NULL) |
| 865 | { |
| 866 | rp = *rvp; |
| 867 | |
| 868 | if (tTd(21, 35)) |
| 869 | { |
| 870 | printf("ADVANCE rp="); |
| 871 | xputs(rp); |
| 872 | printf(", ap="); |
| 873 | xputs(ap); |
| 874 | printf("\n"); |
| 875 | } |
| 876 | |
| 877 | if (extend_match) |
| 878 | extend_match = FALSE; |
| 879 | else |
| 880 | { |
| 881 | mlp->first = avp; |
| 882 | mlp->last = mlp->flags == 0 || (mlp->flags & OP_NONZLEN) ? |
| 883 | avp + 1 : avp; |
| 884 | } |
| 885 | |
| 886 | if (rp == NULL) |
| 887 | /* end-of-pattern before end-of-address */ |
| 888 | goto backup; |
| 889 | |
| 890 | /* Premature end of address */ |
| 891 | if (ap == NULL && avp != mlp->last) |
| 892 | goto backup; |
| 893 | |
| 894 | /* |
| 895 | ** Simplest case - exact token comparison between |
| 896 | ** pattern and address. Such a match is not saved |
| 897 | ** in mlp. |
| 898 | */ |
| 899 | |
| 900 | if (rvp < mlp->source) |
| 901 | { |
| 902 | if (ap == NULL || strcasecmp(ap, rp)) |
| 903 | goto backup; |
| 904 | rvp++; |
| 905 | avp++; |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | #ifdef MACVALUE |
| 910 | /* |
| 911 | ** This is special case handled. The match is exact, |
| 912 | ** but might span zero or more address tokens. The |
| 913 | ** result is saved in mlp. |
| 914 | */ |
| 915 | |
| 916 | if (*rp == MACVALUE) |
| 917 | { |
| 918 | int len; |
| 919 | rp = macvalue(rp[1], CurEnv); |
| 920 | |
| 921 | if (rp) |
| 922 | while (*rp) |
| 923 | { |
| 924 | if (*avp == NULL || strncasecmp(rp,*avp,len = strlen(*avp))) |
| 925 | goto backup; |
| 926 | rp += len; |
| 927 | avp++; |
| 928 | } |
| 929 | mlp->last = avp; |
| 930 | rvp++; |
| 931 | mlp++; |
| 932 | continue; |
| 933 | } |
| 934 | #endif /* MACVALUE */ |
| 935 | |
| 936 | /* |
| 937 | ** All other matches are saved in mlp. Initially |
| 938 | ** assume they match at the shortest possible length |
| 939 | ** for this pattern. Variable patterns will be |
| 940 | ** extended later as needed. |
| 941 | */ |
| 942 | |
| 943 | /* Fixed length first */ |
| 944 | if (!(mlp->flags & OP_VARLEN)) |
| 945 | { |
| 946 | switch (*rp) |
| 947 | { |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | avp = mlp->last; |
| 952 | rvp++; |
| 953 | mlp++; |
| 954 | break; |
| 955 | |
| 956 | case MATCHZERO: |
| 957 | /* match zero tokens */ |
| 958 | continue; |
| 959 | } |
| 960 | |
| 961 | case MACRODEXPAND: |
| 962 | /* |
| 963 | ** Match against run-time macro. |
| 964 | ** This algorithm is broken for the |
| 965 | ** general case (no recursive macros, |
| 966 | ** improper tokenization) but should |
| 967 | ** work for the usual cases. |
| 968 | */ |
| 969 | |
| 970 | ap = macvalue(rp[1], e); |
| 971 | mlp->first = avp; |
| 972 | if (tTd(21, 2)) |
| 973 | printf("rewrite: LHS $&%c => \"%s\"\n", |
| 974 | rp[1], |
| 975 | ap == NULL ? "(NULL)" : ap); |
| 976 | |
| 977 | if (ap == NULL) |
| 978 | break; |
| 979 | while (*ap != '\0') |
| 980 | { |
| 981 | if (*avp == NULL || |
| 982 | strncasecmp(ap, *avp, strlen(*avp)) != 0) |
| 983 | { |
| 984 | /* no match */ |
| 985 | avp = mlp->first; |
| 986 | goto backup; |
| 987 | } |
| 988 | ap += strlen(*avp++); |
| 989 | } |
| 990 | |
| 991 | /* match */ |
| 992 | break; |
| 993 | |
| 994 | /* |
| 995 | ** We now have a variable length item. It could |
| 996 | ** be $+ or $* in which case no special checking |
| 997 | ** is needed. But a class match such as $=x must |
| 998 | ** be verified. |
| 999 | ** |
| 1000 | ** As a speedup, if a variable length item is |
| 1001 | ** followed by a plain character token, we initially |
| 1002 | ** extend the match to the first such token we find. |
| 1003 | ** If the required character token cannot be found, |
| 1004 | ** we fail the match at this point. |
| 1005 | */ |
| 1006 | |
| 1007 | avp = mlp->last; |
| 1008 | |
| 1009 | /* If next token is char token */ |
| 1010 | if (&rvp[1] < mlp[1].source) |
| 1011 | { |
| 1012 | while (*avp && strcasecmp(*avp, rvp[1])) |
| 1013 | avp++; |
| 1014 | |
| 1015 | /* |
| 1016 | ** If we can't find the proper ending token, |
| 1017 | ** leave avp point to NULL. This indicates |
| 1018 | ** we have run out of address tokens. It is |
| 1019 | ** pointless to advance the beginning of this |
| 1020 | ** match and retry. |
| 1021 | */ |
| 1022 | |
| 1023 | if (*avp == NULL) |
| 1024 | goto backup; |
| 1025 | mlp->last = avp; |
| 1026 | } |
| 1027 | else if (rvp[1] == NULL) |
| 1028 | /* next token is end of address */ |
| 1029 | { |
| 1030 | while (*avp) |
| 1031 | avp++; |
| 1032 | mlp->last = avp; |
| 1033 | } |
| 1034 | |
| 1035 | if (mlp->flags & OP_CLASS) |
| 1036 | { |
| 1037 | register char *cp = tokbuf; |
| 1038 | |
| 1039 | avp = mlp->first; |
| 1040 | strcpy(cp, *avp); |
| 1041 | avp++; |
| 1042 | for (;;) |
| 1043 | { |
| 1044 | while (avp < mlp->last) |
| 1045 | { |
| 1046 | while (*cp) |
| 1047 | cp++; |
| 1048 | strcpy(cp, *avp); |
| 1049 | avp++; |
| 1050 | } |
| 1051 | switch (*rp) |
| 1052 | { |
| 1053 | register STAB *s; |
| 1054 | |
| 1055 | case MATCHCLASS: |
| 1056 | s = stab(tokbuf, ST_CLASS, ST_FIND); |
| 1057 | if (s != NULL && bitnset(rp[1], s->s_class)) |
| 1058 | goto have_match; |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | ** Class match initially failed. |
| 1064 | ** Extend the tentative match. |
| 1065 | ** Again, if followed by a character |
| 1066 | ** token, extend all the way to that |
| 1067 | ** token before checking. |
| 1068 | */ |
| 1069 | |
| 1070 | if (*avp) |
| 1071 | { |
| 1072 | (mlp->last)++; |
| 1073 | if (&rvp[1] < mlp[1].source) |
| 1074 | { |
| 1075 | while (*(mlp->last) && strcasecmp(*(mlp->last), rvp[1])) |
| 1076 | (mlp->last)++; |
| 1077 | if (*(mlp->last) == NULL) |
| 1078 | avp = mlp->last; |
| 1079 | } |
| 1080 | } |
| 1081 | if (*avp == NULL) |
| 1082 | { |
| 1083 | /* |
| 1084 | ** We could not find the |
| 1085 | ** ending token. But we had |
| 1086 | ** found ending tokens before. |
| 1087 | ** A match is still plausible |
| 1088 | ** if the start of the |
| 1089 | ** tentative match is advanced. |
| 1090 | ** Hence we must not leave avp |
| 1091 | ** pointing to NULL. |
| 1092 | */ |
| 1093 | avp = mlp->first; |
| 1094 | goto backup; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | have_match: |
| 1100 | rvp++; |
| 1101 | mlp++; |
| 1102 | continue; |
| 1103 | |
| 1104 | backup: |
| 1105 | /* We failed to match. mlp marks point of failure */ |
| 1106 | |
| 1107 | /* |
| 1108 | ** There is a special case when we have exhausted |
| 1109 | ** the address, but have not exhausted the pattern. |
| 1110 | ** Under normal circumstances we could consider the |
| 1111 | ** failure permanent, since extending the number of |
| 1112 | ** address tokens matched by a '$+' or a '$*' will |
| 1113 | ** only worsen the situation. |
| 1114 | ** |
| 1115 | ** There is an exception, however. It is possible |
| 1116 | ** that we have matched a class token, say '$=x', |
| 1117 | ** with three or more tokens. Extending a '$+' say, |
| 1118 | ** which precedes the '$=x' will move the beginning |
| 1119 | ** of the '$=x' match to the right, but it might match |
| 1120 | ** a smaller number of tokens then, possibly |
| 1121 | ** correcting the mismatch. |
| 1122 | ** |
| 1123 | ** Thus in this case we initially back up to the |
| 1124 | ** $=x which matches three or more tokens. |
| 1125 | */ |
| 1126 | |
| 1127 | if (*avp == NULL) |
| 1128 | { |
| 1129 | rvp = mlp->pattern; |
| 1130 | while (--mlp > mlist) |
| 1131 | { |
| 1132 | if ((mlp->flags & OP_CLASS) && |
| 1133 | mlp->last > 2 + mlp->first) |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | ** Now backup till we find a match with a pattern |
| 1140 | ** whose length is extendable, and extend that. |
| 1141 | */ |
| 1142 | |
| 1143 | mlp--; |
| 1144 | while (mlp >= mlist && !(mlp->flags & OP_VARLEN)) |
| 1145 | mlp--; |
| 1146 | |
| 1147 | /* Total failure to match */ |
| 1148 | if (mlp < mlist) |
| 1149 | break; |
| 1150 | |
| 1151 | avp = ++(mlp->last); |
| 1152 | rvp = mlp->source; |
| 1153 | |
| 1154 | /* |
| 1155 | ** We have found a backup point. Normally we would |
| 1156 | ** increase the matched amount by one token, and |
| 1157 | ** continue from the next item in the pattern. But |
| 1158 | ** there are two special cases. If this is a |
| 1159 | ** class-type match (OP_CLASS), we must test the |
| 1160 | ** validity of the extended match. If this pattern |
| 1161 | ** item is directly followed by a character token, it |
| 1162 | ** is worth going back and locating the next such |
| 1163 | ** character token before we continue on. |
| 1164 | */ |
| 1165 | if ((mlp->flags & OP_CLASS) || (&rvp[1] < mlp[1].source)) |
| 1166 | { |
| 1167 | avp = mlp->first; |
| 1168 | extend_match = TRUE; |
| 1169 | } |
| 1170 | else |
| 1171 | { |
| 1172 | mlp++; |
| 1173 | rvp++; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | ** See if we successfully matched. |
| 1179 | */ |
| 1180 | |
| 1181 | if (mlp < mlist) |
| 1182 | { |
| 1183 | if (tTd(21, 10)) |
| 1184 | printf("----- rule fails\n"); |
| 1185 | rwr = rwr->r_next; |
| 1186 | ruleno++; |
| 1187 | loopcount = 0; |
| 1188 | nmatches = 0; |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | if (nmatches++ > 200) |
| 1193 | { |
| 1194 | syserr("Loop in ruleset %d, rule %d (too many matches)", |
| 1195 | ruleset, rwr - RewriteRules[ruleset]); |
| 1196 | rwr = rwr->r_next; |
| 1197 | nmatches = 0; |
| 1198 | continue; |
| 1199 | } |
| 1200 | |
| 1201 | rvp = rwr->r_rhs; |
| 1202 | if (tTd(21, 12)) |
| 1203 | { |
| 1204 | printf("-----rule matches:"); |
| 1205 | printcav(rvp); |
| 1206 | } |
| 1207 | |
| 1208 | rp = *rvp; |
| 1209 | if ((*rp & 0377) == CANONUSER) |
| 1210 | { |
| 1211 | rvp++; |
| 1212 | rwr = rwr->r_next; |
| 1213 | ruleno++; |
| 1214 | loopcount = 0; |
| 1215 | nmatches = 0; |
| 1216 | } |
| 1217 | else if ((*rp & 0377) == CANONHOST) |
| 1218 | { |
| 1219 | rvp++; |
| 1220 | rwr = NULL; |
| 1221 | } |
| 1222 | else if ((*rp & 0377) == CANONNET) |
| 1223 | rwr = NULL; |
| 1224 | |
| 1225 | /* substitute */ |
| 1226 | dolookup = FALSE; |
| 1227 | for (avp = npvp; *rvp != NULL; rvp++) |
| 1228 | { |
| 1229 | register struct match *m; |
| 1230 | register char **pp; |
| 1231 | |
| 1232 | rp = *rvp; |
| 1233 | |
| 1234 | /* check to see if we should do a lookup */ |
| 1235 | if (*rp == MATCHLOOKUP) |
| 1236 | dolookup = TRUE; |
| 1237 | |
| 1238 | /* see if there is substitution here */ |
| 1239 | if (*rp == MATCHREPL && rp[1] >= '1' && rp[1] <= '9') |
| 1240 | { |
| 1241 | /* substitute from LHS */ |
| 1242 | m = &mlist[rp[1] - '1']; |
| 1243 | if (m < mlist || m >= mlp) |
| 1244 | { |
| 1245 | toolong: |
| 1246 | syserr("rewrite: ruleset %d: replacement #%c out of bounds", |
| 1247 | ruleset, rp[1]); |
| 1248 | return EX_CONFIG; |
| 1249 | } |
| 1250 | if (tTd(21, 15)) |
| 1251 | { |
| 1252 | printf("$%c:", rp[1]); |
| 1253 | pp = m->first; |
| 1254 | while (pp < m->last) |
| 1255 | { |
| 1256 | printf(" %x=\"", *pp); |
| 1257 | (void) fflush(stdout); |
| 1258 | printf("%s\"", *pp++); |
| 1259 | } |
| 1260 | printf("\n"); |
| 1261 | } |
| 1262 | pp = m->first; |
| 1263 | while (pp < m->last) |
| 1264 | { |
| 1265 | if (avp >= &npvp[MAXATOM]) |
| 1266 | goto toolong; |
| 1267 | *avp++ = *pp++; |
| 1268 | } |
| 1269 | } |
| 1270 | else |
| 1271 | { |
| 1272 | /* vanilla replacement */ |
| 1273 | if (avp >= &npvp[MAXATOM]) |
| 1274 | goto toolong; |
| 1275 | #ifdef MACVALUE |
| 1276 | if (*rp == MACVALUE) |
| 1277 | { |
| 1278 | char *p = macvalue(rp[1], CurEnv); |
| 1279 | |
| 1280 | if (tTd(21, 2)) |
| 1281 | printf("expanding runtime macro '%c' to \"%s\"\n", |
| 1282 | rp[1], p ? p : "(null)"); |
| 1283 | if (p) |
| 1284 | *avp++ = p; |
| 1285 | } |
| 1286 | else |
| 1287 | #endif /* MACVALUE */ |
| 1288 | *avp++ = rp; |
| 1289 | } |
| 1290 | } |
| 1291 | *avp++ = NULL; |
| 1292 | |
| 1293 | /* |
| 1294 | ** Check for any hostname/keyword lookups. |
| 1295 | */ |
| 1296 | |
| 1297 | for (rvp = npvp; *rvp != NULL; rvp++) |
| 1298 | { |
| 1299 | char **hbrvp, **ubrvp; |
| 1300 | char **xpvp; |
| 1301 | int trsize; |
| 1302 | char *replac; |
| 1303 | int endtoken; |
| 1304 | STAB *map; |
| 1305 | char *mapname; |
| 1306 | char **key_rvp; |
| 1307 | char **arg_rvp; |
| 1308 | char **default_rvp; |
| 1309 | char hbuf[MAXNAME + 1], ubuf[MAXNAME + 1]; |
| 1310 | char *pvpb1[MAXATOM + 1]; |
| 1311 | char *argvect[10]; |
| 1312 | char pvpbuf[PSBUFSIZE]; |
| 1313 | char *nullpvp[1]; |
| 1314 | bool match, defaultpart; |
| 1315 | char begintype; |
| 1316 | char db = '\0'; |
| 1317 | |
| 1318 | if ((**rvp & 0377) != HOSTBEGIN && |
| 1319 | (**rvp & 0377) != LOOKUPBEGIN) |
| 1320 | continue; |
| 1321 | |
| 1322 | /* |
| 1323 | ** Got a hostname/keyword lookup. |
| 1324 | ** |
| 1325 | ** This could be optimized fairly easily. |
| 1326 | */ |
| 1327 | |
| 1328 | begintype = **rvp; |
| 1329 | hbrvp = rvp; |
| 1330 | ubrvp = NULL; |
| 1331 | if ((**rvp & 0377) == HOSTBEGIN) |
| 1332 | { |
| 1333 | endtoken = HOSTEND; |
| 1334 | mapname = "host"; |
| 1335 | } |
| 1336 | else |
| 1337 | { |
| 1338 | endtoken = LOOKUPEND; |
| 1339 | mapname = *++rvp; |
| 1340 | } |
| 1341 | map = stab(mapname, ST_MAP, ST_FIND); |
| 1342 | if (map == NULL) |
| 1343 | syserr("554 rewrite: map %s not found", mapname); |
| 1344 | |
| 1345 | /* extract the match part */ |
| 1346 | key_rvp = ++rvp; |
| 1347 | default_rvp = NULL; |
| 1348 | arg_rvp = argvect; |
| 1349 | xpvp = NULL; |
| 1350 | replac = pvpbuf; |
| 1351 | while (*rvp != NULL && (**rvp & 0377) != endtoken) |
| 1352 | { |
| 1353 | int nodetype = **rvp & 0377; |
| 1354 | |
| 1355 | if (nodetype != CANONHOST && nodetype != CANONUSER) |
| 1356 | { |
| 1357 | rvp++; |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | *rvp++ = NULL; |
| 1362 | |
| 1363 | if (xpvp != NULL) |
| 1364 | { |
| 1365 | cataddr(xpvp, NULL, replac, |
| 1366 | &pvpbuf[sizeof pvpbuf] - replac, |
| 1367 | '\0'); |
| 1368 | *++arg_rvp = replac; |
| 1369 | replac += strlen(replac) + 1; |
| 1370 | xpvp = NULL; |
| 1371 | } |
| 1372 | switch (nodetype) |
| 1373 | { |
| 1374 | case CANONHOST: |
| 1375 | xpvp = rvp; |
| 1376 | break; |
| 1377 | |
| 1378 | case CANONUSER: |
| 1379 | default_rvp = rvp; |
| 1380 | break; |
| 1381 | } |
| 1382 | } |
| 1383 | if (*rvp != NULL) |
| 1384 | *rvp++ = NULL; |
| 1385 | if (xpvp != NULL) |
| 1386 | { |
| 1387 | cataddr(xpvp, NULL, replac, |
| 1388 | &pvpbuf[sizeof pvpbuf] - replac, |
| 1389 | '\0'); |
| 1390 | *++arg_rvp = replac; |
| 1391 | } |
| 1392 | *++arg_rvp = NULL; |
| 1393 | |
| 1394 | /* save the remainder of the input string */ |
| 1395 | trsize = (int) (avp - rvp + 1) * sizeof *rvp; |
| 1396 | bcopy((char *) rvp, (char *) pvpb1, trsize); |
| 1397 | |
| 1398 | /* append it to the token list */ |
| 1399 | for (avp = hbrvp; *xpvp != NULL; xpvp++) |
| 1400 | { |
| 1401 | *avp++ = newstr(*xpvp); |
| 1402 | if (avp >= &npvp[MAXATOM]) |
| 1403 | goto toolong; |
| 1404 | } |
| 1405 | } |
| 1406 | else |
| 1407 | avp = hbrvp; |
| 1408 | |
| 1409 | /* restore the old trailing information */ |
| 1410 | rvp = avp - 1; |
| 1411 | for (xpvp = pvpb1; *xpvp != NULL; xpvp++) |
| 1412 | { |
| 1413 | if (defaultpart && **xpvp == HOSTEND) |
| 1414 | { |
| 1415 | defaultpart = FALSE; |
| 1416 | rvp = avp - 1; |
| 1417 | } |
| 1418 | else if (!defaultpart || !match) |
| 1419 | *avp++ = *xpvp; |
| 1420 | if (avp >= &npvp[MAXATOM]) |
| 1421 | goto toolong; |
| 1422 | } |
| 1423 | *avp++ = NULL; |
| 1424 | |
| 1425 | /*break;*/ |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | ** Check for subroutine calls. |
| 1430 | ** Then copy vector back into original space. |
| 1431 | */ |
| 1432 | |
| 1433 | callsubr(npvp); |
| 1434 | |
| 1435 | for (avp = npvp; *avp++ != NULL;); |
| 1436 | subr = atoi(*++rvp); |
| 1437 | rvp++; |
| 1438 | |
| 1439 | else |
| 1440 | subr = -1; |
| 1441 | |
| 1442 | /* |
| 1443 | ** Copy result back to original string. |
| 1444 | */ |
| 1445 | |
| 1446 | for (avp = pvp; *rvp != NULL; rvp++) |
| 1447 | *avp++ = *rvp; |
| 1448 | *avp = NULL; |
| 1449 | |
| 1450 | /* |
| 1451 | ** If this specified a subroutine, call it. |
| 1452 | */ |
| 1453 | |
| 1454 | if (subr >= 0) |
| 1455 | { |
| 1456 | # ifdef DEBUG |
| 1457 | if (tTd(21, 3)) |
| 1458 | printf("-----callsubr %s\n", subr); |
| 1459 | # endif DEBUG |
| 1460 | rewrite(pvp, subr); |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | ** Done with rewriting this pass. |
| 1465 | */ |
| 1466 | |
| 1467 | if (tTd(21, 4)) |
| 1468 | { |
| 1469 | printf("rewritten as:"); |
| 1470 | printcav(pvp); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | if (OpMode == MD_TEST || tTd(21, 2)) |
| 1475 | { |
| 1476 | printf("rewrite: ruleset %2d returns:", ruleset); |
| 1477 | printcav(pvp); |
| 1478 | } |
| 1479 | |
| 1480 | return rstat; |
| 1481 | } |
| 1482 | \f/* |
| 1483 | ** CALLSUBR -- call subroutines in rewrite vector |
| 1484 | ** |
| 1485 | ** Parameters: |
| 1486 | ** pvp -- pointer to token vector. |
| 1487 | ** |
| 1488 | ** Returns: |
| 1489 | ** none. |
| 1490 | ** |
| 1491 | ** Side Effects: |
| 1492 | ** pvp is modified. |
| 1493 | */ |
| 1494 | |
| 1495 | static void |
| 1496 | callsubr(pvp) |
| 1497 | char **pvp; |
| 1498 | { |
| 1499 | char **rvp; |
| 1500 | int subr; |
| 1501 | |
| 1502 | for (; *pvp != NULL; pvp++) |
| 1503 | if (**pvp == CALLSUBR && pvp[1] != NULL && isdigit(pvp[1][0])) |
| 1504 | { |
| 1505 | subr = atoi(pvp[1]); |
| 1506 | |
| 1507 | if (tTd(21, 3)) |
| 1508 | printf("-----callsubr %d\n", subr); |
| 1509 | |
| 1510 | /* |
| 1511 | ** Take care of possible inner calls. |
| 1512 | */ |
| 1513 | callsubr(pvp+2); |
| 1514 | |
| 1515 | /* |
| 1516 | ** Move vector up over calling opcode. |
| 1517 | */ |
| 1518 | for (rvp = pvp+2; *rvp != NULL; rvp++) |
| 1519 | rvp[-2] = rvp[0]; |
| 1520 | rvp[-2] = NULL; |
| 1521 | |
| 1522 | /* |
| 1523 | ** Call inferior ruleset. |
| 1524 | */ |
| 1525 | _rewrite(pvp, subr); |
| 1526 | |
| 1527 | break; |
| 1528 | } |
| 1529 | } |
| 1530 | \f/* |
| 1531 | ** BUILDADDR -- build address from token vector. |
| 1532 | ** |
| 1533 | ** Parameters: |
| 1534 | ** tv -- token vector. |
| 1535 | ** a -- pointer to address descriptor to fill. |
| 1536 | ** If NULL, one will be allocated. |
| 1537 | ** flags -- info regarding whether this is a sender or |
| 1538 | ** a recipient. |
| 1539 | ** e -- the current envelope. |
| 1540 | ** |
| 1541 | ** Returns: |
| 1542 | ** NULL if there was an error. |
| 1543 | ** 'a' otherwise. |
| 1544 | ** |
| 1545 | ** Side Effects: |
| 1546 | ** fills in 'a' |
| 1547 | */ |
| 1548 | |
| 1549 | struct errcodes |
| 1550 | { |
| 1551 | char *ec_name; /* name of error code */ |
| 1552 | int ec_code; /* numeric code */ |
| 1553 | } ErrorCodes[] = |
| 1554 | { |
| 1555 | "usage", EX_USAGE, |
| 1556 | "nouser", EX_NOUSER, |
| 1557 | "nohost", EX_NOHOST, |
| 1558 | "unavailable", EX_UNAVAILABLE, |
| 1559 | "software", EX_SOFTWARE, |
| 1560 | "tempfail", EX_TEMPFAIL, |
| 1561 | "protocol", EX_PROTOCOL, |
| 1562 | #ifdef EX_CONFIG |
| 1563 | "config", EX_CONFIG, |
| 1564 | #endif |
| 1565 | NULL, EX_UNAVAILABLE, |
| 1566 | }; |
| 1567 | |
| 1568 | static ADDRESS * |
| 1569 | buildaddr(tv, a, flags, e) |
| 1570 | register char **tv; |
| 1571 | register ADDRESS *a; |
| 1572 | int flags; |
| 1573 | register ENVELOPE *e; |
| 1574 | { |
| 1575 | struct mailer **mp; |
| 1576 | register struct mailer *m; |
| 1577 | char *bp; |
| 1578 | int spaceleft; |
| 1579 | static MAILER errormailer; |
| 1580 | static char *errorargv[] = { "ERROR", NULL }; |
| 1581 | static char buf[MAXNAME]; |
| 1582 | |
| 1583 | if (tTd(24, 5)) |
| 1584 | { |
| 1585 | printf("buildaddr, flags=%o, tv=", flags); |
| 1586 | printav(tv); |
| 1587 | } |
| 1588 | |
| 1589 | if (a == NULL) |
| 1590 | a = (ADDRESS *) xalloc(sizeof *a); |
| 1591 | clear((char *) a, sizeof *a); |
| 1592 | |
| 1593 | /* figure out what net/mailer to use */ |
| 1594 | if (*tv == NULL || **tv != CANONNET) |
| 1595 | { |
| 1596 | syserr("554 buildaddr: no net"); |
| 1597 | badaddr: |
| 1598 | a->q_flags |= QBADADDR; |
| 1599 | a->q_mailer = &errormailer; |
| 1600 | if (errormailer.m_name == NULL) |
| 1601 | { |
| 1602 | /* initialize the bogus mailer */ |
| 1603 | errormailer.m_name = "*error*"; |
| 1604 | errormailer.m_mailer = "ERROR"; |
| 1605 | errormailer.m_argv = errorargv; |
| 1606 | } |
| 1607 | return a; |
| 1608 | } |
| 1609 | tv++; |
| 1610 | if (strcasecmp(*tv, "error") == 0) |
| 1611 | { |
| 1612 | if ((**++tv & 0377) == CANONHOST) |
| 1613 | { |
| 1614 | register struct errcodes *ep; |
| 1615 | |
| 1616 | if (isascii(**++tv) && isdigit(**tv)) |
| 1617 | { |
| 1618 | setstat(atoi(*tv)); |
| 1619 | } |
| 1620 | else |
| 1621 | { |
| 1622 | for (ep = ErrorCodes; ep->ec_name != NULL; ep++) |
| 1623 | if (strcasecmp(ep->ec_name, *tv) == 0) |
| 1624 | break; |
| 1625 | setstat(ep->ec_code); |
| 1626 | } |
| 1627 | tv++; |
| 1628 | } |
| 1629 | else |
| 1630 | setstat(EX_UNAVAILABLE); |
| 1631 | buf[0] = '\0'; |
| 1632 | for (; (*tv != NULL) && (**tv != CANONUSER); tv++) |
| 1633 | { |
| 1634 | if (buf[0] != '\0') |
| 1635 | (void) strcat(buf, " "); |
| 1636 | (void) strcat(buf, *tv); |
| 1637 | } |
| 1638 | if ((**tv & 0377) != CANONUSER) |
| 1639 | syserr("554 buildaddr: error: no user"); |
| 1640 | cataddr(++tv, NULL, buf, sizeof buf, ' '); |
| 1641 | stripquotes(buf); |
| 1642 | #ifdef LOG |
| 1643 | if (LogLevel > 8) |
| 1644 | syslog (LOG_DEBUG, "%s: Trace: $#ERROR $: %s", |
| 1645 | CurEnv->e_id, buf); |
| 1646 | #endif /* LOG */ |
| 1647 | if (isascii(buf[0]) && isdigit(buf[0]) && |
| 1648 | isascii(buf[1]) && isdigit(buf[1]) && |
| 1649 | isascii(buf[2]) && isdigit(buf[2]) && |
| 1650 | buf[3] == ' ') |
| 1651 | { |
| 1652 | char fmt[10]; |
| 1653 | |
| 1654 | strncpy(fmt, buf, 3); |
| 1655 | strcpy(&fmt[3], " %s"); |
| 1656 | usrerr(fmt, buf + 4); |
| 1657 | } |
| 1658 | else |
| 1659 | { |
| 1660 | usrerr("%s", buf); |
| 1661 | } |
| 1662 | goto badaddr; |
| 1663 | } |
| 1664 | |
| 1665 | for (mp = Mailer; (m = *mp++) != NULL; ) |
| 1666 | { |
| 1667 | if (strcasecmp(m->m_name, *tv) == 0) |
| 1668 | break; |
| 1669 | } |
| 1670 | if (m == NULL) |
| 1671 | { |
| 1672 | syserr("554 buildaddr: unknown mailer %s", *tv); |
| 1673 | goto badaddr; |
| 1674 | } |
| 1675 | a->q_mailer = m; |
| 1676 | |
| 1677 | /* figure out what host (if any) */ |
| 1678 | if (**++tv != CANONHOST) |
| 1679 | { |
| 1680 | a->q_host = NULL; |
| 1681 | } |
| 1682 | else |
| 1683 | { |
| 1684 | else |
| 1685 | a->q_host = NULL; |
| 1686 | } |
| 1687 | else |
| 1688 | { |
| 1689 | while (*++tv != NULL && **tv != CANONUSER) |
| 1690 | (void) strcat(buf, *tv); |
| 1691 | a->q_host = newstr(buf); |
| 1692 | } |
| 1693 | |
| 1694 | /* figure out the user */ |
| 1695 | if (*tv == NULL || (**tv & 0377) != CANONUSER) |
| 1696 | { |
| 1697 | syserr("554 buildaddr: no user"); |
| 1698 | goto badaddr; |
| 1699 | } |
| 1700 | tv++; |
| 1701 | |
| 1702 | /* do special mapping for local mailer */ |
| 1703 | if (m == LocalMailer && *tv != NULL) |
| 1704 | { |
| 1705 | register char *p = *tv; |
| 1706 | |
| 1707 | if (*p == '"') |
| 1708 | p++; |
| 1709 | if (*p == '|') |
| 1710 | a->q_mailer = m = ProgMailer; |
| 1711 | else if (*p == '/') |
| 1712 | a->q_mailer = m = FileMailer; |
| 1713 | else if (*p == ':') |
| 1714 | { |
| 1715 | /* may be :include: */ |
| 1716 | cataddr(tv, NULL, buf, sizeof buf, '\0'); |
| 1717 | stripquotes(buf); |
| 1718 | if (strncasecmp(buf, ":include:", 9) == 0) |
| 1719 | { |
| 1720 | /* if :include:, don't need further rewriting */ |
| 1721 | a->q_mailer = m = InclMailer; |
| 1722 | a->q_user = &buf[9]; |
| 1723 | return (a); |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) |
| 1729 | { |
| 1730 | tv++; |
| 1731 | a->q_flags |= QNOTREMOTE; |
| 1732 | } |
| 1733 | |
| 1734 | if (m->m_r_rwset > 0) |
| 1735 | rewrite(tv, m->m_r_rwset); |
| 1736 | (void) rewrite(tv, 4, 0, e); |
| 1737 | |
| 1738 | /* save the result for the command line/RCPT argument */ |
| 1739 | cataddr(tv, NULL, buf, sizeof buf, '\0'); |
| 1740 | a->q_user = buf; |
| 1741 | |
| 1742 | /* |
| 1743 | ** Do mapping to lower case as requested by mailer |
| 1744 | */ |
| 1745 | |
| 1746 | if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) |
| 1747 | makelower(a->q_host); |
| 1748 | if (!bitnset(M_USR_UPPER, m->m_flags)) |
| 1749 | makelower(a->q_user); |
| 1750 | |
| 1751 | return (a); |
| 1752 | } |
| 1753 | \f/* |
| 1754 | ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) |
| 1755 | ** |
| 1756 | ** Parameters: |
| 1757 | ** pvp -- parameter vector to rebuild. |
| 1758 | ** evp -- last parameter to include. Can be NULL to |
| 1759 | ** use entire pvp. |
| 1760 | ** buf -- buffer to build the string into. |
| 1761 | ** sz -- size of buf. |
| 1762 | ** spacesub -- the space separator character; if null, |
| 1763 | ** use SpaceSub. |
| 1764 | ** |
| 1765 | ** Returns: |
| 1766 | ** none. |
| 1767 | ** |
| 1768 | ** Side Effects: |
| 1769 | ** Destroys buf. |
| 1770 | */ |
| 1771 | |
| 1772 | void |
| 1773 | cataddr(pvp, evp, buf, sz, spacesub) |
| 1774 | char **pvp; |
| 1775 | char **evp; |
| 1776 | char *buf; |
| 1777 | register int sz; |
| 1778 | char spacesub; |
| 1779 | { |
| 1780 | bool oatomtok = FALSE; |
| 1781 | bool natomtok; |
| 1782 | register int i; |
| 1783 | register char *p; |
| 1784 | |
| 1785 | if (spacesub == '\0') |
| 1786 | spacesub = SpaceSub; |
| 1787 | |
| 1788 | if (pvp == NULL) |
| 1789 | { |
| 1790 | (void) strcpy(buf, ""); |
| 1791 | return; |
| 1792 | } |
| 1793 | p = buf; |
| 1794 | sz -= 2; |
| 1795 | while (*pvp != NULL && (i = strlen(*pvp)) < sz) |
| 1796 | { |
| 1797 | natomtok = (toktype(**pvp) == ATM); |
| 1798 | if (oatomtok && natomtok) |
| 1799 | *p++ = spacesub; |
| 1800 | (void) strcpy(p, *pvp); |
| 1801 | oatomtok = natomtok; |
| 1802 | p += i; |
| 1803 | sz -= i + 1; |
| 1804 | if (pvp++ == evp) |
| 1805 | break; |
| 1806 | } |
| 1807 | *p = '\0'; |
| 1808 | } |
| 1809 | \f/* |
| 1810 | ** SAMEADDR -- Determine if two addresses are the same |
| 1811 | ** |
| 1812 | ** This is not just a straight comparison -- if the mailer doesn't |
| 1813 | ** care about the host we just ignore it, etc. |
| 1814 | ** |
| 1815 | ** Parameters: |
| 1816 | ** a, b -- pointers to the internal forms to compare. |
| 1817 | ** |
| 1818 | ** Returns: |
| 1819 | ** TRUE -- they represent the same mailbox. |
| 1820 | ** FALSE -- they don't. |
| 1821 | ** |
| 1822 | ** Side Effects: |
| 1823 | ** none. |
| 1824 | */ |
| 1825 | |
| 1826 | bool |
| 1827 | sameaddr(a, b) |
| 1828 | register ADDRESS *a; |
| 1829 | register ADDRESS *b; |
| 1830 | { |
| 1831 | register ADDRESS *ca, *cb; |
| 1832 | |
| 1833 | /* if they don't have the same mailer, forget it */ |
| 1834 | if (a->q_mailer != b->q_mailer) |
| 1835 | return (FALSE); |
| 1836 | |
| 1837 | /* if the user isn't the same, we can drop out */ |
| 1838 | if (strcasecmp(a->q_user, b->q_user)) |
| 1839 | return (FALSE); |
| 1840 | |
| 1841 | /* if we have good uids for both but they differ, these are different */ |
| 1842 | if (a->q_mailer == ProgMailer) |
| 1843 | { |
| 1844 | ca = getctladdr(a); |
| 1845 | cb = getctladdr(b); |
| 1846 | if (ca != NULL && cb != NULL && |
| 1847 | bitset(QGOODUID, ca->q_flags & cb->q_flags) && |
| 1848 | ca->q_uid != cb->q_uid) |
| 1849 | return (FALSE); |
| 1850 | } |
| 1851 | |
| 1852 | /* otherwise compare hosts (but be careful for NULL ptrs) */ |
| 1853 | if (a->q_host == b->q_host) |
| 1854 | { |
| 1855 | /* probably both null pointers */ |
| 1856 | return (TRUE); |
| 1857 | } |
| 1858 | if (a->q_host == NULL || b->q_host == NULL) |
| 1859 | { |
| 1860 | /* only one is a null pointer */ |
| 1861 | return (FALSE); |
| 1862 | } |
| 1863 | if (strcasecmp(a->q_host, b->q_host)) |
| 1864 | return (FALSE); |
| 1865 | |
| 1866 | return (TRUE); |
| 1867 | } |
| 1868 | \f/* |
| 1869 | ** PRINTADDR -- print address (for debugging) |
| 1870 | ** |
| 1871 | ** Parameters: |
| 1872 | ** a -- the address to print |
| 1873 | ** follow -- follow the q_next chain. |
| 1874 | ** |
| 1875 | ** Returns: |
| 1876 | ** none. |
| 1877 | ** |
| 1878 | ** Side Effects: |
| 1879 | ** none. |
| 1880 | */ |
| 1881 | |
| 1882 | void |
| 1883 | printaddr(a, follow) |
| 1884 | register ADDRESS *a; |
| 1885 | bool follow; |
| 1886 | { |
| 1887 | bool first = TRUE; |
| 1888 | register MAILER *m; |
| 1889 | MAILER pseudomailer; |
| 1890 | |
| 1891 | static int indent; |
| 1892 | register int i; |
| 1893 | |
| 1894 | while (a != NULL) |
| 1895 | { |
| 1896 | first = FALSE; |
| 1897 | for (i = indent; i > 0; i--) |
| 1898 | printf("\t"); |
| 1899 | printf("%x=", a); |
| 1900 | (void) fflush(stdout); |
| 1901 | |
| 1902 | /* find the mailer -- carefully */ |
| 1903 | m = a->q_mailer; |
| 1904 | if (m == NULL) |
| 1905 | { |
| 1906 | m = &pseudomailer; |
| 1907 | m->m_mno = -1; |
| 1908 | m->m_name = "NULL"; |
| 1909 | } |
| 1910 | |
| 1911 | for (i = indent; i > 0; i--) |
| 1912 | printf("\t"); |
| 1913 | printf("\tnext=%x, flags=%o, rmailer %d, alias=%x, sibling=%x, child=%x\n", |
| 1914 | a->q_next, a->q_flags, a->q_rmailer, a->q_alias, |
| 1915 | a->q_sibling, a->q_child); |
| 1916 | |
| 1917 | /* follow the chain if appropriate */ |
| 1918 | if (!follow) |
| 1919 | return; |
| 1920 | |
| 1921 | indent++; |
| 1922 | printaddr(a->q_child, TRUE); |
| 1923 | indent--; |
| 1924 | a = a->q_sibling; |
| 1925 | } |
| 1926 | if (first) |
| 1927 | printf("[NULL]\n"); |
| 1928 | } |
| 1929 | |
| 1930 | \f/* |
| 1931 | ** REMOTENAME -- return the name relative to the current mailer |
| 1932 | ** |
| 1933 | ** Parameters: |
| 1934 | ** name -- the name to translate. |
| 1935 | ** m -- the mailer that we want to do rewriting relative |
| 1936 | ** to. |
| 1937 | ** flags -- fine tune operations. |
| 1938 | ** pstat -- pointer to status word. |
| 1939 | ** e -- the current envelope. |
| 1940 | ** |
| 1941 | ** Returns: |
| 1942 | ** the text string representing this address relative to |
| 1943 | ** the receiving mailer. |
| 1944 | ** |
| 1945 | ** Side Effects: |
| 1946 | ** none. |
| 1947 | ** |
| 1948 | ** Warnings: |
| 1949 | ** The text string returned is tucked away locally; |
| 1950 | ** copy it if you intend to save it. |
| 1951 | */ |
| 1952 | |
| 1953 | char * |
| 1954 | remotename(name, m, flags, pstat, e) |
| 1955 | char *name; |
| 1956 | MAILER *m; |
| 1957 | int flags; |
| 1958 | int *pstat; |
| 1959 | register ENVELOPE *e; |
| 1960 | { |
| 1961 | register char **pvp; |
| 1962 | char *fancy; |
| 1963 | char *oldg = macvalue('g', e); |
| 1964 | int rwset; |
| 1965 | static char buf[MAXNAME]; |
| 1966 | char lbuf[MAXNAME]; |
| 1967 | char pvpbuf[PSBUFSIZE]; |
| 1968 | |
| 1969 | if (tTd(12, 1)) |
| 1970 | printf("remotename(%s)\n", name); |
| 1971 | |
| 1972 | /* don't do anything if we are tagging it as special */ |
| 1973 | if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) |
| 1974 | return (name); |
| 1975 | |
| 1976 | /* |
| 1977 | ** Do a heuristic crack of this name to extract any comment info. |
| 1978 | ** This will leave the name as a comment and a $g macro. |
| 1979 | */ |
| 1980 | |
| 1981 | if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) |
| 1982 | fancy = "\201g"; |
| 1983 | else |
| 1984 | fancy = crackaddr(name); |
| 1985 | |
| 1986 | /* |
| 1987 | ** Turn the name into canonical form. |
| 1988 | ** Normally this will be RFC 822 style, i.e., "user@domain". |
| 1989 | ** If this only resolves to "user", and the "C" flag is |
| 1990 | ** specified in the sending mailer, then the sender's |
| 1991 | ** domain will be appended. |
| 1992 | */ |
| 1993 | |
| 1994 | pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL); |
| 1995 | if (pvp == NULL) |
| 1996 | return (name); |
| 1997 | if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) |
| 1998 | *pstat = EX_TEMPFAIL; |
| 1999 | if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) |
| 2000 | { |
| 2001 | /* append from domain to this address */ |
| 2002 | register char **pxp = pvp; |
| 2003 | |
| 2004 | /* see if there is an "@domain" in the current name */ |
| 2005 | while (*pxp != NULL && strcmp(*pxp, "@") != 0) |
| 2006 | pxp++; |
| 2007 | if (*pxp == NULL) |
| 2008 | { |
| 2009 | /* no.... append the "@domain" from the sender */ |
| 2010 | register char **qxq = e->e_fromdomain; |
| 2011 | |
| 2012 | while ((*pxp++ = *qxq++) != NULL) |
| 2013 | continue; |
| 2014 | if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) |
| 2015 | *pstat = EX_TEMPFAIL; |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | ** Do more specific rewriting. |
| 2021 | ** Rewrite using ruleset 1 or 2 for envelope addresses and |
| 2022 | ** 5 or 6 for header addresses depending on whether this |
| 2023 | ** is a sender address or not. |
| 2024 | ** Then run it through any receiving-mailer-specific rulesets. |
| 2025 | */ |
| 2026 | |
| 2027 | else |
| 2028 | { |
| 2029 | if (rwset > 0) |
| 2030 | { |
| 2031 | if (rewrite(pvp, rwset, 0, e) == EX_TEMPFAIL) |
| 2032 | *pstat = EX_TEMPFAIL; |
| 2033 | } |
| 2034 | |
| 2035 | /* |
| 2036 | ** Do any final sanitation the address may require. |
| 2037 | ** This will normally be used to turn internal forms |
| 2038 | ** (e.g., user@host.LOCAL) into external form. This |
| 2039 | ** may be used as a default to the above rules. |
| 2040 | */ |
| 2041 | |
| 2042 | if (rewrite(pvp, 4, 0, e) == EX_TEMPFAIL) |
| 2043 | *pstat = EX_TEMPFAIL; |
| 2044 | |
| 2045 | /* |
| 2046 | ** Now restore the comment information we had at the beginning. |
| 2047 | */ |
| 2048 | |
| 2049 | cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); |
| 2050 | define('g', lbuf, e); |
| 2051 | |
| 2052 | /* need to make sure route-addrs have <angle brackets> */ |
| 2053 | if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@') |
| 2054 | expand("<\201g>", buf, &buf[sizeof buf - 1], e); |
| 2055 | else |
| 2056 | expand(fancy, buf, &buf[sizeof buf - 1], e); |
| 2057 | |
| 2058 | define('g', oldg, e); |
| 2059 | |
| 2060 | if (tTd(12, 1)) |
| 2061 | printf("remotename => `%s'\n", buf); |
| 2062 | return (buf); |
| 2063 | } |
| 2064 | \f/* |
| 2065 | ** UURELATIVIZE -- Make an address !-relative to recipient/sender nodes |
| 2066 | ** |
| 2067 | ** Parameters: |
| 2068 | ** from -- the sending node (usually "$k" or "$w") |
| 2069 | ** to -- the receiving node (usually "$h") |
| 2070 | ** pvp -- address vector |
| 2071 | ** |
| 2072 | ** Returns: |
| 2073 | ** none. |
| 2074 | ** |
| 2075 | ** Side Effects: |
| 2076 | ** The pvp is rewritten to be relative the "to" node |
| 2077 | ** wrt the "from" node. In other words, if the pvp |
| 2078 | ** is headed by "to!" that part is stripped; otherwise |
| 2079 | ** "from!" is prepended. Exception: "to!user" addresses |
| 2080 | ** with no '!'s in the user part are sent as is. |
| 2081 | ** |
| 2082 | ** Bugs: |
| 2083 | ** The pvp may overflow, but we don't catch it. |
| 2084 | */ |
| 2085 | |
| 2086 | static void |
| 2087 | uurelativize(from, to, pvp) |
| 2088 | const char *from, *to; |
| 2089 | char **pvp; |
| 2090 | { |
| 2091 | register char **pxp = pvp; |
| 2092 | char expfrom[MAXNAME], expto[MAXNAME]; |
| 2093 | |
| 2094 | expand(from, expfrom, &expfrom[sizeof expfrom - 1], CurEnv); |
| 2095 | expand(to, expto, &expto[sizeof expto - 1], CurEnv); |
| 2096 | |
| 2097 | /* |
| 2098 | * supposing that we've got something, should |
| 2099 | * we add "from!" or remove "to!"? |
| 2100 | */ |
| 2101 | if (pvp[0] != NULL) |
| 2102 | if (pvp[1] == NULL || strcmp(pvp[1], "!") != 0 || |
| 2103 | /*strcasecmp?*/ strcmp(pvp[0], expto) != 0) |
| 2104 | { |
| 2105 | /* either local name, no UUCP address, */ |
| 2106 | /* or not to "to!" ==> prepend address with "from!" */ |
| 2107 | |
| 2108 | /* already there? */ |
| 2109 | if (pvp[1] == NULL || strcmp(pvp[1], "!") != 0 || |
| 2110 | /*strcasecmp?*/ strcmp(pvp[0], expfrom) != 0) |
| 2111 | { |
| 2112 | |
| 2113 | /* no, put it there */ |
| 2114 | while (*pxp != NULL) |
| 2115 | pxp++; |
| 2116 | do |
| 2117 | pxp[2] = *pxp; |
| 2118 | while (pxp-- != pvp); |
| 2119 | pvp[0] = newstr(expfrom); |
| 2120 | pvp[1] = "!"; |
| 2121 | } |
| 2122 | } |
| 2123 | else |
| 2124 | { |
| 2125 | /* address is to "to!" -- remove if not "to!user" */ |
| 2126 | for (pxp = &pvp[2]; |
| 2127 | *pxp != NULL && strcmp(*pxp, "!") != 0; pxp++) |
| 2128 | ; |
| 2129 | if (*pxp != NULL) |
| 2130 | for (pxp = pvp; *pxp != NULL; pxp++) |
| 2131 | *pxp = pxp[2]; |
| 2132 | } |
| 2133 | } |
| 2134 | \f/* |
| 2135 | ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection |
| 2136 | ** |
| 2137 | ** Parameters: |
| 2138 | ** a -- the address to map (but just the user name part). |
| 2139 | ** sendq -- the sendq in which to install any replacement |
| 2140 | ** addresses. |
| 2141 | ** |
| 2142 | ** Returns: |
| 2143 | ** none. |
| 2144 | */ |
| 2145 | |
| 2146 | maplocaluser(a, sendq, e) |
| 2147 | register ADDRESS *a; |
| 2148 | ADDRESS **sendq; |
| 2149 | ENVELOPE *e; |
| 2150 | { |
| 2151 | register char **pvp; |
| 2152 | register ADDRESS *a1 = NULL; |
| 2153 | auto char *delimptr; |
| 2154 | char pvpbuf[PSBUFSIZE]; |
| 2155 | |
| 2156 | if (tTd(29, 1)) |
| 2157 | { |
| 2158 | printf("maplocaluser: "); |
| 2159 | printaddr(a, FALSE); |
| 2160 | } |
| 2161 | pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr); |
| 2162 | if (pvp == NULL) |
| 2163 | return; |
| 2164 | |
| 2165 | (void) rewrite(pvp, 5, 0, e); |
| 2166 | if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) |
| 2167 | return; |
| 2168 | |
| 2169 | /* if non-null, mailer destination specified -- has it changed? */ |
| 2170 | a1 = buildaddr(pvp, NULL, 0, e); |
| 2171 | if (a1 == NULL || sameaddr(a, a1)) |
| 2172 | return; |
| 2173 | |
| 2174 | /* mark old address as dead; insert new address */ |
| 2175 | a->q_flags |= QDONTSEND; |
| 2176 | if (tTd(29, 5)) |
| 2177 | { |
| 2178 | printf("maplocaluser: QDONTSEND "); |
| 2179 | printaddr(a, FALSE); |
| 2180 | } |
| 2181 | a1->q_alias = a; |
| 2182 | allocaddr(a1, RF_COPYALL, NULL); |
| 2183 | (void) recipient(a1, sendq, e); |
| 2184 | } |
| 2185 | \f/* |
| 2186 | ** DEQUOTE_INIT -- initialize dequote map |
| 2187 | ** |
| 2188 | ** This is a no-op. |
| 2189 | ** |
| 2190 | ** Parameters: |
| 2191 | ** map -- the internal map structure. |
| 2192 | ** args -- arguments. |
| 2193 | ** |
| 2194 | ** Returns: |
| 2195 | ** TRUE. |
| 2196 | */ |
| 2197 | |
| 2198 | bool |
| 2199 | dequote_init(map, args) |
| 2200 | MAP *map; |
| 2201 | char *args; |
| 2202 | { |
| 2203 | register char *p = args; |
| 2204 | |
| 2205 | for (;;) |
| 2206 | { |
| 2207 | while (isascii(*p) && isspace(*p)) |
| 2208 | p++; |
| 2209 | if (*p != '-') |
| 2210 | break; |
| 2211 | switch (*++p) |
| 2212 | { |
| 2213 | case 'a': |
| 2214 | map->map_app = ++p; |
| 2215 | break; |
| 2216 | } |
| 2217 | while (*p != '\0' && !(isascii(*p) && isspace(*p))) |
| 2218 | p++; |
| 2219 | if (*p != '\0') |
| 2220 | *p = '\0'; |
| 2221 | } |
| 2222 | if (map->map_app != NULL) |
| 2223 | map->map_app = newstr(map->map_app); |
| 2224 | |
| 2225 | return TRUE; |
| 2226 | } |
| 2227 | \f/* |
| 2228 | ** DEQUOTE_MAP -- unquote an address |
| 2229 | ** |
| 2230 | ** Parameters: |
| 2231 | ** map -- the internal map structure (ignored). |
| 2232 | ** name -- the name to dequote. |
| 2233 | ** av -- arguments (ignored). |
| 2234 | ** statp -- pointer to status out-parameter. |
| 2235 | ** |
| 2236 | ** Returns: |
| 2237 | ** NULL -- if there were no quotes, or if the resulting |
| 2238 | ** unquoted buffer would not be acceptable to prescan. |
| 2239 | ** else -- The dequoted buffer. |
| 2240 | */ |
| 2241 | |
| 2242 | char * |
| 2243 | dequote_map(map, name, av, statp) |
| 2244 | MAP *map; |
| 2245 | char *name; |
| 2246 | char **av; |
| 2247 | int *statp; |
| 2248 | { |
| 2249 | register char *p; |
| 2250 | register char *q; |
| 2251 | register char c; |
| 2252 | int anglecnt; |
| 2253 | int cmntcnt; |
| 2254 | int quotecnt; |
| 2255 | int spacecnt; |
| 2256 | bool quotemode; |
| 2257 | bool bslashmode; |
| 2258 | |
| 2259 | anglecnt = 0; |
| 2260 | cmntcnt = 0; |
| 2261 | quotecnt = 0; |
| 2262 | spacecnt = 0; |
| 2263 | quotemode = FALSE; |
| 2264 | bslashmode = FALSE; |
| 2265 | |
| 2266 | for (p = q = name; (c = *p++) != '\0'; ) |
| 2267 | { |
| 2268 | if (bslashmode) |
| 2269 | { |
| 2270 | bslashmode = FALSE; |
| 2271 | *q++ = c; |
| 2272 | continue; |
| 2273 | } |
| 2274 | |
| 2275 | switch (c) |
| 2276 | { |
| 2277 | case '\\': |
| 2278 | bslashmode = TRUE; |
| 2279 | break; |
| 2280 | |
| 2281 | case '(': |
| 2282 | cmntcnt++; |
| 2283 | break; |
| 2284 | |
| 2285 | case ')': |
| 2286 | if (cmntcnt-- <= 0) |
| 2287 | return NULL; |
| 2288 | break; |
| 2289 | |
| 2290 | case ' ': |
| 2291 | spacecnt++; |
| 2292 | break; |
| 2293 | } |
| 2294 | |
| 2295 | if (cmntcnt > 0) |
| 2296 | { |
| 2297 | *q++ = c; |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | switch (c) |
| 2302 | { |
| 2303 | case '"': |
| 2304 | quotemode = !quotemode; |
| 2305 | quotecnt++; |
| 2306 | continue; |
| 2307 | |
| 2308 | case '<': |
| 2309 | anglecnt++; |
| 2310 | break; |
| 2311 | |
| 2312 | case '>': |
| 2313 | if (anglecnt-- <= 0) |
| 2314 | return NULL; |
| 2315 | break; |
| 2316 | } |
| 2317 | *q++ = c; |
| 2318 | } |
| 2319 | |
| 2320 | if (anglecnt != 0 || cmntcnt != 0 || bslashmode || |
| 2321 | quotemode || quotecnt <= 0 || spacecnt != 0) |
| 2322 | return NULL; |
| 2323 | *q++ = '\0'; |
| 2324 | return name; |
| 2325 | } |