1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 * 8 * @(#)sendmail.h 5.17 (Berkeley) 03/12/91 9 */ 10 11 /* 12 ** SENDMAIL.H -- Global definitions for sendmail. 13 */ 14 15 # ifdef _DEFINE 16 # define EXTERN 17 # ifndef lint 18 static char SmailSccsId[] = "@(#)sendmail.h 5.17 03/12/91"; 19 # endif lint 20 # else _DEFINE 21 # define EXTERN extern 22 # endif _DEFINE 23 24 # include <stdio.h> 25 # include <ctype.h> 26 # include <setjmp.h> 27 # include "conf.h" 28 # include "useful.h" 29 30 # ifdef LOG 31 # include <sys/syslog.h> 32 # endif LOG 33 34 # ifdef DAEMON 35 # ifdef VMUNIX 36 # include <sys/socket.h> 37 # include <netinet/in.h> 38 # endif VMUNIX 39 # endif DAEMON 40 41 42 # define PSBUFSIZE (MAXNAME + MAXATOM) /* size of prescan buffer */ 43 44 45 /* 46 ** Data structure for bit maps. 47 ** 48 ** Each bit in this map can be referenced by an ascii character. 49 ** This is 128 possible bits, or 12 8-bit bytes. 50 */ 51 52 #define BITMAPBYTES 16 /* number of bytes in a bit map */ 53 #define BYTEBITS 8 /* number of bits in a byte */ 54 55 /* internal macros */ 56 #define _BITWORD(bit) (bit / (BYTEBITS * sizeof (int))) 57 #define _BITBIT(bit) (1 << (bit % (BYTEBITS * sizeof (int)))) 58 59 typedef int BITMAP[BITMAPBYTES / sizeof (int)]; 60 61 /* test bit number N */ 62 #define bitnset(bit, map) ((map)[_BITWORD(bit)] & _BITBIT(bit)) 63 64 /* set bit number N */ 65 #define setbitn(bit, map) (map)[_BITWORD(bit)] |= _BITBIT(bit) 66 67 /* clear bit number N */ 68 #define clrbitn(bit, map) (map)[_BITWORD(bit)] &= ~_BITBIT(bit) 69 70 /* clear an entire bit map */ 71 #define clrbitmap(map) bzero((char *) map, BITMAPBYTES) 72 /* 73 ** Address structure. 74 ** Addresses are stored internally in this structure. 75 */ 76 77 struct address 78 { 79 char *q_paddr; /* the printname for the address */ 80 char *q_user; /* user name */ 81 char *q_ruser; /* real user name, or NULL if q_user */ 82 char *q_host; /* host name */ 83 struct mailer *q_mailer; /* mailer to use */ 84 u_short q_flags; /* status flags, see below */ 85 short q_uid; /* user-id of receiver (if known) */ 86 short q_gid; /* group-id of receiver (if known) */ 87 char *q_home; /* home dir (local mailer only) */ 88 char *q_fullname; /* full name if known */ 89 struct address *q_next; /* chain */ 90 struct address *q_alias; /* address this results from */ 91 struct address *q_tchain; /* temporary use chain */ 92 time_t q_timeout; /* timeout for this address */ 93 }; 94 95 typedef struct address ADDRESS; 96 97 # define QDONTSEND 000001 /* don't send to this address */ 98 # define QBADADDR 000002 /* this address is verified bad */ 99 # define QGOODUID 000004 /* the q_uid q_gid fields are good */ 100 # define QPRIMARY 000010 /* set from argv */ 101 # define QQUEUEUP 000020 /* queue for later transmission */ 102 # define QSENT 000040 /* has been successfully delivered */ 103 /* 104 ** Mailer definition structure. 105 ** Every mailer known to the system is declared in this 106 ** structure. It defines the pathname of the mailer, some 107 ** flags associated with it, and the argument vector to 108 ** pass to it. The flags are defined in conf.c 109 ** 110 ** The argument vector is expanded before actual use. All 111 ** words except the first are passed through the macro 112 ** processor. 113 */ 114 115 struct mailer 116 { 117 char *m_name; /* symbolic name of this mailer */ 118 char *m_mailer; /* pathname of the mailer to use */ 119 BITMAP m_flags; /* status flags, see below */ 120 short m_mno; /* mailer number internally */ 121 char **m_argv; /* template argument vector */ 122 short m_s_rwset; /* rewriting set for sender addresses */ 123 short m_r_rwset; /* rewriting set for recipient addresses */ 124 char *m_eol; /* end of line string */ 125 long m_maxsize; /* size limit on message to this mailer */ 126 }; 127 128 typedef struct mailer MAILER; 129 130 /* bits for m_flags */ 131 # define M_CANONICAL 'C' /* make addresses canonical "u@dom" */ 132 # define M_EXPENSIVE 'e' /* it costs to use this mailer.... */ 133 # define M_ESCFROM 'E' /* escape From lines to >From */ 134 # define M_FOPT 'f' /* mailer takes picky -f flag */ 135 # define M_HST_UPPER 'h' /* preserve host case distinction */ 136 # define M_INTERNAL 'I' /* SMTP to another sendmail site */ 137 # define M_LOCAL 'l' /* delivery is to this host */ 138 # define M_LIMITS 'L' /* must enforce SMTP line limits */ 139 # define M_MUSER 'm' /* can handle multiple users at once */ 140 # define M_NHDR 'n' /* don't insert From line */ 141 # define M_FROMPATH 'p' /* use reverse-path in MAIL FROM: */ 142 # define M_ROPT 'r' /* mailer takes picky -r flag */ 143 # define M_SECURE_PORT 'R' /* try to send on a reserved TCP port */ 144 # define M_STRIPQ 's' /* strip quote chars from user/host */ 145 # define M_RESTR 'S' /* must be daemon to execute */ 146 # define M_USR_UPPER 'u' /* preserve user case distinction */ 147 # define M_UGLYUUCP 'U' /* this wants an ugly UUCP from line */ 148 # define M_XDOT 'X' /* use hidden-dot algorithm */ 149 150 EXTERN MAILER *Mailer[MAXMAILERS+1]; 151 152 EXTERN MAILER *LocalMailer; /* ptr to local mailer */ 153 EXTERN MAILER *ProgMailer; /* ptr to program mailer */ 154 /* 155 ** Header structure. 156 ** This structure is used internally to store header items. 157 */ 158 159 struct header 160 { 161 char *h_field; /* the name of the field */ 162 char *h_value; /* the value of that field */ 163 struct header *h_link; /* the next header */ 164 u_short h_flags; /* status bits, see below */ 165 BITMAP h_mflags; /* m_flags bits needed */ 166 }; 167 168 typedef struct header HDR; 169 170 /* 171 ** Header information structure. 172 ** Defined in conf.c, this struct declares the header fields 173 ** that have some magic meaning. 174 */ 175 176 struct hdrinfo 177 { 178 char *hi_field; /* the name of the field */ 179 u_short hi_flags; /* status bits, see below */ 180 }; 181 182 extern struct hdrinfo HdrInfo[]; 183 184 /* bits for h_flags and hi_flags */ 185 # define H_EOH 00001 /* this field terminates header */ 186 # define H_RCPT 00002 /* contains recipient addresses */ 187 # define H_DEFAULT 00004 /* if another value is found, drop this */ 188 # define H_RESENT 00010 /* this address is a "Resent-..." address */ 189 # define H_CHECK 00020 /* check h_mflags against m_flags */ 190 # define H_ACHECK 00040 /* ditto, but always (not just default) */ 191 # define H_FORCE 00100 /* force this field, even if default */ 192 # define H_TRACE 00200 /* this field contains trace information */ 193 # define H_FROM 00400 /* this is a from-type field */ 194 # define H_VALID 01000 /* this field has a validated value */ 195 /* 196 ** Envelope structure. 197 ** This structure defines the message itself. There is usually 198 ** only one of these -- for the message that we originally read 199 ** and which is our primary interest -- but other envelopes can 200 ** be generated during processing. For example, error messages 201 ** will have their own envelope. 202 */ 203 204 struct envelope 205 { 206 HDR *e_header; /* head of header list */ 207 long e_msgpriority; /* adjusted priority of this message */ 208 time_t e_ctime; /* time message appeared in the queue */ 209 char *e_to; /* the target person */ 210 char *e_receiptto; /* return receipt address */ 211 ADDRESS e_from; /* the person it is from */ 212 char **e_fromdomain; /* the domain part of the sender */ 213 ADDRESS *e_sendqueue; /* list of message recipients */ 214 ADDRESS *e_errorqueue; /* the queue for error responses */ 215 long e_msgsize; /* size of the message in bytes */ 216 int e_nrcpts; /* number of recipients */ 217 short e_class; /* msg class (priority, junk, etc.) */ 218 short e_flags; /* flags, see below */ 219 short e_hopcount; /* number of times processed */ 220 int (*e_puthdr)(); /* function to put header of message */ 221 int (*e_putbody)(); /* function to put body of message */ 222 struct envelope *e_parent; /* the message this one encloses */ 223 struct envelope *e_sibling; /* the next envelope of interest */ 224 char *e_df; /* location of temp file */ 225 FILE *e_dfp; /* temporary file */ 226 char *e_id; /* code for this entry in queue */ 227 FILE *e_xfp; /* transcript file */ 228 char *e_message; /* error message */ 229 char *e_macro[128]; /* macro definitions */ 230 }; 231 232 typedef struct envelope ENVELOPE; 233 234 /* values for e_flags */ 235 #define EF_OLDSTYLE 000001 /* use spaces (not commas) in hdrs */ 236 #define EF_INQUEUE 000002 /* this message is fully queued */ 237 #define EF_TIMEOUT 000004 /* this message is too old */ 238 #define EF_CLRQUEUE 000010 /* disk copy is no longer needed */ 239 #define EF_SENDRECEIPT 000020 /* send a return receipt */ 240 #define EF_FATALERRS 000040 /* fatal errors occured */ 241 #define EF_KEEPQUEUE 000100 /* keep queue files always */ 242 #define EF_RESPONSE 000200 /* this is an error or return receipt */ 243 #define EF_RESENT 000400 /* this message is being forwarded */ 244 245 EXTERN ENVELOPE *CurEnv; /* envelope currently being processed */ 246 /* 247 ** Message priority classes. 248 ** 249 ** The message class is read directly from the Priority: header 250 ** field in the message. 251 ** 252 ** CurEnv->e_msgpriority is the number of bytes in the message plus 253 ** the creation time (so that jobs ``tend'' to be ordered correctly), 254 ** adjusted by the message class, the number of recipients, and the 255 ** amount of time the message has been sitting around. This number 256 ** is used to order the queue. Higher values mean LOWER priority. 257 ** 258 ** Each priority class point is worth WkClassFact priority points; 259 ** each recipient is worth WkRecipFact priority points. Each time 260 ** we reprocess a message the priority is adjusted by WkTimeFact. 261 ** WkTimeFact should normally decrease the priority so that jobs 262 ** that have historically failed will be run later; thanks go to 263 ** Jay Lepreau at Utah for pointing out the error in my thinking. 264 ** 265 ** The "class" is this number, unadjusted by the age or size of 266 ** this message. Classes with negative representations will have 267 ** error messages thrown away if they are not local. 268 */ 269 270 struct priority 271 { 272 char *pri_name; /* external name of priority */ 273 int pri_val; /* internal value for same */ 274 }; 275 276 EXTERN struct priority Priorities[MAXPRIORITIES]; 277 EXTERN int NumPriorities; /* pointer into Priorities */ 278 /* 279 ** Rewrite rules. 280 */ 281 282 struct rewrite 283 { 284 char **r_lhs; /* pattern match */ 285 char **r_rhs; /* substitution value */ 286 struct rewrite *r_next;/* next in chain */ 287 }; 288 289 EXTERN struct rewrite *RewriteRules[MAXRWSETS]; 290 291 /* 292 ** Special characters in rewriting rules. 293 ** These are used internally only. 294 ** The COND* rules are actually used in macros rather than in 295 ** rewriting rules, but are given here because they 296 ** cannot conflict. 297 */ 298 299 /* left hand side items */ 300 # define MATCHZANY '\020' /* match zero or more tokens */ 301 # define MATCHANY '\021' /* match one or more tokens */ 302 # define MATCHONE '\022' /* match exactly one token */ 303 # define MATCHCLASS '\023' /* match one token in a class */ 304 # define MATCHNCLASS '\024' /* match anything not in class */ 305 # define MATCHREPL '\025' /* replacement on RHS for above */ 306 307 /* right hand side items */ 308 # define CANONNET '\026' /* canonical net, next token */ 309 # define CANONHOST '\027' /* canonical host, next token */ 310 # define CANONUSER '\030' /* canonical user, next N tokens */ 311 # define CALLSUBR '\031' /* call another rewriting set */ 312 313 /* conditionals in macros */ 314 # define CONDIF '\032' /* conditional if-then */ 315 # define CONDELSE '\033' /* conditional else */ 316 # define CONDFI '\034' /* conditional fi */ 317 318 /* bracket characters for host name lookup */ 319 # define HOSTBEGIN '\035' /* hostname lookup begin */ 320 # define HOSTEND '\036' /* hostname lookup end */ 321 322 /* \001 is also reserved as the macro expansion character */ 323 /* 324 ** Information about hosts that we have looked up recently. 325 ** 326 ** This stuff is 4.2/3bsd specific. 327 */ 328 329 # ifdef DAEMON 330 # ifdef VMUNIX 331 332 # define HOSTINFO struct hostinfo 333 334 HOSTINFO 335 { 336 char *ho_name; /* name of this host */ 337 struct in_addr ho_inaddr; /* internet address */ 338 short ho_flags; /* flag bits, see below */ 339 short ho_errno; /* error number on last connection */ 340 short ho_exitstat; /* exit status from last connection */ 341 }; 342 343 344 /* flag bits */ 345 #define HOF_VALID 00001 /* this entry is valid */ 346 347 # endif VMUNIX 348 # endif DAEMON 349 /* 350 ** Symbol table definitions 351 */ 352 353 struct symtab 354 { 355 char *s_name; /* name to be entered */ 356 char s_type; /* general type (see below) */ 357 struct symtab *s_next; /* pointer to next in chain */ 358 union 359 { 360 BITMAP sv_class; /* bit-map of word classes */ 361 ADDRESS *sv_addr; /* pointer to address header */ 362 MAILER *sv_mailer; /* pointer to mailer */ 363 char *sv_alias; /* alias */ 364 # ifdef HOSTINFO 365 HOSTINFO sv_host; /* host information */ 366 # endif HOSTINFO 367 } s_value; 368 }; 369 370 typedef struct symtab STAB; 371 372 /* symbol types */ 373 # define ST_UNDEF 0 /* undefined type */ 374 # define ST_CLASS 1 /* class map */ 375 # define ST_ADDRESS 2 /* an address in parsed format */ 376 # define ST_MAILER 3 /* a mailer header */ 377 # define ST_ALIAS 4 /* an alias */ 378 # define ST_HOST 5 /* host information */ 379 380 # define s_class s_value.sv_class 381 # define s_address s_value.sv_addr 382 # define s_mailer s_value.sv_mailer 383 # define s_alias s_value.sv_alias 384 # define s_host s_value.sv_host 385 386 extern STAB *stab(); 387 388 /* opcodes to stab */ 389 # define ST_FIND 0 /* find entry */ 390 # define ST_ENTER 1 /* enter if not there */ 391 /* 392 ** STRUCT EVENT -- event queue. 393 ** 394 ** Maintained in sorted order. 395 ** 396 ** We store the pid of the process that set this event to insure 397 ** that when we fork we will not take events intended for the parent. 398 */ 399 400 struct event 401 { 402 time_t ev_time; /* time of the function call */ 403 int (*ev_func)(); /* function to call */ 404 int ev_arg; /* argument to ev_func */ 405 int ev_pid; /* pid that set this event */ 406 struct event *ev_link; /* link to next item */ 407 }; 408 409 typedef struct event EVENT; 410 411 EXTERN EVENT *EventQueue; /* head of event queue */ 412 /* 413 ** Operation, send, and error modes 414 ** 415 ** The operation mode describes the basic operation of sendmail. 416 ** This can be set from the command line, and is "send mail" by 417 ** default. 418 ** 419 ** The send mode tells how to send mail. It can be set in the 420 ** configuration file. It's setting determines how quickly the 421 ** mail will be delivered versus the load on your system. If the 422 ** -v (verbose) flag is given, it will be forced to SM_DELIVER 423 ** mode. 424 ** 425 ** The error mode tells how to return errors. 426 */ 427 428 EXTERN char OpMode; /* operation mode, see below */ 429 430 #define MD_DELIVER 'm' /* be a mail sender */ 431 #define MD_ARPAFTP 'a' /* old-style arpanet protocols */ 432 #define MD_SMTP 's' /* run SMTP on standard input */ 433 #define MD_DAEMON 'd' /* run as a daemon */ 434 #define MD_VERIFY 'v' /* verify: don't collect or deliver */ 435 #define MD_TEST 't' /* test mode: resolve addrs only */ 436 #define MD_INITALIAS 'i' /* initialize alias database */ 437 #define MD_PRINT 'p' /* print the queue */ 438 #define MD_FREEZE 'z' /* freeze the configuration file */ 439 440 441 EXTERN char SendMode; /* send mode, see below */ 442 443 #define SM_DELIVER 'i' /* interactive delivery */ 444 #define SM_QUICKD 'j' /* deliver w/o queueing */ 445 #define SM_FORK 'b' /* deliver in background */ 446 #define SM_QUEUE 'q' /* queue, don't deliver */ 447 #define SM_VERIFY 'v' /* verify only (used internally) */ 448 449 /* used only as a parameter to sendall */ 450 #define SM_DEFAULT '\0' /* unspecified, use SendMode */ 451 452 453 EXTERN char ErrorMode; /* error mode, see below */ 454 455 #define EM_PRINT 'p' /* print errors */ 456 #define EM_MAIL 'm' /* mail back errors */ 457 #define EM_WRITE 'w' /* write back errors */ 458 #define EM_BERKNET 'e' /* special berknet processing */ 459 #define EM_QUIET 'q' /* don't print messages (stat only) */ 460 461 /* offset used to issure that the error messages for name server error 462 * codes are unique. 463 */ 464 #define MAX_ERRNO 100 465 /* 466 ** Global variables. 467 */ 468 469 EXTERN bool FromFlag; /* if set, "From" person is explicit */ 470 EXTERN bool NoAlias; /* if set, don't do any aliasing */ 471 EXTERN bool ForceMail; /* if set, mail even if already got a copy */ 472 EXTERN bool MeToo; /* send to the sender also */ 473 EXTERN bool IgnrDot; /* don't let dot end messages */ 474 EXTERN bool SaveFrom; /* save leading "From" lines */ 475 EXTERN bool Verbose; /* set if blow-by-blow desired */ 476 EXTERN bool GrabTo; /* if set, get recipients from msg */ 477 EXTERN bool NoReturn; /* don't return letter to sender */ 478 EXTERN bool SuprErrs; /* set if we are suppressing errors */ 479 EXTERN bool QueueRun; /* currently running message from the queue */ 480 EXTERN bool HoldErrs; /* only output errors to transcript */ 481 EXTERN bool NoConnect; /* don't connect to non-local mailers */ 482 EXTERN bool SuperSafe; /* be extra careful, even if expensive */ 483 EXTERN bool ForkQueueRuns; /* fork for each job when running the queue */ 484 EXTERN bool AutoRebuild; /* auto-rebuild the alias database as needed */ 485 EXTERN bool CheckAliases; /* parse addresses during newaliases */ 486 EXTERN bool UseNameServer; /* use internet domain name server */ 487 EXTERN int SafeAlias; /* minutes to wait until @:@ in alias file */ 488 EXTERN time_t TimeOut; /* time until timeout */ 489 EXTERN FILE *InChannel; /* input connection */ 490 EXTERN FILE *OutChannel; /* output connection */ 491 EXTERN int RealUid; /* when Daemon, real uid of caller */ 492 EXTERN int RealGid; /* when Daemon, real gid of caller */ 493 EXTERN int DefUid; /* default uid to run as */ 494 EXTERN char *DefUser; /* default user to run as (from DefUid) */ 495 EXTERN int DefGid; /* default gid to run as */ 496 EXTERN int OldUmask; /* umask when sendmail starts up */ 497 EXTERN int Errors; /* set if errors (local to single pass) */ 498 EXTERN int ExitStat; /* exit status code */ 499 EXTERN int AliasLevel; /* depth of aliasing */ 500 EXTERN int MotherPid; /* proc id of parent process */ 501 EXTERN int LineNumber; /* line number in current input */ 502 EXTERN time_t ReadTimeout; /* timeout on reads */ 503 EXTERN int LogLevel; /* level of logging to perform */ 504 EXTERN int FileMode; /* mode on files */ 505 EXTERN int QueueLA; /* load average starting forced queueing */ 506 EXTERN int RefuseLA; /* load average refusing connections are */ 507 EXTERN int QueueFactor; /* slope of queue function */ 508 EXTERN time_t QueueIntvl; /* intervals between running the queue */ 509 EXTERN char *AliasFile; /* location of alias file */ 510 EXTERN char *HelpFile; /* location of SMTP help file */ 511 EXTERN char *StatFile; /* location of statistics summary */ 512 EXTERN char *QueueDir; /* location of queue directory */ 513 EXTERN char *FileName; /* name to print on error messages */ 514 EXTERN char *SmtpPhase; /* current phase in SMTP processing */ 515 EXTERN char *MyHostName; /* name of this host for SMTP messages */ 516 EXTERN char *RealHostName; /* name of host we are talking to */ 517 EXTERN struct sockaddr_in RealHostAddr;/* address of host we are talking to */ 518 EXTERN char *CurHostName; /* current host we are dealing with */ 519 EXTERN jmp_buf TopFrame; /* branch-to-top-of-loop-on-error frame */ 520 EXTERN bool QuickAbort; /* .... but only if we want a quick abort */ 521 extern char *ConfFile; /* location of configuration file [conf.c] */ 522 extern char *FreezeFile; /* location of frozen memory image [conf.c] */ 523 extern char Arpa_Info[]; /* the reply code for Arpanet info [conf.c] */ 524 extern ADDRESS NullAddress; /* a null (template) address [main.c] */ 525 EXTERN char SpaceSub; /* substitution for <lwsp> */ 526 EXTERN int WkClassFact; /* multiplier for message class -> priority */ 527 EXTERN int WkRecipFact; /* multiplier for # of recipients -> priority */ 528 EXTERN int WkTimeFact; /* priority offset each time this job is run */ 529 EXTERN int CheckPointLimit; /* deliveries before checkpointing */ 530 EXTERN int Nmx; /* number of MX RRs */ 531 EXTERN char *PostMasterCopy; /* address to get errs cc's */ 532 EXTERN char *MxHosts[MAXMXHOSTS+1]; /* for MX RRs */ 533 EXTERN char *TrustedUsers[MAXTRUST+1]; /* list of trusted users */ 534 EXTERN char *UserEnviron[MAXUSERENVIRON+1]; /* saved user environment */ 535 EXTERN int CheckpointInterval; /* queue file checkpoint interval */ 536 /* 537 ** Trace information 538 */ 539 540 /* trace vector and macros for debugging flags */ 541 EXTERN u_char tTdvect[100]; 542 # define tTd(flag, level) (tTdvect[flag] >= level) 543 # define tTdlevel(flag) (tTdvect[flag]) 544 /* 545 ** Miscellaneous information. 546 */ 547 548 # include <sysexits.h> 549 550 551 /* 552 ** Some in-line functions 553 */ 554 555 /* set exit status */ 556 #define setstat(s) { \ 557 if (ExitStat == EX_OK || ExitStat == EX_TEMPFAIL) \ 558 ExitStat = s; \ 559 } 560 561 /* make a copy of a string */ 562 #define newstr(s) strcpy(xalloc(strlen(s) + 1), s) 563 564 #define STRUCTCOPY(s, d) d = s 565 566 567 /* 568 ** Declarations of useful functions 569 */ 570 571 extern ADDRESS *parseaddr(); 572 extern char *xalloc(); 573 extern bool sameaddr(); 574 extern FILE *dfopen(); 575 extern EVENT *setevent(); 576 extern char *sfgets(); 577 extern char *queuename(); 578 extern time_t curtime(); 579