2 * Copyright (c) 1998-2001, 2003 Proofpoint, 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: stab.c,v 8.92 2013-11-22 20:51:56 ca Exp $")
19 ** STAB -- manage the symbol table
22 ** name -- the name to be looked up or inserted.
23 ** type -- the type of symbol.
25 ** ST_ENTER -- enter the name if not already present.
26 ** ST_FIND -- find it only.
29 ** pointer to a STAB entry for this name.
30 ** NULL if not found and not entered.
33 ** can update the symbol table.
37 #define SM_LOWER(c) ((isascii(c) && isupper(c)) ? tolower(c) : (c))
39 static STAB *SymTab[STABSIZE];
54 sm_dprintf("STAB: %s %d ", name, type);
57 ** Compute the hashing function
61 for (p = name; *p != '\0'; p++)
62 hfunc = ((hfunc << 1) ^ (SM_LOWER(*p) & 0377)) % STABSIZE;
65 sm_dprintf("(hfunc=%d) ", hfunc);
68 if (type == ST_MACRO || type == ST_RULESET || type == ST_NAMECANON)
70 while ((s = *ps) != NULL &&
71 (s->s_symtype != type || strcmp(name, s->s_name)))
76 while ((s = *ps) != NULL &&
77 (s->s_symtype != type || sm_strcasecmp(name, s->s_name)))
82 ** Dispose of the entry.
85 if (s != NULL || op == ST_FIND)
90 sm_dprintf("not found\n");
93 long *lp = (long *) s->s_class;
95 sm_dprintf("type %d val %lx %lx %lx %lx\n",
96 s->s_symtype, lp[0], lp[1], lp[2], lp[3]);
103 ** Make a new entry and link it in.
107 sm_dprintf("entered\n");
109 /* determine size of new entry */
113 len = sizeof(s->s_class);
117 len = sizeof(s->s_mailer);
121 len = sizeof(s->s_alias);
125 len = sizeof(s->s_mapclass);
129 len = sizeof(s->s_map);
133 len = sizeof(s->s_hostsig);
137 len = sizeof(s->s_namecanon);
141 len = sizeof(s->s_macro);
145 len = sizeof(s->s_ruleset);
149 len = sizeof(s->s_header);
153 len = sizeof(s->s_service);
158 len = sizeof(s->s_lmap);
164 len = sizeof(s->s_milter);
169 len = sizeof(s->s_quegrp);
174 len = sizeof(s->s_socketmap);
176 #endif /* SOCKETMAP */
180 ** Each mailer has its own MCI stab entry:
182 ** s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
184 ** Therefore, anything ST_MCI or larger is an s_mci.
188 len = sizeof(s->s_mci);
191 syserr("stab: unknown symbol type %d", type);
192 len = sizeof(s->s_value);
196 len += sizeof(*s) - sizeof(s->s_value);
199 sm_dprintf("size of stab entry: %d\n", len);
202 s = (STAB *) sm_pmalloc_x(len);
203 memset((char *) s, '\0', len);
204 s->s_name = sm_pstrdup_x(name);
210 /* set a default value for rulesets */
211 if (type == ST_RULESET)
217 ** STABAPPLY -- apply function to all stab entries
220 ** func -- the function to apply. It will be given two
221 ** parameters (the stab entry and the arg).
222 ** arg -- an arbitrary argument, passed to func.
230 void (*func)__P((STAB *, int));
233 register STAB **shead;
236 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
238 for (s = *shead; s != NULL; s = s->s_next)
241 sm_dprintf("stabapply: trying %d/%s\n",
242 s->s_symtype, s->s_name);
248 ** QUEUEUP_MACROS -- queueup the macros in a class
250 ** Write the macros listed in the specified class into the
251 ** file referenced by qfp.
254 ** class -- class ID.
255 ** qfp -- file pointer to the queue file.
256 ** e -- the envelope.
263 queueup_macros(class, qfp, e)
268 register STAB **shead;
274 class = bitidx(class);
275 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
277 for (s = *shead; s != NULL; s = s->s_next)
282 if (s->s_symtype == ST_CLASS &&
283 bitnset(bitidx(class), s->s_class) &&
284 (m = macid(s->s_name)) != 0 &&
285 (p = macvalue(m, e)) != NULL)
287 (void) sm_io_fprintf(qfp, SM_TIME_DEFAULT,
297 ** COPY_CLASS -- copy class members from one class to another
300 ** src -- source class.
301 ** dst -- destination class.
312 register STAB **shead;
317 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
319 for (s = *shead; s != NULL; s = s->s_next)
321 if (s->s_symtype == ST_CLASS &&
322 bitnset(src, s->s_class))
323 setbitn(dst, s->s_class);
329 ** RMEXPSTAB -- remove expired entries from SymTab.
331 ** These entries need to be removed in long-running processes,
332 ** e.g., persistent queue runners, to avoid consuming memory.
334 ** XXX It might be useful to restrict the maximum TTL to avoid
335 ** caching data very long.
344 ** can remove entries from the symbol table.
347 #define SM_STAB_FREE(x) \
364 for (i = 0; i < STABSIZE; i++)
370 switch (s->s_symtype)
373 if (s->s_hostsig.hs_exp >= now)
374 goto next; /* not expired */
375 SM_STAB_FREE(s->s_hostsig.hs_sig); /* XXX */
379 if (s->s_namecanon.nc_exp >= now)
380 goto next; /* not expired */
381 SM_STAB_FREE(s->s_namecanon.nc_cname); /* XXX */
385 if (s->s_symtype >= ST_MCI)
387 /* call mci_uncache? */
388 SM_STAB_FREE(s->s_mci.mci_status);
389 SM_STAB_FREE(s->s_mci.mci_rstatus);
390 SM_STAB_FREE(s->s_mci.mci_heloname);
392 /* not dynamically allocated */
393 SM_STAB_FREE(s->s_mci.mci_host);
394 SM_STAB_FREE(s->s_mci.mci_tolist);
397 /* should always by NULL */
398 SM_STAB_FREE(s->s_mci.mci_sasl_string);
400 if (s->s_mci.mci_rpool != NULL)
402 sm_rpool_free(s->s_mci.mci_rpool);
403 s->s_mci.mci_macro.mac_rpool = NULL;
404 s->s_mci.mci_rpool = NULL;
415 SM_STAB_FREE(s->s_name); /* XXX */
418 sm_free(f); /* XXX */
429 ** DUMPSTAB -- dump symbol table.
434 #define MAXSTTYPES (ST_MCI + 1)
439 int i, t, total, types[MAXSTTYPES];
441 static int prevt[MAXSTTYPES], prev = 0;
444 for (i = 0; i < MAXSTTYPES; i++)
446 for (i = 0; i < STABSIZE; i++)
453 if (t > MAXSTTYPES - 1)
459 sm_syslog(LOG_INFO, NOQID, "stab: total=%d (%d)", total, total - prev);
461 for (i = 0; i < MAXSTTYPES; i++)
465 sm_syslog(LOG_INFO, NOQID, "stab: type[%2d]=%2d (%d)",
466 i, types[i], types[i] - prevt[i]);
471 #endif /* SM_HEAP_CHECK */