2 * Copyright (c) 1998-2003 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.205.2.3 2003/01/07 03:56:19 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 NOOP
51 ** before we use them; if the NOOP fails we close down the
52 ** connection and reopen it. Note that this means that a
53 ** server SMTP that doesn't support NOOP will hose the
54 ** algorithm -- but that doesn't seem too likely.
56 ** The persistent MCI code is donated by Mark Lovell and Paul
57 ** Vixie. It is based on the long term host status code in KJS
58 ** written by Paul but has been adapted by Mark to fit into the
62 static MCI **MciCache; /* the open connection cache */
65 ** MCI_CACHE -- enter a connection structure into the open connection cache
67 ** This may cause something else to be flushed.
70 ** mci -- the connection to cache.
80 register MCI **mcislot;
83 ** Find the best slot. This may cause expired connections
87 mcislot = mci_scan(mci);
90 /* we don't support caching */
94 if (mci->mci_host == NULL)
97 /* if this is already cached, we are done */
98 if (bitset(MCIF_CACHED, mci->mci_flags))
101 /* otherwise we may have to clear the slot */
102 if (*mcislot != NULL)
103 mci_uncache(mcislot, true);
106 sm_dprintf("mci_cache: caching %p (%s) in slot %d\n",
107 mci, mci->mci_host, (int) (mcislot - MciCache));
109 sm_syslog(LOG_DEBUG, CurEnv->e_id,
110 "mci_cache: caching %lx (%.100s) in slot %d",
111 (unsigned long) mci, mci->mci_host,
112 (int) (mcislot - MciCache));
115 mci->mci_flags |= MCIF_CACHED;
118 ** MCI_SCAN -- scan the cache, flush junk, and return best slot
121 ** savemci -- never flush this one. Can be null.
124 ** The LRU (or empty) slot.
132 register MCI **bestmci;
136 if (MaxMciCache <= 0)
138 /* we don't support caching */
142 if (MciCache == NULL)
145 MciCache = (MCI **) sm_pmalloc_x(MaxMciCache * sizeof *MciCache);
146 memset((char *) MciCache, '\0', MaxMciCache * sizeof *MciCache);
151 bestmci = &MciCache[0];
152 for (i = 0; i < MaxMciCache; i++)
155 if (mci == NULL || mci->mci_state == MCIS_CLOSED)
157 bestmci = &MciCache[i];
160 if ((mci->mci_lastuse + MciCacheTimeout <= now ||
161 (mci->mci_mailer != NULL &&
162 mci->mci_mailer->m_maxdeliveries > 0 &&
163 mci->mci_deliveries + 1 >= mci->mci_mailer->m_maxdeliveries))&&
166 /* connection idle too long or too many deliveries */
167 bestmci = &MciCache[i];
170 mci_uncache(bestmci, true);
173 if (*bestmci == NULL)
175 if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
176 bestmci = &MciCache[i];
181 ** MCI_UNCACHE -- remove a connection from a slot.
183 ** May close a connection.
186 ** mcislot -- the slot to empty.
187 ** doquit -- if true, send QUIT protocol on this connection.
188 ** if false, we are assumed to be in a forked child;
189 ** all we want to do is close the file(s).
196 mci_uncache(mcislot, doquit)
197 register MCI **mcislot;
201 extern ENVELOPE BlankEnvelope;
207 if (mci->mci_host == NULL)
210 mci_unlock_host(mci);
213 sm_dprintf("mci_uncache: uncaching %p (%s) from slot %d (%d)\n",
214 mci, mci->mci_host, (int) (mcislot - MciCache),
217 sm_syslog(LOG_DEBUG, CurEnv->e_id,
218 "mci_uncache: uncaching %lx (%.100s) from slot %d (%d)",
219 (unsigned long) mci, mci->mci_host,
220 (int) (mcislot - MciCache), doquit);
222 mci->mci_deliveries = 0;
225 message("Closing connection to %s", mci->mci_host);
227 mci->mci_flags &= ~MCIF_CACHED;
229 /* only uses the envelope to flush the transcript file */
230 if (mci->mci_state != MCIS_CLOSED)
231 smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
233 xla_host_end(mci->mci_host);
238 if (mci->mci_in != NULL)
239 (void) sm_io_close(mci->mci_in, SM_TIME_DEFAULT);
240 if (mci->mci_out != NULL)
241 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
242 mci->mci_in = mci->mci_out = NULL;
243 mci->mci_state = MCIS_CLOSED;
244 mci->mci_exitstat = EX_OK;
248 mci->mci_retryrcpt = false;
249 mci->mci_tolist = NULL;
251 mci->mci_okrcpts = 0;
252 #endif /* PIPELINING */
255 SM_FREE_CLR(mci->mci_status);
256 SM_FREE_CLR(mci->mci_rstatus);
257 SM_FREE_CLR(mci->mci_heloname);
258 if (mci->mci_rpool != NULL)
260 sm_rpool_free(mci->mci_rpool);
261 mci->mci_macro.mac_rpool = NULL;
262 mci->mci_rpool = NULL;
266 ** MCI_FLUSH -- flush the entire cache
269 ** doquit -- if true, send QUIT protocol.
270 ** if false, just close the connection.
271 ** allbut -- but leave this one open.
278 mci_flush(doquit, allbut)
284 if (MciCache == NULL)
287 for (i = 0; i < MaxMciCache; i++)
289 if (allbut != MciCache[i])
290 mci_uncache(&MciCache[i], doquit);
294 ** MCI_GET -- get information about a particular host
297 ** host -- host to look for.
301 ** mci for this host (might be new).
311 extern SOCKADDR CurHostAddr;
313 /* clear CurHostAddr so we don't get a bogus address with this name */
314 memset(&CurHostAddr, '\0', sizeof CurHostAddr);
316 /* clear out any expired connections */
317 (void) mci_scan(NULL);
320 syserr("!negative mno %d (%s)", m->m_mno, m->m_name);
322 s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
325 /* initialize per-message data */
326 mci->mci_retryrcpt = false;
327 mci->mci_tolist = NULL;
329 mci->mci_okrcpts = 0;
330 #endif /* PIPELINING */
332 if (mci->mci_rpool == NULL)
333 mci->mci_rpool = sm_rpool_new_x(NULL);
335 if (mci->mci_macro.mac_rpool == NULL)
336 mci->mci_macro.mac_rpool = mci->mci_rpool;
339 ** We don't need to load the persistent data if we have data
340 ** already loaded in the cache.
343 if (mci->mci_host == NULL &&
344 (mci->mci_host = s->s_name) != NULL &&
345 !mci_load_persistent(mci))
348 sm_dprintf("mci_get(%s %s): lock failed\n",
350 mci->mci_exitstat = EX_TEMPFAIL;
351 mci->mci_state = MCIS_CLOSED;
352 mci->mci_statfile = NULL;
358 sm_dprintf("mci_get(%s %s): mci_state=%d, _flags=%lx, _exitstat=%d, _errno=%d\n",
359 host, m->m_name, mci->mci_state, mci->mci_flags,
360 mci->mci_exitstat, mci->mci_errno);
363 if (mci->mci_state == MCIS_OPEN)
365 /* poke the connection to see if it's still alive */
366 (void) smtpprobe(mci);
368 /* reset the stored state in the event of a timeout */
369 if (mci->mci_state != MCIS_OPEN)
372 mci->mci_exitstat = EX_OK;
373 mci->mci_state = MCIS_CLOSED;
377 /* get peer host address */
378 /* (this should really be in the mci struct) */
379 SOCKADDR_LEN_T socklen = sizeof CurHostAddr;
381 (void) getpeername(sm_io_getinfo(mci->mci_in,
382 SM_IO_WHAT_FD, NULL),
383 (struct sockaddr *) &CurHostAddr, &socklen);
386 if (mci->mci_state == MCIS_CLOSED)
388 time_t now = curtime();
390 /* if this info is stale, ignore it */
391 if (mci->mci_lastuse + MciInfoTimeout <= now)
393 mci->mci_lastuse = now;
395 mci->mci_exitstat = EX_OK;
402 ** MCI_NEW -- allocate new MCI structure
405 ** rpool -- if non-NULL: allocate from that rpool.
418 mci = (MCI *) sm_malloc_x(sizeof *mci);
420 mci = (MCI *) sm_rpool_malloc_x(rpool, sizeof *mci);
421 memset((char *) mci, '\0', sizeof *mci);
422 mci->mci_rpool = sm_rpool_new_x(NULL);
423 mci->mci_macro.mac_rpool = mci->mci_rpool;
427 ** MCI_MATCH -- check connection cache for a particular host
430 ** host -- host to look for.
434 ** true iff open connection exists.
445 if (m->m_mno < 0 || m->m_mno > MAXMAILERS)
447 s = stab(host, ST_MCI + m->m_mno, ST_FIND);
452 return mci->mci_state == MCIS_OPEN;
455 ** MCI_SETSTAT -- set status codes in MCI structure.
458 ** mci -- the MCI structure to set.
459 ** xstat -- the exit status code.
460 ** dstat -- the DSN status code.
461 ** rstat -- the SMTP status code.
468 mci_setstat(mci, xstat, dstat, rstat)
474 /* protocol errors should never be interpreted as sticky */
475 if (xstat != EX_NOTSTICKY && xstat != EX_PROTOCOL)
476 mci->mci_exitstat = xstat;
478 SM_FREE_CLR(mci->mci_status);
480 mci->mci_status = sm_strdup_x(dstat);
482 SM_FREE_CLR(mci->mci_rstatus);
484 mci->mci_rstatus = sm_strdup_x(rstat);
487 ** MCI_DUMP -- dump the contents of an MCI structure.
490 ** mci -- the MCI structure to dump.
501 int mcif_bit; /* flag bit */
502 char *mcif_name; /* flag name */
504 static struct mcifbits MciFlags[] =
506 { MCIF_VALID, "VALID" },
507 { MCIF_CACHED, "CACHED" },
508 { MCIF_ESMTP, "ESMTP" },
509 { MCIF_EXPN, "EXPN" },
510 { MCIF_SIZE, "SIZE" },
511 { MCIF_8BITMIME, "8BITMIME" },
512 { MCIF_7BIT, "7BIT" },
513 { MCIF_INHEADER, "INHEADER" },
514 { MCIF_CVT8TO7, "CVT8TO7" },
516 { MCIF_8BITOK, "8BITOK" },
517 { MCIF_CVT7TO8, "CVT7TO8" },
518 { MCIF_INMIME, "INMIME" },
519 { MCIF_AUTH, "AUTH" },
520 { MCIF_AUTHACT, "AUTHACT" },
521 { MCIF_ENHSTAT, "ENHSTAT" },
522 { MCIF_PIPELINED, "PIPELINED" },
525 { MCIF_TLSACT, "TLSACT" },
526 #endif /* STARTTLS */
527 { MCIF_DLVR_BY, "DLVR_BY" },
540 sep = logit ? " " : "\n\t";
542 (void) sm_snprintf(p, SPACELEFT(buf, p), "MCI@%p: ", mci);
546 (void) sm_snprintf(p, SPACELEFT(buf, p), "NULL");
549 (void) sm_snprintf(p, SPACELEFT(buf, p), "flags=%lx", mci->mci_flags);
551 if (mci->mci_flags != 0)
556 for (f = MciFlags; f->mcif_bit != 0; f++)
558 if (!bitset(f->mcif_bit, mci->mci_flags))
560 (void) sm_strlcpyn(p, SPACELEFT(buf, p), 2,
567 /* Note: sm_snprintf() takes care of NULL arguments for %s */
568 (void) sm_snprintf(p, SPACELEFT(buf, p),
569 ",%serrno=%d, herrno=%d, exitstat=%d, state=%d, pid=%d,%s",
570 sep, mci->mci_errno, mci->mci_herrno,
571 mci->mci_exitstat, mci->mci_state, (int) mci->mci_pid, sep);
573 (void) sm_snprintf(p, SPACELEFT(buf, p),
574 "maxsize=%ld, phase=%s, mailer=%s,%s",
575 mci->mci_maxsize, mci->mci_phase,
576 mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name,
579 (void) sm_snprintf(p, SPACELEFT(buf, p),
580 "status=%s, rstatus=%s,%s",
581 mci->mci_status, mci->mci_rstatus, sep);
583 (void) sm_snprintf(p, SPACELEFT(buf, p),
584 "host=%s, lastuse=%s",
585 mci->mci_host, ctime(&mci->mci_lastuse));
588 sm_syslog(LOG_DEBUG, CurEnv->e_id, "%.1000s", buf);
590 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s\n", buf);
593 ** MCI_DUMP_ALL -- print the entire MCI cache
596 ** logit -- if set, log the result instead of printing
609 if (MciCache == NULL)
612 for (i = 0; i < MaxMciCache; i++)
613 mci_dump(MciCache[i], logit);
616 ** MCI_LOCK_HOST -- Lock host while sending.
618 ** If we are contacting a host, we'll need to
619 ** update the status information in the host status
620 ** file, and if we want to do that, we ought to have
621 ** locked it. This has the (according to some)
622 ** desirable effect of serializing connectivity with
623 ** remote hosts -- i.e.: one connection to a given
627 ** mci -- containing the host we want to lock.
630 ** EX_OK -- got the lock.
631 ** EX_TEMPFAIL -- didn't get the lock.
641 sm_dprintf("mci_lock_host: NULL mci\n");
645 if (!SingleThreadDelivery)
648 return mci_lock_host_statfile(mci);
652 mci_lock_host_statfile(mci)
655 int save_errno = errno;
657 char fname[MAXPATHLEN];
659 if (HostStatDir == NULL || mci->mci_host == NULL)
663 sm_dprintf("mci_lock_host: attempting to lock %s\n",
666 if (mci_generate_persistent_path(mci->mci_host, fname, sizeof fname,
669 /* of course this should never happen */
671 sm_dprintf("mci_lock_host: Failed to generate host path for %s\n",
674 retVal = EX_TEMPFAIL;
678 mci->mci_statfile = safefopen(fname, O_RDWR, FileMode,
679 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH|SFF_CREAT);
681 if (mci->mci_statfile == NULL)
683 syserr("mci_lock_host: cannot create host lock file %s", fname);
687 if (!lockfile(sm_io_getinfo(mci->mci_statfile, SM_IO_WHAT_FD, NULL),
688 fname, "", LOCK_EX|LOCK_NB))
691 sm_dprintf("mci_lock_host: couldn't get lock on %s\n",
693 (void) sm_io_close(mci->mci_statfile, SM_TIME_DEFAULT);
694 mci->mci_statfile = NULL;
695 retVal = EX_TEMPFAIL;
699 if (tTd(56, 12) && mci->mci_statfile != NULL)
700 sm_dprintf("mci_lock_host: Sanity check -- lock is good\n");
707 ** MCI_UNLOCK_HOST -- unlock host
709 ** Clean up the lock on a host, close the file, let
710 ** someone else use it.
723 int save_errno = errno;
728 sm_dprintf("mci_unlock_host: NULL mci\n");
732 if (HostStatDir == NULL || mci->mci_host == NULL)
735 if (!SingleThreadDelivery && mci_lock_host_statfile(mci) == EX_TEMPFAIL)
738 sm_dprintf("mci_unlock_host: stat file already locked\n");
743 sm_dprintf("mci_unlock_host: store prior to unlock\n");
744 mci_store_persistent(mci);
747 if (mci->mci_statfile != NULL)
749 (void) sm_io_close(mci->mci_statfile, SM_TIME_DEFAULT);
750 mci->mci_statfile = NULL;
756 ** MCI_LOAD_PERSISTENT -- load persistent host info
758 ** Load information about host that is kept
759 ** in common for all running sendmails.
762 ** mci -- the host/connection to load persistent info for.
765 ** true -- lock was successful
766 ** false -- lock failed
770 mci_load_persistent(mci)
773 int save_errno = errno;
776 char fname[MAXPATHLEN];
781 sm_dprintf("mci_load_persistent: NULL mci\n");
785 if (IgnoreHostStatus || HostStatDir == NULL || mci->mci_host == NULL)
788 /* Already have the persistent information in memory */
789 if (SingleThreadDelivery && mci->mci_statfile != NULL)
793 sm_dprintf("mci_load_persistent: Attempting to load persistent information for %s\n",
796 if (mci_generate_persistent_path(mci->mci_host, fname, sizeof fname,
799 /* Not much we can do if the file isn't there... */
801 sm_dprintf("mci_load_persistent: Couldn't generate host path\n");
805 fp = safefopen(fname, O_RDONLY, FileMode,
806 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
809 /* I can't think of any reason this should ever happen */
811 sm_dprintf("mci_load_persistent: open(%s): %s\n",
812 fname, sm_errstring(errno));
817 locked = lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), fname, "",
821 (void) mci_read_persistent(fp, mci);
822 (void) lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), fname,
826 (void) sm_io_close(fp, SM_TIME_DEFAULT);
833 ** MCI_READ_PERSISTENT -- read persistent host status file
836 ** fp -- the file pointer to read.
837 ** mci -- the pointer to fill in.
840 ** -1 -- if the file was corrupt.
844 ** This code makes the assumption that this data
845 ** will be read in an atomic fashion, and that the data
846 ** was written in an atomic fashion. Any other functioning
847 ** may lead to some form of insanity. This should be
848 ** perfectly safe due to underlying stdio buffering.
852 mci_read_persistent(fp, mci)
858 int saveLineNumber = LineNumber;
862 syserr("mci_read_persistent: NULL fp");
864 syserr("mci_read_persistent: NULL mci");
867 sm_dprintf("mci_read_persistent: fp=%lx, mci=",
871 SM_FREE_CLR(mci->mci_status);
872 SM_FREE_CLR(mci->mci_rstatus);
874 sm_io_rewind(fp, SM_TIME_DEFAULT);
877 while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof buf) != NULL)
880 p = strchr(buf, '\n');
885 case 'V': /* version stamp */
887 if (ver < 0 || ver > 0)
888 syserr("Unknown host status version %d: %d max",
892 case 'E': /* UNIX error number */
893 mci->mci_errno = atoi(&buf[1]);
896 case 'H': /* DNS error number */
897 mci->mci_herrno = atoi(&buf[1]);
900 case 'S': /* UNIX exit status */
901 mci->mci_exitstat = atoi(&buf[1]);
904 case 'D': /* DSN status */
905 mci->mci_status = newstr(&buf[1]);
908 case 'R': /* SMTP status */
909 mci->mci_rstatus = newstr(&buf[1]);
912 case 'U': /* last usage time */
913 mci->mci_lastuse = atol(&buf[1]);
916 case '.': /* end of file */
918 mci_dump(mci, false);
922 sm_syslog(LOG_CRIT, NOQID,
923 "%s: line %d: Unknown host status line \"%s\"",
924 FileName == NULL ? mci->mci_host : FileName,
926 LineNumber = saveLineNumber;
930 LineNumber = saveLineNumber;
932 sm_dprintf("incomplete (missing dot for EOF)\n");
938 ** MCI_STORE_PERSISTENT -- Store persistent MCI information
940 ** Store information about host that is kept
941 ** in common for all running sendmails.
944 ** mci -- the host/connection to store persistent info for.
951 mci_store_persistent(mci)
954 int save_errno = errno;
959 sm_dprintf("mci_store_persistent: NULL mci\n");
963 if (HostStatDir == NULL || mci->mci_host == NULL)
967 sm_dprintf("mci_store_persistent: Storing information for %s\n",
970 if (mci->mci_statfile == NULL)
973 sm_dprintf("mci_store_persistent: no statfile\n");
977 sm_io_rewind(mci->mci_statfile, SM_TIME_DEFAULT);
979 (void) ftruncate(sm_io_getinfo(mci->mci_statfile, SM_IO_WHAT_FD, NULL),
981 #endif /* !NOFTRUNCATE */
983 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "V0\n");
984 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "E%d\n",
986 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "H%d\n",
988 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "S%d\n",
990 if (mci->mci_status != NULL)
991 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT,
993 denlstring(mci->mci_status, true, false));
994 if (mci->mci_rstatus != NULL)
995 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT,
997 denlstring(mci->mci_rstatus, true, false));
998 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "U%ld\n",
999 (long)(mci->mci_lastuse));
1000 (void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, ".\n");
1002 (void) sm_io_flush(mci->mci_statfile, SM_TIME_DEFAULT);
1008 ** MCI_TRAVERSE_PERSISTENT -- walk persistent status tree
1010 ** Recursively find all the mci host files in `pathname'. Default to
1011 ** main host status directory if no path is provided.
1012 ** Call (*action)(pathname, host) for each file found.
1014 ** Note: all information is collected in a list before it is processed.
1015 ** This may not be the best way to do it, but it seems safest, since
1016 ** the file system would be touched while we are attempting to traverse
1017 ** the directory tree otherwise (during purges).
1020 ** action -- function to call on each node. If returns < 0,
1021 ** return immediately.
1022 ** pathname -- root of tree. If null, use main host status
1026 ** < 0 -- if any action routine returns a negative value, that
1027 ** value is returned.
1028 ** 0 -- if we successfully went to completion.
1029 ** > 0 -- return status from action()
1033 mci_traverse_persistent(action, pathname)
1037 struct stat statbuf;
1041 if (pathname == NULL)
1042 pathname = HostStatDir;
1043 if (pathname == NULL)
1047 sm_dprintf("mci_traverse: pathname is %s\n", pathname);
1049 ret = stat(pathname, &statbuf);
1053 sm_dprintf("mci_traverse: Failed to stat %s: %s\n",
1054 pathname, sm_errstring(errno));
1057 if (S_ISDIR(statbuf.st_mode))
1059 bool leftone, removedone;
1063 char newpath[MAXPATHLEN];
1065 if ((d = opendir(pathname)) == NULL)
1068 sm_dprintf("mci_traverse: opendir %s: %s\n",
1069 pathname, sm_errstring(errno));
1072 len = sizeof(newpath) - MAXNAMLEN - 3;
1073 if (sm_strlcpy(newpath, pathname, len) >= len)
1076 sm_dprintf("mci_traverse: path \"%s\" too long",
1080 newptr = newpath + strlen(newpath);
1084 ** repeat until no file has been removed
1085 ** this may become ugly when several files "expire"
1086 ** during these loops, but it's better than doing
1087 ** a rewinddir() inside the inner loop
1092 leftone = removedone = false;
1093 while ((e = readdir(d)) != NULL)
1095 if (e->d_name[0] == '.')
1098 (void) sm_strlcpy(newptr, e->d_name,
1100 (newptr - newpath));
1104 ret = mci_traverse_persistent(action, newpath);
1109 if (!removedone && ret == 0 &&
1110 action == mci_purge_persistent)
1117 ** The following appears to be
1118 ** necessary during purges, since
1119 ** we modify the directory structure
1125 sm_dprintf("mci_traverse: path %s: ret %d removed %d left %d\n",
1126 pathname, ret, removedone, leftone);
1127 } while (removedone);
1129 /* purge (or whatever) the directory proper */
1133 ret = (*action)(newpath, NULL);
1137 else if (S_ISREG(statbuf.st_mode))
1139 char *end = pathname + strlen(pathname) - 1;
1142 char host[MAXHOSTNAMELEN];
1143 char *hostptr = host;
1146 ** Reconstruct the host name from the path to the
1147 ** persistent information.
1152 if (hostptr != host)
1155 while (*(start - 1) != '/')
1161 for (scan = start; scan <= end; scan++)
1162 *(hostptr++) = *scan;
1165 } while (*end == '.');
1170 ** Do something with the file containing the persistent
1174 ret = (*action)(pathname, host);
1180 ** MCI_PRINT_PERSISTENT -- print persistent info
1182 ** Dump the persistent information in the file 'pathname'
1185 ** pathname -- the pathname to the status file.
1186 ** hostname -- the corresponding host name.
1193 mci_print_persistent(pathname, hostname)
1197 static bool initflag = false;
1199 int width = Verbose ? 78 : 25;
1203 /* skip directories */
1204 if (hostname == NULL)
1213 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1214 " -------------- Hostname --------------- How long ago ---------Results---------\n");
1217 fp = safefopen(pathname, O_RDONLY, FileMode,
1218 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
1223 sm_dprintf("mci_print_persistent: cannot open %s: %s\n",
1224 pathname, sm_errstring(errno));
1228 FileName = pathname;
1229 memset(&mcib, '\0', sizeof mcib);
1230 if (mci_read_persistent(fp, &mcib) < 0)
1232 syserr("%s: could not read status file", pathname);
1233 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1238 locked = !lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), pathname,
1239 "", LOCK_SH|LOCK_NB);
1240 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1243 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%c%-39s %12s ",
1244 locked ? '*' : ' ', hostname,
1245 pintvl(curtime() - mcib.mci_lastuse, true));
1246 if (mcib.mci_rstatus != NULL)
1247 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n", width,
1249 else if (mcib.mci_exitstat == EX_TEMPFAIL && mcib.mci_errno != 0)
1250 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1251 "Deferred: %.*s\n", width - 10,
1252 sm_errstring(mcib.mci_errno));
1253 else if (mcib.mci_exitstat != 0)
1255 char *exmsg = sm_sysexmsg(mcib.mci_exitstat);
1261 (void) sm_snprintf(buf, sizeof buf,
1262 "Unknown mailer error %d",
1264 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n",
1268 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n",
1271 else if (mcib.mci_errno == 0)
1272 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK\n");
1274 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK: %.*s\n",
1275 width - 4, sm_errstring(mcib.mci_errno));
1280 ** MCI_PURGE_PERSISTENT -- Remove a persistence status file.
1283 ** pathname -- path to the status file.
1284 ** hostname -- name of host corresponding to that file.
1285 ** NULL if this is a directory (domain).
1289 ** 1 -- file not deleted (too young, incorrect format)
1290 ** < 0 -- some error occurred
1294 mci_purge_persistent(pathname, hostname)
1298 struct stat statbuf;
1299 char *end = pathname + strlen(pathname) - 1;
1303 sm_dprintf("mci_purge_persistent: purging %s\n", pathname);
1305 ret = stat(pathname, &statbuf);
1309 sm_dprintf("mci_purge_persistent: Failed to stat %s: %s\n",
1310 pathname, sm_errstring(errno));
1313 if (curtime() - statbuf.st_mtime <= MciInfoTimeout)
1315 if (hostname != NULL)
1317 /* remove the file */
1318 ret = unlink(pathname);
1322 sm_syslog(LOG_ERR, NOQID,
1323 "mci_purge_persistent: failed to unlink %s: %s",
1324 pathname, sm_errstring(errno));
1326 sm_dprintf("mci_purge_persistent: failed to unlink %s: %s\n",
1327 pathname, sm_errstring(errno));
1333 /* remove the directory */
1338 sm_dprintf("mci_purge_persistent: dpurge %s\n", pathname);
1340 ret = rmdir(pathname);
1344 sm_dprintf("mci_purge_persistent: rmdir %s: %s\n",
1345 pathname, sm_errstring(errno));
1353 ** MCI_GENERATE_PERSISTENT_PATH -- generate path from hostname
1355 ** Given `host', convert from a.b.c to $QueueDir/.hoststat/c./b./a,
1356 ** putting the result into `path'. if `createflag' is set, intervening
1357 ** directories will be created as needed.
1360 ** host -- host name to convert from.
1361 ** path -- place to store result.
1362 ** pathlen -- length of path buffer.
1363 ** createflag -- if set, create intervening directories as
1372 mci_generate_persistent_path(host, path, pathlen, createflag)
1378 char *elem, *p, *x, ch;
1381 char t_host[MAXHOSTNAMELEN];
1383 struct in6_addr in6_addr;
1384 #endif /* NETINET6 */
1387 ** Rationality check the arguments.
1392 syserr("mci_generate_persistent_path: null host");
1397 syserr("mci_generate_persistent_path: null path");
1402 sm_dprintf("mci_generate_persistent_path(%s): ", host);
1404 if (*host == '\0' || *host == '.')
1407 /* make certain this is not a bracketed host number */
1408 if (strlen(host) > sizeof t_host - 1)
1411 (void) sm_strlcpy(t_host, host + 1, sizeof t_host);
1413 (void) sm_strlcpy(t_host, host, sizeof t_host);
1416 ** Delete any trailing dots from the hostname.
1417 ** Leave 'elem' pointing at the \0.
1420 elem = t_host + strlen(t_host);
1421 while (elem > t_host &&
1422 (elem[-1] == '.' || (host[0] == '[' && elem[-1] == ']')))
1425 /* check for bogus bracketed address */
1430 if (anynet_pton(AF_INET6, t_host, &in6_addr) == 1)
1432 # endif /* NETINET6 */
1434 if (inet_addr(t_host) != INADDR_NONE)
1436 # endif /* NETINET */
1441 /* check for what will be the final length of the path */
1442 len = strlen(HostStatDir) + 2;
1443 for (p = (char *) t_host; *p != '\0'; p++)
1448 if (p[0] == '.' && p[1] == '.')
1451 if (len > pathlen || len < 1)
1453 (void) sm_strlcpy(path, HostStatDir, pathlen);
1454 p = path + strlen(path);
1455 while (elem > t_host)
1457 if (!path_is_dir(path, createflag))
1463 while (elem >= t_host && *elem != '.')
1467 while ((ch = *x++) != '\0' && ch != '.')
1469 if (isascii(ch) && isupper(ch))
1472 ch = ':'; /* / -> : */
1482 sm_dprintf("FAILURE %d\n", ret);
1484 sm_dprintf("SUCCESS %s\n", path);