2 * Copyright (c) 1998-2005, 2010 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 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: mci.c,v 8.224 2013/03/12 15:24:53 ca Exp $")
18 #if NETINET || NETINET6
19 # include <arpa/inet.h>
20 #endif /* NETINET || NETINET6 */
24 static int mci_generate_persistent_path __P((const char *, char *,
26 static bool mci_load_persistent __P((MCI *));
27 static void mci_uncache __P((MCI **, bool));
28 static int mci_lock_host_statfile __P((MCI *));
29 static int mci_read_persistent __P((SM_FILE_T *, MCI *));
32 ** Mail Connection Information (MCI) Caching Module.
34 ** There are actually two separate things cached. The first is
35 ** the set of all open connections -- these are stored in a
36 ** (small) list. The second is stored in the symbol table; it
37 ** has the overall status for all hosts, whether or not there
38 ** is a connection open currently.
40 ** There should never be too many connections open (since this
41 ** could flood the socket table), nor should a connection be
42 ** allowed to sit idly for too long.
44 ** MaxMciCache is the maximum number of open connections that
47 ** MciCacheTimeout is the time (in seconds) that a connection
48 ** is permitted to survive without activity.
50 ** We actually try any cached connections by sending a RSET
51 ** before we use them; if the RSET fails we close down the
52 ** connection and reopen it (see smtpprobe()).
54 ** The persistent MCI code is donated by Mark Lovell and Paul
55 ** Vixie. It is based on the long term host status code in KJS
56 ** written by Paul but has been adapted by Mark to fit into the
60 static MCI **MciCache; /* the open connection cache */
63 ** MCI_CACHE -- enter a connection structure into the open connection cache
65 ** This may cause something else to be flushed.
68 ** mci -- the connection to cache.
78 register MCI **mcislot;
81 ** Find the best slot. This may cause expired connections
85 mcislot = mci_scan(mci);
88 /* we don't support caching */
92 if (mci->mci_host == NULL)
95 /* if this is already cached, we are done */
96 if (bitset(MCIF_CACHED, mci->mci_flags))
99 /* otherwise we may have to clear the slot */
100 if (*mcislot != NULL)
101 mci_uncache(mcislot, true);
104 sm_dprintf("mci_cache: caching %p (%s) in slot %d\n",
105 mci, mci->mci_host, (int) (mcislot - MciCache));
107 sm_syslog(LOG_DEBUG, CurEnv->e_id,
108 "mci_cache: caching %lx (%.100s) in slot %d",
109 (unsigned long) mci, mci->mci_host,
110 (int) (mcislot - MciCache));
113 mci->mci_flags |= MCIF_CACHED;
116 ** MCI_SCAN -- scan the cache, flush junk, and return best slot
119 ** savemci -- never flush this one. Can be null.
122 ** The LRU (or empty) slot.
130 register MCI **bestmci;
134 if (MaxMciCache <= 0)
136 /* we don't support caching */
140 if (MciCache == NULL)
143 MciCache = (MCI **) sm_pmalloc_x(MaxMciCache * sizeof(*MciCache));
144 memset((char *) MciCache, '\0', MaxMciCache * sizeof(*MciCache));
149 bestmci = &MciCache[0];
150 for (i = 0; i < MaxMciCache; i++)
153 if (mci == NULL || mci->mci_state == MCIS_CLOSED)
155 bestmci = &MciCache[i];
158 if ((mci->mci_lastuse + MciCacheTimeout <= now ||
159 (mci->mci_mailer != NULL &&
160 mci->mci_mailer->m_maxdeliveries > 0 &&
161 mci->mci_deliveries + 1 >= mci->mci_mailer->m_maxdeliveries))&&
164 /* connection idle too long or too many deliveries */
165 bestmci = &MciCache[i];
168 mci_uncache(bestmci, true);
171 if (*bestmci == NULL)
173 if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
174 bestmci = &MciCache[i];
179 ** MCI_UNCACHE -- remove a connection from a slot.
181 ** May close a connection.
184 ** mcislot -- the slot to empty.
185 ** doquit -- if true, send QUIT protocol on this connection.
186 ** if false, we are assumed to be in a forked child;
187 ** all we want to do is close the file(s).
194 mci_uncache(mcislot, doquit)
195 register MCI **mcislot;
199 extern ENVELOPE BlankEnvelope;
205 if (mci->mci_host == NULL)
208 mci_unlock_host(mci);
211 sm_dprintf("mci_uncache: uncaching %p (%s) from slot %d (%d)\n",
212 mci, mci->mci_host, (int) (mcislot - MciCache),
215 sm_syslog(LOG_DEBUG, CurEnv->e_id,
216 "mci_uncache: uncaching %lx (%.100s) from slot %d (%d)",
217 (unsigned long) mci, mci->mci_host,
218 (int) (mcislot - MciCache), doquit);
220 mci->mci_deliveries = 0;
223 message("Closing connection to %s", mci->mci_host);
225 mci->mci_flags &= ~MCIF_CACHED;
227 /* only uses the envelope to flush the transcript file */
228 if (mci->mci_state != MCIS_CLOSED)
229 smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
231 xla_host_end(mci->mci_host);
236 if (mci->mci_in != NULL)
237 (void) sm_io_close(mci->mci_in, SM_TIME_DEFAULT);
238 if (mci->mci_out != NULL)
239 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
240 mci->mci_in = mci->mci_out = NULL;
241 mci->mci_state = MCIS_CLOSED;
242 mci->mci_exitstat = EX_OK;
246 mci->mci_retryrcpt = false;
247 mci->mci_tolist = NULL;
249 mci->mci_okrcpts = 0;
250 #endif /* PIPELINING */
253 SM_FREE_CLR(mci->mci_status);
254 SM_FREE_CLR(mci->mci_rstatus);
255 SM_FREE_CLR(mci->mci_heloname);
256 if (mci->mci_rpool != NULL)
258 sm_rpool_free(mci->mci_rpool);
259 mci->mci_macro.mac_rpool = NULL;
260 mci->mci_rpool = NULL;
264 ** MCI_FLUSH -- flush the entire cache
267 ** doquit -- if true, send QUIT protocol.
268 ** if false, just close the connection.
269 ** allbut -- but leave this one open.
276 mci_flush(doquit, allbut)
282 if (MciCache == NULL)
285 for (i = 0; i < MaxMciCache; i++)
287 if (allbut != MciCache[i])
288 mci_uncache(&MciCache[i], doquit);
293 ** MCI_CLR_EXTENSIONS -- clear knowledge about SMTP extensions
296 ** mci -- the connection to clear.
303 mci_clr_extensions(mci)
309 mci->mci_flags &= ~MCIF_EXTENS;
310 mci->mci_maxsize = 0;
313 mci->mci_saslcap = NULL;
318 ** MCI_GET -- get information about a particular host
321 ** host -- host to look for.
325 ** mci for this host (might be new).
335 extern SOCKADDR CurHostAddr;
337 /* clear CurHostAddr so we don't get a bogus address with this name */
338 memset(&CurHostAddr, '\0', sizeof(CurHostAddr));
340 /* clear out any expired connections */
341 (void) mci_scan(NULL);
344 syserr("!negative mno %d (%s)", m->m_mno, m->m_name);
346 s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
349 /* initialize per-message data */
350 mci->mci_retryrcpt = false;
351 mci->mci_tolist = NULL;
353 mci->mci_okrcpts = 0;
354 #endif /* PIPELINING */
356 if (mci->mci_rpool == NULL)
357 mci->mci_rpool = sm_rpool_new_x(NULL);
359 if (mci->mci_macro.mac_rpool == NULL)
360 mci->mci_macro.mac_rpool = mci->mci_rpool;
363 ** We don't need to load the persistent data if we have data
364 ** already loaded in the cache.
367 if (mci->mci_host == NULL &&
368 (mci->mci_host = s->s_name) != NULL &&
369 !mci_load_persistent(mci))
372 sm_dprintf("mci_get(%s %s): lock failed\n",
374 mci->mci_exitstat = EX_TEMPFAIL;
375 mci->mci_state = MCIS_CLOSED;
376 mci->mci_statfile = NULL;
382 sm_dprintf("mci_get(%s %s): mci_state=%d, _flags=%lx, _exitstat=%d, _errno=%d\n",
383 host, m->m_name, mci->mci_state, mci->mci_flags,
384 mci->mci_exitstat, mci->mci_errno);
387 if (mci->mci_state == MCIS_OPEN)
389 /* poke the connection to see if it's still alive */
390 (void) smtpprobe(mci);
392 /* reset the stored state in the event of a timeout */
393 if (mci->mci_state != MCIS_OPEN)
396 mci->mci_exitstat = EX_OK;
397 mci->mci_state = MCIS_CLOSED;
401 /* get peer host address */
402 /* (this should really be in the mci struct) */
403 SOCKADDR_LEN_T socklen = sizeof(CurHostAddr);
405 (void) getpeername(sm_io_getinfo(mci->mci_in,
406 SM_IO_WHAT_FD, NULL),
407 (struct sockaddr *) &CurHostAddr, &socklen);
410 if (mci->mci_state == MCIS_CLOSED)
412 time_t now = curtime();
414 /* if this info is stale, ignore it */
415 if (mci->mci_lastuse + MciInfoTimeout <= now)
417 mci->mci_lastuse = now;
419 mci->mci_exitstat = EX_OK;
427 ** MCI_CLOSE -- (forcefully) close files used for a connection.
428 ** Note: this is a last resort, usually smtpquit() or endmailer()
429 ** should be used to close a connection.
432 ** mci -- the connection to close.
433 ** where -- where has this been called?
440 mci_close(mci, where)
449 if (mci->mci_out != NULL)
453 sm_dprintf("mci_close: mci_out!=NULL, where=%s\n",
455 mci_dump(sm_debug_file(), mci, false);
458 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
461 if (mci->mci_in != NULL)
465 sm_dprintf("mci_close: mci_in!=NULL, where=%s\n",
468 mci_dump(sm_debug_file(), mci, false);
470 (void) sm_io_close(mci->mci_in, SM_TIME_DEFAULT);
473 mci->mci_state = MCIS_CLOSED;
477 ** MCI_NEW -- allocate new MCI structure
480 ** rpool -- if non-NULL: allocate from that rpool.
493 mci = (MCI *) sm_malloc_x(sizeof(*mci));
495 mci = (MCI *) sm_rpool_malloc_x(rpool, sizeof(*mci));
496 memset((char *) mci, '\0', sizeof(*mci));
497 mci->mci_rpool = sm_rpool_new_x(NULL);
498 mci->mci_macro.mac_rpool = mci->mci_rpool;
502 ** MCI_MATCH -- check connection cache for a particular host
505 ** host -- host to look for.
509 ** true iff open connection exists.
520 if (m->m_mno < 0 || m->m_mno > MAXMAILERS)
522 s = stab(host, ST_MCI + m->m_mno, ST_FIND);
527 return mci->mci_state == MCIS_OPEN;
530 ** MCI_SETSTAT -- set status codes in MCI structure.
533 ** mci -- the MCI structure to set.
534 ** xstat -- the exit status code.
535 ** dstat -- the DSN status code.
536 ** rstat -- the SMTP status code.
543 mci_setstat(mci, xstat, dstat, rstat)
549 /* protocol errors should never be interpreted as sticky */
550 if (xstat != EX_NOTSTICKY && xstat != EX_PROTOCOL)
551 mci->mci_exitstat = xstat;
553 SM_FREE_CLR(mci->mci_status);
555 mci->mci_status = sm_strdup_x(dstat);
557 SM_FREE_CLR(mci->mci_rstatus);
559 mci->mci_rstatus = sm_strdup_x(rstat);
562 ** MCI_DUMP -- dump the contents of an MCI structure.
565 ** fp -- output file pointer
566 ** mci -- the MCI structure to dump.
577 int mcif_bit; /* flag bit */
578 char *mcif_name; /* flag name */
580 static struct mcifbits MciFlags[] =
582 { MCIF_VALID, "VALID" },
583 { MCIF_CACHED, "CACHED" },
584 { MCIF_ESMTP, "ESMTP" },
585 { MCIF_EXPN, "EXPN" },
586 { MCIF_SIZE, "SIZE" },
587 { MCIF_8BITMIME, "8BITMIME" },
588 { MCIF_7BIT, "7BIT" },
589 { MCIF_INHEADER, "INHEADER" },
590 { MCIF_CVT8TO7, "CVT8TO7" },
592 { MCIF_8BITOK, "8BITOK" },
593 { MCIF_CVT7TO8, "CVT7TO8" },
594 { MCIF_INMIME, "INMIME" },
595 { MCIF_AUTH, "AUTH" },
596 { MCIF_AUTH2, "AUTH2" },
597 { MCIF_AUTHACT, "AUTHACT" },
598 { MCIF_ENHSTAT, "ENHSTAT" },
599 { MCIF_PIPELINED, "PIPELINED" },
602 { MCIF_TLSACT, "TLSACT" },
603 #endif /* STARTTLS */
604 { MCIF_DLVR_BY, "DLVR_BY" },
609 mci_dump(fp, mci, logit)
618 sep = logit ? " " : "\n\t";
620 (void) sm_snprintf(p, SPACELEFT(buf, p), "MCI@%p: ", mci);
624 (void) sm_snprintf(p, SPACELEFT(buf, p), "NULL");
627 (void) sm_snprintf(p, SPACELEFT(buf, p), "flags=%lx", mci->mci_flags);
631 ** The following check is just for paranoia. It protects the
632 ** assignment in the if() clause. If there's not some minimum
633 ** amount of space we can stop right now. The check will not
634 ** trigger as long as sizeof(buf)=4000.
637 if (p >= buf + sizeof(buf) - 4)
639 if (mci->mci_flags != 0)
643 *p++ = '<'; /* protected above */
644 for (f = MciFlags; f->mcif_bit != 0; f++)
646 if (!bitset(f->mcif_bit, mci->mci_flags))
648 (void) sm_strlcpyn(p, SPACELEFT(buf, p), 2,
655 /* Note: sm_snprintf() takes care of NULL arguments for %s */
656 (void) sm_snprintf(p, SPACELEFT(buf, p),
657 ",%serrno=%d, herrno=%d, exitstat=%d, state=%d, pid=%d,%s",
658 sep, mci->mci_errno, mci->mci_herrno,
659 mci->mci_exitstat, mci->mci_state, (int) mci->mci_pid, sep);
661 (void) sm_snprintf(p, SPACELEFT(buf, p),
662 "maxsize=%ld, phase=%s, mailer=%s,%s",
663 mci->mci_maxsize, mci->mci_phase,
664 mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name,
667 (void) sm_snprintf(p, SPACELEFT(buf, p),
668 "status=%s, rstatus=%s,%s",
669 mci->mci_status, mci->mci_rstatus, sep);
671 (void) sm_snprintf(p, SPACELEFT(buf, p),
672 "host=%s, lastuse=%s",
673 mci->mci_host, ctime(&mci->mci_lastuse));
676 sm_syslog(LOG_DEBUG, CurEnv->e_id, "%.1000s", buf);
678 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s\n", buf);
681 ** MCI_DUMP_ALL -- print the entire MCI cache
684 ** fp -- output file pointer
685 ** logit -- if set, log the result instead of printing
693 mci_dump_all(fp, logit)
699 if (MciCache == NULL)
702 for (i = 0; i < MaxMciCache; i++)
703 mci_dump(fp, MciCache[i], logit);
706 ** MCI_LOCK_HOST -- Lock host while sending.
708 ** If we are contacting a host, we'll need to
709 ** update the status information in the host status
710 ** file, and if we want to do that, we ought to have
711 ** locked it. This has the (according to some)
712 ** desirable effect of serializing connectivity with
713 ** remote hosts -- i.e.: one connection to a given
717 ** mci -- containing the host we want to lock.
720 ** EX_OK -- got the lock.
721 ** EX_TEMPFAIL -- didn't get the lock.
731 sm_dprintf("mci_lock_host: NULL mci\n");
735 if (!SingleThreadDelivery)
738 return mci_lock_host_statfile(mci);
742 mci_lock_host_statfile(mci)
745 int save_errno = errno;
747 char fname[MAXPATHLEN];
749 if (HostStatDir == NULL || mci->mci_host == NULL)
753 sm_dprintf("mci_lock_host: attempting to lock %s\n",
756 if (mci_generate_persistent_path(mci->mci_host, fname, sizeof(fname),
759 /* of course this should never happen */
761 sm_dprintf("mci_lock_host: Failed to generate host path for %s\n",
764 retVal = EX_TEMPFAIL;
768 mci->mci_statfile = safefopen(fname, O_RDWR, FileMode,
769 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH|SFF_CREAT);
771 if (mci->mci_statfile == NULL)
773 syserr("mci_lock_host: cannot create host lock file %s", fname);
777 if (!lockfile(sm_io_getinfo(mci->mci_statfile, SM_IO_WHAT_FD, NULL),
778 fname, "", LOCK_EX|LOCK_NB))
781 sm_dprintf("mci_lock_host: couldn't get lock on %s\n",
783 (void) sm_io_close(mci->mci_statfile, SM_TIME_DEFAULT);
784 mci->mci_statfile = NULL;
785 retVal = EX_TEMPFAIL;
789 if (tTd(56, 12) && mci->mci_statfile != NULL)
790 sm_dprintf("mci_lock_host: Sanity check -- lock is good\n");
797 ** MCI_UNLOCK_HOST -- unlock host
799 ** Clean up the lock on a host, close the file, let
800 ** someone else use it.
813 int save_errno = errno;
818 sm_dprintf("mci_unlock_host: NULL mci\n");
822 if (HostStatDir == NULL || mci->mci_host == NULL)
825 if (!SingleThreadDelivery && mci_lock_host_statfile(mci) == EX_TEMPFAIL)
828 sm_dprintf("mci_unlock_host: stat file already locked\n");
833 sm_dprintf("mci_unlock_host: store prior to unlock\n");
834 mci_store_persistent(mci);
837 if (mci->mci_statfile != NULL)
839 (void) sm_io_close(mci->mci_statfile, SM_TIME_DEFAULT);
840 mci->mci_statfile = NULL;
846 ** MCI_LOAD_PERSISTENT -- load persistent host info
848 ** Load information about host that is kept
849 ** in common for all running sendmails.
852 ** mci -- the host/connection to load persistent info for.
855 ** true -- lock was successful
856 ** false -- lock failed
860 mci_load_persistent(mci)
863 int save_errno = errno;
866 char fname[MAXPATHLEN];
871 sm_dprintf("mci_load_persistent: NULL mci\n");
875 if (IgnoreHostStatus || HostStatDir == NULL || mci->mci_host == NULL)
878 /* Already have the persistent information in memory */
879 if (SingleThreadDelivery && mci->mci_statfile != NULL)
883 sm_dprintf("mci_load_persistent: Attempting to load persistent information for %s\n",
886 if (mci_generate_persistent_path(mci->mci_host, fname, sizeof(fname),
889 /* Not much we can do if the file isn't there... */
891 sm_dprintf("mci_load_persistent: Couldn't generate host path\n");
895 fp = safefopen(fname, O_RDONLY, FileMode,
896 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
899 /* I can't think of any reason this should ever happen */
901 sm_dprintf("mci_load_persistent: open(%s): %s\n",
902 fname, sm_errstring(errno));
907 locked = lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), fname, "",
911 (void) mci_read_persistent(fp, mci);
912 (void) lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), fname,
916 (void) sm_io_close(fp, SM_TIME_DEFAULT);
923 ** MCI_READ_PERSISTENT -- read persistent host status file
926 ** fp -- the file pointer to read.
927 ** mci -- the pointer to fill in.
930 ** -1 -- if the file was corrupt.
934 ** This code makes the assumption that this data
935 ** will be read in an atomic fashion, and that the data
936 ** was written in an atomic fashion. Any other functioning
937 ** may lead to some form of insanity. This should be
938 ** perfectly safe due to underlying stdio buffering.
942 mci_read_persistent(fp, mci)
948 int saveLineNumber = LineNumber;
953 syserr("mci_read_persistent: NULL fp");
959 syserr("mci_read_persistent: NULL mci");
965 sm_dprintf("mci_read_persistent: fp=%lx, mci=",
969 SM_FREE_CLR(mci->mci_status);
970 SM_FREE_CLR(mci->mci_rstatus);
972 sm_io_rewind(fp, SM_TIME_DEFAULT);
975 while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof(buf)) >= 0)
978 p = strchr(buf, '\n');
983 case 'V': /* version stamp */
985 if (ver < 0 || ver > 0)
986 syserr("Unknown host status version %d: %d max",
990 case 'E': /* UNIX error number */
991 mci->mci_errno = atoi(&buf[1]);
994 case 'H': /* DNS error number */
995 mci->mci_herrno = atoi(&buf[1]);
998 case 'S': /* UNIX exit status */
999 mci->mci_exitstat = atoi(&buf[1]);
1002 case 'D': /* DSN status */
1003 mci->mci_status = newstr(&buf[1]);
1006 case 'R': /* SMTP status */
1007 mci->mci_rstatus = newstr(&buf[1]);
1010 case 'U': /* last usage time */
1011 mci->mci_lastuse = atol(&buf[1]);
1014 case '.': /* end of file */
1016 mci_dump(sm_debug_file(), mci, false);
1020 sm_syslog(LOG_CRIT, NOQID,
1021 "%s: line %d: Unknown host status line \"%s\"",
1022 FileName == NULL ? mci->mci_host : FileName,
1024 LineNumber = saveLineNumber;
1028 LineNumber = saveLineNumber;
1030 sm_dprintf("incomplete (missing dot for EOF)\n");
1036 ** MCI_STORE_PERSISTENT -- Store persistent MCI information
1038 ** Store information about host that is kept
1039 ** in common for all running sendmails.
1042 ** mci -- the host/connection to store persistent info for.
1049 mci_store_persistent(mci)
1052 int save_errno = errno;
1057 sm_dprintf("mci_store_persistent: NULL mci\n");
1061 if (HostStatDir == NULL || mci->mci_host == NULL)
1065 sm_dprintf("mci_store_persistent: Storing information for %s\n",
1068 if (mci->mci_statfile == NULL)
1071 sm_dprintf("mci_store_persistent: no statfile\n");
1075 sm_io_rewind(mci->mci_statfile, SM_TIME_DEFAULT);
1077 (void) ftruncate(sm_io_getinfo(mci->mci_statfile, SM_IO_WHAT_FD, NULL),
1079 #endif /* !NOFTRUNCATE */
1081 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "V0\n");
1082 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "E%d\n",
1084 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "H%d\n",
1086 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "S%d\n",
1088 if (mci->mci_status != NULL)
1089 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT,
1091 denlstring(mci->mci_status, true, false));
1092 if (mci->mci_rstatus != NULL)
1093 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT,
1095 denlstring(mci->mci_rstatus, true, false));
1096 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "U%ld\n",
1097 (long)(mci->mci_lastuse));
1098 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, ".\n");
1100 (void) sm_io_flush(mci->mci_statfile, SM_TIME_DEFAULT);
1106 ** MCI_TRAVERSE_PERSISTENT -- walk persistent status tree
1108 ** Recursively find all the mci host files in `pathname'. Default to
1109 ** main host status directory if no path is provided.
1110 ** Call (*action)(pathname, host) for each file found.
1112 ** Note: all information is collected in a list before it is processed.
1113 ** This may not be the best way to do it, but it seems safest, since
1114 ** the file system would be touched while we are attempting to traverse
1115 ** the directory tree otherwise (during purges).
1118 ** action -- function to call on each node. If returns < 0,
1119 ** return immediately.
1120 ** pathname -- root of tree. If null, use main host status
1124 ** < 0 -- if any action routine returns a negative value, that
1125 ** value is returned.
1126 ** 0 -- if we successfully went to completion.
1127 ** > 0 -- return status from action()
1131 mci_traverse_persistent(action, pathname)
1132 int (*action)__P((char *, char *));
1135 struct stat statbuf;
1139 if (pathname == NULL)
1140 pathname = HostStatDir;
1141 if (pathname == NULL)
1145 sm_dprintf("mci_traverse: pathname is %s\n", pathname);
1147 ret = stat(pathname, &statbuf);
1151 sm_dprintf("mci_traverse: Failed to stat %s: %s\n",
1152 pathname, sm_errstring(errno));
1155 if (S_ISDIR(statbuf.st_mode))
1157 bool leftone, removedone;
1161 char newpath[MAXPATHLEN];
1162 #if MAXPATHLEN <= MAXNAMLEN - 3
1163 ERROR "MAXPATHLEN <= MAXNAMLEN - 3"
1164 #endif /* MAXPATHLEN <= MAXNAMLEN - 3 */
1166 if ((d = opendir(pathname)) == NULL)
1169 sm_dprintf("mci_traverse: opendir %s: %s\n",
1170 pathname, sm_errstring(errno));
1175 ** Reserve space for trailing '/', at least one
1176 ** character, and '\0'
1179 len = sizeof(newpath) - 3;
1180 if (sm_strlcpy(newpath, pathname, len) >= len)
1182 int save_errno = errno;
1185 sm_dprintf("mci_traverse: path \"%s\" too long",
1191 newptr = newpath + strlen(newpath);
1193 len = sizeof(newpath) - (newptr - newpath);
1196 ** repeat until no file has been removed
1197 ** this may become ugly when several files "expire"
1198 ** during these loops, but it's better than doing
1199 ** a rewinddir() inside the inner loop
1204 leftone = removedone = false;
1205 while ((e = readdir(d)) != NULL)
1207 if (e->d_name[0] == '.')
1210 if (sm_strlcpy(newptr, e->d_name, len) >= len)
1212 /* Skip truncated copies */
1216 sm_dprintf("mci_traverse: path \"%s%s\" too long",
1217 newpath, e->d_name);
1224 ret = mci_traverse_persistent(action, newpath);
1229 if (!removedone && ret == 0 &&
1230 action == mci_purge_persistent)
1237 ** The following appears to be
1238 ** necessary during purges, since
1239 ** we modify the directory structure
1245 sm_dprintf("mci_traverse: path %s: ret %d removed %d left %d\n",
1246 pathname, ret, removedone, leftone);
1247 } while (removedone);
1249 /* purge (or whatever) the directory proper */
1253 ret = (*action)(newpath, NULL);
1257 else if (S_ISREG(statbuf.st_mode))
1259 char *end = pathname + strlen(pathname) - 1;
1262 char host[MAXHOSTNAMELEN];
1263 char *hostptr = host;
1266 ** Reconstruct the host name from the path to the
1267 ** persistent information.
1272 if (hostptr != host)
1275 while (start > pathname && *(start - 1) != '/')
1281 for (scan = start; scan <= end; scan++)
1282 *(hostptr++) = *scan;
1285 } while (end > pathname && *end == '.');
1290 ** Do something with the file containing the persistent
1294 ret = (*action)(pathname, host);
1300 ** MCI_PRINT_PERSISTENT -- print persistent info
1302 ** Dump the persistent information in the file 'pathname'
1305 ** pathname -- the pathname to the status file.
1306 ** hostname -- the corresponding host name.
1313 mci_print_persistent(pathname, hostname)
1317 static bool initflag = false;
1319 int width = Verbose ? 78 : 25;
1323 /* skip directories */
1324 if (hostname == NULL)
1333 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1334 " -------------- Hostname --------------- How long ago ---------Results---------\n");
1337 fp = safefopen(pathname, O_RDONLY, FileMode,
1338 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
1343 sm_dprintf("mci_print_persistent: cannot open %s: %s\n",
1344 pathname, sm_errstring(errno));
1348 FileName = pathname;
1349 memset(&mcib, '\0', sizeof(mcib));
1350 if (mci_read_persistent(fp, &mcib) < 0)
1352 syserr("%s: could not read status file", pathname);
1353 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1358 locked = !lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), pathname,
1359 "", LOCK_SH|LOCK_NB);
1360 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1363 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%c%-39s %12s ",
1364 locked ? '*' : ' ', hostname,
1365 pintvl(curtime() - mcib.mci_lastuse, true));
1366 if (mcib.mci_rstatus != NULL)
1367 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n", width,
1369 else if (mcib.mci_exitstat == EX_TEMPFAIL && mcib.mci_errno != 0)
1370 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1371 "Deferred: %.*s\n", width - 10,
1372 sm_errstring(mcib.mci_errno));
1373 else if (mcib.mci_exitstat != 0)
1375 char *exmsg = sm_sysexmsg(mcib.mci_exitstat);
1381 (void) sm_snprintf(buf, sizeof(buf),
1382 "Unknown mailer error %d",
1384 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n",
1388 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n",
1391 else if (mcib.mci_errno == 0)
1392 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK\n");
1394 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK: %.*s\n",
1395 width - 4, sm_errstring(mcib.mci_errno));
1400 ** MCI_PURGE_PERSISTENT -- Remove a persistence status file.
1403 ** pathname -- path to the status file.
1404 ** hostname -- name of host corresponding to that file.
1405 ** NULL if this is a directory (domain).
1409 ** 1 -- file not deleted (too young, incorrect format)
1410 ** < 0 -- some error occurred
1414 mci_purge_persistent(pathname, hostname)
1418 struct stat statbuf;
1419 char *end = pathname + strlen(pathname) - 1;
1423 sm_dprintf("mci_purge_persistent: purging %s\n", pathname);
1425 ret = stat(pathname, &statbuf);
1429 sm_dprintf("mci_purge_persistent: Failed to stat %s: %s\n",
1430 pathname, sm_errstring(errno));
1433 if (curtime() - statbuf.st_mtime <= MciInfoTimeout)
1435 if (hostname != NULL)
1437 /* remove the file */
1438 ret = unlink(pathname);
1442 sm_syslog(LOG_ERR, NOQID,
1443 "mci_purge_persistent: failed to unlink %s: %s",
1444 pathname, sm_errstring(errno));
1446 sm_dprintf("mci_purge_persistent: failed to unlink %s: %s\n",
1447 pathname, sm_errstring(errno));
1453 /* remove the directory */
1458 sm_dprintf("mci_purge_persistent: dpurge %s\n", pathname);
1460 ret = rmdir(pathname);
1464 sm_dprintf("mci_purge_persistent: rmdir %s: %s\n",
1465 pathname, sm_errstring(errno));
1473 ** MCI_GENERATE_PERSISTENT_PATH -- generate path from hostname
1475 ** Given `host', convert from a.b.c to $HostStatDir/c./b./a,
1476 ** putting the result into `path'. if `createflag' is set, intervening
1477 ** directories will be created as needed.
1480 ** host -- host name to convert from.
1481 ** path -- place to store result.
1482 ** pathlen -- length of path buffer.
1483 ** createflag -- if set, create intervening directories as
1492 mci_generate_persistent_path(host, path, pathlen, createflag)
1498 char *elem, *p, *x, ch;
1501 char t_host[MAXHOSTNAMELEN];
1503 struct in6_addr in6_addr;
1504 #endif /* NETINET6 */
1507 ** Rationality check the arguments.
1512 syserr("mci_generate_persistent_path: null host");
1517 syserr("mci_generate_persistent_path: null path");
1522 sm_dprintf("mci_generate_persistent_path(%s): ", host);
1524 if (*host == '\0' || *host == '.')
1527 /* make certain this is not a bracketed host number */
1528 if (strlen(host) > sizeof(t_host) - 1)
1531 (void) sm_strlcpy(t_host, host + 1, sizeof(t_host));
1533 (void) sm_strlcpy(t_host, host, sizeof(t_host));
1536 ** Delete any trailing dots from the hostname.
1537 ** Leave 'elem' pointing at the \0.
1540 elem = t_host + strlen(t_host);
1541 while (elem > t_host &&
1542 (elem[-1] == '.' || (host[0] == '[' && elem[-1] == ']')))
1545 /* check for bogus bracketed address */
1550 if (anynet_pton(AF_INET6, t_host, &in6_addr) == 1)
1552 # endif /* NETINET6 */
1554 if (inet_addr(t_host) != INADDR_NONE)
1556 # endif /* NETINET */
1561 /* check for what will be the final length of the path */
1562 len = strlen(HostStatDir) + 2;
1563 for (p = (char *) t_host; *p != '\0'; p++)
1568 if (p[0] == '.' && p[1] == '.')
1571 if (len > pathlen || len < 1)
1573 (void) sm_strlcpy(path, HostStatDir, pathlen);
1574 p = path + strlen(path);
1575 while (elem > t_host)
1577 if (!path_is_dir(path, createflag))
1583 while (elem >= t_host && *elem != '.')
1587 while ((ch = *x++) != '\0' && ch != '.')
1589 if (isascii(ch) && isupper(ch))
1592 ch = ':'; /* / -> : */
1602 sm_dprintf("FAILURE %d\n", ret);
1604 sm_dprintf("SUCCESS %s\n", path);