2 * Copyright (c) 1998-2000 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.
15 static char id[] = "@(#)$Id: mci.c,v 8.133.10.3 2000/06/23 16:17:06 ca Exp $";
21 #if NETINET || NETINET6
22 # include <arpa/inet.h>
23 #endif /* NETINET || NETINET6 */
27 static int mci_generate_persistent_path __P((const char *, char *,
29 static bool mci_load_persistent __P((MCI *));
30 static void mci_uncache __P((MCI **, bool));
31 static int mci_lock_host_statfile __P((MCI *));
32 static int mci_read_persistent __P((FILE *, MCI *));
35 ** Mail Connection Information (MCI) Caching Module.
37 ** There are actually two separate things cached. The first is
38 ** the set of all open connections -- these are stored in a
39 ** (small) list. The second is stored in the symbol table; it
40 ** has the overall status for all hosts, whether or not there
41 ** is a connection open currently.
43 ** There should never be too many connections open (since this
44 ** could flood the socket table), nor should a connection be
45 ** allowed to sit idly for too long.
47 ** MaxMciCache is the maximum number of open connections that
50 ** MciCacheTimeout is the time (in seconds) that a connection
51 ** is permitted to survive without activity.
53 ** We actually try any cached connections by sending a NOOP
54 ** before we use them; if the NOOP fails we close down the
55 ** connection and reopen it. Note that this means that a
56 ** server SMTP that doesn't support NOOP will hose the
57 ** algorithm -- but that doesn't seem too likely.
59 ** The persistent MCI code is donated by Mark Lovell and Paul
60 ** Vixie. It is based on the long term host status code in KJS
61 ** written by Paul but has been adapted by Mark to fit into the
65 static MCI **MciCache; /* the open connection cache */
68 ** MCI_CACHE -- enter a connection structure into the open connection cache
70 ** This may cause something else to be flushed.
73 ** mci -- the connection to cache.
83 register MCI **mcislot;
86 ** Find the best slot. This may cause expired connections
90 mcislot = mci_scan(mci);
93 /* we don't support caching */
97 if (mci->mci_host == NULL)
100 /* if this is already cached, we are done */
101 if (bitset(MCIF_CACHED, mci->mci_flags))
104 /* otherwise we may have to clear the slot */
105 if (*mcislot != NULL)
106 mci_uncache(mcislot, TRUE);
109 dprintf("mci_cache: caching %lx (%s) in slot %d\n",
110 (u_long) mci, mci->mci_host,
111 (int)(mcislot - MciCache));
113 sm_syslog(LOG_DEBUG, CurEnv->e_id,
114 "mci_cache: caching %lx (%.100s) in slot %d",
115 (u_long) mci, mci->mci_host, mcislot - MciCache);
118 mci->mci_flags |= MCIF_CACHED;
121 ** MCI_SCAN -- scan the cache, flush junk, and return best slot
124 ** savemci -- never flush this one. Can be null.
127 ** The LRU (or empty) slot.
135 register MCI **bestmci;
139 if (MaxMciCache <= 0)
141 /* we don't support caching */
145 if (MciCache == NULL)
148 MciCache = (MCI **) xalloc(MaxMciCache * sizeof *MciCache);
149 memset((char *) MciCache, '\0', MaxMciCache * sizeof *MciCache);
154 bestmci = &MciCache[0];
155 for (i = 0; i < MaxMciCache; i++)
158 if (mci == NULL || mci->mci_state == MCIS_CLOSED)
160 bestmci = &MciCache[i];
163 if ((mci->mci_lastuse + MciCacheTimeout < now ||
164 (mci->mci_mailer != NULL &&
165 mci->mci_mailer->m_maxdeliveries > 0 &&
166 mci->mci_deliveries + 1 >= mci->mci_mailer->m_maxdeliveries))&&
169 /* connection idle too long or too many deliveries */
170 bestmci = &MciCache[i];
173 mci_uncache(bestmci, TRUE);
176 if (*bestmci == NULL)
178 if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
179 bestmci = &MciCache[i];
184 ** MCI_UNCACHE -- remove a connection from a slot.
186 ** May close a connection.
189 ** mcislot -- the slot to empty.
190 ** doquit -- if TRUE, send QUIT protocol on this connection.
191 ** if FALSE, we are assumed to be in a forked child;
192 ** all we want to do is close the file(s).
199 mci_uncache(mcislot, doquit)
200 register MCI **mcislot;
204 extern ENVELOPE BlankEnvelope;
210 if (mci->mci_host == NULL)
213 mci_unlock_host(mci);
216 dprintf("mci_uncache: uncaching %lx (%s) from slot %d (%d)\n",
217 (u_long) mci, mci->mci_host,
218 (int)(mcislot - MciCache), doquit);
220 sm_syslog(LOG_DEBUG, CurEnv->e_id,
221 "mci_uncache: uncaching %lx (%.100s) from slot %d (%d)",
222 (u_long) mci, mci->mci_host,
223 mcislot - MciCache, doquit);
225 mci->mci_deliveries = 0;
229 message("Closing connection to %s", mci->mci_host);
231 mci->mci_flags &= ~MCIF_CACHED;
233 /* only uses the envelope to flush the transcript file */
234 if (mci->mci_state != MCIS_CLOSED)
235 smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
237 xla_host_end(mci->mci_host);
243 if (mci->mci_in != NULL)
244 (void) fclose(mci->mci_in);
245 if (mci->mci_out != NULL)
246 (void) fclose(mci->mci_out);
247 mci->mci_in = mci->mci_out = NULL;
248 mci->mci_state = MCIS_CLOSED;
249 mci->mci_exitstat = EX_OK;
255 ** MCI_FLUSH -- flush the entire cache
258 ** doquit -- if TRUE, send QUIT protocol.
259 ** if FALSE, just close the connection.
260 ** allbut -- but leave this one open.
267 mci_flush(doquit, allbut)
273 if (MciCache == NULL)
276 for (i = 0; i < MaxMciCache; i++)
277 if (allbut != MciCache[i])
278 mci_uncache(&MciCache[i], doquit);
281 ** MCI_GET -- get information about a particular host
293 extern SOCKADDR CurHostAddr;
295 /* clear CurHostAddr so we don't get a bogus address with this name */
296 memset(&CurHostAddr, '\0', sizeof CurHostAddr);
299 /* clear out any expired connections */
300 (void) mci_scan(NULL);
303 syserr("negative mno %d (%s)", m->m_mno, m->m_name);
305 s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
309 ** We don't need to load the peristent data if we have data
310 ** already loaded in the cache.
313 if (mci->mci_host == NULL &&
314 (mci->mci_host = s->s_name) != NULL &&
315 !mci_load_persistent(mci))
318 dprintf("mci_get(%s %s): lock failed\n",
320 mci->mci_exitstat = EX_TEMPFAIL;
321 mci->mci_state = MCIS_CLOSED;
322 mci->mci_statfile = NULL;
328 dprintf("mci_get(%s %s): mci_state=%d, _flags=%lx, _exitstat=%d, _errno=%d\n",
329 host, m->m_name, mci->mci_state, mci->mci_flags,
330 mci->mci_exitstat, mci->mci_errno);
334 if (mci->mci_state == MCIS_OPEN)
336 /* poke the connection to see if it's still alive */
337 (void) smtpprobe(mci);
339 /* reset the stored state in the event of a timeout */
340 if (mci->mci_state != MCIS_OPEN)
343 mci->mci_exitstat = EX_OK;
344 mci->mci_state = MCIS_CLOSED;
349 /* get peer host address for logging reasons only */
350 /* (this should really be in the mci struct) */
351 SOCKADDR_LEN_T socklen = sizeof CurHostAddr;
353 (void) getpeername(fileno(mci->mci_in),
354 (struct sockaddr *) &CurHostAddr, &socklen);
359 if (mci->mci_state == MCIS_CLOSED)
361 time_t now = curtime();
363 /* if this info is stale, ignore it */
364 if (now > mci->mci_lastuse + MciInfoTimeout)
366 mci->mci_lastuse = now;
368 mci->mci_exitstat = EX_OK;
375 ** MCI_MATCH -- check connection cache for a particular host
388 s = stab(host, ST_MCI + m->m_mno, ST_FIND);
393 if (mci->mci_state == MCIS_OPEN)
398 ** MCI_SETSTAT -- set status codes in MCI structure.
401 ** mci -- the MCI structure to set.
402 ** xstat -- the exit status code.
403 ** dstat -- the DSN status code.
404 ** rstat -- the SMTP status code.
411 mci_setstat(mci, xstat, dstat, rstat)
417 /* protocol errors should never be interpreted as sticky */
418 if (xstat != EX_NOTSTICKY && xstat != EX_PROTOCOL)
419 mci->mci_exitstat = xstat;
421 mci->mci_status = dstat;
422 if (mci->mci_rstatus != NULL)
423 free(mci->mci_rstatus);
425 rstat = newstr(rstat);
426 mci->mci_rstatus = rstat;
429 ** MCI_DUMP -- dump the contents of an MCI structure.
432 ** mci -- the MCI structure to dump.
443 int mcif_bit; /* flag bit */
444 char *mcif_name; /* flag name */
446 static struct mcifbits MciFlags[] =
448 { MCIF_VALID, "VALID" },
449 { MCIF_TEMP, "TEMP" },
450 { MCIF_CACHED, "CACHED" },
451 { MCIF_ESMTP, "ESMTP" },
452 { MCIF_EXPN, "EXPN" },
453 { MCIF_SIZE, "SIZE" },
454 { MCIF_8BITMIME, "8BITMIME" },
455 { MCIF_7BIT, "7BIT" },
456 { MCIF_MULTSTAT, "MULTSTAT" },
457 { MCIF_INHEADER, "INHEADER" },
458 { MCIF_CVT8TO7, "CVT8TO7" },
460 { MCIF_8BITOK, "8BITOK" },
461 { MCIF_CVT7TO8, "CVT7TO8" },
462 { MCIF_INMIME, "INMIME" },
476 sep = logit ? " " : "\n\t";
478 snprintf(p, SPACELEFT(buf, p), "MCI@%lx: ", (u_long) mci);
482 snprintf(p, SPACELEFT(buf, p), "NULL");
485 snprintf(p, SPACELEFT(buf, p), "flags=%lx", mci->mci_flags);
487 if (mci->mci_flags != 0)
492 for (f = MciFlags; f->mcif_bit != 0; f++)
494 if (!bitset(f->mcif_bit, mci->mci_flags))
496 snprintf(p, SPACELEFT(buf, p), "%s,", f->mcif_name);
501 snprintf(p, SPACELEFT(buf, p),
502 ",%serrno=%d, herrno=%d, exitstat=%d, state=%d, pid=%d,%s",
503 sep, mci->mci_errno, mci->mci_herrno,
504 mci->mci_exitstat, mci->mci_state, (int) mci->mci_pid, sep);
506 snprintf(p, SPACELEFT(buf, p),
507 "maxsize=%ld, phase=%s, mailer=%s,%s",
509 mci->mci_phase == NULL ? "NULL" : mci->mci_phase,
510 mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name,
513 snprintf(p, SPACELEFT(buf, p),
514 "status=%s, rstatus=%s,%s",
515 mci->mci_status == NULL ? "NULL" : mci->mci_status,
516 mci->mci_rstatus == NULL ? "NULL" : mci->mci_rstatus,
519 snprintf(p, SPACELEFT(buf, p),
520 "host=%s, lastuse=%s",
521 mci->mci_host == NULL ? "NULL" : mci->mci_host,
522 ctime(&mci->mci_lastuse));
525 sm_syslog(LOG_DEBUG, CurEnv->e_id, "%.1000s", buf);
530 ** MCI_DUMP_ALL -- print the entire MCI cache
533 ** logit -- if set, log the result instead of printing
546 if (MciCache == NULL)
549 for (i = 0; i < MaxMciCache; i++)
550 mci_dump(MciCache[i], logit);
553 ** MCI_LOCK_HOST -- Lock host while sending.
555 ** If we are contacting a host, we'll need to
556 ** update the status information in the host status
557 ** file, and if we want to do that, we ought to have
558 ** locked it. This has the (according to some)
559 ** desirable effect of serializing connectivity with
560 ** remote hosts -- i.e.: one connection to a give
564 ** mci -- containing the host we want to lock.
567 ** EX_OK -- got the lock.
568 ** EX_TEMPFAIL -- didn't get the lock.
578 dprintf("mci_lock_host: NULL mci\n");
582 if (!SingleThreadDelivery)
585 return mci_lock_host_statfile(mci);
589 mci_lock_host_statfile(mci)
592 int save_errno = errno;
594 char fname[MAXPATHLEN + 1];
596 if (HostStatDir == NULL || mci->mci_host == NULL)
600 dprintf("mci_lock_host: attempting to lock %s\n",
603 if (mci_generate_persistent_path(mci->mci_host, fname, sizeof fname, TRUE) < 0)
605 /* of course this should never happen */
607 dprintf("mci_lock_host: Failed to generate host path for %s\n",
610 retVal = EX_TEMPFAIL;
614 mci->mci_statfile = safefopen(fname, O_RDWR, FileMode,
615 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH|SFF_CREAT);
617 if (mci->mci_statfile == NULL)
619 syserr("mci_lock_host: cannot create host lock file %s",
624 if (!lockfile(fileno(mci->mci_statfile), fname, "", LOCK_EX|LOCK_NB))
627 dprintf("mci_lock_host: couldn't get lock on %s\n",
629 (void) fclose(mci->mci_statfile);
630 mci->mci_statfile = NULL;
631 retVal = EX_TEMPFAIL;
635 if (tTd(56, 12) && mci->mci_statfile != NULL)
636 dprintf("mci_lock_host: Sanity check -- lock is good\n");
643 ** MCI_UNLOCK_HOST -- unlock host
645 ** Clean up the lock on a host, close the file, let
646 ** someone else use it.
659 int save_errno = errno;
664 dprintf("mci_unlock_host: NULL mci\n");
668 if (HostStatDir == NULL || mci->mci_host == NULL)
671 if (!SingleThreadDelivery && mci_lock_host_statfile(mci) == EX_TEMPFAIL)
674 dprintf("mci_unlock_host: stat file already locked\n");
679 dprintf("mci_unlock_host: store prior to unlock\n");
681 mci_store_persistent(mci);
684 if (mci->mci_statfile != NULL)
686 (void) fclose(mci->mci_statfile);
687 mci->mci_statfile = NULL;
693 ** MCI_LOAD_PERSISTENT -- load persistent host info
695 ** Load information about host that is kept
696 ** in common for all running sendmails.
699 ** mci -- the host/connection to load persistent info
703 ** TRUE -- lock was successful
704 ** FALSE -- lock failed
708 mci_load_persistent(mci)
711 int save_errno = errno;
714 char fname[MAXPATHLEN + 1];
719 dprintf("mci_load_persistent: NULL mci\n");
723 if (IgnoreHostStatus || HostStatDir == NULL || mci->mci_host == NULL)
726 /* Already have the persistent information in memory */
727 if (SingleThreadDelivery && mci->mci_statfile != NULL)
731 dprintf("mci_load_persistent: Attempting to load persistent information for %s\n",
734 if (mci_generate_persistent_path(mci->mci_host, fname, sizeof fname, FALSE) < 0)
736 /* Not much we can do if the file isn't there... */
738 dprintf("mci_load_persistent: Couldn't generate host path\n");
742 fp = safefopen(fname, O_RDONLY, FileMode,
743 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
746 /* I can't think of any reason this should ever happen */
748 dprintf("mci_load_persistent: open(%s): %s\n",
749 fname, errstring(errno));
754 locked = lockfile(fileno(fp), fname, "", LOCK_SH|LOCK_NB);
757 (void) mci_read_persistent(fp, mci);
758 (void) lockfile(fileno(fp), fname, "", LOCK_UN);
768 ** MCI_READ_PERSISTENT -- read persistent host status file
771 ** fp -- the file pointer to read.
772 ** mci -- the pointer to fill in.
775 ** -1 -- if the file was corrupt.
779 ** This code makes the assumption that this data
780 ** will be read in an atomic fashion, and that the data
781 ** was written in an atomic fashion. Any other functioning
782 ** may lead to some form of insanity. This should be
783 ** perfectly safe due to underlying stdio buffering.
787 mci_read_persistent(fp, mci)
793 int saveLineNumber = LineNumber;
797 syserr("mci_read_persistent: NULL fp");
799 syserr("mci_read_persistent: NULL mci");
802 dprintf("mci_read_persistent: fp=%lx, mci=", (u_long) fp);
803 mci_dump(mci, FALSE);
806 mci->mci_status = NULL;
807 if (mci->mci_rstatus != NULL)
808 free(mci->mci_rstatus);
809 mci->mci_rstatus = NULL;
814 while (fgets(buf, sizeof buf, fp) != NULL)
817 p = strchr(buf, '\n');
822 case 'V': /* version stamp */
824 if (ver < 0 || ver > 0)
825 syserr("Unknown host status version %d: %d max",
829 case 'E': /* UNIX error number */
830 mci->mci_errno = atoi(&buf[1]);
833 case 'H': /* DNS error number */
834 mci->mci_herrno = atoi(&buf[1]);
837 case 'S': /* UNIX exit status */
838 mci->mci_exitstat = atoi(&buf[1]);
841 case 'D': /* DSN status */
842 mci->mci_status = newstr(&buf[1]);
845 case 'R': /* SMTP status */
846 mci->mci_rstatus = newstr(&buf[1]);
849 case 'U': /* last usage time */
850 mci->mci_lastuse = atol(&buf[1]);
853 case '.': /* end of file */
857 sm_syslog(LOG_CRIT, NOQID,
858 "%s: line %d: Unknown host status line \"%s\"",
859 FileName == NULL ? mci->mci_host : FileName,
861 LineNumber = saveLineNumber;
865 LineNumber = saveLineNumber;
871 ** MCI_STORE_PERSISTENT -- Store persistent MCI information
873 ** Store information about host that is kept
874 ** in common for all running sendmails.
877 ** mci -- the host/connection to store persistent info for.
884 mci_store_persistent(mci)
887 int save_errno = errno;
892 dprintf("mci_store_persistent: NULL mci\n");
896 if (HostStatDir == NULL || mci->mci_host == NULL)
900 dprintf("mci_store_persistent: Storing information for %s\n",
903 if (mci->mci_statfile == NULL)
906 dprintf("mci_store_persistent: no statfile\n");
910 rewind(mci->mci_statfile);
912 (void) ftruncate(fileno(mci->mci_statfile), (off_t) 0);
913 #endif /* !NOFTRUNCATE */
915 fprintf(mci->mci_statfile, "V0\n");
916 fprintf(mci->mci_statfile, "E%d\n", mci->mci_errno);
917 fprintf(mci->mci_statfile, "H%d\n", mci->mci_herrno);
918 fprintf(mci->mci_statfile, "S%d\n", mci->mci_exitstat);
919 if (mci->mci_status != NULL)
920 fprintf(mci->mci_statfile, "D%.80s\n",
921 denlstring(mci->mci_status, TRUE, FALSE));
922 if (mci->mci_rstatus != NULL)
923 fprintf(mci->mci_statfile, "R%.80s\n",
924 denlstring(mci->mci_rstatus, TRUE, FALSE));
925 fprintf(mci->mci_statfile, "U%ld\n", (long)(mci->mci_lastuse));
926 fprintf(mci->mci_statfile, ".\n");
928 (void) fflush(mci->mci_statfile);
934 ** MCI_TRAVERSE_PERSISTENT -- walk persistent status tree
936 ** Recursively find all the mci host files in `pathname'. Default to
937 ** main host status directory if no path is provided.
938 ** Call (*action)(pathname, host) for each file found.
940 ** Note: all information is collected in a list before it is processed.
941 ** This may not be the best way to do it, but it seems safest, since
942 ** the file system would be touched while we are attempting to traverse
943 ** the directory tree otherwise (during purges).
946 ** action -- function to call on each node. If returns < 0,
947 ** return immediately.
948 ** pathname -- root of tree. If null, use main host status
952 ** < 0 -- if any action routine returns a negative value, that
953 ** value is returned.
954 ** 0 -- if we successfully went to completion.
955 ** > 0 -- return status from action()
959 mci_traverse_persistent(action, pathname)
967 if (pathname == NULL)
968 pathname = HostStatDir;
969 if (pathname == NULL)
973 dprintf("mci_traverse: pathname is %s\n", pathname);
975 ret = stat(pathname, &statbuf);
979 dprintf("mci_traverse: Failed to stat %s: %s\n",
980 pathname, errstring(errno));
983 if (S_ISDIR(statbuf.st_mode))
987 char newpath[MAXPATHLEN + 1];
988 bool leftone, removedone;
990 if ((d = opendir(pathname)) == NULL)
993 dprintf("mci_traverse: opendir %s: %s\n",
994 pathname, errstring(errno));
998 if (strlen(pathname) >= sizeof newpath - MAXNAMLEN - 3)
1001 dprintf("mci_traverse: path \"%s\" too long",
1005 (void) strlcpy(newpath, pathname, sizeof newpath);
1006 newptr = newpath + strlen(newpath);
1010 ** repeat until no file has been removed
1011 ** this may become ugly when several files "expire"
1012 ** during these loops, but it's better than doing
1013 ** a rewinddir() inside the inner loop
1017 leftone = removedone = FALSE;
1018 while ((e = readdir(d)) != NULL)
1020 if (e->d_name[0] == '.')
1023 (void) strlcpy(newptr, e->d_name,
1025 (newptr - newpath));
1027 ret = mci_traverse_persistent(action, newpath);
1032 if (!removedone && ret == 0 &&
1033 action == mci_purge_persistent)
1039 ** The following appears to be
1040 ** necessary during purges, since
1041 ** we modify the directory structure
1046 dprintf("mci_traverse: path %s: ret %d removed %d left %d\n",
1047 pathname, ret, removedone, leftone);
1048 } while (removedone);
1050 /* purge (or whatever) the directory proper */
1054 ret = (*action)(newpath, NULL);
1058 else if (S_ISREG(statbuf.st_mode))
1060 char *end = pathname + strlen(pathname) - 1;
1063 char host[MAXHOSTNAMELEN];
1064 char *hostptr = host;
1067 ** Reconstruct the host name from the path to the
1068 ** persistent information.
1073 if (hostptr != host)
1076 while (*(start - 1) != '/')
1082 for (scan = start; scan <= end; scan++)
1083 *(hostptr++) = *scan;
1086 } while (*end == '.');
1091 ** Do something with the file containing the persistent
1094 ret = (*action)(pathname, host);
1100 ** MCI_PRINT_PERSISTENT -- print persistent info
1102 ** Dump the persistent information in the file 'pathname'
1105 ** pathname -- the pathname to the status file.
1106 ** hostname -- the corresponding host name.
1113 mci_print_persistent(pathname, hostname)
1117 static int initflag = FALSE;
1119 int width = Verbose ? 78 : 25;
1123 /* skip directories */
1124 if (hostname == NULL)
1130 printf(" -------------- Hostname --------------- How long ago ---------Results---------\n");
1133 fp = safefopen(pathname, O_RDWR, FileMode,
1134 SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
1139 dprintf("mci_print_persistent: cannot open %s: %s\n",
1140 pathname, errstring(errno));
1144 FileName = pathname;
1145 memset(&mcib, '\0', sizeof mcib);
1146 if (mci_read_persistent(fp, &mcib) < 0)
1148 syserr("%s: could not read status file", pathname);
1154 locked = !lockfile(fileno(fp), pathname, "", LOCK_EX|LOCK_NB);
1158 printf("%c%-39s %12s ",
1159 locked ? '*' : ' ', hostname,
1160 pintvl(curtime() - mcib.mci_lastuse, TRUE));
1161 if (mcib.mci_rstatus != NULL)
1162 printf("%.*s\n", width, mcib.mci_rstatus);
1163 else if (mcib.mci_exitstat == EX_TEMPFAIL && mcib.mci_errno != 0)
1164 printf("Deferred: %.*s\n", width - 10, errstring(mcib.mci_errno));
1165 else if (mcib.mci_exitstat != 0)
1167 int i = mcib.mci_exitstat - EX__BASE;
1169 extern char *SysExMsg[];
1171 if (i < 0 || i >= N_SysEx)
1175 snprintf(buf, sizeof buf, "Unknown mailer error %d",
1177 printf("%.*s\n", width, buf);
1180 printf("%.*s\n", width, &(SysExMsg[i])[5]);
1182 else if (mcib.mci_errno == 0)
1185 printf("OK: %.*s\n", width - 4, errstring(mcib.mci_errno));
1190 ** MCI_PURGE_PERSISTENT -- Remove a persistence status file.
1193 ** pathname -- path to the status file.
1194 ** hostname -- name of host corresponding to that file.
1195 ** NULL if this is a directory (domain).
1199 ** 1 -- file not deleted (too young, incorrect format)
1200 ** < 0 -- some error occurred
1204 mci_purge_persistent(pathname, hostname)
1208 struct stat statbuf;
1209 char *end = pathname + strlen(pathname) - 1;
1213 dprintf("mci_purge_persistent: purging %s\n", pathname);
1215 ret = stat(pathname, &statbuf);
1219 dprintf("mci_purge_persistent: Failed to stat %s: %s\n",
1220 pathname, errstring(errno));
1223 if (curtime() - statbuf.st_mtime < MciInfoTimeout)
1225 if (hostname != NULL)
1227 /* remove the file */
1228 if (unlink(pathname) < 0)
1231 dprintf("mci_purge_persistent: failed to unlink %s: %s\n",
1232 pathname, errstring(errno));
1237 /* remove the directory */
1242 dprintf("mci_purge_persistent: dpurge %s\n", pathname);
1244 if (rmdir(pathname) < 0)
1247 dprintf("mci_purge_persistent: rmdir %s: %s\n",
1248 pathname, errstring(errno));
1256 ** MCI_GENERATE_PERSISTENT_PATH -- generate path from hostname
1258 ** Given `host', convert from a.b.c to $QueueDir/.hoststat/c./b./a,
1259 ** putting the result into `path'. if `createflag' is set, intervening
1260 ** directories will be created as needed.
1263 ** host -- host name to convert from.
1264 ** path -- place to store result.
1265 ** pathlen -- length of path buffer.
1266 ** createflag -- if set, create intervening directories as
1275 mci_generate_persistent_path(host, path, pathlen, createflag)
1281 char *elem, *p, *x, ch;
1284 char t_host[MAXHOSTNAMELEN];
1286 struct in6_addr in6_addr;
1287 #endif /* NETINET6 */
1290 ** Rationality check the arguments.
1295 syserr("mci_generate_persistent_path: null host");
1300 syserr("mci_generate_persistent_path: null path");
1305 dprintf("mci_generate_persistent_path(%s): ", host);
1307 if (*host == '\0' || *host == '.')
1310 /* make certain this is not a bracketed host number */
1311 if (strlen(host) > sizeof t_host - 1)
1314 (void) strlcpy(t_host, host + 1, sizeof t_host);
1316 (void) strlcpy(t_host, host, sizeof t_host);
1319 ** Delete any trailing dots from the hostname.
1320 ** Leave 'elem' pointing at the \0.
1323 elem = t_host + strlen(t_host);
1324 while (elem > t_host &&
1325 (elem[-1] == '.' || (host[0] == '[' && elem[-1] == ']')))
1328 #if NETINET || NETINET6
1329 /* check for bogus bracketed address */
1330 if (host[0] == '[' &&
1332 inet_pton(AF_INET6, t_host, &in6_addr) != 1 &&
1333 # endif /* NETINET6 */
1335 inet_addr(t_host) == INADDR_NONE
1336 # endif /* NETINET */
1339 #endif /* NETINET || NETINET6 */
1341 /* check for what will be the final length of the path */
1342 len = strlen(HostStatDir) + 2;
1343 for (p = (char *) t_host; *p != '\0'; p++)
1348 if (p[0] == '.' && p[1] == '.')
1351 if (len > pathlen || len < 1)
1354 (void) strlcpy(path, HostStatDir, pathlen);
1355 p = path + strlen(path);
1357 while (elem > t_host)
1359 if (!path_is_dir(path, createflag))
1365 while (elem >= t_host && *elem != '.')
1369 while ((ch = *x++) != '\0' && ch != '.')
1371 if (isascii(ch) && isupper(ch))
1374 ch = ':'; /* / -> : */
1385 dprintf("FAILURE %d\n", ret);
1387 dprintf("SUCCESS %s\n", path);