2 * Copyright (c) 1998-2001, 2003, 2006 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: macro.c,v 8.102 2006/12/21 23:06:10 ca Exp $")
18 #include <sm/sendmail.h>
19 #if MAXMACROID != (BITMAPBITS - 1)
20 ERROR Read the comment in conf.h
21 #endif /* MAXMACROID != (BITMAPBITS - 1) */
23 static char *MacroName[MAXMACROID + 1]; /* macro id to name table */
24 int NextMacroId = 0240; /* codes for long named macros */
25 /* see sendmail.h: Special characters in rewriting rules. */
29 ** INITMACROS -- initialize the macro system
31 ** This just involves defining some macros that are actually
32 ** used internally as metasymbols to be themselves.
41 ** initializes several macros to be themselves.
44 struct metamac MetaMacros[] =
46 /* LHS pattern matching characters */
47 { '*', MATCHZANY }, { '+', MATCHANY }, { '-', MATCHONE },
48 { '=', MATCHCLASS }, { '~', MATCHNCLASS },
50 /* these are RHS metasymbols */
51 { '#', CANONNET }, { '@', CANONHOST }, { ':', CANONUSER },
54 /* the conditional operations */
55 { '?', CONDIF }, { '|', CONDELSE }, { '.', CONDFI },
57 /* the hostname lookup characters */
58 { '[', HOSTBEGIN }, { ']', HOSTEND },
59 { '(', LOOKUPBEGIN }, { ')', LOOKUPEND },
61 /* miscellaneous control characters */
62 { '&', MACRODEXPAND },
67 #define MACBINDING(name, mid) \
68 stab(name, ST_MACRO, ST_ENTER)->s_macro = mid; \
69 MacroName[mid] = name;
79 for (m = MetaMacros; m->metaname != '\0'; m++)
83 macdefine(&e->e_macro, A_TEMP, m->metaname, buf);
87 for (c = '0'; c <= '9'; c++)
90 macdefine(&e->e_macro, A_TEMP, c, buf);
93 /* set defaults for some macros sendmail will use later */
94 macdefine(&e->e_macro, A_PERM, 'n', "MAILER-DAEMON");
96 /* set up external names for some internal macros */
97 MACBINDING("opMode", MID_OPMODE);
98 /*XXX should probably add equivalents for all short macros here XXX*/
102 ** EXPAND/DOEXPAND -- macro expand a string using $x escapes.
104 ** After expansion, the expansion will be in external form (that is,
105 ** there will be no sendmail metacharacters and METAQUOTEs will have
106 ** been stripped out).
109 ** s -- the string to expand.
110 ** buf -- the place to put the expansion.
111 ** bufsize -- the size of the buffer.
112 ** explevel -- the depth of expansion (doexpand only)
113 ** e -- envelope in which to work.
122 static void doexpand __P(( char *, char *, size_t, int, ENVELOPE *));
125 doexpand(s, buf, bufsize, explevel, e)
134 bool skipping; /* set if conditionally skipping output */
135 bool recurse; /* set if recursion required */
137 int skiplev; /* skipping nesting level */
138 int iflev; /* if nesting level */
139 bool quotenext; /* quote the following character */
140 char xbuf[MACBUFSIZE];
144 sm_dprintf("expand(");
145 xputs(sm_debug_file(), s);
156 for (xp = xbuf; *s != '\0'; s++)
161 ** Check for non-ordinary (special?) character.
162 ** 'q' will be the interpolated quantity.
171 goto simpleinterpolate;
176 case CONDIF: /* see if var set */
186 skipping = (mv == NULL || *mv == '\0');
190 case CONDELSE: /* change state of skipping */
192 break; /* XXX: error */
194 skipping = !skipping;
197 case CONDFI: /* stop skipping */
199 break; /* XXX: error */
207 case MACROEXPAND: /* macro interpolation */
221 /* next octet completely quoted */
227 ** Interpolate q or output one character
231 if (skipping || xp >= &xbuf[sizeof(xbuf) - 1])
237 /* copy to end of q or max space remaining in buf */
238 bool hiderecurse = false;
240 while ((c = *q++) != '\0' &&
241 xp < &xbuf[sizeof(xbuf) - 1])
243 /* check for any sendmail metacharacters */
244 if (!hiderecurse && (c & 0340) == 0200)
248 /* give quoted characters a free ride */
249 hiderecurse = (c & 0377) == METAQUOTE;
257 sm_dprintf("expand(%d) ==> ", explevel);
258 xputs(sm_debug_file(), xbuf);
262 /* recurse as appropriate */
265 if (explevel < MaxMacroRecursion)
267 doexpand(xbuf, buf, bufsize, explevel + 1, e);
270 syserr("expand: recursion too deep (%d max)",
274 /* copy results out */
276 (void) sm_strlcpy(buf, xbuf, bufsize);
279 /* leave in internal form */
283 memmove(buf, xbuf, i);
289 sm_dprintf("expand ==> ");
290 xputs(sm_debug_file(), buf);
296 expand(s, buf, bufsize, e)
302 doexpand(s, buf, bufsize, 0, e);
306 ** MACDEFINE -- bind a macro name to a value
308 ** Set a macro to a value, with fancy storage management.
309 ** macdefine will make a copy of the value, if required,
310 ** and will ensure that the storage for the previous value
314 ** mac -- Macro table.
315 ** vclass -- storage class of 'value', ignored if value==NULL.
316 ** A_HEAP means that the value was allocated by
317 ** malloc, and that macdefine owns the storage.
318 ** A_TEMP means that value points to temporary storage,
319 ** and thus macdefine needs to make a copy.
320 ** A_PERM means that value points to storage that
321 ** will remain allocated and unchanged for
322 ** at least the lifetime of mac. Use A_PERM if:
324 ** -- value points to a string literal,
325 ** -- value was allocated from mac->mac_rpool
326 ** or (in the case of an envelope macro)
328 ** -- in the case of an envelope macro,
329 ** value is a string member of the envelope
330 ** such as e->e_sender.
331 ** id -- Macro id. This is a single character macro name
332 ** such as 'g', or a value returned by macid().
333 ** value -- Macro value: either NULL, or a string.
338 macdefine_tagged(mac, vclass, id, value, file, line, grp)
339 #else /* SM_HEAP_CHECK */
340 macdefine(mac, vclass, id, value)
341 #endif /* SM_HEAP_CHECK */
350 #endif /* SM_HEAP_CHECK */
354 if (id < 0 || id > MAXMACROID)
359 sm_dprintf("%sdefine(%s as ",
360 mac->mac_table[id] == NULL ? "" : "re", macname(id));
361 xputs(sm_debug_file(), value);
365 if (mac->mac_rpool == NULL)
369 if (mac->mac_table[id] != NULL &&
370 bitnset(id, mac->mac_allocated))
371 freeit = mac->mac_table[id];
373 if (value == NULL || vclass == A_HEAP)
375 sm_heap_checkptr_tagged(value, file, line);
377 clrbitn(id, mac->mac_allocated);
382 newvalue = sm_strdup_tagged_x(value, file, line, 0);
383 #else /* SM_HEAP_CHECK */
384 newvalue = sm_strdup_x(value);
385 #endif /* SM_HEAP_CHECK */
386 setbitn(id, mac->mac_allocated);
388 mac->mac_table[id] = newvalue;
394 if (value == NULL || vclass == A_PERM)
397 newvalue = sm_rpool_strdup_x(mac->mac_rpool, value);
398 mac->mac_table[id] = newvalue;
399 if (vclass == A_HEAP)
403 #if _FFR_RESET_MACRO_GLOBALS
407 PSTRSET(MyHostName, value);
410 #endif /* _FFR_RESET_MACRO_GLOBALS */
414 ** MACSET -- set a named macro to a value (low level)
416 ** No fancy storage management; the caller takes full responsibility.
417 ** Often used with macget; see also macdefine.
420 ** mac -- Macro table.
421 ** i -- Macro name, specified as an integer offset.
422 ** value -- Macro value: either NULL, or a string.
426 macset(mac, i, value)
431 if (i < 0 || i > MAXMACROID)
436 sm_dprintf("macset(%s as ", macname(i));
437 xputs(sm_debug_file(), value);
440 mac->mac_table[i] = value;
444 ** MACVALUE -- return uninterpreted value of a macro.
446 ** Does fancy path searching.
447 ** The low level counterpart is macget.
450 ** n -- the name of the macro.
451 ** e -- envelope in which to start looking for the macro.
466 if (e != NULL && e->e_mci != NULL)
468 char *p = e->e_mci->mci_macro.mac_table[n];
475 char *p = e->e_macro.mac_table[n];
479 if (e == e->e_parent)
483 return GlobalMacros.mac_table[n];
487 ** MACNAME -- return the name of a macro given its internal id
490 ** n -- the id of the macro
508 n = (int)(unsigned char)n;
510 return "***OUT OF RANGE MACRO***";
512 /* if not ASCII printable, look up the name */
513 if (n <= 0x20 || n > 0x7f)
515 char *p = MacroName[n];
519 return "***UNDEFINED MACRO***";
522 /* if in the ASCII graphic range, just return the id directly */
529 ** MACID_PARSE -- return id of macro identified by its name
532 ** p -- pointer to name string -- either a single
533 ** character or {name}.
534 ** ep -- filled in with the pointer to the byte
538 ** 0 -- An error was detected.
539 ** 1..MAXMACROID -- The internal id code for this macro.
542 ** If this is a new macro name, a new id is allocated.
543 ** On error, syserr is called.
553 char mbuf[MAXMACNAMELEN + 1];
557 sm_dprintf("macid(");
558 xputs(sm_debug_file(), p);
562 if (*p == '\0' || (p[0] == '{' && p[1] == '}'))
564 syserr("Name required for macro/class");
568 sm_dprintf("NULL\n");
573 /* the macro is its own code */
582 xputs(sm_debug_file(), buf);
588 while (*++p != '\0' && *p != '}' && bp < &mbuf[sizeof(mbuf) - 1])
590 if (isascii(*p) && (isalnum(*p) || *p == '_'))
593 syserr("Invalid macro/class character %c", *p);
599 syserr("Unbalanced { on %s", mbuf); /* missing } */
603 syserr("Macro/class name ({%s}) too long (%d chars max)",
604 mbuf, (int) (sizeof(mbuf) - 1));
606 else if (mbuf[1] == '\0')
609 mid = bitidx(mbuf[0]);
616 s = stab(mbuf, ST_MACRO, ST_ENTER);
621 if (NextMacroId > MAXMACROID)
623 syserr("Macro/class {%s}: too many long names",
629 MacroName[NextMacroId] = s->s_name;
630 s->s_macro = mid = NextMacroId++;
637 if (mid < 0 || mid > MAXMACROID)
639 syserr("Unable to assign macro/class ID (mid = 0x%x)", mid);
641 sm_dprintf("NULL\n");
645 sm_dprintf("0x%x\n", mid);
650 ** WORDINCLASS -- tell if a word is in a specific class
653 ** str -- the name of the word to look up.
654 ** cl -- the class name.
657 ** true if str can be found in cl.
668 s = stab(str, ST_CLASS, ST_FIND);
669 return s != NULL && bitnset(bitidx(cl), s->s_class);