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 $")
23 ** STAB -- manage the symbol table
26 ** name -- the name to be looked up or inserted.
27 ** type -- the type of symbol.
29 ** ST_ENTER -- enter the name if not already present.
30 ** ST_FIND -- find it only.
33 ** pointer to a STAB entry for this name.
34 ** NULL if not found and not entered.
37 ** can update the symbol table.
41 #define SM_LOWER(c) ((isascii(c) && isupper(c)) ? tolower(c) : (c))
43 static STAB *SymTab[STABSIZE];
58 sm_dprintf("STAB: %s %d ", name, type);
61 ** Compute the hashing function
65 for (p = name; *p != '\0'; p++)
66 hfunc = ((hfunc << 1) ^ (SM_LOWER(*p) & 0377)) % STABSIZE;
69 sm_dprintf("(hfunc=%d) ", hfunc);
72 if (type == ST_MACRO || type == ST_RULESET || type == ST_NAMECANON)
74 while ((s = *ps) != NULL &&
75 (s->s_symtype != type || strcmp(name, s->s_name)))
80 while ((s = *ps) != NULL &&
81 (s->s_symtype != type || sm_strcasecmp(name, s->s_name)))
86 ** Dispose of the entry.
89 if (s != NULL || op == ST_FIND)
94 sm_dprintf("not found\n");
97 long *lp = (long *) s->s_class;
99 sm_dprintf("type %d val %lx %lx %lx %lx\n",
100 s->s_symtype, lp[0], lp[1], lp[2], lp[3]);
107 ** Make a new entry and link it in.
111 sm_dprintf("entered\n");
113 /* determine size of new entry */
117 len = sizeof(s->s_class);
121 len = sizeof(s->s_mailer);
125 len = sizeof(s->s_alias);
129 len = sizeof(s->s_mapclass);
133 len = sizeof(s->s_map);
137 len = sizeof(s->s_hostsig);
141 len = sizeof(s->s_namecanon);
145 len = sizeof(s->s_macro);
149 len = sizeof(s->s_ruleset);
153 len = sizeof(s->s_header);
157 len = sizeof(s->s_service);
162 len = sizeof(s->s_lmap);
168 len = sizeof(s->s_milter);
173 len = sizeof(s->s_quegrp);
178 len = sizeof(s->s_socketmap);
184 len = sizeof(s->s_tlsa);
190 ** Each mailer has its own MCI stab entry:
192 ** s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
194 ** Therefore, anything ST_MCI or larger is an s_mci.
198 len = sizeof(s->s_mci);
201 syserr("stab: unknown symbol type %d", type);
202 len = sizeof(s->s_value);
206 len += sizeof(*s) - sizeof(s->s_value);
209 sm_dprintf("size of stab entry: %d\n", len);
212 s = (STAB *) sm_pmalloc_x(len);
213 memset((char *) s, '\0', len);
214 s->s_name = sm_pstrdup_x(name);
220 /* set a default value for rulesets */
221 if (type == ST_RULESET)
227 ** STABAPPLY -- apply function to all stab entries
230 ** func -- the function to apply. It will be given two
231 ** parameters (the stab entry and the arg).
232 ** arg -- an arbitrary argument, passed to func.
240 void (*func)__P((STAB *, int));
243 register STAB **shead;
246 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
248 for (s = *shead; s != NULL; s = s->s_next)
251 sm_dprintf("stabapply: trying %d/%s\n",
252 s->s_symtype, s->s_name);
258 ** QUEUEUP_MACROS -- queueup the macros in a class
260 ** Write the macros listed in the specified class into the
261 ** file referenced by qfp.
264 ** class -- class ID.
265 ** qfp -- file pointer to the queue file.
266 ** e -- the envelope.
273 queueup_macros(class, qfp, e)
278 register STAB **shead;
284 class = bitidx(class);
285 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
287 for (s = *shead; s != NULL; s = s->s_next)
292 if (s->s_symtype == ST_CLASS &&
293 bitnset(bitidx(class), s->s_class) &&
294 (m = macid(s->s_name)) != 0 &&
295 (p = macvalue(m, e)) != NULL)
297 (void) sm_io_fprintf(qfp, SM_TIME_DEFAULT,
307 ** COPY_CLASS -- copy class members from one class to another
310 ** src -- source class.
311 ** dst -- destination class.
322 register STAB **shead;
327 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
329 for (s = *shead; s != NULL; s = s->s_next)
331 if (s->s_symtype == ST_CLASS &&
332 bitnset(src, s->s_class))
333 setbitn(dst, s->s_class);
339 ** RMEXPSTAB -- remove expired entries from SymTab.
341 ** These entries need to be removed in long-running processes,
342 ** e.g., persistent queue runners, to avoid consuming memory.
344 ** XXX It might be useful to restrict the maximum TTL to avoid
345 ** caching data very long.
354 ** can remove entries from the symbol table.
357 #define SM_STAB_FREE(x) \
374 for (i = 0; i < STABSIZE; i++)
380 switch (s->s_symtype)
383 if (s->s_hostsig.hs_exp >= now)
384 goto next; /* not expired */
385 SM_STAB_FREE(s->s_hostsig.hs_sig); /* XXX */
389 if (s->s_namecanon.nc_exp >= now)
390 goto next; /* not expired */
391 SM_STAB_FREE(s->s_namecanon.nc_cname); /* XXX */
396 if (s->s_tlsa->dane_tlsa_exp >= now)
397 goto next; /* not expired */
398 (void) dane_tlsa_free(s->s_tlsa);
404 if (s->s_symtype >= ST_MCI)
406 /* call mci_uncache? */
407 SM_STAB_FREE(s->s_mci.mci_status);
408 SM_STAB_FREE(s->s_mci.mci_rstatus);
409 SM_STAB_FREE(s->s_mci.mci_heloname);
411 /* not dynamically allocated */
412 SM_STAB_FREE(s->s_mci.mci_host);
413 SM_STAB_FREE(s->s_mci.mci_tolist);
416 /* should always by NULL */
417 SM_STAB_FREE(s->s_mci.mci_sasl_string);
419 if (s->s_mci.mci_rpool != NULL)
421 sm_rpool_free(s->s_mci.mci_rpool);
422 s->s_mci.mci_macro.mac_rpool = NULL;
423 s->s_mci.mci_rpool = NULL;
434 SM_STAB_FREE(s->s_name); /* XXX */
437 sm_free(f); /* XXX */
448 ** DUMPSTAB -- dump symbol table.
453 #define MAXSTTYPES (ST_MCI + 1)
458 int i, t, total, types[MAXSTTYPES];
460 static int prevt[MAXSTTYPES], prev = 0;
463 for (i = 0; i < MAXSTTYPES; i++)
465 for (i = 0; i < STABSIZE; i++)
472 if (t > MAXSTTYPES - 1)
478 sm_syslog(LOG_INFO, NOQID, "stab: total=%d (%d)", total, total - prev);
480 for (i = 0; i < MAXSTTYPES; i++)
484 sm_syslog(LOG_INFO, NOQID, "stab: type[%2d]=%2d (%d)",
485 i, types[i], types[i] - prevt[i]);
490 #endif /* SM_HEAP_CHECK */