1 /*
   2  * Copyright (c) 1998-2009 Sendmail, Inc. and its suppliers.
   3  *      All rights reserved.
   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.
   7  *
   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.
  11  *
  12  */
  13 
  14 #include <sendmail.h>
  15 #include <sm/sem.h>
  16 
  17 SM_RCSID("@(#)$Id: queue.c,v 8.987 2009/12/18 17:08:01 ca Exp $")
  18 
  19 #include <sys/types.h>
  20 #include <sys/mkdev.h>
  21 #include <dirent.h>
  22 
  23 # define RELEASE_QUEUE  (void) 0
  24 # define ST_INODE(st)   (st).st_ino
  25 
  26 #  define sm_file_exists(errno) ((errno) == EEXIST)
  27 
  28 # if HASFLOCK && defined(O_EXLOCK)
  29 #   define SM_OPEN_EXLOCK 1
  30 #   define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL|O_EXLOCK)
  31 # else /* HASFLOCK && defined(O_EXLOCK) */
  32 #  define TF_OPEN_FLAGS (O_CREAT|O_WRONLY|O_EXCL)
  33 # endif /* HASFLOCK && defined(O_EXLOCK) */
  34 
  35 #ifndef SM_OPEN_EXLOCK
  36 # define SM_OPEN_EXLOCK 0
  37 #endif /* ! SM_OPEN_EXLOCK */
  38 
  39 /*
  40 **  Historical notes:
  41 **      QF_VERSION == 4 was sendmail 8.10/8.11 without _FFR_QUEUEDELAY
  42 **      QF_VERSION == 5 was sendmail 8.10/8.11 with    _FFR_QUEUEDELAY
  43 **      QF_VERSION == 6 was sendmail 8.12      without _FFR_QUEUEDELAY
  44 **      QF_VERSION == 7 was sendmail 8.12      with    _FFR_QUEUEDELAY
  45 **      QF_VERSION == 8 is  sendmail 8.13
  46 */
  47 
  48 #define QF_VERSION      8       /* version number of this queue format */
  49 
  50 static char     queue_letter __P((ENVELOPE *, int));
  51 static bool     quarantine_queue_item __P((int, int, ENVELOPE *, char *));
  52 
  53 /* Naming convention: qgrp: index of queue group, qg: QUEUEGROUP */
  54 
  55 /*
  56 **  Work queue.
  57 */
  58 
  59 struct work
  60 {
  61         char            *w_name;        /* name of control file */
  62         char            *w_host;        /* name of recipient host */
  63         bool            w_lock;         /* is message locked? */
  64         bool            w_tooyoung;     /* is it too young to run? */
  65         long            w_pri;          /* priority of message, see below */
  66         time_t          w_ctime;        /* creation time */
  67         time_t          w_mtime;        /* modification time */
  68         int             w_qgrp;         /* queue group located in */
  69         int             w_qdir;         /* queue directory located in */
  70         struct work     *w_next;        /* next in queue */
  71 };
  72 
  73 typedef struct work     WORK;
  74 
  75 static WORK     *WorkQ;         /* queue of things to be done */
  76 static int      NumWorkGroups;  /* number of work groups */
  77 static time_t   Current_LA_time = 0;
  78 
  79 /* Get new load average every 30 seconds. */
  80 #define GET_NEW_LA_TIME 30
  81 
  82 #define SM_GET_LA(now)  \
  83         do                                                      \
  84         {                                                       \
  85                 now = curtime();                                \
  86                 if (Current_LA_time < now - GET_NEW_LA_TIME) \
  87                 {                                               \
  88                         sm_getla();                             \
  89                         Current_LA_time = now;                  \
  90                 }                                               \
  91         } while (0)
  92 
  93 /*
  94 **  DoQueueRun indicates that a queue run is needed.
  95 **      Notice: DoQueueRun is modified in a signal handler!
  96 */
  97 
  98 static bool     volatile DoQueueRun; /* non-interrupt time queue run needed */
  99 
 100 /*
 101 **  Work group definition structure.
 102 **      Each work group contains one or more queue groups. This is done
 103 **      to manage the number of queue group runners active at the same time
 104 **      to be within the constraints of MaxQueueChildren (if it is set).
 105 **      The number of queue groups that can be run on the next work run
 106 **      is kept track of. The queue groups are run in a round robin.
 107 */
 108 
 109 struct workgrp
 110 {
 111         int             wg_numqgrp;     /* number of queue groups in work grp */
 112         int             wg_runners;     /* total runners */
 113         int             wg_curqgrp;     /* current queue group */
 114         QUEUEGRP        **wg_qgs;       /* array of queue groups */
 115         int             wg_maxact;      /* max # of active runners */
 116         time_t          wg_lowqintvl;   /* lowest queue interval */
 117         int             wg_restart;     /* needs restarting? */
 118         int             wg_restartcnt;  /* count of times restarted */
 119 };
 120 
 121 typedef struct workgrp WORKGRP;
 122 
 123 static WORKGRP  volatile WorkGrp[MAXWORKGROUPS + 1];    /* work groups */
 124 
 125 #if SM_HEAP_CHECK
 126 static SM_DEBUG_T DebugLeakQ = SM_DEBUG_INITIALIZER("leak_q",
 127         "@(#)$Debug: leak_q - trace memory leaks during queue processing $");
 128 #endif /* SM_HEAP_CHECK */
 129 
 130 /*
 131 **  We use EmptyString instead of "" to avoid
 132 **  'zero-length format string' warnings from gcc
 133 */
 134 
 135 static const char EmptyString[] = "";
 136 
 137 static void     grow_wlist __P((int, int));
 138 static int      multiqueue_cache __P((char *, int, QUEUEGRP *, int, unsigned int *));
 139 static int      gatherq __P((int, int, bool, bool *, bool *, int *));
 140 static int      sortq __P((int));
 141 static void     printctladdr __P((ADDRESS *, SM_FILE_T *));
 142 static bool     readqf __P((ENVELOPE *, bool));
 143 static void     restart_work_group __P((int));
 144 static void     runner_work __P((ENVELOPE *, int, bool, int, int));
 145 static void     schedule_queue_runs __P((bool, int, bool));
 146 static char     *strrev __P((char *));
 147 static ADDRESS  *setctluser __P((char *, int, ENVELOPE *));
 148 #if _FFR_RHS
 149 static int      sm_strshufflecmp __P((char *, char *));
 150 static void     init_shuffle_alphabet __P(());
 151 #endif /* _FFR_RHS */
 152 
 153 /*
 154 **  Note: workcmpf?() don't use a prototype because it will cause a conflict
 155 **  with the qsort() call (which expects something like
 156 **  int (*compar)(const void *, const void *), not (WORK *, WORK *))
 157 */
 158 
 159 static int      workcmpf0();
 160 static int      workcmpf1();
 161 static int      workcmpf2();
 162 static int      workcmpf3();
 163 static int      workcmpf4();
 164 static int      randi = 3;      /* index for workcmpf5() */
 165 static int      workcmpf5();
 166 static int      workcmpf6();
 167 #if _FFR_RHS
 168 static int      workcmpf7();
 169 #endif /* _FFR_RHS */
 170 
 171 #if RANDOMSHIFT
 172 # define get_rand_mod(m)        ((get_random() >> RANDOMSHIFT) % (m))
 173 #else /* RANDOMSHIFT */
 174 # define get_rand_mod(m)        (get_random() % (m))
 175 #endif /* RANDOMSHIFT */
 176 
 177 /*
 178 **  File system definition.
 179 **      Used to keep track of how much free space is available
 180 **      on a file system in which one or more queue directories reside.
 181 */
 182 
 183 typedef struct filesys_shared   FILESYS;
 184 
 185 struct filesys_shared
 186 {
 187         dev_t   fs_dev;         /* unique device id */
 188         long    fs_avail;       /* number of free blocks available */
 189         long    fs_blksize;     /* block size, in bytes */
 190 };
 191 
 192 /* probably kept in shared memory */
 193 static FILESYS  FileSys[MAXFILESYS];    /* queue file systems */
 194 static const char *FSPath[MAXFILESYS];  /* pathnames for file systems */
 195 
 196 #if SM_CONF_SHM
 197 
 198 /*
 199 **  Shared memory data
 200 **
 201 **  Current layout:
 202 **      size -- size of shared memory segment
 203 **      pid -- pid of owner, should be a unique id to avoid misinterpretations
 204 **              by other processes.
 205 **      tag -- should be a unique id to avoid misinterpretations by others.
 206 **              idea: hash over configuration data that will be stored here.
 207 **      NumFileSys -- number of file systems.
 208 **      FileSys -- (arrary of) structure for used file systems.
 209 **      RSATmpCnt -- counter for number of uses of ephemeral RSA key.
 210 **      QShm -- (array of) structure for information about queue directories.
 211 */
 212 
 213 /*
 214 **  Queue data in shared memory
 215 */
 216 
 217 typedef struct queue_shared     QUEUE_SHM_T;
 218 
 219 struct queue_shared
 220 {
 221         int     qs_entries;     /* number of entries */
 222         /* XXX more to follow? */
 223 };
 224 
 225 static void     *Pshm;          /* pointer to shared memory */
 226 static FILESYS  *PtrFileSys;    /* pointer to queue file system array */
 227 int             ShmId = SM_SHM_NO_ID;   /* shared memory id */
 228 static QUEUE_SHM_T      *QShm;          /* pointer to shared queue data */
 229 static size_t shms;
 230 
 231 # define SHM_OFF_PID(p) (((char *) (p)) + sizeof(int))
 232 # define SHM_OFF_TAG(p) (((char *) (p)) + sizeof(pid_t) + sizeof(int))
 233 # define SHM_OFF_HEAD   (sizeof(pid_t) + sizeof(int) * 2)
 234 
 235 /* how to access FileSys */
 236 # define FILE_SYS(i)    (PtrFileSys[i])
 237 
 238 /* first entry is a tag, for now just the size */
 239 # define OFF_FILE_SYS(p)        (((char *) (p)) + SHM_OFF_HEAD)
 240 
 241 /* offset for PNumFileSys */
 242 # define OFF_NUM_FILE_SYS(p)    (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys))
 243 
 244 /* offset for PRSATmpCnt */
 245 # define OFF_RSA_TMP_CNT(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int))
 246 int     *PRSATmpCnt;
 247 
 248 /* offset for queue_shm */
 249 # define OFF_QUEUE_SHM(p) (((char *) (p)) + SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
 250 
 251 # define QSHM_ENTRIES(i)        QShm[i].qs_entries
 252 
 253 /* basic size of shared memory segment */
 254 # define SM_T_SIZE      (SHM_OFF_HEAD + sizeof(FileSys) + sizeof(int) * 2)
 255 
 256 static unsigned int     hash_q __P((char *, unsigned int));
 257 
 258 /*
 259 **  HASH_Q -- simple hash function
 260 **
 261 **      Parameters:
 262 **              p -- string to hash.
 263 **              h -- hash start value (from previous run).
 264 **
 265 **      Returns:
 266 **              hash value.
 267 */
 268 
 269 static unsigned int
 270 hash_q(p, h)
 271         char *p;
 272         unsigned int h;
 273 {
 274         int c, d;
 275 
 276         while (*p != '\0')
 277         {
 278                 d = *p++;
 279                 c = d;
 280                 c ^= c<<6;
 281                 h += (c<<11) ^ (c>>1);
 282                 h ^= (d<<14) + (d<<7) + (d<<4) + d;
 283         }
 284         return h;
 285 }
 286 
 287 
 288 #else /* SM_CONF_SHM */
 289 # define FILE_SYS(i)    FileSys[i]
 290 #endif /* SM_CONF_SHM */
 291 
 292 /* access to the various components of file system data */
 293 #define FILE_SYS_NAME(i)        FSPath[i]
 294 #define FILE_SYS_AVAIL(i)       FILE_SYS(i).fs_avail
 295 #define FILE_SYS_BLKSIZE(i)     FILE_SYS(i).fs_blksize
 296 #define FILE_SYS_DEV(i) FILE_SYS(i).fs_dev
 297 
 298 
 299 /*
 300 **  Current qf file field assignments:
 301 **
 302 **      A       AUTH= parameter
 303 **      B       body type
 304 **      C       controlling user
 305 **      D       data file name
 306 **      d       data file directory name (added in 8.12)
 307 **      E       error recipient
 308 **      F       flag bits
 309 **      G       free (was: queue delay algorithm if _FFR_QUEUEDELAY)
 310 **      H       header
 311 **      I       data file's inode number
 312 **      K       time of last delivery attempt
 313 **      L       Solaris Content-Length: header (obsolete)
 314 **      M       message
 315 **      N       number of delivery attempts
 316 **      P       message priority
 317 **      q       quarantine reason
 318 **      Q       original recipient (ORCPT=)
 319 **      r       final recipient (Final-Recipient: DSN field)
 320 **      R       recipient
 321 **      S       sender
 322 **      T       init time
 323 **      V       queue file version
 324 **      X       free (was: character set if _FFR_SAVE_CHARSET)
 325 **      Y       free (was: current delay if _FFR_QUEUEDELAY)
 326 **      Z       original envelope id from ESMTP
 327 **      !       deliver by (added in 8.12)
 328 **      $       define macro
 329 **      .       terminate file
 330 */
 331 
 332 /*
 333 **  QUEUEUP -- queue a message up for future transmission.
 334 **
 335 **      Parameters:
 336 **              e -- the envelope to queue up.
 337 **              announce -- if true, tell when you are queueing up.
 338 **              msync -- if true, then fsync() if SuperSafe interactive mode.
 339 **
 340 **      Returns:
 341 **              none.
 342 **
 343 **      Side Effects:
 344 **              The current request is saved in a control file.
 345 **              The queue file is left locked.
 346 */
 347 
 348 void
 349 queueup(e, announce, msync)
 350         register ENVELOPE *e;
 351         bool announce;
 352         bool msync;
 353 {
 354         register SM_FILE_T *tfp;
 355         register HDR *h;
 356         register ADDRESS *q;
 357         int tfd = -1;
 358         int i;
 359         bool newid;
 360         register char *p;
 361         MAILER nullmailer;
 362         MCI mcibuf;
 363         char qf[MAXPATHLEN];
 364         char tf[MAXPATHLEN];
 365         char df[MAXPATHLEN];
 366         char buf[MAXLINE];
 367 
 368         /*
 369         **  Create control file.
 370         */
 371 
 372 #define OPEN_TF do                                                      \
 373                 {                                                       \
 374                         MODE_T oldumask = 0;                            \
 375                                                                         \
 376                         if (bitset(S_IWGRP, QueueFileMode))             \
 377                                 oldumask = umask(002);                  \
 378                         tfd = open(tf, TF_OPEN_FLAGS, QueueFileMode);   \
 379                         if (bitset(S_IWGRP, QueueFileMode))             \
 380                                 (void) umask(oldumask);                 \
 381                 } while (0)
 382 
 383 
 384         newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
 385         (void) sm_strlcpy(tf, queuename(e, NEWQFL_LETTER), sizeof(tf));
 386         tfp = e->e_lockfp;
 387         if (tfp == NULL && newid)
 388         {
 389                 /*
 390                 **  open qf file directly: this will give an error if the file
 391                 **  already exists and hence prevent problems if a queue-id
 392                 **  is reused (e.g., because the clock is set back).
 393                 */
 394 
 395                 (void) sm_strlcpy(tf, queuename(e, ANYQFL_LETTER), sizeof(tf));
 396                 OPEN_TF;
 397                 if (tfd < 0 ||
 398 #if !SM_OPEN_EXLOCK
 399                     !lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB) ||
 400 #endif /* !SM_OPEN_EXLOCK */
 401                     (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
 402                                          (void *) &tfd, SM_IO_WRONLY,
 403                                          NULL)) == NULL)
 404                 {
 405                         int save_errno = errno;
 406 
 407                         printopenfds(true);
 408                         errno = save_errno;
 409                         syserr("!queueup: cannot create queue file %s, euid=%d, fd=%d, fp=%p",
 410                                 tf, (int) geteuid(), tfd, tfp);
 411                         /* NOTREACHED */
 412                 }
 413                 e->e_lockfp = tfp;
 414                 upd_qs(e, 1, 0, "queueup");
 415         }
 416 
 417         /* if newid, write the queue file directly (instead of temp file) */
 418         if (!newid)
 419         {
 420                 /* get a locked tf file */
 421                 for (i = 0; i < 128; i++)
 422                 {
 423                         if (tfd < 0)
 424                         {
 425                                 OPEN_TF;
 426                                 if (tfd < 0)
 427                                 {
 428                                         if (errno != EEXIST)
 429                                                 break;
 430                                         if (LogLevel > 0 && (i % 32) == 0)
 431                                                 sm_syslog(LOG_ALERT, e->e_id,
 432                                                           "queueup: cannot create %s, euid=%d: %s",
 433                                                           tf, (int) geteuid(),
 434                                                           sm_errstring(errno));
 435                                 }
 436 #if SM_OPEN_EXLOCK
 437                                 else
 438                                         break;
 439 #endif /* SM_OPEN_EXLOCK */
 440                         }
 441                         if (tfd >= 0)
 442                         {
 443 #if SM_OPEN_EXLOCK
 444                                 /* file is locked by open() */
 445                                 break;
 446 #else /* SM_OPEN_EXLOCK */
 447                                 if (lockfile(tfd, tf, NULL, LOCK_EX|LOCK_NB))
 448                                         break;
 449                                 else
 450 #endif /* SM_OPEN_EXLOCK */
 451                                 if (LogLevel > 0 && (i % 32) == 0)
 452                                         sm_syslog(LOG_ALERT, e->e_id,
 453                                                   "queueup: cannot lock %s: %s",
 454                                                   tf, sm_errstring(errno));
 455                                 if ((i % 32) == 31)
 456                                 {
 457                                         (void) close(tfd);
 458                                         tfd = -1;
 459                                 }
 460                         }
 461 
 462                         if ((i % 32) == 31)
 463                         {
 464                                 /* save the old temp file away */
 465                                 (void) rename(tf, queuename(e, TEMPQF_LETTER));
 466                         }
 467                         else
 468                                 (void) sleep(i % 32);
 469                 }
 470                 if (tfd < 0 || (tfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
 471                                                  (void *) &tfd, SM_IO_WRONLY_B,
 472                                                  NULL)) == NULL)
 473                 {
 474                         int save_errno = errno;
 475 
 476                         printopenfds(true);
 477                         errno = save_errno;
 478                         syserr("!queueup: cannot create queue temp file %s, uid=%d",
 479                                 tf, (int) geteuid());
 480                 }
 481         }
 482 
 483         if (tTd(40, 1))
 484                 sm_dprintf("\n>>>>> queueing %s/%s%s >>>>>\n",
 485                            qid_printqueue(e->e_qgrp, e->e_qdir),
 486                            queuename(e, ANYQFL_LETTER),
 487                            newid ? " (new id)" : "");
 488         if (tTd(40, 3))
 489         {
 490                 sm_dprintf("  e_flags=");
 491                 printenvflags(e);
 492         }
 493         if (tTd(40, 32))
 494         {
 495                 sm_dprintf("  sendq=");
 496                 printaddr(sm_debug_file(), e->e_sendqueue, true);
 497         }
 498         if (tTd(40, 9))
 499         {
 500                 sm_dprintf("  tfp=");
 501                 dumpfd(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL), true, false);
 502                 sm_dprintf("  lockfp=");
 503                 if (e->e_lockfp == NULL)
 504                         sm_dprintf("NULL\n");
 505                 else
 506                         dumpfd(sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL),
 507                                true, false);
 508         }
 509 
 510         /*
 511         **  If there is no data file yet, create one.
 512         */
 513 
 514         (void) sm_strlcpy(df, queuename(e, DATAFL_LETTER), sizeof(df));
 515         if (bitset(EF_HAS_DF, e->e_flags))
 516         {
 517                 if (e->e_dfp != NULL &&
 518                     SuperSafe != SAFE_REALLY &&
 519                     SuperSafe != SAFE_REALLY_POSTMILTER &&
 520                     sm_io_setinfo(e->e_dfp, SM_BF_COMMIT, NULL) < 0 &&
 521                     errno != EINVAL)
 522                 {
 523                         syserr("!queueup: cannot commit data file %s, uid=%d",
 524                                queuename(e, DATAFL_LETTER), (int) geteuid());
 525                 }
 526                 if (e->e_dfp != NULL &&
 527                     SuperSafe == SAFE_INTERACTIVE && msync)
 528                 {
 529                         if (tTd(40,32))
 530                                 sm_syslog(LOG_INFO, e->e_id,
 531                                           "queueup: fsync(e->e_dfp)");
 532 
 533                         if (fsync(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD,
 534                                                 NULL)) < 0)
 535                         {
 536                                 if (newid)
 537                                         syserr("!552 Error writing data file %s",
 538                                                df);
 539                                 else
 540                                         syserr("!452 Error writing data file %s",
 541                                                df);
 542                         }
 543                 }
 544         }
 545         else
 546         {
 547                 int dfd;
 548                 MODE_T oldumask = 0;
 549                 register SM_FILE_T *dfp = NULL;
 550                 struct stat stbuf;
 551 
 552                 if (e->e_dfp != NULL &&
 553                     sm_io_getinfo(e->e_dfp, SM_IO_WHAT_ISTYPE, BF_FILE_TYPE))
 554                         syserr("committing over bf file");
 555 
 556                 if (bitset(S_IWGRP, QueueFileMode))
 557                         oldumask = umask(002);
 558                 dfd = open(df, O_WRONLY|O_CREAT|O_TRUNC|QF_O_EXTRA,
 559                            QueueFileMode);
 560                 if (bitset(S_IWGRP, QueueFileMode))
 561                         (void) umask(oldumask);
 562                 if (dfd < 0 || (dfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
 563                                                  (void *) &dfd, SM_IO_WRONLY_B,
 564                                                  NULL)) == NULL)
 565                         syserr("!queueup: cannot create data temp file %s, uid=%d",
 566                                 df, (int) geteuid());
 567                 if (fstat(dfd, &stbuf) < 0)
 568                         e->e_dfino = -1;
 569                 else
 570                 {
 571                         e->e_dfdev = stbuf.st_dev;
 572                         e->e_dfino = ST_INODE(stbuf);
 573                 }
 574                 e->e_flags |= EF_HAS_DF;
 575                 memset(&mcibuf, '\0', sizeof(mcibuf));
 576                 mcibuf.mci_out = dfp;
 577                 mcibuf.mci_mailer = FileMailer;
 578                 (*e->e_putbody)(&mcibuf, e, NULL);
 579 
 580                 if (SuperSafe == SAFE_REALLY ||
 581                     SuperSafe == SAFE_REALLY_POSTMILTER ||
 582                     (SuperSafe == SAFE_INTERACTIVE && msync))
 583                 {
 584                         if (tTd(40,32))
 585                                 sm_syslog(LOG_INFO, e->e_id,
 586                                           "queueup: fsync(dfp)");
 587 
 588                         if (fsync(sm_io_getinfo(dfp, SM_IO_WHAT_FD, NULL)) < 0)
 589                         {
 590                                 if (newid)
 591                                         syserr("!552 Error writing data file %s",
 592                                                df);
 593                                 else
 594                                         syserr("!452 Error writing data file %s",
 595                                                df);
 596                         }
 597                 }
 598 
 599                 if (sm_io_close(dfp, SM_TIME_DEFAULT) < 0)
 600                         syserr("!queueup: cannot save data temp file %s, uid=%d",
 601                                 df, (int) geteuid());
 602                 e->e_putbody = putbody;
 603         }
 604 
 605         /*
 606         **  Output future work requests.
 607         **      Priority and creation time should be first, since
 608         **      they are required by gatherq.
 609         */
 610 
 611         /* output queue version number (must be first!) */
 612         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "V%d\n", QF_VERSION);
 613 
 614         /* output creation time */
 615         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "T%ld\n", (long) e->e_ctime);
 616 
 617         /* output last delivery time */
 618         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "K%ld\n", (long) e->e_dtime);
 619 
 620         /* output number of delivery attempts */
 621         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "N%d\n", e->e_ntries);
 622 
 623         /* output message priority */
 624         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "P%ld\n", e->e_msgpriority);
 625 
 626         /*
 627         **  If data file is in a different directory than the queue file,
 628         **  output a "d" record naming the directory of the data file.
 629         */
 630 
 631         if (e->e_dfqgrp != e->e_qgrp)
 632         {
 633                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "d%s\n",
 634                         Queue[e->e_dfqgrp]->qg_qpaths[e->e_dfqdir].qp_name);
 635         }
 636 
 637         /* output inode number of data file */
 638         /* XXX should probably include device major/minor too */
 639         if (e->e_dfino != -1)
 640         {
 641                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "I%ld/%ld/%llu\n",
 642                                      (long) major(e->e_dfdev),
 643                                      (long) minor(e->e_dfdev),
 644                                      (ULONGLONG_T) e->e_dfino);
 645         }
 646 
 647         /* output body type */
 648         if (e->e_bodytype != NULL)
 649                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "B%s\n",
 650                                      denlstring(e->e_bodytype, true, false));
 651 
 652         /* quarantine reason */
 653         if (e->e_quarmsg != NULL)
 654                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "q%s\n",
 655                                      denlstring(e->e_quarmsg, true, false));
 656 
 657         /* message from envelope, if it exists */
 658         if (e->e_message != NULL)
 659                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
 660                                      denlstring(e->e_message, true, false));
 661 
 662         /* send various flag bits through */
 663         p = buf;
 664         if (bitset(EF_WARNING, e->e_flags))
 665                 *p++ = 'w';
 666         if (bitset(EF_RESPONSE, e->e_flags))
 667                 *p++ = 'r';
 668         if (bitset(EF_HAS8BIT, e->e_flags))
 669                 *p++ = '8';
 670         if (bitset(EF_DELETE_BCC, e->e_flags))
 671                 *p++ = 'b';
 672         if (bitset(EF_RET_PARAM, e->e_flags))
 673                 *p++ = 'd';
 674         if (bitset(EF_NO_BODY_RETN, e->e_flags))
 675                 *p++ = 'n';
 676         if (bitset(EF_SPLIT, e->e_flags))
 677                 *p++ = 's';
 678         *p++ = '\0';
 679         if (buf[0] != '\0')
 680                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "F%s\n", buf);
 681 
 682         /* save $={persistentMacros} macro values */
 683         queueup_macros(macid("{persistentMacros}"), tfp, e);
 684 
 685         /* output name of sender */
 686         if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
 687                 p = e->e_sender;
 688         else
 689                 p = e->e_from.q_paddr;
 690         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "S%s\n",
 691                              denlstring(p, true, false));
 692 
 693         /* output ESMTP-supplied "original" information */
 694         if (e->e_envid != NULL)
 695                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Z%s\n",
 696                                      denlstring(e->e_envid, true, false));
 697 
 698         /* output AUTH= parameter */
 699         if (e->e_auth_param != NULL)
 700                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "A%s\n",
 701                                      denlstring(e->e_auth_param, true, false));
 702         if (e->e_dlvr_flag != 0)
 703                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "!%c %ld\n",
 704                                      (char) e->e_dlvr_flag, e->e_deliver_by);
 705 
 706         /* output list of recipient addresses */
 707         printctladdr(NULL, NULL);
 708         for (q = e->e_sendqueue; q != NULL; q = q->q_next)
 709         {
 710                 if (!QS_IS_UNDELIVERED(q->q_state))
 711                         continue;
 712 
 713                 /* message for this recipient, if it exists */
 714                 if (q->q_message != NULL)
 715                         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "M%s\n",
 716                                              denlstring(q->q_message, true,
 717                                                         false));
 718 
 719                 printctladdr(q, tfp);
 720                 if (q->q_orcpt != NULL)
 721                         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "Q%s\n",
 722                                              denlstring(q->q_orcpt, true,
 723                                                         false));
 724                 if (q->q_finalrcpt != NULL)
 725                         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "r%s\n",
 726                                              denlstring(q->q_finalrcpt, true,
 727                                                         false));
 728                 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'R');
 729                 if (bitset(QPRIMARY, q->q_flags))
 730                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'P');
 731                 if (bitset(QHASNOTIFY, q->q_flags))
 732                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'N');
 733                 if (bitset(QPINGONSUCCESS, q->q_flags))
 734                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'S');
 735                 if (bitset(QPINGONFAILURE, q->q_flags))
 736                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'F');
 737                 if (bitset(QPINGONDELAY, q->q_flags))
 738                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'D');
 739                 if (q->q_alias != NULL &&
 740                     bitset(QALIAS, q->q_alias->q_flags))
 741                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT, 'A');
 742                 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, ':');
 743                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s\n",
 744                                      denlstring(q->q_paddr, true, false));
 745                 if (announce)
 746                 {
 747                         char *tag = "queued";
 748 
 749                         if (e->e_quarmsg != NULL)
 750                                 tag = "quarantined";
 751 
 752                         e->e_to = q->q_paddr;
 753                         message(tag);
 754                         if (LogLevel > 8)
 755                                 logdelivery(q->q_mailer, NULL, q->q_status,
 756                                             tag, NULL, (time_t) 0, e);
 757                         e->e_to = NULL;
 758                 }
 759                 if (tTd(40, 1))
 760                 {
 761                         sm_dprintf("queueing ");
 762                         printaddr(sm_debug_file(), q, false);
 763                 }
 764         }
 765 
 766         /*
 767         **  Output headers for this message.
 768         **      Expand macros completely here.  Queue run will deal with
 769         **      everything as absolute headers.
 770         **              All headers that must be relative to the recipient
 771         **              can be cracked later.
 772         **      We set up a "null mailer" -- i.e., a mailer that will have
 773         **      no effect on the addresses as they are output.
 774         */
 775 
 776         memset((char *) &nullmailer, '\0', sizeof(nullmailer));
 777         nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
 778                         nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
 779         nullmailer.m_eol = "\n";
 780         memset(&mcibuf, '\0', sizeof(mcibuf));
 781         mcibuf.mci_mailer = &nullmailer;
 782         mcibuf.mci_out = tfp;
 783 
 784         macdefine(&e->e_macro, A_PERM, 'g', "\201f");
 785         for (h = e->e_header; h != NULL; h = h->h_link)
 786         {
 787                 if (h->h_value == NULL)
 788                         continue;
 789 
 790                 /* don't output resent headers on non-resent messages */
 791                 if (bitset(H_RESENT, h->h_flags) &&
 792                     !bitset(EF_RESENT, e->e_flags))
 793                         continue;
 794 
 795                 /* expand macros; if null, don't output header at all */
 796                 if (bitset(H_DEFAULT, h->h_flags))
 797                 {
 798                         (void) expand(h->h_value, buf, sizeof(buf), e);
 799                         if (buf[0] == '\0')
 800                                 continue;
 801                         if (buf[0] == ' ' && buf[1] == '\0')
 802                                 continue;
 803                 }
 804 
 805                 /* output this header */
 806                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "H?");
 807 
 808                 /* output conditional macro if present */
 809                 if (h->h_macro != '\0')
 810                 {
 811                         if (bitset(0200, h->h_macro))
 812                                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
 813                                                      "${%s}",
 814                                                       macname(bitidx(h->h_macro)));
 815                         else
 816                                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT,
 817                                                      "$%c", h->h_macro);
 818                 }
 819                 else if (!bitzerop(h->h_mflags) &&
 820                          bitset(H_CHECK|H_ACHECK, h->h_flags))
 821                 {
 822                         int j;
 823 
 824                         /* if conditional, output the set of conditions */
 825                         for (j = '\0'; j <= '\177'; j++)
 826                                 if (bitnset(j, h->h_mflags))
 827                                         (void) sm_io_putc(tfp, SM_TIME_DEFAULT,
 828                                                           j);
 829                 }
 830                 (void) sm_io_putc(tfp, SM_TIME_DEFAULT, '?');
 831 
 832                 /* output the header: expand macros, convert addresses */
 833                 if (bitset(H_DEFAULT, h->h_flags) &&
 834                     !bitset(H_BINDLATE, h->h_flags))
 835                 {
 836                         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
 837                                              h->h_field,
 838                                              denlstring(buf, false, true));
 839                 }
 840                 else if (bitset(H_FROM|H_RCPT, h->h_flags) &&
 841                          !bitset(H_BINDLATE, h->h_flags))
 842                 {
 843                         bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
 844                         SM_FILE_T *savetrace = TrafficLogFile;
 845 
 846                         TrafficLogFile = NULL;
 847 
 848                         if (bitset(H_FROM, h->h_flags))
 849                                 oldstyle = false;
 850                         commaize(h, h->h_value, oldstyle, &mcibuf, e,
 851                                  PXLF_HEADER);
 852 
 853                         TrafficLogFile = savetrace;
 854                 }
 855                 else
 856                 {
 857                         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "%s:%s\n",
 858                                              h->h_field,
 859                                              denlstring(h->h_value, false,
 860                                                         true));
 861                 }
 862         }
 863 
 864         /*
 865         **  Clean up.
 866         **
 867         **      Write a terminator record -- this is to prevent
 868         **      scurrilous crackers from appending any data.
 869         */
 870 
 871         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ".\n");
 872 
 873         if (sm_io_flush(tfp, SM_TIME_DEFAULT) != 0 ||
 874             ((SuperSafe == SAFE_REALLY ||
 875               SuperSafe == SAFE_REALLY_POSTMILTER ||
 876               (SuperSafe == SAFE_INTERACTIVE && msync)) &&
 877              fsync(sm_io_getinfo(tfp, SM_IO_WHAT_FD, NULL)) < 0) ||
 878             sm_io_error(tfp))
 879         {
 880                 if (newid)
 881                         syserr("!552 Error writing control file %s", tf);
 882                 else
 883                         syserr("!452 Error writing control file %s", tf);
 884         }
 885 
 886         if (!newid)
 887         {
 888                 char new = queue_letter(e, ANYQFL_LETTER);
 889 
 890                 /* rename (locked) tf to be (locked) [qh]f */
 891                 (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER),
 892                                   sizeof(qf));
 893                 if (rename(tf, qf) < 0)
 894                         syserr("cannot rename(%s, %s), uid=%d",
 895                                 tf, qf, (int) geteuid());
 896                 else
 897                 {
 898                         /*
 899                         **  Check if type has changed and only
 900                         **  remove the old item if the rename above
 901                         **  succeeded.
 902                         */
 903 
 904                         if (e->e_qfletter != '\0' &&
 905                             e->e_qfletter != new)
 906                         {
 907                                 if (tTd(40, 5))
 908                                 {
 909                                         sm_dprintf("type changed from %c to %c\n",
 910                                                    e->e_qfletter, new);
 911                                 }
 912 
 913                                 if (unlink(queuename(e, e->e_qfletter)) < 0)
 914                                 {
 915                                         /* XXX: something more drastic? */
 916                                         if (LogLevel > 0)
 917                                                 sm_syslog(LOG_ERR, e->e_id,
 918                                                           "queueup: unlink(%s) failed: %s",
 919                                                           queuename(e, e->e_qfletter),
 920                                                           sm_errstring(errno));
 921                                 }
 922                         }
 923                 }
 924                 e->e_qfletter = new;
 925 
 926                 /*
 927                 **  fsync() after renaming to make sure metadata is
 928                 **  written to disk on filesystems in which renames are
 929                 **  not guaranteed.
 930                 */
 931 
 932                 if (SuperSafe != SAFE_NO)
 933                 {
 934                         /* for softupdates */
 935                         if (tfd >= 0 && fsync(tfd) < 0)
 936                         {
 937                                 syserr("!queueup: cannot fsync queue temp file %s",
 938                                        tf);
 939                         }
 940                         SYNC_DIR(qf, true);
 941                 }
 942 
 943                 /* close and unlock old (locked) queue file */
 944                 if (e->e_lockfp != NULL)
 945                         (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
 946                 e->e_lockfp = tfp;
 947 
 948                 /* save log info */
 949                 if (LogLevel > 79)
 950                         sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", qf);
 951         }
 952         else
 953         {
 954                 /* save log info */
 955                 if (LogLevel > 79)
 956                         sm_syslog(LOG_DEBUG, e->e_id, "queueup %s", tf);
 957 
 958                 e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
 959         }
 960 
 961         errno = 0;
 962         e->e_flags |= EF_INQUEUE;
 963 
 964         if (tTd(40, 1))
 965                 sm_dprintf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
 966         return;
 967 }
 968 
 969 /*
 970 **  PRINTCTLADDR -- print control address to file.
 971 **
 972 **      Parameters:
 973 **              a -- address.
 974 **              tfp -- file pointer.
 975 **
 976 **      Returns:
 977 **              none.
 978 **
 979 **      Side Effects:
 980 **              The control address (if changed) is printed to the file.
 981 **              The last control address and uid are saved.
 982 */
 983 
 984 static void
 985 printctladdr(a, tfp)
 986         register ADDRESS *a;
 987         SM_FILE_T *tfp;
 988 {
 989         char *user;
 990         register ADDRESS *q;
 991         uid_t uid;
 992         gid_t gid;
 993         static ADDRESS *lastctladdr = NULL;
 994         static uid_t lastuid;
 995 
 996         /* initialization */
 997         if (a == NULL || a->q_alias == NULL || tfp == NULL)
 998         {
 999                 if (lastctladdr != NULL && tfp != NULL)
1000                         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C\n");
1001                 lastctladdr = NULL;
1002                 lastuid = 0;
1003                 return;
1004         }
1005 
1006         /* find the active uid */
1007         q = getctladdr(a);
1008         if (q == NULL)
1009         {
1010                 user = NULL;
1011                 uid = 0;
1012                 gid = 0;
1013         }
1014         else
1015         {
1016                 user = q->q_ruser != NULL ? q->q_ruser : q->q_user;
1017                 uid = q->q_uid;
1018                 gid = q->q_gid;
1019         }
1020         a = a->q_alias;
1021 
1022         /* check to see if this is the same as last time */
1023         if (lastctladdr != NULL && uid == lastuid &&
1024             strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
1025                 return;
1026         lastuid = uid;
1027         lastctladdr = a;
1028 
1029         if (uid == 0 || user == NULL || user[0] == '\0')
1030                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C");
1031         else
1032                 (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, "C%s:%ld:%ld",
1033                                      denlstring(user, true, false), (long) uid,
1034                                      (long) gid);
1035         (void) sm_io_fprintf(tfp, SM_TIME_DEFAULT, ":%s\n",
1036                              denlstring(a->q_paddr, true, false));
1037 }
1038 
1039 /*
1040 **  RUNNERS_SIGTERM -- propagate a SIGTERM to queue runner process
1041 **
1042 **      This propagates the signal to the child processes that are queue
1043 **      runners. This is for a queue runner "cleanup". After all of the
1044 **      child queue runner processes are signaled (it should be SIGTERM
1045 **      being the sig) then the old signal handler (Oldsh) is called
1046 **      to handle any cleanup set for this process (provided it is not
1047 **      SIG_DFL or SIG_IGN). The signal may not be handled immediately
1048 **      if the BlockOldsh flag is set. If the current process doesn't
1049 **      have a parent then handle the signal immediately, regardless of
1050 **      BlockOldsh.
1051 **
1052 **      Parameters:
1053 **              sig -- the signal number being sent
1054 **
1055 **      Returns:
1056 **              none.
1057 **
1058 **      Side Effects:
1059 **              Sets the NoMoreRunners boolean to true to stop more runners
1060 **              from being started in runqueue().
1061 **
1062 **      NOTE:   THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
1063 **              ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1064 **              DOING.
1065 */
1066 
1067 static bool             volatile NoMoreRunners = false;
1068 static sigfunc_t        Oldsh_term = SIG_DFL;
1069 static sigfunc_t        Oldsh_hup = SIG_DFL;
1070 static sigfunc_t        volatile Oldsh = SIG_DFL;
1071 static bool             BlockOldsh = false;
1072 static int              volatile Oldsig = 0;
1073 static SIGFUNC_DECL     runners_sigterm __P((int));
1074 static SIGFUNC_DECL     runners_sighup __P((int));
1075 
1076 static SIGFUNC_DECL
1077 runners_sigterm(sig)
1078         int sig;
1079 {
1080         int save_errno = errno;
1081 
1082         FIX_SYSV_SIGNAL(sig, runners_sigterm);
1083         errno = save_errno;
1084         CHECK_CRITICAL(sig);
1085         NoMoreRunners = true;
1086         Oldsh = Oldsh_term;
1087         Oldsig = sig;
1088         proc_list_signal(PROC_QUEUE, sig);
1089 
1090         if (!BlockOldsh || getppid() <= 1)
1091         {
1092                 /* Check that a valid 'old signal handler' is callable */
1093                 if (Oldsh_term != SIG_DFL && Oldsh_term != SIG_IGN &&
1094                     Oldsh_term != runners_sigterm)
1095                         (*Oldsh_term)(sig);
1096         }
1097         errno = save_errno;
1098         return SIGFUNC_RETURN;
1099 }
1100 /*
1101 **  RUNNERS_SIGHUP -- propagate a SIGHUP to queue runner process
1102 **
1103 **      This propagates the signal to the child processes that are queue
1104 **      runners. This is for a queue runner "cleanup". After all of the
1105 **      child queue runner processes are signaled (it should be SIGHUP
1106 **      being the sig) then the old signal handler (Oldsh) is called to
1107 **      handle any cleanup set for this process (provided it is not SIG_DFL
1108 **      or SIG_IGN). The signal may not be handled immediately if the
1109 **      BlockOldsh flag is set. If the current process doesn't have
1110 **      a parent then handle the signal immediately, regardless of
1111 **      BlockOldsh.
1112 **
1113 **      Parameters:
1114 **              sig -- the signal number being sent
1115 **
1116 **      Returns:
1117 **              none.
1118 **
1119 **      Side Effects:
1120 **              Sets the NoMoreRunners boolean to true to stop more runners
1121 **              from being started in runqueue().
1122 **
1123 **      NOTE:   THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
1124 **              ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1125 **              DOING.
1126 */
1127 
1128 static SIGFUNC_DECL
1129 runners_sighup(sig)
1130         int sig;
1131 {
1132         int save_errno = errno;
1133 
1134         FIX_SYSV_SIGNAL(sig, runners_sighup);
1135         errno = save_errno;
1136         CHECK_CRITICAL(sig);
1137         NoMoreRunners = true;
1138         Oldsh = Oldsh_hup;
1139         Oldsig = sig;
1140         proc_list_signal(PROC_QUEUE, sig);
1141 
1142         if (!BlockOldsh || getppid() <= 1)
1143         {
1144                 /* Check that a valid 'old signal handler' is callable */
1145                 if (Oldsh_hup != SIG_DFL && Oldsh_hup != SIG_IGN &&
1146                     Oldsh_hup != runners_sighup)
1147                         (*Oldsh_hup)(sig);
1148         }
1149         errno = save_errno;
1150         return SIGFUNC_RETURN;
1151 }
1152 /*
1153 **  MARK_WORK_GROUP_RESTART -- mark a work group as needing a restart
1154 **
1155 **  Sets a workgroup for restarting.
1156 **
1157 **      Parameters:
1158 **              wgrp -- the work group id to restart.
1159 **              reason -- why (signal?), -1 to turn off restart
1160 **
1161 **      Returns:
1162 **              none.
1163 **
1164 **      Side effects:
1165 **              May set global RestartWorkGroup to true.
1166 **
1167 **      NOTE:   THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
1168 **              ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1169 **              DOING.
1170 */
1171 
1172 void
1173 mark_work_group_restart(wgrp, reason)
1174         int wgrp;
1175         int reason;
1176 {
1177         if (wgrp < 0 || wgrp > NumWorkGroups)
1178                 return;
1179 
1180         WorkGrp[wgrp].wg_restart = reason;
1181         if (reason >= 0)
1182                 RestartWorkGroup = true;
1183 }
1184 /*
1185 **  RESTART_MARKED_WORK_GROUPS -- restart work groups marked as needing restart
1186 **
1187 **  Restart any workgroup marked as needing a restart provided more
1188 **  runners are allowed.
1189 **
1190 **      Parameters:
1191 **              none.
1192 **
1193 **      Returns:
1194 **              none.
1195 **
1196 **      Side effects:
1197 **              Sets global RestartWorkGroup to false.
1198 */
1199 
1200 void
1201 restart_marked_work_groups()
1202 {
1203         int i;
1204         int wasblocked;
1205 
1206         if (NoMoreRunners)
1207                 return;
1208 
1209         /* Block SIGCHLD so reapchild() doesn't mess with us */
1210         wasblocked = sm_blocksignal(SIGCHLD);
1211 
1212         for (i = 0; i < NumWorkGroups; i++)
1213         {
1214                 if (WorkGrp[i].wg_restart >= 0)
1215                 {
1216                         if (LogLevel > 8)
1217                                 sm_syslog(LOG_ERR, NOQID,
1218                                           "restart queue runner=%d due to signal 0x%x",
1219                                           i, WorkGrp[i].wg_restart);
1220                         restart_work_group(i);
1221                 }
1222         }
1223         RestartWorkGroup = false;
1224 
1225         if (wasblocked == 0)
1226                 (void) sm_releasesignal(SIGCHLD);
1227 }
1228 /*
1229 **  RESTART_WORK_GROUP -- restart a specific work group
1230 **
1231 **  Restart a specific workgroup provided more runners are allowed.
1232 **  If the requested work group has been restarted too many times log
1233 **  this and refuse to restart.
1234 **
1235 **      Parameters:
1236 **              wgrp -- the work group id to restart
1237 **
1238 **      Returns:
1239 **              none.
1240 **
1241 **      Side Effects:
1242 **              starts another process doing the work of wgrp
1243 */
1244 
1245 #define MAX_PERSIST_RESTART     10      /* max allowed number of restarts */
1246 
1247 static void
1248 restart_work_group(wgrp)
1249         int wgrp;
1250 {
1251         if (NoMoreRunners ||
1252             wgrp < 0 || wgrp > NumWorkGroups)
1253                 return;
1254 
1255         WorkGrp[wgrp].wg_restart = -1;
1256         if (WorkGrp[wgrp].wg_restartcnt < MAX_PERSIST_RESTART)
1257         {
1258                 /* avoid overflow; increment here */
1259                 WorkGrp[wgrp].wg_restartcnt++;
1260                 (void) run_work_group(wgrp, RWG_FORK|RWG_PERSISTENT|RWG_RUNALL);
1261         }
1262         else
1263         {
1264                 sm_syslog(LOG_ERR, NOQID,
1265                           "ERROR: persistent queue runner=%d restarted too many times, queue runner lost",
1266                           wgrp);
1267         }
1268 }
1269 /*
1270 **  SCHEDULE_QUEUE_RUNS -- schedule the next queue run for a work group.
1271 **
1272 **      Parameters:
1273 **              runall -- schedule even if individual bit is not set.
1274 **              wgrp -- the work group id to schedule.
1275 **              didit -- the queue run was performed for this work group.
1276 **
1277 **      Returns:
1278 **              nothing
1279 */
1280 
1281 #define INCR_MOD(v, m)  if (++v >= m)        \
1282                                 v = 0;  \
1283                         else
1284 
1285 static void
1286 schedule_queue_runs(runall, wgrp, didit)
1287         bool runall;
1288         int wgrp;
1289         bool didit;
1290 {
1291         int qgrp, cgrp, endgrp;
1292 #if _FFR_QUEUE_SCHED_DBG
1293         time_t lastsched;
1294         bool sched;
1295 #endif /* _FFR_QUEUE_SCHED_DBG */
1296         time_t now;
1297         time_t minqintvl;
1298 
1299         /*
1300         **  This is a bit ugly since we have to duplicate the
1301         **  code that "walks" through a work queue group.
1302         */
1303 
1304         now = curtime();
1305         minqintvl = 0;
1306         cgrp = endgrp = WorkGrp[wgrp].wg_curqgrp;
1307         do
1308         {
1309                 time_t qintvl;
1310 
1311 #if _FFR_QUEUE_SCHED_DBG
1312                 lastsched = 0;
1313                 sched = false;
1314 #endif /* _FFR_QUEUE_SCHED_DBG */
1315                 qgrp = WorkGrp[wgrp].wg_qgs[cgrp]->qg_index;
1316                 if (Queue[qgrp]->qg_queueintvl > 0)
1317                         qintvl = Queue[qgrp]->qg_queueintvl;
1318                 else if (QueueIntvl > 0)
1319                         qintvl = QueueIntvl;
1320                 else
1321                         qintvl = (time_t) 0;
1322 #if _FFR_QUEUE_SCHED_DBG
1323                 lastsched = Queue[qgrp]->qg_nextrun;
1324 #endif /* _FFR_QUEUE_SCHED_DBG */
1325                 if ((runall || Queue[qgrp]->qg_nextrun <= now) && qintvl > 0)
1326                 {
1327 #if _FFR_QUEUE_SCHED_DBG
1328                         sched = true;
1329 #endif /* _FFR_QUEUE_SCHED_DBG */
1330                         if (minqintvl == 0 || qintvl < minqintvl)
1331                                 minqintvl = qintvl;
1332 
1333                         /*
1334                         **  Only set a new time if a queue run was performed
1335                         **  for this queue group.  If the queue was not run,
1336                         **  we could starve it by setting a new time on each
1337                         **  call.
1338                         */
1339 
1340                         if (didit)
1341                                 Queue[qgrp]->qg_nextrun += qintvl;
1342                 }
1343 #if _FFR_QUEUE_SCHED_DBG
1344                 if (tTd(69, 10))
1345                         sm_syslog(LOG_INFO, NOQID,
1346                                 "sqr: wgrp=%d, cgrp=%d, qgrp=%d, intvl=%ld, QI=%ld, runall=%d, lastrun=%ld, nextrun=%ld, sched=%d",
1347                                 wgrp, cgrp, qgrp, Queue[qgrp]->qg_queueintvl,
1348                                 QueueIntvl, runall, lastsched,
1349                                 Queue[qgrp]->qg_nextrun, sched);
1350 #endif /* _FFR_QUEUE_SCHED_DBG */
1351                 INCR_MOD(cgrp, WorkGrp[wgrp].wg_numqgrp);
1352         } while (endgrp != cgrp);
1353         if (minqintvl > 0)
1354                 (void) sm_setevent(minqintvl, runqueueevent, 0);
1355 }
1356 
1357 #if _FFR_QUEUE_RUN_PARANOIA
1358 /*
1359 **  CHECKQUEUERUNNER -- check whether a queue group hasn't been run.
1360 **
1361 **      Use this if events may get lost and hence queue runners may not
1362 **      be started and mail will pile up in a queue.
1363 **
1364 **      Parameters:
1365 **              none.
1366 **
1367 **      Returns:
1368 **              true if a queue run is necessary.
1369 **
1370 **      Side Effects:
1371 **              may schedule a queue run.
1372 */
1373 
1374 bool
1375 checkqueuerunner()
1376 {
1377         int qgrp;
1378         time_t now, minqintvl;
1379 
1380         now = curtime();
1381         minqintvl = 0;
1382         for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
1383         {
1384                 time_t qintvl;
1385 
1386                 if (Queue[qgrp]->qg_queueintvl > 0)
1387                         qintvl = Queue[qgrp]->qg_queueintvl;
1388                 else if (QueueIntvl > 0)
1389                         qintvl = QueueIntvl;
1390                 else
1391                         qintvl = (time_t) 0;
1392                 if (Queue[qgrp]->qg_nextrun <= now - qintvl)
1393                 {
1394                         if (minqintvl == 0 || qintvl < minqintvl)
1395                                 minqintvl = qintvl;
1396                         if (LogLevel > 1)
1397                                 sm_syslog(LOG_WARNING, NOQID,
1398                                         "checkqueuerunner: queue %d should have been run at %s, queue interval %ld",
1399                                         qgrp,
1400                                         arpadate(ctime(&Queue[qgrp]->qg_nextrun)),
1401                                         qintvl);
1402                 }
1403         }
1404         if (minqintvl > 0)
1405         {
1406                 (void) sm_setevent(minqintvl, runqueueevent, 0);
1407                 return true;
1408         }
1409         return false;
1410 }
1411 #endif /* _FFR_QUEUE_RUN_PARANOIA */
1412 
1413 /*
1414 **  RUNQUEUE -- run the jobs in the queue.
1415 **
1416 **      Gets the stuff out of the queue in some presumably logical
1417 **      order and processes them.
1418 **
1419 **      Parameters:
1420 **              forkflag -- true if the queue scanning should be done in
1421 **                      a child process.  We double-fork so it is not our
1422 **                      child and we don't have to clean up after it.
1423 **                      false can be ignored if we have multiple queues.
1424 **              verbose -- if true, print out status information.
1425 **              persistent -- persistent queue runner?
1426 **              runall -- run all groups or only a subset (DoQueueRun)?
1427 **
1428 **      Returns:
1429 **              true if the queue run successfully began.
1430 **
1431 **      Side Effects:
1432 **              runs things in the mail queue using run_work_group().
1433 **              maybe schedules next queue run.
1434 */
1435 
1436 static ENVELOPE QueueEnvelope;          /* the queue run envelope */
1437 static time_t   LastQueueTime = 0;      /* last time a queue ID assigned */
1438 static pid_t    LastQueuePid = -1;      /* last PID which had a queue ID */
1439 
1440 /* values for qp_supdirs */
1441 #define QP_NOSUB        0x0000  /* No subdirectories */
1442 #define QP_SUBDF        0x0001  /* "df" subdirectory */
1443 #define QP_SUBQF        0x0002  /* "qf" subdirectory */
1444 #define QP_SUBXF        0x0004  /* "xf" subdirectory */
1445 
1446 bool
1447 runqueue(forkflag, verbose, persistent, runall)
1448         bool forkflag;
1449         bool verbose;
1450         bool persistent;
1451         bool runall;
1452 {
1453         int i;
1454         bool ret = true;
1455         static int curnum = 0;
1456         sigfunc_t cursh;
1457 #if SM_HEAP_CHECK
1458         SM_NONVOLATILE int oldgroup = 0;
1459 
1460         if (sm_debug_active(&DebugLeakQ, 1))
1461         {
1462                 oldgroup = sm_heap_group();
1463                 sm_heap_newgroup();
1464                 sm_dprintf("runqueue() heap group #%d\n", sm_heap_group());
1465         }
1466 #endif /* SM_HEAP_CHECK */
1467 
1468         /* queue run has been started, don't do any more this time */
1469         DoQueueRun = false;
1470 
1471         /* more than one queue or more than one directory per queue */
1472         if (!forkflag && !verbose &&
1473             (WorkGrp[0].wg_qgs[0]->qg_numqueues > 1 || NumWorkGroups > 1 ||
1474              WorkGrp[0].wg_numqgrp > 1))
1475                 forkflag = true;
1476 
1477         /*
1478         **  For controlling queue runners via signals sent to this process.
1479         **  Oldsh* will get called too by runners_sig* (if it is not SIG_IGN
1480         **  or SIG_DFL) to preserve cleanup behavior. Now that this process
1481         **  will have children (and perhaps grandchildren) this handler will
1482         **  be left in place. This is because this process, once it has
1483         **  finished spinning off queue runners, may go back to doing something
1484         **  else (like being a daemon). And we still want on a SIG{TERM,HUP} to
1485         **  clean up the child queue runners. Only install 'runners_sig*' once
1486         **  else we'll get stuck looping forever.
1487         */
1488 
1489         cursh = sm_signal(SIGTERM, runners_sigterm);
1490         if (cursh != runners_sigterm)
1491                 Oldsh_term = cursh;
1492         cursh = sm_signal(SIGHUP, runners_sighup);
1493         if (cursh != runners_sighup)
1494                 Oldsh_hup = cursh;
1495 
1496         for (i = 0; i < NumWorkGroups && !NoMoreRunners; i++)
1497         {
1498                 int rwgflags = RWG_NONE;
1499 
1500                 /*
1501                 **  If MaxQueueChildren active then test whether the start
1502                 **  of the next queue group's additional queue runners (maximum)
1503                 **  will result in MaxQueueChildren being exceeded.
1504                 **
1505                 **  Note: do not use continue; even though another workgroup
1506                 **      may have fewer queue runners, this would be "unfair",
1507                 **      i.e., this work group might "starve" then.
1508                 */
1509 
1510 #if _FFR_QUEUE_SCHED_DBG
1511                 if (tTd(69, 10))
1512                         sm_syslog(LOG_INFO, NOQID,
1513                                 "rq: curnum=%d, MaxQueueChildren=%d, CurRunners=%d, WorkGrp[curnum].wg_maxact=%d",
1514                                 curnum, MaxQueueChildren, CurRunners,
1515                                 WorkGrp[curnum].wg_maxact);
1516 #endif /* _FFR_QUEUE_SCHED_DBG */
1517                 if (MaxQueueChildren > 0 &&
1518                     CurRunners + WorkGrp[curnum].wg_maxact > MaxQueueChildren)
1519                         break;
1520 
1521                 /*
1522                 **  Pick up where we left off (curnum), in case we
1523                 **  used up all the children last time without finishing.
1524                 **  This give a round-robin fairness to queue runs.
1525                 **
1526                 **  Increment CurRunners before calling run_work_group()
1527                 **  to avoid a "race condition" with proc_list_drop() which
1528                 **  decrements CurRunners if the queue runners terminate.
1529                 **  Notice: CurRunners is an upper limit, in some cases
1530                 **  (too few jobs in the queue) this value is larger than
1531                 **  the actual number of queue runners. The discrepancy can
1532                 **  increase if some queue runners "hang" for a long time.
1533                 */
1534 
1535                 CurRunners += WorkGrp[curnum].wg_maxact;
1536                 if (forkflag)
1537                         rwgflags |= RWG_FORK;
1538                 if (verbose)
1539                         rwgflags |= RWG_VERBOSE;
1540                 if (persistent)
1541                         rwgflags |= RWG_PERSISTENT;
1542                 if (runall)
1543                         rwgflags |= RWG_RUNALL;
1544                 ret = run_work_group(curnum, rwgflags);
1545 
1546                 /*
1547                 **  Failure means a message was printed for ETRN
1548                 **  and subsequent queues are likely to fail as well.
1549                 **  Decrement CurRunners in that case because
1550                 **  none have been started.
1551                 */
1552 
1553                 if (!ret)
1554                 {
1555                         CurRunners -= WorkGrp[curnum].wg_maxact;
1556                         break;
1557                 }
1558 
1559                 if (!persistent)
1560                         schedule_queue_runs(runall, curnum, true);
1561                 INCR_MOD(curnum, NumWorkGroups);
1562         }
1563 
1564         /* schedule left over queue runs */
1565         if (i < NumWorkGroups && !NoMoreRunners && !persistent)
1566         {
1567                 int h;
1568 
1569                 for (h = curnum; i < NumWorkGroups; i++)
1570                 {
1571                         schedule_queue_runs(runall, h, false);
1572                         INCR_MOD(h, NumWorkGroups);
1573                 }
1574         }
1575 
1576 
1577 #if SM_HEAP_CHECK
1578         if (sm_debug_active(&DebugLeakQ, 1))
1579                 sm_heap_setgroup(oldgroup);
1580 #endif /* SM_HEAP_CHECK */
1581         return ret;
1582 }
1583 
1584 #if _FFR_SKIP_DOMAINS
1585 /*
1586 **  SKIP_DOMAINS -- Skip 'skip' number of domains in the WorkQ.
1587 **
1588 **  Added by Stephen Frost <sfrost@snowman.net> to support
1589 **  having each runner process every N'th domain instead of
1590 **  every N'th message.
1591 **
1592 **      Parameters:
1593 **              skip -- number of domains in WorkQ to skip.
1594 **
1595 **      Returns:
1596 **              total number of messages skipped.
1597 **
1598 **      Side Effects:
1599 **              may change WorkQ
1600 */
1601 
1602 static int
1603 skip_domains(skip)
1604         int skip;
1605 {
1606         int n, seqjump;
1607 
1608         for (n = 0, seqjump = 0; n < skip && WorkQ != NULL; seqjump++)
1609         {
1610                 if (WorkQ->w_next != NULL)
1611                 {
1612                         if (WorkQ->w_host != NULL &&
1613                             WorkQ->w_next->w_host != NULL)
1614                         {
1615                                 if (sm_strcasecmp(WorkQ->w_host,
1616                                                 WorkQ->w_next->w_host) != 0)
1617                                         n++;
1618                         }
1619                         else
1620                         {
1621                                 if ((WorkQ->w_host != NULL &&
1622                                      WorkQ->w_next->w_host == NULL) ||
1623                                     (WorkQ->w_host == NULL &&
1624                                      WorkQ->w_next->w_host != NULL))
1625                                              n++;
1626                         }
1627                 }
1628                 WorkQ = WorkQ->w_next;
1629         }
1630         return seqjump;
1631 }
1632 #endif /* _FFR_SKIP_DOMAINS */
1633 
1634 /*
1635 **  RUNNER_WORK -- have a queue runner do its work
1636 **
1637 **  Have a queue runner do its work a list of entries.
1638 **  When work isn't directly being done then this process can take a signal
1639 **  and terminate immediately (in a clean fashion of course).
1640 **  When work is directly being done, it's not to be interrupted
1641 **  immediately: the work should be allowed to finish at a clean point
1642 **  before termination (in a clean fashion of course).
1643 **
1644 **      Parameters:
1645 **              e -- envelope.
1646 **              sequenceno -- 'th process to run WorkQ.
1647 **              didfork -- did the calling process fork()?
1648 **              skip -- process only each skip'th item.
1649 **              njobs -- number of jobs in WorkQ.
1650 **
1651 **      Returns:
1652 **              none.
1653 **
1654 **      Side Effects:
1655 **              runs things in the mail queue.
1656 */
1657 
1658 static void
1659 runner_work(e, sequenceno, didfork, skip, njobs)
1660         register ENVELOPE *e;
1661         int sequenceno;
1662         bool didfork;
1663         int skip;
1664         int njobs;
1665 {
1666         int n, seqjump;
1667         WORK *w;
1668         time_t now;
1669 
1670         SM_GET_LA(now);
1671 
1672         /*
1673         **  Here we temporarily block the second calling of the handlers.
1674         **  This allows us to handle the signal without terminating in the
1675         **  middle of direct work. If a signal does come, the test for
1676         **  NoMoreRunners will find it.
1677         */
1678 
1679         BlockOldsh = true;
1680         seqjump = skip;
1681 
1682         /* process them once at a time */
1683         while (WorkQ != NULL)
1684         {
1685 #if SM_HEAP_CHECK
1686                 SM_NONVOLATILE int oldgroup = 0;
1687 
1688                 if (sm_debug_active(&DebugLeakQ, 1))
1689                 {
1690                         oldgroup = sm_heap_group();
1691                         sm_heap_newgroup();
1692                         sm_dprintf("run_queue_group() heap group #%d\n",
1693                                 sm_heap_group());
1694                 }
1695 #endif /* SM_HEAP_CHECK */
1696 
1697                 /* do no more work */
1698                 if (NoMoreRunners)
1699                 {
1700                         /* Check that a valid signal handler is callable */
1701                         if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1702                             Oldsh != runners_sighup &&
1703                             Oldsh != runners_sigterm)
1704                                 (*Oldsh)(Oldsig);
1705                         break;
1706                 }
1707 
1708                 w = WorkQ; /* assign current work item */
1709 
1710                 /*
1711                 **  Set the head of the WorkQ to the next work item.
1712                 **  It is set 'skip' ahead (the number of parallel queue
1713                 **  runners working on WorkQ together) since each runner
1714                 **  works on every 'skip'th (N-th) item.
1715 #if _FFR_SKIP_DOMAINS
1716                 **  In the case of the BYHOST Queue Sort Order, the 'item'
1717                 **  is a domain, so we work on every 'skip'th (N-th) domain.
1718 #endif * _FFR_SKIP_DOMAINS *
1719                 */
1720 
1721 #if _FFR_SKIP_DOMAINS
1722                 if (QueueSortOrder == QSO_BYHOST)
1723                 {
1724                         seqjump = 1;
1725                         if (WorkQ->w_next != NULL)
1726                         {
1727                                 if (WorkQ->w_host != NULL &&
1728                                     WorkQ->w_next->w_host != NULL)
1729                                 {
1730                                         if (sm_strcasecmp(WorkQ->w_host,
1731                                                         WorkQ->w_next->w_host)
1732                                                                 != 0)
1733                                                 seqjump = skip_domains(skip);
1734                                         else
1735                                                 WorkQ = WorkQ->w_next;
1736                                 }
1737                                 else
1738                                 {
1739                                         if ((WorkQ->w_host != NULL &&
1740                                              WorkQ->w_next->w_host == NULL) ||
1741                                             (WorkQ->w_host == NULL &&
1742                                              WorkQ->w_next->w_host != NULL))
1743                                                 seqjump = skip_domains(skip);
1744                                         else
1745                                                 WorkQ = WorkQ->w_next;
1746                                 }
1747                         }
1748                         else
1749                                 WorkQ = WorkQ->w_next;
1750                 }
1751                 else
1752 #endif /* _FFR_SKIP_DOMAINS */
1753                 {
1754                         for (n = 0; n < skip && WorkQ != NULL; n++)
1755                                 WorkQ = WorkQ->w_next;
1756                 }
1757 
1758                 e->e_to = NULL;
1759 
1760                 /*
1761                 **  Ignore jobs that are too expensive for the moment.
1762                 **
1763                 **      Get new load average every GET_NEW_LA_TIME seconds.
1764                 */
1765 
1766                 SM_GET_LA(now);
1767                 if (shouldqueue(WkRecipFact, Current_LA_time))
1768                 {
1769                         char *msg = "Aborting queue run: load average too high";
1770 
1771                         if (Verbose)
1772                                 message("%s", msg);
1773                         if (LogLevel > 8)
1774                                 sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1775                         break;
1776                 }
1777                 if (shouldqueue(w->w_pri, w->w_ctime))
1778                 {
1779                         if (Verbose)
1780                                 message(EmptyString);
1781                         if (QueueSortOrder == QSO_BYPRIORITY)
1782                         {
1783                                 if (Verbose)
1784                                         message("Skipping %s/%s (sequence %d of %d) and flushing rest of queue",
1785                                                 qid_printqueue(w->w_qgrp,
1786                                                                w->w_qdir),
1787                                                 w->w_name + 2, sequenceno,
1788                                                 njobs);
1789                                 if (LogLevel > 8)
1790                                         sm_syslog(LOG_INFO, NOQID,
1791                                                   "runqueue: Flushing queue from %s/%s (pri %ld, LA %d, %d of %d)",
1792                                                   qid_printqueue(w->w_qgrp,
1793                                                                  w->w_qdir),
1794                                                   w->w_name + 2, w->w_pri,
1795                                                   CurrentLA, sequenceno,
1796                                                   njobs);
1797                                 break;
1798                         }
1799                         else if (Verbose)
1800                                 message("Skipping %s/%s (sequence %d of %d)",
1801                                         qid_printqueue(w->w_qgrp, w->w_qdir),
1802                                         w->w_name + 2, sequenceno, njobs);
1803                 }
1804                 else
1805                 {
1806                         if (Verbose)
1807                         {
1808                                 message(EmptyString);
1809                                 message("Running %s/%s (sequence %d of %d)",
1810                                         qid_printqueue(w->w_qgrp, w->w_qdir),
1811                                         w->w_name + 2, sequenceno, njobs);
1812                         }
1813                         if (didfork && MaxQueueChildren > 0)
1814                         {
1815                                 sm_blocksignal(SIGCHLD);
1816                                 (void) sm_signal(SIGCHLD, reapchild);
1817                         }
1818                         if (tTd(63, 100))
1819                                 sm_syslog(LOG_DEBUG, NOQID,
1820                                           "runqueue %s dowork(%s)",
1821                                           qid_printqueue(w->w_qgrp, w->w_qdir),
1822                                           w->w_name + 2);
1823 
1824                         (void) dowork(w->w_qgrp, w->w_qdir, w->w_name + 2,
1825                                       ForkQueueRuns, false, e);
1826                         errno = 0;
1827                 }
1828                 sm_free(w->w_name); /* XXX */
1829                 if (w->w_host != NULL)
1830                         sm_free(w->w_host); /* XXX */
1831                 sm_free((char *) w); /* XXX */
1832                 sequenceno += seqjump; /* next sequence number */
1833 #if SM_HEAP_CHECK
1834                 if (sm_debug_active(&DebugLeakQ, 1))
1835                         sm_heap_setgroup(oldgroup);
1836 #endif /* SM_HEAP_CHECK */
1837         }
1838 
1839         BlockOldsh = false;
1840 
1841         /* check the signals didn't happen during the revert */
1842         if (NoMoreRunners)
1843         {
1844                 /* Check that a valid signal handler is callable */
1845                 if (Oldsh != SIG_DFL && Oldsh != SIG_IGN &&
1846                     Oldsh != runners_sighup && Oldsh != runners_sigterm)
1847                         (*Oldsh)(Oldsig);
1848         }
1849 
1850         Oldsh = SIG_DFL; /* after the NoMoreRunners check */
1851 }
1852 /*
1853 **  RUN_WORK_GROUP -- run the jobs in a queue group from a work group.
1854 **
1855 **      Gets the stuff out of the queue in some presumably logical
1856 **      order and processes them.
1857 **
1858 **      Parameters:
1859 **              wgrp -- work group to process.
1860 **              flags -- RWG_* flags
1861 **
1862 **      Returns:
1863 **              true if the queue run successfully began.
1864 **
1865 **      Side Effects:
1866 **              runs things in the mail queue.
1867 */
1868 
1869 /* Minimum sleep time for persistent queue runners */
1870 #define MIN_SLEEP_TIME  5
1871 
1872 bool
1873 run_work_group(wgrp, flags)
1874         int wgrp;
1875         int flags;
1876 {
1877         register ENVELOPE *e;
1878         int njobs, qdir;
1879         int sequenceno = 1;
1880         int qgrp, endgrp, h, i;
1881         time_t now;
1882         bool full, more;
1883         SM_RPOOL_T *rpool;
1884         extern ENVELOPE BlankEnvelope;
1885         extern SIGFUNC_DECL reapchild __P((int));
1886 
1887         if (wgrp < 0)
1888                 return false;
1889 
1890         /*
1891         **  If no work will ever be selected, don't even bother reading
1892         **  the queue.
1893         */
1894 
1895         SM_GET_LA(now);
1896 
1897         if (!bitset(RWG_PERSISTENT, flags) &&
1898             shouldqueue(WkRecipFact, Current_LA_time))
1899         {
1900                 char *msg = "Skipping queue run -- load average too high";
1901 
1902                 if (bitset(RWG_VERBOSE, flags))
1903                         message("458 %s\n", msg);
1904                 if (LogLevel > 8)
1905                         sm_syslog(LOG_INFO, NOQID, "runqueue: %s", msg);
1906                 return false;
1907         }
1908 
1909         /*
1910         **  See if we already have too many children.
1911         */
1912 
1913         if (bitset(RWG_FORK, flags) &&
1914             WorkGrp[wgrp].wg_lowqintvl > 0 &&
1915             !bitset(RWG_PERSISTENT, flags) &&
1916             MaxChildren > 0 && CurChildren >= MaxChildren)
1917         {
1918                 char *msg = "Skipping queue run -- too many children";
1919 
1920                 if (bitset(RWG_VERBOSE, flags))
1921                         message("458 %s (%d)\n", msg, CurChildren);
1922                 if (LogLevel > 8)
1923                         sm_syslog(LOG_INFO, NOQID, "runqueue: %s (%d)",
1924                                   msg, CurChildren);
1925                 return false;
1926         }
1927 
1928         /*
1929         **  See if we want to go off and do other useful work.
1930         */
1931 
1932         if (bitset(RWG_FORK, flags))
1933         {
1934                 pid_t pid;
1935 
1936                 (void) sm_blocksignal(SIGCHLD);
1937                 (void) sm_signal(SIGCHLD, reapchild);
1938 
1939                 pid = dofork();
1940                 if (pid == -1)
1941                 {
1942                         const char *msg = "Skipping queue run -- fork() failed";
1943                         const char *err = sm_errstring(errno);
1944 
1945                         if (bitset(RWG_VERBOSE, flags))
1946                                 message("458 %s: %s\n", msg, err);
1947                         if (LogLevel > 8)
1948                                 sm_syslog(LOG_INFO, NOQID, "runqueue: %s: %s",
1949                                           msg, err);
1950                         (void) sm_releasesignal(SIGCHLD);
1951                         return false;
1952                 }
1953                 if (pid != 0)
1954                 {
1955                         /* parent -- pick up intermediate zombie */
1956                         (void) sm_blocksignal(SIGALRM);
1957 
1958                         /* wgrp only used when queue runners are persistent */
1959                         proc_list_add(pid, "Queue runner", PROC_QUEUE,
1960                                       WorkGrp[wgrp].wg_maxact,
1961                                       bitset(RWG_PERSISTENT, flags) ? wgrp : -1,
1962                                       NULL);
1963                         (void) sm_releasesignal(SIGALRM);
1964                         (void) sm_releasesignal(SIGCHLD);
1965                         return true;
1966                 }
1967 
1968                 /* child -- clean up signals */
1969 
1970                 /* Reset global flags */
1971                 RestartRequest = NULL;
1972                 RestartWorkGroup = false;
1973                 ShutdownRequest = NULL;
1974                 PendingSignal = 0;
1975                 CurrentPid = getpid();
1976                 close_sendmail_pid();
1977 
1978                 /*
1979                 **  Initialize exception stack and default exception
1980                 **  handler for child process.
1981                 */
1982 
1983                 sm_exc_newthread(fatal_error);
1984                 clrcontrol();
1985                 proc_list_clear();
1986 
1987                 /* Add parent process as first child item */
1988                 proc_list_add(CurrentPid, "Queue runner child process",
1989                               PROC_QUEUE_CHILD, 0, -1, NULL);
1990                 (void) sm_releasesignal(SIGCHLD);
1991                 (void) sm_signal(SIGCHLD, SIG_DFL);
1992                 (void) sm_signal(SIGHUP, SIG_DFL);
1993                 (void) sm_signal(SIGTERM, intsig);
1994         }
1995 
1996         /*
1997         **  Release any resources used by the daemon code.
1998         */
1999 
2000         clrdaemon();
2001 
2002         /* force it to run expensive jobs */
2003         NoConnect = false;
2004 
2005         /* drop privileges */
2006         if (geteuid() == (uid_t) 0)
2007                 (void) drop_privileges(false);
2008 
2009         /*
2010         **  Create ourselves an envelope
2011         */
2012 
2013         CurEnv = &QueueEnvelope;
2014         rpool = sm_rpool_new_x(NULL);
2015         e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2016         e->e_flags = BlankEnvelope.e_flags;
2017         e->e_parent = NULL;
2018 
2019         /* make sure we have disconnected from parent */
2020         if (bitset(RWG_FORK, flags))
2021         {
2022                 disconnect(1, e);
2023                 QuickAbort = false;
2024         }
2025 
2026         /*
2027         **  If we are running part of the queue, always ignore stored
2028         **  host status.
2029         */
2030 
2031         if (QueueLimitId != NULL || QueueLimitSender != NULL ||
2032             QueueLimitQuarantine != NULL ||
2033             QueueLimitRecipient != NULL)
2034         {
2035                 IgnoreHostStatus = true;
2036                 MinQueueAge = 0;
2037         }
2038 
2039         /*
2040         **  Here is where we choose the queue group from the work group.
2041         **  The caller of the "domorework" label must setup a new envelope.
2042         */
2043 
2044         endgrp = WorkGrp[wgrp].wg_curqgrp; /* to not spin endlessly */
2045 
2046   domorework:
2047 
2048         /*
2049         **  Run a queue group if:
2050         **  RWG_RUNALL bit is set or the bit for this group is set.
2051         */
2052 
2053         now = curtime();
2054         for (;;)
2055         {
2056                 /*
2057                 **  Find the next queue group within the work group that
2058                 **  has been marked as needing a run.
2059                 */
2060 
2061                 qgrp = WorkGrp[wgrp].wg_qgs[WorkGrp[wgrp].wg_curqgrp]->qg_index;
2062                 WorkGrp[wgrp].wg_curqgrp++; /* advance */
2063                 WorkGrp[wgrp].wg_curqgrp %= WorkGrp[wgrp].wg_numqgrp; /* wrap */
2064                 if (bitset(RWG_RUNALL, flags) ||
2065                     (Queue[qgrp]->qg_nextrun <= now &&
2066                      Queue[qgrp]->qg_nextrun != (time_t) -1))
2067                         break;
2068                 if (endgrp == WorkGrp[wgrp].wg_curqgrp)
2069                 {
2070                         e->e_id = NULL;
2071                         if (bitset(RWG_FORK, flags))
2072                                 finis(true, true, ExitStat);
2073                         return true; /* we're done */
2074                 }
2075         }
2076 
2077         qdir = Queue[qgrp]->qg_curnum; /* round-robin init of queue position */
2078 #if _FFR_QUEUE_SCHED_DBG
2079         if (tTd(69, 12))
2080                 sm_syslog(LOG_INFO, NOQID,
2081                         "rwg: wgrp=%d, qgrp=%d, qdir=%d, name=%s, curqgrp=%d, numgrps=%d",
2082                         wgrp, qgrp, qdir, qid_printqueue(qgrp, qdir),
2083                         WorkGrp[wgrp].wg_curqgrp, WorkGrp[wgrp].wg_numqgrp);
2084 #endif /* _FFR_QUEUE_SCHED_DBG */
2085 
2086 #if HASNICE
2087         /* tweak niceness of queue runs */
2088         if (Queue[qgrp]->qg_nice > 0)
2089                 (void) nice(Queue[qgrp]->qg_nice);
2090 #endif /* HASNICE */
2091 
2092         /* XXX running queue group... */
2093         sm_setproctitle(true, CurEnv, "running queue: %s",
2094                         qid_printqueue(qgrp, qdir));
2095 
2096         if (LogLevel > 69 || tTd(63, 99))
2097                 sm_syslog(LOG_DEBUG, NOQID,
2098                           "runqueue %s, pid=%d, forkflag=%d",
2099                           qid_printqueue(qgrp, qdir), (int) CurrentPid,
2100                           bitset(RWG_FORK, flags));
2101 
2102         /*
2103         **  Start making passes through the queue.
2104         **      First, read and sort the entire queue.
2105         **      Then, process the work in that order.
2106         **              But if you take too long, start over.
2107         */
2108 
2109         for (i = 0; i < Queue[qgrp]->qg_numqueues; i++)
2110         {
2111                 (void) gatherq(qgrp, qdir, false, &full, &more, &h);
2112 #if SM_CONF_SHM
2113                 if (ShmId != SM_SHM_NO_ID)
2114                         QSHM_ENTRIES(Queue[qgrp]->qg_qpaths[qdir].qp_idx) = h;
2115 #endif /* SM_CONF_SHM */
2116                 /* If there are no more items in this queue advance */
2117                 if (!more)
2118                 {
2119                         /* A round-robin advance */
2120                         qdir++;
2121                         qdir %= Queue[qgrp]->qg_numqueues;
2122                 }
2123 
2124                 /* Has the WorkList reached the limit? */
2125                 if (full)
2126                         break; /* don't try to gather more */
2127         }
2128 
2129         /* order the existing work requests */
2130         njobs = sortq(Queue[qgrp]->qg_maxlist);
2131         Queue[qgrp]->qg_curnum = qdir; /* update */
2132 
2133 
2134         if (!Verbose && bitnset(QD_FORK, Queue[qgrp]->qg_flags))
2135         {
2136                 int loop, maxrunners;
2137                 pid_t pid;
2138 
2139                 /*
2140                 **  For this WorkQ we want to fork off N children (maxrunners)
2141                 **  at this point. Each child has a copy of WorkQ. Each child
2142                 **  will process every N-th item. The parent will wait for all
2143                 **  of the children to finish before moving on to the next
2144                 **  queue group within the work group. This saves us forking
2145                 **  a new runner-child for each work item.
2146                 **  It's valid for qg_maxqrun == 0 since this may be an
2147                 **  explicit "don't run this queue" setting.
2148                 */
2149 
2150                 maxrunners = Queue[qgrp]->qg_maxqrun;
2151 
2152                 /*
2153                 **  If no runners are configured for this group but
2154                 **  the queue is "forced" then lets use 1 runner.
2155                 */
2156 
2157                 if (maxrunners == 0 && bitset(RWG_FORCE, flags))
2158                         maxrunners = 1;
2159 
2160                 /* No need to have more runners then there are jobs */
2161                 if (maxrunners > njobs)
2162                         maxrunners = njobs;
2163                 for (loop = 0; loop < maxrunners; loop++)
2164                 {
2165                         /*
2166                         **  Since the delivery may happen in a child and the
2167                         **  parent does not wait, the parent may close the
2168                         **  maps thereby removing any shared memory used by
2169                         **  the map.  Therefore, close the maps now so the
2170                         **  child will dynamically open them if necessary.
2171                         */
2172 
2173                         closemaps(false);
2174 
2175                         pid = fork();
2176                         if (pid < 0)
2177                         {
2178                                 syserr("run_work_group: cannot fork");
2179                                 return false;
2180                         }
2181                         else if (pid > 0)
2182                         {
2183                                 /* parent -- clean out connection cache */
2184                                 mci_flush(false, NULL);
2185 #if _FFR_SKIP_DOMAINS
2186                                 if (QueueSortOrder == QSO_BYHOST)
2187                                 {
2188                                         sequenceno += skip_domains(1);
2189                                 }
2190                                 else
2191 #endif /* _FFR_SKIP_DOMAINS */
2192                                 {
2193                                         /* for the skip */
2194                                         WorkQ = WorkQ->w_next;
2195                                         sequenceno++;
2196                                 }
2197                                 proc_list_add(pid, "Queue child runner process",
2198                                               PROC_QUEUE_CHILD, 0, -1, NULL);
2199 
2200                                 /* No additional work, no additional runners */
2201                                 if (WorkQ == NULL)
2202                                         break;
2203                         }
2204                         else
2205                         {
2206                                 /* child -- Reset global flags */
2207                                 RestartRequest = NULL;
2208                                 RestartWorkGroup = false;
2209                                 ShutdownRequest = NULL;
2210                                 PendingSignal = 0;
2211                                 CurrentPid = getpid();
2212                                 close_sendmail_pid();
2213 
2214                                 /*
2215                                 **  Initialize exception stack and default
2216                                 **  exception handler for child process.
2217                                 **  When fork()'d the child now has a private
2218                                 **  copy of WorkQ at its current position.
2219                                 */
2220 
2221                                 sm_exc_newthread(fatal_error);
2222 
2223                                 /*
2224                                 **  SMTP processes (whether -bd or -bs) set
2225                                 **  SIGCHLD to reapchild to collect
2226                                 **  children status.  However, at delivery
2227                                 **  time, that status must be collected
2228                                 **  by sm_wait() to be dealt with properly
2229                                 **  (check success of delivery based
2230                                 **  on status code, etc).  Therefore, if we
2231                                 **  are an SMTP process, reset SIGCHLD
2232                                 **  back to the default so reapchild
2233                                 **  doesn't collect status before
2234                                 **  sm_wait().
2235                                 */
2236 
2237                                 if (OpMode == MD_SMTP ||
2238                                     OpMode == MD_DAEMON ||
2239                                     MaxQueueChildren > 0)
2240                                 {
2241                                         proc_list_clear();
2242                                         sm_releasesignal(SIGCHLD);
2243                                         (void) sm_signal(SIGCHLD, SIG_DFL);
2244                                 }
2245 
2246                                 /* child -- error messages to the transcript */
2247                                 QuickAbort = OnlyOneError = false;
2248                                 runner_work(e, sequenceno, true,
2249                                             maxrunners, njobs);
2250 
2251                                 /* This child is done */
2252                                 finis(true, true, ExitStat);
2253                                 /* NOTREACHED */
2254                         }
2255                 }
2256 
2257                 sm_releasesignal(SIGCHLD);
2258 
2259                 /*
2260                 **  Wait until all of the runners have completed before
2261                 **  seeing if there is another queue group in the
2262                 **  work group to process.
2263                 **  XXX Future enhancement: don't wait() for all children
2264                 **  here, just go ahead and make sure that overall the number
2265                 **  of children is not exceeded.
2266                 */
2267 
2268                 while (CurChildren > 0)
2269                 {
2270                         int status;
2271                         pid_t ret;
2272 
2273                         while ((ret = sm_wait(&status)) <= 0)
2274                                 continue;
2275                         proc_list_drop(ret, status, NULL);
2276                 }
2277         }
2278         else if (Queue[qgrp]->qg_maxqrun > 0 || bitset(RWG_FORCE, flags))
2279         {
2280                 /*
2281                 **  When current process will not fork children to do the work,
2282                 **  it will do the work itself. The 'skip' will be 1 since
2283                 **  there are no child runners to divide the work across.
2284                 */
2285 
2286                 runner_work(e, sequenceno, false, 1, njobs);
2287         }
2288 
2289         /* free memory allocated by newenvelope() above */
2290         sm_rpool_free(rpool);
2291         QueueEnvelope.e_rpool = NULL;
2292 
2293         /* Are there still more queues in the work group to process? */
2294         if (endgrp != WorkGrp[wgrp].wg_curqgrp)
2295         {
2296                 rpool = sm_rpool_new_x(NULL);
2297                 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2298                 e->e_flags = BlankEnvelope.e_flags;
2299                 goto domorework;
2300         }
2301 
2302         /* No more queues in work group to process. Now check persistent. */
2303         if (bitset(RWG_PERSISTENT, flags))
2304         {
2305                 sequenceno = 1;
2306                 sm_setproctitle(true, CurEnv, "running queue: %s",
2307                                 qid_printqueue(qgrp, qdir));
2308 
2309                 /*
2310                 **  close bogus maps, i.e., maps which caused a tempfail,
2311                 **      so we get fresh map connections on the next lookup.
2312                 **  closemaps() is also called when children are started.
2313                 */
2314 
2315                 closemaps(true);
2316 
2317                 /* Close any cached connections. */
2318                 mci_flush(true, NULL);
2319 
2320                 /* Clean out expired related entries. */
2321                 rmexpstab();
2322 
2323 #if NAMED_BIND
2324                 /* Update MX records for FallbackMX. */
2325                 if (FallbackMX != NULL)
2326                         (void) getfallbackmxrr(FallbackMX);
2327 #endif /* NAMED_BIND */
2328 
2329 #if USERDB
2330                 /* close UserDatabase */
2331                 _udbx_close();
2332 #endif /* USERDB */
2333 
2334 #if SM_HEAP_CHECK
2335                 if (sm_debug_active(&SmHeapCheck, 2)
2336                     && access("memdump", F_OK) == 0
2337                    )
2338                 {
2339                         SM_FILE_T *out;
2340 
2341                         remove("memdump");
2342                         out = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2343                                          "memdump.out", SM_IO_APPEND, NULL);
2344                         if (out != NULL)
2345                         {
2346                                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "----------------------\n");
2347                                 sm_heap_report(out,
2348                                         sm_debug_level(&SmHeapCheck) - 1);
2349                                 (void) sm_io_close(out, SM_TIME_DEFAULT);
2350                         }
2351                 }
2352 #endif /* SM_HEAP_CHECK */
2353 
2354                 /* let me rest for a second to catch my breath */
2355                 if (njobs == 0 && WorkGrp[wgrp].wg_lowqintvl < MIN_SLEEP_TIME)
2356                         sleep(MIN_SLEEP_TIME);
2357                 else if (WorkGrp[wgrp].wg_lowqintvl <= 0)
2358                         sleep(QueueIntvl > 0 ? QueueIntvl : MIN_SLEEP_TIME);
2359                 else
2360                         sleep(WorkGrp[wgrp].wg_lowqintvl);
2361 
2362                 /*
2363                 **  Get the LA outside the WorkQ loop if necessary.
2364                 **  In a persistent queue runner the code is repeated over
2365                 **  and over but gatherq() may ignore entries due to
2366                 **  shouldqueue() (do we really have to do this twice?).
2367                 **  Hence the queue runners would just idle around when once
2368                 **  CurrentLA caused all entries in a queue to be ignored.
2369                 */
2370 
2371                 if (njobs == 0)
2372                         SM_GET_LA(now);
2373                 rpool = sm_rpool_new_x(NULL);
2374                 e = newenvelope(&QueueEnvelope, CurEnv, rpool);
2375                 e->e_flags = BlankEnvelope.e_flags;
2376                 goto domorework;
2377         }
2378 
2379         /* exit without the usual cleanup */
2380         e->e_id = NULL;
2381         if (bitset(RWG_FORK, flags))
2382                 finis(true, true, ExitStat);
2383         /* NOTREACHED */
2384         return true;
2385 }
2386 
2387 /*
2388 **  DOQUEUERUN -- do a queue run?
2389 */
2390 
2391 bool
2392 doqueuerun()
2393 {
2394         return DoQueueRun;
2395 }
2396 
2397 /*
2398 **  RUNQUEUEEVENT -- Sets a flag to indicate that a queue run should be done.
2399 **
2400 **      Parameters:
2401 **              none.
2402 **
2403 **      Returns:
2404 **              none.
2405 **
2406 **      Side Effects:
2407 **              The invocation of this function via an alarm may interrupt
2408 **              a set of actions. Thus errno may be set in that context.
2409 **              We need to restore errno at the end of this function to ensure
2410 **              that any work done here that sets errno doesn't return a
2411 **              misleading/false errno value. Errno may be EINTR upon entry to
2412 **              this function because of non-restartable/continuable system
2413 **              API was active. Iff this is true we will override errno as
2414 **              a timeout (as a more accurate error message).
2415 **
2416 **      NOTE:   THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
2417 **              ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2418 **              DOING.
2419 */
2420 
2421 void
2422 runqueueevent(ignore)
2423         int ignore;
2424 {
2425         int save_errno = errno;
2426 
2427         /*
2428         **  Set the general bit that we want a queue run,
2429         **  tested in doqueuerun()
2430         */
2431 
2432         DoQueueRun = true;
2433 #if _FFR_QUEUE_SCHED_DBG
2434         if (tTd(69, 10))
2435                 sm_syslog(LOG_INFO, NOQID, "rqe: done");
2436 #endif /* _FFR_QUEUE_SCHED_DBG */
2437 
2438         errno = save_errno;
2439         if (errno == EINTR)
2440                 errno = ETIMEDOUT;
2441 }
2442 /*
2443 **  GATHERQ -- gather messages from the message queue(s) the work queue.
2444 **
2445 **      Parameters:
2446 **              qgrp -- the index of the queue group.
2447 **              qdir -- the index of the queue directory.
2448 **              doall -- if set, include everything in the queue (even
2449 **                      the jobs that cannot be run because the load
2450 **                      average is too high, or MaxQueueRun is reached).
2451 **                      Otherwise, exclude those jobs.
2452 **              full -- (optional) to be set 'true' if WorkList is full
2453 **              more -- (optional) to be set 'true' if there are still more
2454 **                      messages in this queue not added to WorkList
2455 **              pnentries -- (optional) total nuber of entries in queue
2456 **
2457 **      Returns:
2458 **              The number of request in the queue (not necessarily
2459 **              the number of requests in WorkList however).
2460 **
2461 **      Side Effects:
2462 **              prepares available work into WorkList
2463 */
2464 
2465 #define NEED_P          0001    /* 'P': priority */
2466 #define NEED_T          0002    /* 'T': time */
2467 #define NEED_R          0004    /* 'R': recipient */
2468 #define NEED_S          0010    /* 'S': sender */
2469 #define NEED_H          0020    /* host */
2470 #define HAS_QUARANTINE  0040    /* has an unexpected 'q' line */
2471 #define NEED_QUARANTINE 0100    /* 'q': reason */
2472 
2473 static WORK     *WorkList = NULL;       /* list of unsort work */
2474 static int      WorkListSize = 0;       /* current max size of WorkList */
2475 static int      WorkListCount = 0;      /* # of work items in WorkList */
2476 
2477 static int
2478 gatherq(qgrp, qdir, doall, full, more, pnentries)
2479         int qgrp;
2480         int qdir;
2481         bool doall;
2482         bool *full;
2483         bool *more;
2484         int *pnentries;
2485 {
2486         register struct dirent *d;
2487         register WORK *w;
2488         register char *p;
2489         DIR *f;
2490         int i, num_ent, wn, nentries;
2491         QUEUE_CHAR *check;
2492         char qd[MAXPATHLEN];
2493         char qf[MAXPATHLEN];
2494 
2495         wn = WorkListCount - 1;
2496         num_ent = 0;
2497         nentries = 0;
2498         if (qdir == NOQDIR)
2499                 (void) sm_strlcpy(qd, ".", sizeof(qd));
2500         else
2501                 (void) sm_strlcpyn(qd, sizeof(qd), 2,
2502                         Queue[qgrp]->qg_qpaths[qdir].qp_name,
2503                         (bitset(QP_SUBQF,
2504                                 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
2505                                         ? "/qf" : ""));
2506 
2507         if (tTd(41, 1))
2508         {
2509                 sm_dprintf("gatherq:\n");
2510 
2511                 check = QueueLimitId;
2512                 while (check != NULL)
2513                 {
2514                         sm_dprintf("\tQueueLimitId = %s%s\n",
2515                                 check->queue_negate ? "!" : "",
2516                                 check->queue_match);
2517                         check = check->queue_next;
2518                 }
2519 
2520                 check = QueueLimitSender;
2521                 while (check != NULL)
2522                 {
2523                         sm_dprintf("\tQueueLimitSender = %s%s\n",
2524                                 check->queue_negate ? "!" : "",
2525                                 check->queue_match);
2526                         check = check->queue_next;
2527                 }
2528 
2529                 check = QueueLimitRecipient;
2530                 while (check != NULL)
2531                 {
2532                         sm_dprintf("\tQueueLimitRecipient = %s%s\n",
2533                                 check->queue_negate ? "!" : "",
2534                                 check->queue_match);
2535                         check = check->queue_next;
2536                 }
2537 
2538                 if (QueueMode == QM_QUARANTINE)
2539                 {
2540                         check = QueueLimitQuarantine;
2541                         while (check != NULL)
2542                         {
2543                                 sm_dprintf("\tQueueLimitQuarantine = %s%s\n",
2544                                            check->queue_negate ? "!" : "",
2545                                            check->queue_match);
2546                                 check = check->queue_next;
2547                         }
2548                 }
2549         }
2550 
2551         /* open the queue directory */
2552         f = opendir(qd);
2553         if (f == NULL)
2554         {
2555                 syserr("gatherq: cannot open \"%s\"",
2556                         qid_printqueue(qgrp, qdir));
2557                 if (full != NULL)
2558                         *full = WorkListCount >= MaxQueueRun && MaxQueueRun > 0;
2559                 if (more != NULL)
2560                         *more = false;
2561                 return 0;
2562         }
2563 
2564         /*
2565         **  Read the work directory.
2566         */
2567 
2568         while ((d = readdir(f)) != NULL)
2569         {
2570                 SM_FILE_T *cf;
2571                 int qfver = 0;
2572                 char lbuf[MAXNAME + 1];
2573                 struct stat sbuf;
2574 
2575                 if (tTd(41, 50))
2576                         sm_dprintf("gatherq: checking %s..", d->d_name);
2577 
2578                 /* is this an interesting entry? */
2579                 if (!(((QueueMode == QM_NORMAL &&
2580                         d->d_name[0] == NORMQF_LETTER) ||
2581                        (QueueMode == QM_QUARANTINE &&
2582                         d->d_name[0] == QUARQF_LETTER) ||
2583                        (QueueMode == QM_LOST &&
2584                         d->d_name[0] == LOSEQF_LETTER)) &&
2585                       d->d_name[1] == 'f'))
2586                 {
2587                         if (tTd(41, 50))
2588                                 sm_dprintf("  skipping\n");
2589                         continue;
2590                 }
2591                 if (tTd(41, 50))
2592                         sm_dprintf("\n");
2593 
2594                 if (strlen(d->d_name) >= MAXQFNAME)
2595                 {
2596                         if (Verbose)
2597                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2598                                                      "gatherq: %s too long, %d max characters\n",
2599                                                      d->d_name, MAXQFNAME);
2600                         if (LogLevel > 0)
2601                                 sm_syslog(LOG_ALERT, NOQID,
2602                                           "gatherq: %s too long, %d max characters",
2603                                           d->d_name, MAXQFNAME);
2604                         continue;
2605                 }
2606 
2607                 ++nentries;
2608                 check = QueueLimitId;
2609                 while (check != NULL)
2610                 {
2611                         if (strcontainedin(false, check->queue_match,
2612                                            d->d_name) != check->queue_negate)
2613                                 break;
2614                         else
2615                                 check = check->queue_next;
2616                 }
2617                 if (QueueLimitId != NULL && check == NULL)
2618                         continue;
2619 
2620                 /* grow work list if necessary */
2621                 if (++wn >= MaxQueueRun && MaxQueueRun > 0)
2622                 {
2623                         if (wn == MaxQueueRun && LogLevel > 0)
2624                                 sm_syslog(LOG_WARNING, NOQID,
2625                                           "WorkList for %s maxed out at %d",
2626                                           qid_printqueue(qgrp, qdir),
2627                                           MaxQueueRun);
2628                         if (doall)
2629                                 continue;       /* just count entries */
2630                         break;
2631                 }
2632                 if (wn >= WorkListSize)
2633                 {
2634                         grow_wlist(qgrp, qdir);
2635                         if (wn >= WorkListSize)
2636                                 continue;
2637                 }
2638                 SM_ASSERT(wn >= 0);
2639                 w = &WorkList[wn];
2640 
2641                 (void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", d->d_name);
2642                 if (stat(qf, &sbuf) < 0)
2643                 {
2644                         if (errno != ENOENT)
2645                                 sm_syslog(LOG_INFO, NOQID,
2646                                           "gatherq: can't stat %s/%s",
2647                                           qid_printqueue(qgrp, qdir),
2648                                           d->d_name);
2649                         wn--;
2650                         continue;
2651                 }
2652                 if (!bitset(S_IFREG, sbuf.st_mode))
2653                 {
2654                         /* Yikes!  Skip it or we will hang on open! */
2655                         if (!((d->d_name[0] == DATAFL_LETTER ||
2656                                d->d_name[0] == NORMQF_LETTER ||
2657                                d->d_name[0] == QUARQF_LETTER ||
2658                                d->d_name[0] == LOSEQF_LETTER ||
2659                                d->d_name[0] == XSCRPT_LETTER) &&
2660                               d->d_name[1] == 'f' && d->d_name[2] == '\0'))
2661                                 syserr("gatherq: %s/%s is not a regular file",
2662                                        qid_printqueue(qgrp, qdir), d->d_name);
2663                         wn--;
2664                         continue;
2665                 }
2666 
2667                 /* avoid work if possible */
2668                 if ((QueueSortOrder == QSO_BYFILENAME ||
2669                      QueueSortOrder == QSO_BYMODTIME ||
2670                      QueueSortOrder == QSO_NONE ||
2671                      QueueSortOrder == QSO_RANDOM) &&
2672                     QueueLimitQuarantine == NULL &&
2673                     QueueLimitSender == NULL &&
2674                     QueueLimitRecipient == NULL)
2675                 {
2676                         w->w_qgrp = qgrp;
2677                         w->w_qdir = qdir;
2678                         w->w_name = newstr(d->d_name);
2679                         w->w_host = NULL;
2680                         w->w_lock = w->w_tooyoung = false;
2681                         w->w_pri = 0;
2682                         w->w_ctime = 0;
2683                         w->w_mtime = sbuf.st_mtime;
2684                         ++num_ent;
2685                         continue;
2686                 }
2687 
2688                 /* open control file */
2689                 cf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
2690                                 NULL);
2691                 if (cf == NULL && OpMode != MD_PRINT)
2692                 {
2693                         /* this may be some random person sending hir msgs */
2694                         if (tTd(41, 2))
2695                                 sm_dprintf("gatherq: cannot open %s: %s\n",
2696                                         d->d_name, sm_errstring(errno));
2697                         errno = 0;
2698                         wn--;
2699                         continue;
2700                 }
2701                 w->w_qgrp = qgrp;
2702                 w->w_qdir = qdir;
2703                 w->w_name = newstr(d->d_name);
2704                 w->w_host = NULL;
2705                 if (cf != NULL)
2706                 {
2707                         w->w_lock = !lockfile(sm_io_getinfo(cf, SM_IO_WHAT_FD,
2708                                                             NULL),
2709                                               w->w_name, NULL,
2710                                               LOCK_SH|LOCK_NB);
2711                 }
2712                 w->w_tooyoung = false;
2713 
2714                 /* make sure jobs in creation don't clog queue */
2715                 w->w_pri = 0x7fffffff;
2716                 w->w_ctime = 0;
2717                 w->w_mtime = sbuf.st_mtime;
2718 
2719                 /* extract useful information */
2720                 i = NEED_P|NEED_T;
2721                 if (QueueSortOrder == QSO_BYHOST
2722 #if _FFR_RHS
2723                     || QueueSortOrder == QSO_BYSHUFFLE
2724 #endif /* _FFR_RHS */
2725                    )
2726                 {
2727                         /* need w_host set for host sort order */
2728                         i |= NEED_H;
2729                 }
2730                 if (QueueLimitSender != NULL)
2731                         i |= NEED_S;
2732                 if (QueueLimitRecipient != NULL)
2733                         i |= NEED_R;
2734                 if (QueueLimitQuarantine != NULL)
2735                         i |= NEED_QUARANTINE;
2736                 while (cf != NULL && i != 0 &&
2737                        sm_io_fgets(cf, SM_TIME_DEFAULT, lbuf,
2738                                    sizeof(lbuf)) != NULL)
2739                 {
2740                         int c;
2741                         time_t age;
2742 
2743                         p = strchr(lbuf, '\n');
2744                         if (p != NULL)
2745                                 *p = '\0';
2746                         else
2747                         {
2748                                 /* flush rest of overly long line */
2749                                 while ((c = sm_io_getc(cf, SM_TIME_DEFAULT))
2750                                        != SM_IO_EOF && c != '\n')
2751                                         continue;
2752                         }
2753 
2754                         switch (lbuf[0])
2755                         {
2756                           case 'V':
2757                                 qfver = atoi(&lbuf[1]);
2758                                 break;
2759 
2760                           case 'P':
2761                                 w->w_pri = atol(&lbuf[1]);
2762                                 i &= ~NEED_P;
2763                                 break;
2764 
2765                           case 'T':
2766                                 w->w_ctime = atol(&lbuf[1]);
2767                                 i &= ~NEED_T;
2768                                 break;
2769 
2770                           case 'q':
2771                                 if (QueueMode != QM_QUARANTINE &&
2772                                     QueueMode != QM_LOST)
2773                                 {
2774                                         if (tTd(41, 49))
2775                                                 sm_dprintf("%s not marked as quarantined but has a 'q' line\n",
2776                                                            w->w_name);
2777                                         i |= HAS_QUARANTINE;
2778                                 }
2779                                 else if (QueueMode == QM_QUARANTINE)
2780                                 {
2781                                         if (QueueLimitQuarantine == NULL)
2782                                         {
2783                                                 i &= ~NEED_QUARANTINE;
2784                                                 break;
2785                                         }
2786                                         p = &lbuf[1];
2787                                         check = QueueLimitQuarantine;
2788                                         while (check != NULL)
2789                                         {
2790                                                 if (strcontainedin(false,
2791                                                                    check->queue_match,
2792                                                                    p) !=
2793                                                     check->queue_negate)
2794                                                         break;
2795                                                 else
2796                                                         check = check->queue_next;
2797                                         }
2798                                         if (check != NULL)
2799                                                 i &= ~NEED_QUARANTINE;
2800                                 }
2801                                 break;
2802 
2803                           case 'R':
2804                                 if (w->w_host == NULL &&
2805                                     (p = strrchr(&lbuf[1], '@')) != NULL)
2806                                 {
2807 #if _FFR_RHS
2808                                         if (QueueSortOrder == QSO_BYSHUFFLE)
2809                                                 w->w_host = newstr(&p[1]);
2810                                         else
2811 #endif /* _FFR_RHS */
2812                                                 w->w_host = strrev(&p[1]);
2813                                         makelower(w->w_host);
2814                                         i &= ~NEED_H;
2815                                 }
2816                                 if (QueueLimitRecipient == NULL)
2817                                 {
2818                                         i &= ~NEED_R;
2819                                         break;
2820                                 }
2821                                 if (qfver > 0)
2822                                 {
2823                                         p = strchr(&lbuf[1], ':');
2824                                         if (p == NULL)
2825                                                 p = &lbuf[1];
2826                                         else
2827                                                 ++p; /* skip over ':' */
2828                                 }
2829                                 else
2830                                         p = &lbuf[1];
2831                                 check = QueueLimitRecipient;
2832                                 while (check != NULL)
2833                                 {
2834                                         if (strcontainedin(true,
2835                                                            check->queue_match,
2836                                                            p) !=
2837                                             check->queue_negate)
2838                                                 break;
2839                                         else
2840                                                 check = check->queue_next;
2841                                 }
2842                                 if (check != NULL)
2843                                         i &= ~NEED_R;
2844                                 break;
2845 
2846                           case 'S':
2847                                 check = QueueLimitSender;
2848                                 while (check != NULL)
2849                                 {
2850                                         if (strcontainedin(true,
2851                                                            check->queue_match,
2852                                                            &lbuf[1]) !=
2853                                             check->queue_negate)
2854                                                 break;
2855                                         else
2856                                                 check = check->queue_next;
2857                                 }
2858                                 if (check != NULL)
2859                                         i &= ~NEED_S;
2860                                 break;
2861 
2862                           case 'K':
2863 #if _FFR_EXPDELAY
2864                                 if (MaxQueueAge > 0)
2865                                 {
2866                                         time_t lasttry, delay;    
2867 
2868                                         lasttry = (time_t) atol(&lbuf[1]);
2869                                         delay = MIN(lasttry - w->w_ctime,
2870                                                     MaxQueueAge);
2871                                         age = curtime() - lasttry;
2872                                         if (age < delay)
2873                                                 w->w_tooyoung = true;
2874                                         break;
2875                                 }
2876 #endif /* _FFR_EXPDELAY */
2877 
2878                                 age = curtime() - (time_t) atol(&lbuf[1]);
2879                                 if (age >= 0 && MinQueueAge > 0 &&
2880                                     age < MinQueueAge)
2881                                         w->w_tooyoung = true;
2882                                 break;
2883 
2884                           case 'N':
2885                                 if (atol(&lbuf[1]) == 0)
2886                                         w->w_tooyoung = false;
2887                                 break;
2888                         }
2889                 }
2890                 if (cf != NULL)
2891                         (void) sm_io_close(cf, SM_TIME_DEFAULT);
2892 
2893                 if ((!doall && (shouldqueue(w->w_pri, w->w_ctime) ||
2894                     w->w_tooyoung)) ||
2895                     bitset(HAS_QUARANTINE, i) ||
2896                     bitset(NEED_QUARANTINE, i) ||
2897                     bitset(NEED_R|NEED_S, i))
2898                 {
2899                         /* don't even bother sorting this job in */
2900                         if (tTd(41, 49))
2901                                 sm_dprintf("skipping %s (%x)\n", w->w_name, i);
2902                         sm_free(w->w_name); /* XXX */
2903                         if (w->w_host != NULL)
2904                                 sm_free(w->w_host); /* XXX */
2905                         wn--;
2906                 }
2907                 else
2908                         ++num_ent;
2909         }
2910         (void) closedir(f);
2911         wn++;
2912 
2913         i = wn - WorkListCount;
2914         WorkListCount += SM_MIN(num_ent, WorkListSize);
2915 
2916         if (more != NULL)
2917                 *more = WorkListCount < wn;
2918 
2919         if (full != NULL)
2920                 *full = (wn >= MaxQueueRun && MaxQueueRun > 0) ||
2921                         (WorkList == NULL && wn > 0);
2922 
2923         if (pnentries != NULL)
2924                 *pnentries = nentries;
2925         return i;
2926 }
2927 /*
2928 **  SORTQ -- sort the work list
2929 **
2930 **      First the old WorkQ is cleared away. Then the WorkList is sorted
2931 **      for all items so that important (higher sorting value) items are not
2932 **      trunctated off. Then the most important items are moved from
2933 **      WorkList to WorkQ. The lower count of 'max' or MaxListCount items
2934 **      are moved.
2935 **
2936 **      Parameters:
2937 **              max -- maximum number of items to be placed in WorkQ
2938 **
2939 **      Returns:
2940 **              the number of items in WorkQ
2941 **
2942 **      Side Effects:
2943 **              WorkQ gets released and filled with new work. WorkList
2944 **              gets released. Work items get sorted in order.
2945 */
2946 
2947 static int
2948 sortq(max)
2949         int max;
2950 {
2951         register int i;                 /* local counter */
2952         register WORK *w;               /* tmp item pointer */
2953         int wc = WorkListCount;         /* trim size for WorkQ */
2954 
2955         if (WorkQ != NULL)
2956         {
2957                 WORK *nw;
2958 
2959                 /* Clear out old WorkQ. */
2960                 for (w = WorkQ; w != NULL; w = nw)
2961                 {
2962                         nw = w->w_next;
2963                         sm_free(w->w_name); /* XXX */
2964                         if (w->w_host != NULL)
2965                                 sm_free(w->w_host); /* XXX */
2966                         sm_free((char *) w); /* XXX */
2967                 }
2968                 WorkQ = NULL;
2969         }
2970 
2971         if (WorkList == NULL || wc <= 0)
2972                 return 0;
2973 
2974         /*
2975         **  The sort now takes place using all of the items in WorkList.
2976         **  The list gets trimmed to the most important items after the sort.
2977         **  If the trim were to happen before the sort then one or more
2978         **  important items might get truncated off -- not what we want.
2979         */
2980 
2981         if (QueueSortOrder == QSO_BYHOST)
2982         {
2983                 /*
2984                 **  Sort the work directory for the first time,
2985                 **  based on host name, lock status, and priority.
2986                 */
2987 
2988                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf1);
2989 
2990                 /*
2991                 **  If one message to host is locked, "lock" all messages
2992                 **  to that host.
2993                 */
2994 
2995                 i = 0;
2996                 while (i < wc)
2997                 {
2998                         if (!WorkList[i].w_lock)
2999                         {
3000                                 i++;
3001                                 continue;
3002                         }
3003                         w = &WorkList[i];
3004                         while (++i < wc)
3005                         {
3006                                 if (WorkList[i].w_host == NULL &&
3007                                     w->w_host == NULL)
3008                                         WorkList[i].w_lock = true;
3009                                 else if (WorkList[i].w_host != NULL &&
3010                                          w->w_host != NULL &&
3011                                          sm_strcasecmp(WorkList[i].w_host,
3012                                                        w->w_host) == 0)
3013                                         WorkList[i].w_lock = true;
3014                                 else
3015                                         break;
3016                         }
3017                 }
3018 
3019                 /*
3020                 **  Sort the work directory for the second time,
3021                 **  based on lock status, host name, and priority.
3022                 */
3023 
3024                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf2);
3025         }
3026         else if (QueueSortOrder == QSO_BYTIME)
3027         {
3028                 /*
3029                 **  Simple sort based on submission time only.
3030                 */
3031 
3032                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf3);
3033         }
3034         else if (QueueSortOrder == QSO_BYFILENAME)
3035         {
3036                 /*
3037                 **  Sort based on queue filename.
3038                 */
3039 
3040                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf4);
3041         }
3042         else if (QueueSortOrder == QSO_RANDOM)
3043         {
3044                 /*
3045                 **  Sort randomly.  To avoid problems with an instable sort,
3046                 **  use a random index into the queue file name to start
3047                 **  comparison.
3048                 */
3049 
3050                 randi = get_rand_mod(MAXQFNAME);
3051                 if (randi < 2)
3052                         randi = 3;
3053                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf5);
3054         }
3055         else if (QueueSortOrder == QSO_BYMODTIME)
3056         {
3057                 /*
3058                 **  Simple sort based on modification time of queue file.
3059                 **  This puts the oldest items first.
3060                 */
3061 
3062                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf6);
3063         }
3064 #if _FFR_RHS
3065         else if (QueueSortOrder == QSO_BYSHUFFLE)
3066         {
3067                 /*
3068                 **  Simple sort based on shuffled host name.
3069                 */
3070 
3071                 init_shuffle_alphabet();
3072                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf7);
3073         }
3074 #endif /* _FFR_RHS */
3075         else if (QueueSortOrder == QSO_BYPRIORITY)
3076         {
3077                 /*
3078                 **  Simple sort based on queue priority only.
3079                 */
3080 
3081                 qsort((char *) WorkList, wc, sizeof(*WorkList), workcmpf0);
3082         }
3083         /* else don't sort at all */
3084 
3085         /* Check if the per queue group item limit will be exceeded */
3086         if (wc > max && max > 0)
3087                 wc = max;
3088 
3089         /*
3090         **  Convert the work list into canonical form.
3091         **      Should be turning it into a list of envelopes here perhaps.
3092         **  Only take the most important items up to the per queue group
3093         **  maximum.
3094         */
3095 
3096         for (i = wc; --i >= 0; )
3097         {
3098                 w = (WORK *) xalloc(sizeof(*w));
3099                 w->w_qgrp = WorkList[i].w_qgrp;
3100                 w->w_qdir = WorkList[i].w_qdir;
3101                 w->w_name = WorkList[i].w_name;
3102                 w->w_host = WorkList[i].w_host;
3103                 w->w_lock = WorkList[i].w_lock;
3104                 w->w_tooyoung = WorkList[i].w_tooyoung;
3105                 w->w_pri = WorkList[i].w_pri;
3106                 w->w_ctime = WorkList[i].w_ctime;
3107                 w->w_mtime = WorkList[i].w_mtime;
3108                 w->w_next = WorkQ;
3109                 WorkQ = w;
3110         }
3111 
3112         /* free the rest of the list */
3113         for (i = WorkListCount; --i >= wc; )
3114         {
3115                 sm_free(WorkList[i].w_name);
3116                 if (WorkList[i].w_host != NULL)
3117                         sm_free(WorkList[i].w_host);
3118         }
3119 
3120         if (WorkList != NULL)
3121                 sm_free(WorkList); /* XXX */
3122         WorkList = NULL;
3123         WorkListSize = 0;
3124         WorkListCount = 0;
3125 
3126         if (tTd(40, 1))
3127         {
3128                 for (w = WorkQ; w != NULL; w = w->w_next)
3129                 {
3130                         if (w->w_host != NULL)
3131                                 sm_dprintf("%22s: pri=%ld %s\n",
3132                                         w->w_name, w->w_pri, w->w_host);
3133                         else
3134                                 sm_dprintf("%32s: pri=%ld\n",
3135                                         w->w_name, w->w_pri);
3136                 }
3137         }
3138 
3139         return wc; /* return number of WorkQ items */
3140 }
3141 /*
3142 **  GROW_WLIST -- make the work list larger
3143 **
3144 **      Parameters:
3145 **              qgrp -- the index for the queue group.
3146 **              qdir -- the index for the queue directory.
3147 **
3148 **      Returns:
3149 **              none.
3150 **
3151 **      Side Effects:
3152 **              Adds another QUEUESEGSIZE entries to WorkList if possible.
3153 **              It can fail if there isn't enough memory, so WorkListSize
3154 **              should be checked again upon return.
3155 */
3156 
3157 static void
3158 grow_wlist(qgrp, qdir)
3159         int qgrp;
3160         int qdir;
3161 {
3162         if (tTd(41, 1))
3163                 sm_dprintf("grow_wlist: WorkListSize=%d\n", WorkListSize);
3164         if (WorkList == NULL)
3165         {
3166                 WorkList = (WORK *) xalloc((sizeof(*WorkList)) *
3167                                            (QUEUESEGSIZE + 1));
3168                 WorkListSize = QUEUESEGSIZE;
3169         }
3170         else
3171         {
3172                 int newsize = WorkListSize + QUEUESEGSIZE;
3173                 WORK *newlist = (WORK *) sm_realloc((char *) WorkList,
3174                                           (unsigned) sizeof(WORK) * (newsize + 1));
3175 
3176                 if (newlist != NULL)
3177                 {
3178                         WorkListSize = newsize;
3179                         WorkList = newlist;
3180                         if (LogLevel > 1)
3181                         {
3182                                 sm_syslog(LOG_INFO, NOQID,
3183                                           "grew WorkList for %s to %d",
3184                                           qid_printqueue(qgrp, qdir),
3185                                           WorkListSize);
3186                         }
3187                 }
3188                 else if (LogLevel > 0)
3189                 {
3190                         sm_syslog(LOG_ALERT, NOQID,
3191                                   "FAILED to grow WorkList for %s to %d",
3192                                   qid_printqueue(qgrp, qdir), newsize);
3193                 }
3194         }
3195         if (tTd(41, 1))
3196                 sm_dprintf("grow_wlist: WorkListSize now %d\n", WorkListSize);
3197 }
3198 /*
3199 **  WORKCMPF0 -- simple priority-only compare function.
3200 **
3201 **      Parameters:
3202 **              a -- the first argument.
3203 **              b -- the second argument.
3204 **
3205 **      Returns:
3206 **              -1 if a < b
3207 **               0 if a == b
3208 **              +1 if a > b
3209 **
3210 */
3211 
3212 static int
3213 workcmpf0(a, b)
3214         register WORK *a;
3215         register WORK *b;
3216 {
3217         long pa = a->w_pri;
3218         long pb = b->w_pri;
3219 
3220         if (pa == pb)
3221                 return 0;
3222         else if (pa > pb)
3223                 return 1;
3224         else
3225                 return -1;
3226 }
3227 /*
3228 **  WORKCMPF1 -- first compare function for ordering work based on host name.
3229 **
3230 **      Sorts on host name, lock status, and priority in that order.
3231 **
3232 **      Parameters:
3233 **              a -- the first argument.
3234 **              b -- the second argument.
3235 **
3236 **      Returns:
3237 **              <0 if a < b
3238 **               0 if a == b
3239 **              >0 if a > b
3240 **
3241 */
3242 
3243 static int
3244 workcmpf1(a, b)
3245         register WORK *a;
3246         register WORK *b;
3247 {
3248         int i;
3249 
3250         /* host name */
3251         if (a->w_host != NULL && b->w_host == NULL)
3252                 return 1;
3253         else if (a->w_host == NULL && b->w_host != NULL)
3254                 return -1;
3255         if (a->w_host != NULL && b->w_host != NULL &&
3256             (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3257                 return i;
3258 
3259         /* lock status */
3260         if (a->w_lock != b->w_lock)
3261                 return b->w_lock - a->w_lock;
3262 
3263         /* job priority */
3264         return workcmpf0(a, b);
3265 }
3266 /*
3267 **  WORKCMPF2 -- second compare function for ordering work based on host name.
3268 **
3269 **      Sorts on lock status, host name, and priority in that order.
3270 **
3271 **      Parameters:
3272 **              a -- the first argument.
3273 **              b -- the second argument.
3274 **
3275 **      Returns:
3276 **              <0 if a < b
3277 **               0 if a == b
3278 **              >0 if a > b
3279 **
3280 */
3281 
3282 static int
3283 workcmpf2(a, b)
3284         register WORK *a;
3285         register WORK *b;
3286 {
3287         int i;
3288 
3289         /* lock status */
3290         if (a->w_lock != b->w_lock)
3291                 return a->w_lock - b->w_lock;
3292 
3293         /* host name */
3294         if (a->w_host != NULL && b->w_host == NULL)
3295                 return 1;
3296         else if (a->w_host == NULL && b->w_host != NULL)
3297                 return -1;
3298         if (a->w_host != NULL && b->w_host != NULL &&
3299             (i = sm_strcasecmp(a->w_host, b->w_host)) != 0)
3300                 return i;
3301 
3302         /* job priority */
3303         return workcmpf0(a, b);
3304 }
3305 /*
3306 **  WORKCMPF3 -- simple submission-time-only compare function.
3307 **
3308 **      Parameters:
3309 **              a -- the first argument.
3310 **              b -- the second argument.
3311 **
3312 **      Returns:
3313 **              -1 if a < b
3314 **               0 if a == b
3315 **              +1 if a > b
3316 **
3317 */
3318 
3319 static int
3320 workcmpf3(a, b)
3321         register WORK *a;
3322         register WORK *b;
3323 {
3324         if (a->w_ctime > b->w_ctime)
3325                 return 1;
3326         else if (a->w_ctime < b->w_ctime)
3327                 return -1;
3328         else
3329                 return 0;
3330 }
3331 /*
3332 **  WORKCMPF4 -- compare based on file name
3333 **
3334 **      Parameters:
3335 **              a -- the first argument.
3336 **              b -- the second argument.
3337 **
3338 **      Returns:
3339 **              -1 if a < b
3340 **               0 if a == b
3341 **              +1 if a > b
3342 **
3343 */
3344 
3345 static int
3346 workcmpf4(a, b)
3347         register WORK *a;
3348         register WORK *b;
3349 {
3350         return strcmp(a->w_name, b->w_name);
3351 }
3352 /*
3353 **  WORKCMPF5 -- compare based on assigned random number
3354 **
3355 **      Parameters:
3356 **              a -- the first argument.
3357 **              b -- the second argument.
3358 **
3359 **      Returns:
3360 **              randomly 1/-1
3361 */
3362 
3363 /* ARGSUSED0 */
3364 static int
3365 workcmpf5(a, b)
3366         register WORK *a;
3367         register WORK *b;
3368 {
3369         if (strlen(a->w_name) < randi || strlen(b->w_name) < randi)
3370                 return -1;
3371         return a->w_name[randi] - b->w_name[randi];
3372 }
3373 /*
3374 **  WORKCMPF6 -- simple modification-time-only compare function.
3375 **
3376 **      Parameters:
3377 **              a -- the first argument.
3378 **              b -- the second argument.
3379 **
3380 **      Returns:
3381 **              -1 if a < b
3382 **               0 if a == b
3383 **              +1 if a > b
3384 **
3385 */
3386 
3387 static int
3388 workcmpf6(a, b)
3389         register WORK *a;
3390         register WORK *b;
3391 {
3392         if (a->w_mtime > b->w_mtime)
3393                 return 1;
3394         else if (a->w_mtime < b->w_mtime)
3395                 return -1;
3396         else
3397                 return 0;
3398 }
3399 #if _FFR_RHS
3400 /*
3401 **  WORKCMPF7 -- compare function for ordering work based on shuffled host name.
3402 **
3403 **      Sorts on lock status, host name, and priority in that order.
3404 **
3405 **      Parameters:
3406 **              a -- the first argument.
3407 **              b -- the second argument.
3408 **
3409 **      Returns:
3410 **              <0 if a < b
3411 **               0 if a == b
3412 **              >0 if a > b
3413 **
3414 */
3415 
3416 static int
3417 workcmpf7(a, b)
3418         register WORK *a;
3419         register WORK *b;
3420 {
3421         int i;
3422 
3423         /* lock status */
3424         if (a->w_lock != b->w_lock)
3425                 return a->w_lock - b->w_lock;
3426 
3427         /* host name */
3428         if (a->w_host != NULL && b->w_host == NULL)
3429                 return 1;
3430         else if (a->w_host == NULL && b->w_host != NULL)
3431                 return -1;
3432         if (a->w_host != NULL && b->w_host != NULL &&
3433             (i = sm_strshufflecmp(a->w_host, b->w_host)) != 0)
3434                 return i;
3435 
3436         /* job priority */
3437         return workcmpf0(a, b);
3438 }
3439 #endif /* _FFR_RHS */
3440 /*
3441 **  STRREV -- reverse string
3442 **
3443 **      Returns a pointer to a new string that is the reverse of
3444 **      the string pointed to by fwd.  The space for the new
3445 **      string is obtained using xalloc().
3446 **
3447 **      Parameters:
3448 **              fwd -- the string to reverse.
3449 **
3450 **      Returns:
3451 **              the reversed string.
3452 */
3453 
3454 static char *
3455 strrev(fwd)
3456         char *fwd;
3457 {
3458         char *rev = NULL;
3459         int len, cnt;
3460 
3461         len = strlen(fwd);
3462         rev = xalloc(len + 1);
3463         for (cnt = 0; cnt < len; ++cnt)
3464                 rev[cnt] = fwd[len - cnt - 1];
3465         rev[len] = '\0';
3466         return rev;
3467 }
3468 
3469 #if _FFR_RHS
3470 
3471 # define NASCII 128
3472 # define NCHAR  256
3473 
3474 static unsigned char ShuffledAlphabet[NCHAR];
3475 
3476 void
3477 init_shuffle_alphabet()
3478 {
3479         static bool init = false;
3480         int i;
3481 
3482         if (init)
3483                 return;
3484 
3485         /* fill the ShuffledAlphabet */
3486         for (i = 0; i < NASCII; i++)
3487                 ShuffledAlphabet[i] = i;
3488 
3489         /* mix it */
3490         for (i = 1; i < NASCII; i++)
3491         {
3492                 register int j = get_random() % NASCII;
3493                 register int tmp;
3494 
3495                 tmp = ShuffledAlphabet[j];
3496                 ShuffledAlphabet[j] = ShuffledAlphabet[i];
3497                 ShuffledAlphabet[i] = tmp;
3498         }
3499 
3500         /* make it case insensitive */
3501         for (i = 'A'; i <= 'Z'; i++)
3502                 ShuffledAlphabet[i] = ShuffledAlphabet[i + 'a' - 'A'];
3503 
3504         /* fill the upper part */
3505         for (i = 0; i < NASCII; i++)
3506                 ShuffledAlphabet[i + NASCII] = ShuffledAlphabet[i];
3507         init = true;
3508 }
3509 
3510 static int
3511 sm_strshufflecmp(a, b)
3512         char *a;
3513         char *b;
3514 {
3515         const unsigned char *us1 = (const unsigned char *) a;
3516         const unsigned char *us2 = (const unsigned char *) b;
3517 
3518         while (ShuffledAlphabet[*us1] == ShuffledAlphabet[*us2++])
3519         {
3520                 if (*us1++ == '\0')
3521                         return 0;
3522         }
3523         return (ShuffledAlphabet[*us1] - ShuffledAlphabet[*--us2]);
3524 }
3525 #endif /* _FFR_RHS */
3526 
3527 /*
3528 **  DOWORK -- do a work request.
3529 **
3530 **      Parameters:
3531 **              qgrp -- the index of the queue group for the job.
3532 **              qdir -- the index of the queue directory for the job.
3533 **              id -- the ID of the job to run.
3534 **              forkflag -- if set, run this in background.
3535 **              requeueflag -- if set, reinstantiate the queue quickly.
3536 **                      This is used when expanding aliases in the queue.
3537 **                      If forkflag is also set, it doesn't wait for the
3538 **                      child.
3539 **              e - the envelope in which to run it.
3540 **
3541 **      Returns:
3542 **              process id of process that is running the queue job.
3543 **
3544 **      Side Effects:
3545 **              The work request is satisfied if possible.
3546 */
3547 
3548 pid_t
3549 dowork(qgrp, qdir, id, forkflag, requeueflag, e)
3550         int qgrp;
3551         int qdir;
3552         char *id;
3553         bool forkflag;
3554         bool requeueflag;
3555         register ENVELOPE *e;
3556 {
3557         register pid_t pid;
3558         SM_RPOOL_T *rpool;
3559 
3560         if (tTd(40, 1))
3561                 sm_dprintf("dowork(%s/%s)\n", qid_printqueue(qgrp, qdir), id);
3562 
3563         /*
3564         **  Fork for work.
3565         */
3566 
3567         if (forkflag)
3568         {
3569                 /*
3570                 **  Since the delivery may happen in a child and the
3571                 **  parent does not wait, the parent may close the
3572                 **  maps thereby removing any shared memory used by
3573                 **  the map.  Therefore, close the maps now so the
3574                 **  child will dynamically open them if necessary.
3575                 */
3576 
3577                 closemaps(false);
3578 
3579                 pid = fork();
3580                 if (pid < 0)
3581                 {
3582                         syserr("dowork: cannot fork");
3583                         return 0;
3584                 }
3585                 else if (pid > 0)
3586                 {
3587                         /* parent -- clean out connection cache */
3588                         mci_flush(false, NULL);
3589                 }
3590                 else
3591                 {
3592                         /*
3593                         **  Initialize exception stack and default exception
3594                         **  handler for child process.
3595                         */
3596 
3597                         /* Reset global flags */
3598                         RestartRequest = NULL;
3599                         RestartWorkGroup = false;
3600                         ShutdownRequest = NULL;
3601                         PendingSignal = 0;
3602                         CurrentPid = getpid();
3603                         sm_exc_newthread(fatal_error);
3604 
3605                         /*
3606                         **  See note above about SMTP processes and SIGCHLD.
3607                         */
3608 
3609                         if (OpMode == MD_SMTP ||
3610                             OpMode == MD_DAEMON ||
3611                             MaxQueueChildren > 0)
3612                         {
3613                                 proc_list_clear();
3614                                 sm_releasesignal(SIGCHLD);
3615                                 (void) sm_signal(SIGCHLD, SIG_DFL);
3616                         }
3617 
3618                         /* child -- error messages to the transcript */
3619                         QuickAbort = OnlyOneError = false;
3620                 }
3621         }
3622         else
3623         {
3624                 pid = 0;
3625         }
3626 
3627         if (pid == 0)
3628         {
3629                 /*
3630                 **  CHILD
3631                 **      Lock the control file to avoid duplicate deliveries.
3632                 **              Then run the file as though we had just read it.
3633                 **      We save an idea of the temporary name so we
3634                 **              can recover on interrupt.
3635                 */
3636 
3637                 if (forkflag)
3638                 {
3639                         /* Reset global flags */
3640                         RestartRequest = NULL;
3641                         RestartWorkGroup = false;
3642                         ShutdownRequest = NULL;
3643                         PendingSignal = 0;
3644                 }
3645 
3646                 /* set basic modes, etc. */
3647                 sm_clear_events();
3648                 clearstats();
3649                 rpool = sm_rpool_new_x(NULL);
3650                 clearenvelope(e, false, rpool);
3651                 e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3652                 set_delivery_mode(SM_DELIVER, e);
3653                 e->e_errormode = EM_MAIL;
3654                 e->e_id = id;
3655                 e->e_qgrp = qgrp;
3656                 e->e_qdir = qdir;
3657                 GrabTo = UseErrorsTo = false;
3658                 ExitStat = EX_OK;
3659                 if (forkflag)
3660                 {
3661                         disconnect(1, e);
3662                         set_op_mode(MD_QUEUERUN);
3663                 }
3664                 sm_setproctitle(true, e, "%s from queue", qid_printname(e));
3665                 if (LogLevel > 76)
3666                         sm_syslog(LOG_DEBUG, e->e_id, "dowork, pid=%d",
3667                                   (int) CurrentPid);
3668 
3669                 /* don't use the headers from sendmail.cf... */
3670                 e->e_header = NULL;
3671 
3672                 /* read the queue control file -- return if locked */
3673                 if (!readqf(e, false))
3674                 {
3675                         if (tTd(40, 4) && e->e_id != NULL)
3676                                 sm_dprintf("readqf(%s) failed\n",
3677                                         qid_printname(e));
3678                         e->e_id = NULL;
3679                         if (forkflag)
3680                                 finis(false, true, EX_OK);
3681                         else
3682                         {
3683                                 /* adding this frees 8 bytes */
3684                                 clearenvelope(e, false, rpool);
3685 
3686                                 /* adding this frees 12 bytes */
3687                                 sm_rpool_free(rpool);
3688                                 e->e_rpool = NULL;
3689                                 return 0;
3690                         }
3691                 }
3692 
3693                 e->e_flags |= EF_INQUEUE;
3694                 eatheader(e, requeueflag, true);
3695 
3696                 if (requeueflag)
3697                         queueup(e, false, false);
3698 
3699                 /* do the delivery */
3700                 sendall(e, SM_DELIVER);
3701 
3702                 /* finish up and exit */
3703                 if (forkflag)
3704                         finis(true, true, ExitStat);
3705                 else
3706                 {
3707                         (void) dropenvelope(e, true, false);
3708                         sm_rpool_free(rpool);
3709                         e->e_rpool = NULL;
3710                 }
3711         }
3712         e->e_id = NULL;
3713         return pid;
3714 }
3715 
3716 /*
3717 **  DOWORKLIST -- process a list of envelopes as work requests
3718 **
3719 **      Similar to dowork(), except that after forking, it processes an
3720 **      envelope and its siblings, treating each envelope as a work request.
3721 **
3722 **      Parameters:
3723 **              el -- envelope to be processed including its siblings.
3724 **              forkflag -- if set, run this in background.
3725 **              requeueflag -- if set, reinstantiate the queue quickly.
3726 **                      This is used when expanding aliases in the queue.
3727 **                      If forkflag is also set, it doesn't wait for the
3728 **                      child.
3729 **
3730 **      Returns:
3731 **              process id of process that is running the queue job.
3732 **
3733 **      Side Effects:
3734 **              The work request is satisfied if possible.
3735 */
3736 
3737 pid_t
3738 doworklist(el, forkflag, requeueflag)
3739         ENVELOPE *el;
3740         bool forkflag;
3741         bool requeueflag;
3742 {
3743         register pid_t pid;
3744         ENVELOPE *ei;
3745 
3746         if (tTd(40, 1))
3747                 sm_dprintf("doworklist()\n");
3748 
3749         /*
3750         **  Fork for work.
3751         */
3752 
3753         if (forkflag)
3754         {
3755                 /*
3756                 **  Since the delivery may happen in a child and the
3757                 **  parent does not wait, the parent may close the
3758                 **  maps thereby removing any shared memory used by
3759                 **  the map.  Therefore, close the maps now so the
3760                 **  child will dynamically open them if necessary.
3761                 */
3762 
3763                 closemaps(false);
3764 
3765                 pid = fork();
3766                 if (pid < 0)
3767                 {
3768                         syserr("doworklist: cannot fork");
3769                         return 0;
3770                 }
3771                 else if (pid > 0)
3772                 {
3773                         /* parent -- clean out connection cache */
3774                         mci_flush(false, NULL);
3775                 }
3776                 else
3777                 {
3778                         /*
3779                         **  Initialize exception stack and default exception
3780                         **  handler for child process.
3781                         */
3782 
3783                         /* Reset global flags */
3784                         RestartRequest = NULL;
3785                         RestartWorkGroup = false;
3786                         ShutdownRequest = NULL;
3787                         PendingSignal = 0;
3788                         CurrentPid = getpid();
3789                         sm_exc_newthread(fatal_error);
3790 
3791                         /*
3792                         **  See note above about SMTP processes and SIGCHLD.
3793                         */
3794 
3795                         if (OpMode == MD_SMTP ||
3796                             OpMode == MD_DAEMON ||
3797                             MaxQueueChildren > 0)
3798                         {
3799                                 proc_list_clear();
3800                                 sm_releasesignal(SIGCHLD);
3801                                 (void) sm_signal(SIGCHLD, SIG_DFL);
3802                         }
3803 
3804                         /* child -- error messages to the transcript */
3805                         QuickAbort = OnlyOneError = false;
3806                 }
3807         }
3808         else
3809         {
3810                 pid = 0;
3811         }
3812 
3813         if (pid != 0)
3814                 return pid;
3815 
3816         /*
3817         **  IN CHILD
3818         **      Lock the control file to avoid duplicate deliveries.
3819         **              Then run the file as though we had just read it.
3820         **      We save an idea of the temporary name so we
3821         **              can recover on interrupt.
3822         */
3823 
3824         if (forkflag)
3825         {
3826                 /* Reset global flags */
3827                 RestartRequest = NULL;
3828                 RestartWorkGroup = false;
3829                 ShutdownRequest = NULL;
3830                 PendingSignal = 0;
3831         }
3832 
3833         /* set basic modes, etc. */
3834         sm_clear_events();
3835         clearstats();
3836         GrabTo = UseErrorsTo = false;
3837         ExitStat = EX_OK;
3838         if (forkflag)
3839         {
3840                 disconnect(1, el);
3841                 set_op_mode(MD_QUEUERUN);
3842         }
3843         if (LogLevel > 76)
3844                 sm_syslog(LOG_DEBUG, el->e_id, "doworklist, pid=%d",
3845                           (int) CurrentPid);
3846 
3847         for (ei = el; ei != NULL; ei = ei->e_sibling)
3848         {
3849                 ENVELOPE e;
3850                 SM_RPOOL_T *rpool;
3851 
3852                 if (WILL_BE_QUEUED(ei->e_sendmode))
3853                         continue;
3854                 else if (QueueMode != QM_QUARANTINE &&
3855                          ei->e_quarmsg != NULL)
3856                         continue;
3857 
3858                 rpool = sm_rpool_new_x(NULL);
3859                 clearenvelope(&e, true, rpool);
3860                 e.e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
3861                 set_delivery_mode(SM_DELIVER, &e);
3862                 e.e_errormode = EM_MAIL;
3863                 e.e_id = ei->e_id;
3864                 e.e_qgrp = ei->e_qgrp;
3865                 e.e_qdir = ei->e_qdir;
3866                 openxscript(&e);
3867                 sm_setproctitle(true, &e, "%s from queue", qid_printname(&e));
3868 
3869                 /* don't use the headers from sendmail.cf... */
3870                 e.e_header = NULL;
3871                 CurEnv = &e;
3872 
3873                 /* read the queue control file -- return if locked */
3874                 if (readqf(&e, false))
3875                 {
3876                         e.e_flags |= EF_INQUEUE;
3877                         eatheader(&e, requeueflag, true);
3878 
3879                         if (requeueflag)
3880                                 queueup(&e, false, false);
3881 
3882                         /* do the delivery */
3883                         sendall(&e, SM_DELIVER);
3884                         (void) dropenvelope(&e, true, false);
3885                 }
3886                 else
3887                 {
3888                         if (tTd(40, 4) && e.e_id != NULL)
3889                                 sm_dprintf("readqf(%s) failed\n",
3890                                         qid_printname(&e));
3891                 }
3892                 sm_rpool_free(rpool);
3893                 ei->e_id = NULL;
3894         }
3895 
3896         /* restore CurEnv */
3897         CurEnv = el;
3898 
3899         /* finish up and exit */
3900         if (forkflag)
3901                 finis(true, true, ExitStat);
3902         return 0;
3903 }
3904 /*
3905 **  READQF -- read queue file and set up environment.
3906 **
3907 **      Parameters:
3908 **              e -- the envelope of the job to run.
3909 **              openonly -- only open the qf (returned as e_lockfp)
3910 **
3911 **      Returns:
3912 **              true if it successfully read the queue file.
3913 **              false otherwise.
3914 **
3915 **      Side Effects:
3916 **              The queue file is returned locked.
3917 */
3918 
3919 static bool
3920 readqf(e, openonly)
3921         register ENVELOPE *e;
3922         bool openonly;
3923 {
3924         register SM_FILE_T *qfp;
3925         ADDRESS *ctladdr;
3926         struct stat st, stf;
3927         char *bp;
3928         int qfver = 0;
3929         long hdrsize = 0;
3930         register char *p;
3931         char *frcpt = NULL;
3932         char *orcpt = NULL;
3933         bool nomore = false;
3934         bool bogus = false;
3935         MODE_T qsafe;
3936         char *err;
3937         char qf[MAXPATHLEN];
3938         char buf[MAXLINE];
3939         int bufsize;
3940 
3941         /*
3942         **  Read and process the file.
3943         */
3944 
3945         SM_REQUIRE(e != NULL);
3946         bp = NULL;
3947         (void) sm_strlcpy(qf, queuename(e, ANYQFL_LETTER), sizeof(qf));
3948         qfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDWR_B, NULL);
3949         if (qfp == NULL)
3950         {
3951                 int save_errno = errno;
3952 
3953                 if (tTd(40, 8))
3954                         sm_dprintf("readqf(%s): sm_io_open failure (%s)\n",
3955                                 qf, sm_errstring(errno));
3956                 errno = save_errno;
3957                 if (errno != ENOENT
3958                     )
3959                         syserr("readqf: no control file %s", qf);
3960                 RELEASE_QUEUE;
3961                 return false;
3962         }
3963 
3964         if (!lockfile(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), qf, NULL,
3965                       LOCK_EX|LOCK_NB))
3966         {
3967                 /* being processed by another queuer */
3968                 if (Verbose)
3969                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3970                                              "%s: locked\n", e->e_id);
3971                 if (tTd(40, 8))
3972                         sm_dprintf("%s: locked\n", e->e_id);
3973                 if (LogLevel > 19)
3974                         sm_syslog(LOG_DEBUG, e->e_id, "locked");
3975                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
3976                 RELEASE_QUEUE;
3977                 return false;
3978         }
3979 
3980         RELEASE_QUEUE;
3981 
3982         /*
3983         **  Prevent locking race condition.
3984         **
3985         **  Process A: readqf(): qfp = fopen(qffile)
3986         **  Process B: queueup(): rename(tf, qf)
3987         **  Process B: unlocks(tf)
3988         **  Process A: lockfile(qf);
3989         **
3990         **  Process A (us) has the old qf file (before the rename deleted
3991         **  the directory entry) and will be delivering based on old data.
3992         **  This can lead to multiple deliveries of the same recipients.
3993         **
3994         **  Catch this by checking if the underlying qf file has changed
3995         **  *after* acquiring our lock and if so, act as though the file
3996         **  was still locked (i.e., just return like the lockfile() case
3997         **  above.
3998         */
3999 
4000         if (stat(qf, &stf) < 0 ||
4001             fstat(sm_io_getinfo(qfp, SM_IO_WHAT_FD, NULL), &st) < 0)
4002         {
4003                 /* must have been being processed by someone else */
4004                 if (tTd(40, 8))
4005                         sm_dprintf("readqf(%s): [f]stat failure (%s)\n",
4006                                 qf, sm_errstring(errno));
4007                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4008                 return false;
4009         }
4010 
4011         if (st.st_nlink != stf.st_nlink ||
4012             st.st_dev != stf.st_dev ||
4013             ST_INODE(st) != ST_INODE(stf) ||
4014 #if HAS_ST_GEN && 0             /* AFS returns garbage in st_gen */
4015             st.st_gen != stf.st_gen ||
4016 #endif /* HAS_ST_GEN && 0 */
4017             st.st_uid != stf.st_uid ||
4018             st.st_gid != stf.st_gid ||
4019             st.st_size != stf.st_size)
4020         {
4021                 /* changed after opened */
4022                 if (Verbose)
4023                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4024                                              "%s: changed\n", e->e_id);
4025                 if (tTd(40, 8))
4026                         sm_dprintf("%s: changed\n", e->e_id);
4027                 if (LogLevel > 19)
4028                         sm_syslog(LOG_DEBUG, e->e_id, "changed");
4029                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4030                 return false;
4031         }
4032 
4033         /*
4034         **  Check the queue file for plausibility to avoid attacks.
4035         */
4036 
4037         qsafe = S_IWOTH|S_IWGRP;
4038         if (bitset(S_IWGRP, QueueFileMode))
4039                 qsafe &= ~S_IWGRP;
4040 
4041         bogus = st.st_uid != geteuid() &&
4042                 st.st_uid != TrustedUid &&
4043                 geteuid() != RealUid;
4044 
4045         /*
4046         **  If this qf file results from a set-group-ID binary, then
4047         **  we check whether the directory is group-writable,
4048         **  the queue file mode contains the group-writable bit, and
4049         **  the groups are the same.
4050         **  Notice: this requires that the set-group-ID binary is used to
4051         **  run the queue!
4052         */
4053 
4054         if (bogus && st.st_gid == getegid() && UseMSP)
4055         {
4056                 char delim;
4057                 struct stat dst;
4058 
4059                 bp = SM_LAST_DIR_DELIM(qf);
4060                 if (bp == NULL)
4061                         delim = '\0';
4062                 else
4063                 {
4064                         delim = *bp;
4065                         *bp = '\0';
4066                 }
4067                 if (stat(delim == '\0' ? "." : qf, &dst) < 0)
4068                         syserr("readqf: cannot stat directory %s",
4069                                 delim == '\0' ? "." : qf);
4070                 else
4071                 {
4072                         bogus = !(bitset(S_IWGRP, QueueFileMode) &&
4073                                   bitset(S_IWGRP, dst.st_mode) &&
4074                                   dst.st_gid == st.st_gid);
4075                 }
4076                 if (delim != '\0')
4077                         *bp = delim;
4078                 bp = NULL;
4079         }
4080         if (!bogus)
4081                 bogus = bitset(qsafe, st.st_mode);
4082         if (bogus)
4083         {
4084                 if (LogLevel > 0)
4085                 {
4086                         sm_syslog(LOG_ALERT, e->e_id,
4087                                   "bogus queue file, uid=%d, gid=%d, mode=%o",
4088                                   st.st_uid, st.st_gid, st.st_mode);
4089                 }
4090                 if (tTd(40, 8))
4091                         sm_dprintf("readqf(%s): bogus file\n", qf);
4092                 e->e_flags |= EF_INQUEUE;
4093                 if (!openonly)
4094                         loseqfile(e, "bogus file uid/gid in mqueue");
4095                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4096                 return false;
4097         }
4098 
4099         if (st.st_size == 0)
4100         {
4101                 /* must be a bogus file -- if also old, just remove it */
4102                 if (!openonly && st.st_ctime + 10 * 60 < curtime())
4103                 {
4104                         (void) xunlink(queuename(e, DATAFL_LETTER));
4105                         (void) xunlink(queuename(e, ANYQFL_LETTER));
4106                 }
4107                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4108                 return false;
4109         }
4110 
4111         if (st.st_nlink == 0)
4112         {
4113                 /*
4114                 **  Race condition -- we got a file just as it was being
4115                 **  unlinked.  Just assume it is zero length.
4116                 */
4117 
4118                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4119                 return false;
4120         }
4121 
4122 #if _FFR_TRUSTED_QF
4123         /*
4124         **  If we don't own the file mark it as unsafe.
4125         **  However, allow TrustedUser to own it as well
4126         **  in case TrustedUser manipulates the queue.
4127         */
4128 
4129         if (st.st_uid != geteuid() && st.st_uid != TrustedUid)
4130                 e->e_flags |= EF_UNSAFE;
4131 #else /* _FFR_TRUSTED_QF */
4132         /* If we don't own the file mark it as unsafe */
4133         if (st.st_uid != geteuid())
4134                 e->e_flags |= EF_UNSAFE;
4135 #endif /* _FFR_TRUSTED_QF */
4136 
4137         /* good file -- save this lock */
4138         e->e_lockfp = qfp;
4139 
4140         /* Just wanted the open file */
4141         if (openonly)
4142                 return true;
4143 
4144         /* do basic system initialization */
4145         initsys(e);
4146         macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
4147 
4148         LineNumber = 0;
4149         e->e_flags |= EF_GLOBALERRS;
4150         set_op_mode(MD_QUEUERUN);
4151         ctladdr = NULL;
4152         e->e_qfletter = queue_letter(e, ANYQFL_LETTER);
4153         e->e_dfqgrp = e->e_qgrp;
4154         e->e_dfqdir = e->e_qdir;
4155 #if _FFR_QUEUE_MACRO
4156         macdefine(&e->e_macro, A_TEMP, macid("{queue}"),
4157                   qid_printqueue(e->e_qgrp, e->e_qdir));
4158 #endif /* _FFR_QUEUE_MACRO */
4159         e->e_dfino = -1;
4160         e->e_msgsize = -1;
4161         while (bufsize = sizeof(buf),
4162                (bp = fgetfolded(buf, &bufsize, qfp)) != NULL)
4163         {
4164                 unsigned long qflags;
4165                 ADDRESS *q;
4166                 int r;
4167                 time_t now;
4168                 auto char *ep;
4169 
4170                 if (tTd(40, 4))
4171                         sm_dprintf("+++++ %s\n", bp);
4172                 if (nomore)
4173                 {
4174                         /* hack attack */
4175   hackattack:
4176                         syserr("SECURITY ALERT: extra or bogus data in queue file: %s",
4177                                bp);
4178                         err = "bogus queue line";
4179                         goto fail;
4180                 }
4181                 switch (bp[0])
4182                 {
4183                   case 'A':             /* AUTH= parameter */
4184                         if (!xtextok(&bp[1]))
4185                                 goto hackattack;
4186                         e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4187                         break;
4188 
4189                   case 'B':             /* body type */
4190                         r = check_bodytype(&bp[1]);
4191                         if (!BODYTYPE_VALID(r))
4192                                 goto hackattack;
4193                         e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4194                         break;
4195 
4196                   case 'C':             /* specify controlling user */
4197                         ctladdr = setctluser(&bp[1], qfver, e);
4198                         break;
4199 
4200                   case 'D':             /* data file name */
4201                         /* obsolete -- ignore */
4202                         break;
4203 
4204                   case 'd':             /* data file directory name */
4205                         {
4206                                 int qgrp, qdir;
4207 
4208 #if _FFR_MSP_PARANOIA
4209                                 /* forbid queue groups in MSP? */
4210                                 if (UseMSP)
4211                                         goto hackattack;
4212 #endif /* _FFR_MSP_PARANOIA */
4213                                 for (qgrp = 0;
4214                                      qgrp < NumQueue && Queue[qgrp] != NULL;
4215                                      ++qgrp)
4216                                 {
4217                                         for (qdir = 0;
4218                                              qdir < Queue[qgrp]->qg_numqueues;
4219                                              ++qdir)
4220                                         {
4221                                                 if (strcmp(&bp[1],
4222                                                            Queue[qgrp]->qg_qpaths[qdir].qp_name)
4223                                                     == 0)
4224                                                 {
4225                                                         e->e_dfqgrp = qgrp;
4226                                                         e->e_dfqdir = qdir;
4227                                                         goto done;
4228                                                 }
4229                                         }
4230                                 }
4231                                 err = "bogus queue file directory";
4232                                 goto fail;
4233                           done:
4234                                 break;
4235                         }
4236 
4237                   case 'E':             /* specify error recipient */
4238                         /* no longer used */
4239                         break;
4240 
4241                   case 'F':             /* flag bits */
4242                         if (strncmp(bp, "From ", 5) == 0)
4243                         {
4244                                 /* we are being spoofed! */
4245                                 syserr("SECURITY ALERT: bogus qf line %s", bp);
4246                                 err = "bogus queue line";
4247                                 goto fail;
4248                         }
4249                         for (p = &bp[1]; *p != '\0'; p++)
4250                         {
4251                                 switch (*p)
4252                                 {
4253                                   case '8':     /* has 8 bit data */
4254                                         e->e_flags |= EF_HAS8BIT;
4255                                         break;
4256 
4257                                   case 'b':     /* delete Bcc: header */
4258                                         e->e_flags |= EF_DELETE_BCC;
4259                                         break;
4260 
4261                                   case 'd':     /* envelope has DSN RET= */
4262                                         e->e_flags |= EF_RET_PARAM;
4263                                         break;
4264 
4265                                   case 'n':     /* don't return body */
4266                                         e->e_flags |= EF_NO_BODY_RETN;
4267                                         break;
4268 
4269                                   case 'r':     /* response */
4270                                         e->e_flags |= EF_RESPONSE;
4271                                         break;
4272 
4273                                   case 's':     /* split */
4274                                         e->e_flags |= EF_SPLIT;
4275                                         break;
4276 
4277                                   case 'w':     /* warning sent */
4278                                         e->e_flags |= EF_WARNING;
4279                                         break;
4280                                 }
4281                         }
4282                         break;
4283 
4284                   case 'q':             /* quarantine reason */
4285                         e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4286                         macdefine(&e->e_macro, A_PERM,
4287                                   macid("{quarantine}"), e->e_quarmsg);
4288                         break;
4289 
4290                   case 'H':             /* header */
4291 
4292                         /*
4293                         **  count size before chompheader() destroys the line.
4294                         **  this isn't accurate due to macro expansion, but
4295                         **  better than before. "-3" to skip H?? at least.
4296                         */
4297 
4298                         hdrsize += strlen(bp) - 3;
4299                         (void) chompheader(&bp[1], CHHDR_QUEUE, NULL, e);
4300                         break;
4301 
4302                   case 'I':             /* data file's inode number */
4303                         /* regenerated below */
4304                         break;
4305 
4306                   case 'K':             /* time of last delivery attempt */
4307                         e->e_dtime = atol(&buf[1]);
4308                         break;
4309 
4310                   case 'L':             /* Solaris Content-Length: */
4311                   case 'M':             /* message */
4312                         /* ignore this; we want a new message next time */
4313                         break;
4314 
4315                   case 'N':             /* number of delivery attempts */
4316                         e->e_ntries = atoi(&buf[1]);
4317 
4318                         /* if this has been tried recently, let it be */
4319                         now = curtime();
4320                         if (e->e_ntries > 0 && e->e_dtime <= now &&
4321                             now < e->e_dtime + MinQueueAge)
4322                         {
4323                                 char *howlong;
4324 
4325                                 howlong = pintvl(now - e->e_dtime, true);
4326                                 if (Verbose)
4327                                         (void) sm_io_fprintf(smioout,
4328                                                              SM_TIME_DEFAULT,
4329                                                              "%s: too young (%s)\n",
4330                                                              e->e_id, howlong);
4331                                 if (tTd(40, 8))
4332                                         sm_dprintf("%s: too young (%s)\n",
4333                                                 e->e_id, howlong);
4334                                 if (LogLevel > 19)
4335                                         sm_syslog(LOG_DEBUG, e->e_id,
4336                                                   "too young (%s)",
4337                                                   howlong);
4338                                 e->e_id = NULL;
4339                                 unlockqueue(e);
4340                                 if (bp != buf)
4341                                         sm_free(bp);
4342                                 return false;
4343                         }
4344                         macdefine(&e->e_macro, A_TEMP,
4345                                 macid("{ntries}"), &buf[1]);
4346 
4347 #if NAMED_BIND
4348                         /* adjust BIND parameters immediately */
4349                         if (e->e_ntries == 0)
4350                         {
4351                                 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
4352                                 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
4353                         }
4354                         else
4355                         {
4356                                 _res.retry = TimeOuts.res_retry[RES_TO_NORMAL];
4357                                 _res.retrans = TimeOuts.res_retrans[RES_TO_NORMAL];
4358                         }
4359 #endif /* NAMED_BIND */
4360                         break;
4361 
4362                   case 'P':             /* message priority */
4363                         e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
4364                         break;
4365 
4366                   case 'Q':             /* original recipient */
4367                         orcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4368                         break;
4369 
4370                   case 'r':             /* final recipient */
4371                         frcpt = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4372                         break;
4373 
4374                   case 'R':             /* specify recipient */
4375                         p = bp;
4376                         qflags = 0;
4377                         if (qfver >= 1)
4378                         {
4379                                 /* get flag bits */
4380                                 while (*++p != '\0' && *p != ':')
4381                                 {
4382                                         switch (*p)
4383                                         {
4384                                           case 'N':
4385                                                 qflags |= QHASNOTIFY;
4386                                                 break;
4387 
4388                                           case 'S':
4389                                                 qflags |= QPINGONSUCCESS;
4390                                                 break;
4391 
4392                                           case 'F':
4393                                                 qflags |= QPINGONFAILURE;
4394                                                 break;
4395 
4396                                           case 'D':
4397                                                 qflags |= QPINGONDELAY;
4398                                                 break;
4399 
4400                                           case 'P':
4401                                                 qflags |= QPRIMARY;
4402                                                 break;
4403 
4404                                           case 'A':
4405                                                 if (ctladdr != NULL)
4406                                                         ctladdr->q_flags |= QALIAS;
4407                                                 break;
4408 
4409                                           default: /* ignore or complain? */
4410                                                 break;
4411                                         }
4412                                 }
4413                         }
4414                         else
4415                                 qflags |= QPRIMARY;
4416                         macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4417                                 "e r");
4418                         if (*p != '\0')
4419                                 q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0',
4420                                                 NULL, e, true);
4421                         else
4422                                 q = NULL;
4423                         if (q != NULL)
4424                         {
4425                                 /* make sure we keep the current qgrp */
4426                                 if (ISVALIDQGRP(e->e_qgrp))
4427                                         q->q_qgrp = e->e_qgrp;
4428                                 q->q_alias = ctladdr;
4429                                 if (qfver >= 1)
4430                                         q->q_flags &= ~Q_PINGFLAGS;
4431                                 q->q_flags |= qflags;
4432                                 q->q_finalrcpt = frcpt;
4433                                 q->q_orcpt = orcpt;
4434                                 (void) recipient(q, &e->e_sendqueue, 0, e);
4435                         }
4436                         frcpt = NULL;
4437                         orcpt = NULL;
4438                         macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
4439                                 NULL);
4440                         break;
4441 
4442                   case 'S':             /* sender */
4443                         setsender(sm_rpool_strdup_x(e->e_rpool, &bp[1]),
4444                                   e, NULL, '\0', true);
4445                         break;
4446 
4447                   case 'T':             /* init time */
4448                         e->e_ctime = atol(&bp[1]);
4449                         break;
4450 
4451                   case 'V':             /* queue file version number */
4452                         qfver = atoi(&bp[1]);
4453                         if (qfver <= QF_VERSION)
4454                                 break;
4455                         syserr("Version number in queue file (%d) greater than max (%d)",
4456                                 qfver, QF_VERSION);
4457                         err = "unsupported queue file version";
4458                         goto fail;
4459                         /* NOTREACHED */
4460                         break;
4461 
4462                   case 'Z':             /* original envelope id from ESMTP */
4463                         e->e_envid = sm_rpool_strdup_x(e->e_rpool, &bp[1]);
4464                         macdefine(&e->e_macro, A_PERM,
4465                                 macid("{dsn_envid}"), e->e_envid);
4466                         break;
4467 
4468                   case '!':             /* deliver by */
4469 
4470                         /* format: flag (1 char) space long-integer */
4471                         e->e_dlvr_flag = buf[1];
4472                         e->e_deliver_by = strtol(&buf[3], NULL, 10);
4473 
4474                   case '$':             /* define macro */
4475                         {
4476                                 char *p;
4477 
4478                                 /* XXX elimate p? */
4479                                 r = macid_parse(&bp[1], &ep);
4480                                 if (r == 0)
4481                                         break;
4482                                 p = sm_rpool_strdup_x(e->e_rpool, ep);
4483                                 macdefine(&e->e_macro, A_PERM, r, p);
4484                         }
4485                         break;
4486 
4487                   case '.':             /* terminate file */
4488                         nomore = true;
4489                         break;
4490 
4491 #if _FFR_QUEUEDELAY
4492                   case 'G':
4493                   case 'Y':
4494 
4495                         /*
4496                         **  Maintain backward compatibility for
4497                         **  users who defined _FFR_QUEUEDELAY in
4498                         **  previous releases.  Remove this
4499                         **  code in 8.14 or 8.15.
4500                         */
4501 
4502                         if (qfver == 5 || qfver == 7)
4503                                 break;
4504 
4505                         /* If not qfver 5 or 7, then 'G' or 'Y' is invalid */
4506                         /* FALLTHROUGH */
4507 #endif /* _FFR_QUEUEDELAY */
4508 
4509                   default:
4510                         syserr("readqf: %s: line %d: bad line \"%s\"",
4511                                 qf, LineNumber, shortenstring(bp, MAXSHORTSTR));
4512                         err = "unrecognized line";
4513                         goto fail;
4514                 }
4515 
4516                 if (bp != buf)
4517                         SM_FREE(bp);
4518         }
4519 
4520         /*
4521         **  If we haven't read any lines, this queue file is empty.
4522         **  Arrange to remove it without referencing any null pointers.
4523         */
4524 
4525         if (LineNumber == 0)
4526         {
4527                 errno = 0;
4528                 e->e_flags |= EF_CLRQUEUE|EF_FATALERRS|EF_RESPONSE;
4529                 return true;
4530         }
4531 
4532         /* Check to make sure we have a complete queue file read */
4533         if (!nomore)
4534         {
4535                 syserr("readqf: %s: incomplete queue file read", qf);
4536                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4537                 return false;
4538         }
4539 
4540 #if _FFR_QF_PARANOIA
4541         /* Check to make sure key fields were read */
4542         if (e->e_from.q_mailer == NULL)
4543         {
4544                 syserr("readqf: %s: sender not specified in queue file", qf);
4545                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4546                 return false;
4547         }
4548         /* other checks? */
4549 #endif /* _FFR_QF_PARANOIA */
4550 
4551         /* possibly set ${dsn_ret} macro */
4552         if (bitset(EF_RET_PARAM, e->e_flags))
4553         {
4554                 if (bitset(EF_NO_BODY_RETN, e->e_flags))
4555                         macdefine(&e->e_macro, A_PERM,
4556                                 macid("{dsn_ret}"), "hdrs");
4557                 else
4558                         macdefine(&e->e_macro, A_PERM,
4559                                 macid("{dsn_ret}"), "full");
4560         }
4561 
4562         /*
4563         **  Arrange to read the data file.
4564         */
4565 
4566         p = queuename(e, DATAFL_LETTER);
4567         e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, p, SM_IO_RDONLY_B,
4568                               NULL);
4569         if (e->e_dfp == NULL)
4570         {
4571                 syserr("readqf: cannot open %s", p);
4572         }
4573         else
4574         {
4575                 e->e_flags |= EF_HAS_DF;
4576                 if (fstat(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD, NULL), &st)
4577                     >= 0)
4578                 {
4579                         e->e_msgsize = st.st_size + hdrsize;
4580                         e->e_dfdev = st.st_dev;
4581                         e->e_dfino = ST_INODE(st);
4582                         (void) sm_snprintf(buf, sizeof(buf), "%ld",
4583                                            e->e_msgsize);
4584                         macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"),
4585                                   buf);
4586                 }
4587         }
4588 
4589         return true;
4590 
4591   fail:
4592         /*
4593         **  There was some error reading the qf file (reason is in err var.)
4594         **  Cleanup:
4595         **      close file; clear e_lockfp since it is the same as qfp,
4596         **      hence it is invalid (as file) after qfp is closed;
4597         **      the qf file is on disk, so set the flag to avoid calling
4598         **      queueup() with bogus data.
4599         */
4600 
4601         if (bp != buf)
4602                 SM_FREE(bp);
4603         if (qfp != NULL)
4604                 (void) sm_io_close(qfp, SM_TIME_DEFAULT);
4605         e->e_lockfp = NULL;
4606         e->e_flags |= EF_INQUEUE;
4607         loseqfile(e, err);
4608         return false;
4609 }
4610 /*
4611 **  PRTSTR -- print a string, "unprintable" characters are shown as \oct
4612 **
4613 **      Parameters:
4614 **              s -- string to print
4615 **              ml -- maximum length of output
4616 **
4617 **      Returns:
4618 **              number of entries
4619 **
4620 **      Side Effects:
4621 **              Prints a string on stdout.
4622 */
4623 
4624 static void prtstr __P((char *, int));
4625 
4626 static void
4627 prtstr(s, ml)
4628         char *s;
4629         int ml;
4630 {
4631         int c;
4632 
4633         if (s == NULL)
4634                 return;
4635         while (ml-- > 0 && ((c = *s++) != '\0'))
4636         {
4637                 if (c == '\\')
4638                 {
4639                         if (ml-- > 0)
4640                         {
4641                                 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4642                                 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4643                         }
4644                 }
4645                 else if (isascii(c) && isprint(c))
4646                         (void) sm_io_putc(smioout, SM_TIME_DEFAULT, c);
4647                 else
4648                 {
4649                         if ((ml -= 3) > 0)
4650                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4651                                                      "\\%03o", c & 0xFF);
4652                 }
4653         }
4654 }
4655 /*
4656 **  PRINTNQE -- print out number of entries in the mail queue
4657 **
4658 **      Parameters:
4659 **              out -- output file pointer.
4660 **              prefix -- string to output in front of each line.
4661 **
4662 **      Returns:
4663 **              none.
4664 */
4665 
4666 void
4667 printnqe(out, prefix)
4668         SM_FILE_T *out;
4669         char *prefix;
4670 {
4671 #if SM_CONF_SHM
4672         int i, k = 0, nrequests = 0;
4673         bool unknown = false;
4674 
4675         if (ShmId == SM_SHM_NO_ID)
4676         {
4677                 if (prefix == NULL)
4678                         (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4679                                         "Data unavailable: shared memory not updated\n");
4680                 else
4681                         (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4682                                         "%sNOTCONFIGURED:-1\r\n", prefix);
4683                 return;
4684         }
4685         for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4686         {
4687                 int j;
4688 
4689                 k++;
4690                 for (j = 0; j < Queue[i]->qg_numqueues; j++)
4691                 {
4692                         int n;
4693 
4694                         if (StopRequest)
4695                                 stop_sendmail();
4696 
4697                         n = QSHM_ENTRIES(Queue[i]->qg_qpaths[j].qp_idx);
4698                         if (prefix != NULL)
4699                                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4700                                         "%s%s:%d\r\n",
4701                                         prefix, qid_printqueue(i, j), n);
4702                         else if (n < 0)
4703                         {
4704                                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4705                                         "%s: unknown number of entries\n",
4706                                         qid_printqueue(i, j));
4707                                 unknown = true;
4708                         }
4709                         else if (n == 0)
4710                         {
4711                                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4712                                         "%s is empty\n",
4713                                         qid_printqueue(i, j));
4714                         }
4715                         else if (n > 0)
4716                         {
4717                                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4718                                         "%s: entries=%d\n",
4719                                         qid_printqueue(i, j), n);
4720                                 nrequests += n;
4721                                 k++;
4722                         }
4723                 }
4724         }
4725         if (prefix == NULL && k > 1)
4726                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4727                                      "\t\tTotal requests: %d%s\n",
4728                                      nrequests, unknown ? " (about)" : "");
4729 #else /* SM_CONF_SHM */
4730         if (prefix == NULL)
4731                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4732                              "Data unavailable without shared memory support\n");
4733         else
4734                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
4735                              "%sNOTAVAILABLE:-1\r\n", prefix);
4736 #endif /* SM_CONF_SHM */
4737 }
4738 /*
4739 **  PRINTQUEUE -- print out a representation of the mail queue
4740 **
4741 **      Parameters:
4742 **              none.
4743 **
4744 **      Returns:
4745 **              none.
4746 **
4747 **      Side Effects:
4748 **              Prints a listing of the mail queue on the standard output.
4749 */
4750 
4751 void
4752 printqueue()
4753 {
4754         int i, k = 0, nrequests = 0;
4755 
4756         for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
4757         {
4758                 int j;
4759 
4760                 k++;
4761                 for (j = 0; j < Queue[i]->qg_numqueues; j++)
4762                 {
4763                         if (StopRequest)
4764                                 stop_sendmail();
4765                         nrequests += print_single_queue(i, j);
4766                         k++;
4767                 }
4768         }
4769         if (k > 1)
4770                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4771                                      "\t\tTotal requests: %d\n",
4772                                      nrequests);
4773 }
4774 /*
4775 **  PRINT_SINGLE_QUEUE -- print out a representation of a single mail queue
4776 **
4777 **      Parameters:
4778 **              qgrp -- the index of the queue group.
4779 **              qdir -- the queue directory.
4780 **
4781 **      Returns:
4782 **              number of requests in mail queue.
4783 **
4784 **      Side Effects:
4785 **              Prints a listing of the mail queue on the standard output.
4786 */
4787 
4788 int
4789 print_single_queue(qgrp, qdir)
4790         int qgrp;
4791         int qdir;
4792 {
4793         register WORK *w;
4794         SM_FILE_T *f;
4795         int nrequests;
4796         char qd[MAXPATHLEN];
4797         char qddf[MAXPATHLEN];
4798         char buf[MAXLINE];
4799 
4800         if (qdir == NOQDIR)
4801         {
4802                 (void) sm_strlcpy(qd, ".", sizeof(qd));
4803                 (void) sm_strlcpy(qddf, ".", sizeof(qddf));
4804         }
4805         else
4806         {
4807                 (void) sm_strlcpyn(qd, sizeof(qd), 2,
4808                         Queue[qgrp]->qg_qpaths[qdir].qp_name,
4809                         (bitset(QP_SUBQF,
4810                                 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4811                                         ? "/qf" : ""));
4812                 (void) sm_strlcpyn(qddf, sizeof(qddf), 2,
4813                         Queue[qgrp]->qg_qpaths[qdir].qp_name,
4814                         (bitset(QP_SUBDF,
4815                                 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
4816                                         ? "/df" : ""));
4817         }
4818 
4819         /*
4820         **  Check for permission to print the queue
4821         */
4822 
4823         if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
4824         {
4825                 struct stat st;
4826 #ifdef NGROUPS_MAX
4827                 int n;
4828                 extern GIDSET_T InitialGidSet[NGROUPS_MAX];
4829 #endif /* NGROUPS_MAX */
4830 
4831                 if (stat(qd, &st) < 0)
4832                 {
4833                         syserr("Cannot stat %s",
4834                                 qid_printqueue(qgrp, qdir));
4835                         return 0;
4836                 }
4837 #ifdef NGROUPS_MAX
4838                 n = NGROUPS_MAX;
4839                 while (--n >= 0)
4840                 {
4841                         if (InitialGidSet[n] == st.st_gid)
4842                                 break;
4843                 }
4844                 if (n < 0 && RealGid != st.st_gid)
4845 #else /* NGROUPS_MAX */
4846                 if (RealGid != st.st_gid)
4847 #endif /* NGROUPS_MAX */
4848                 {
4849                         usrerr("510 You are not permitted to see the queue");
4850                         setstat(EX_NOPERM);
4851                         return 0;
4852                 }
4853         }
4854 
4855         /*
4856         **  Read and order the queue.
4857         */
4858 
4859         nrequests = gatherq(qgrp, qdir, true, NULL, NULL, NULL);
4860         (void) sortq(Queue[qgrp]->qg_maxlist);
4861 
4862         /*
4863         **  Print the work list that we have read.
4864         */
4865 
4866         /* first see if there is anything */
4867         if (nrequests <= 0)
4868         {
4869                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s is empty\n",
4870                                      qid_printqueue(qgrp, qdir));
4871                 return 0;
4872         }
4873 
4874         sm_getla();     /* get load average */
4875 
4876         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\t\t%s (%d request%s",
4877                              qid_printqueue(qgrp, qdir),
4878                              nrequests, nrequests == 1 ? "" : "s");
4879         if (MaxQueueRun > 0 && nrequests > MaxQueueRun)
4880                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4881                                      ", only %d printed", MaxQueueRun);
4882         if (Verbose)
4883                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4884                         ")\n-----Q-ID----- --Size-- -Priority- ---Q-Time--- --------Sender/Recipient--------\n");
4885         else
4886                 (void) sm_io_fprintf(smioout,  SM_TIME_DEFAULT,
4887                         ")\n-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------\n");
4888         for (w = WorkQ; w != NULL; w = w->w_next)
4889         {
4890                 struct stat st;
4891                 auto time_t submittime = 0;
4892                 long dfsize;
4893                 int flags = 0;
4894                 int qfver;
4895                 char quarmsg[MAXLINE];
4896                 char statmsg[MAXLINE];
4897                 char bodytype[MAXNAME + 1];
4898                 char qf[MAXPATHLEN];
4899 
4900                 if (StopRequest)
4901                         stop_sendmail();
4902 
4903                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%13s",
4904                                      w->w_name + 2);
4905                 (void) sm_strlcpyn(qf, sizeof(qf), 3, qd, "/", w->w_name);
4906                 f = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, qf, SM_IO_RDONLY_B,
4907                                NULL);
4908                 if (f == NULL)
4909                 {
4910                         if (errno == EPERM)
4911                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4912                                                      " (permission denied)\n");
4913                         else if (errno == ENOENT)
4914                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4915                                                      " (job completed)\n");
4916                         else
4917                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4918                                                      " (%s)\n",
4919                                                      sm_errstring(errno));
4920                         errno = 0;
4921                         continue;
4922                 }
4923                 w->w_name[0] = DATAFL_LETTER;
4924                 (void) sm_strlcpyn(qf, sizeof(qf), 3, qddf, "/", w->w_name);
4925                 if (stat(qf, &st) >= 0)
4926                         dfsize = st.st_size;
4927                 else
4928                 {
4929                         ENVELOPE e;
4930 
4931                         /*
4932                         **  Maybe the df file can't be statted because
4933                         **  it is in a different directory than the qf file.
4934                         **  In order to find out, we must read the qf file.
4935                         */
4936 
4937                         newenvelope(&e, &BlankEnvelope, sm_rpool_new_x(NULL));
4938                         e.e_id = w->w_name + 2;
4939                         e.e_qgrp = qgrp;
4940                         e.e_qdir = qdir;
4941                         dfsize = -1;
4942                         if (readqf(&e, false))
4943                         {
4944                                 char *df = queuename(&e, DATAFL_LETTER);
4945                                 if (stat(df, &st) >= 0)
4946                                         dfsize = st.st_size;
4947                         }
4948                         if (e.e_lockfp != NULL)
4949                         {
4950                                 (void) sm_io_close(e.e_lockfp, SM_TIME_DEFAULT);
4951                                 e.e_lockfp = NULL;
4952                         }
4953                         clearenvelope(&e, false, e.e_rpool);
4954                         sm_rpool_free(e.e_rpool);
4955                 }
4956                 if (w->w_lock)
4957                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "*");
4958                 else if (QueueMode == QM_LOST)
4959                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "?");
4960                 else if (w->w_tooyoung)
4961                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "-");
4962                 else if (shouldqueue(w->w_pri, w->w_ctime))
4963                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "X");
4964                 else
4965                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " ");
4966 
4967                 errno = 0;
4968 
4969                 quarmsg[0] = '\0';
4970                 statmsg[0] = bodytype[0] = '\0';
4971                 qfver = 0;
4972                 while (sm_io_fgets(f, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
4973                 {
4974                         register int i;
4975                         register char *p;
4976 
4977                         if (StopRequest)
4978                                 stop_sendmail();
4979 
4980                         fixcrlf(buf, true);
4981                         switch (buf[0])
4982                         {
4983                           case 'V':     /* queue file version */
4984                                 qfver = atoi(&buf[1]);
4985                                 break;
4986 
4987                           case 'M':     /* error message */
4988                                 if ((i = strlen(&buf[1])) >= sizeof(statmsg))
4989                                         i = sizeof(statmsg) - 1;
4990                                 memmove(statmsg, &buf[1], i);
4991                                 statmsg[i] = '\0';
4992                                 break;
4993 
4994                           case 'q':     /* quarantine reason */
4995                                 if ((i = strlen(&buf[1])) >= sizeof(quarmsg))
4996                                         i = sizeof(quarmsg) - 1;
4997                                 memmove(quarmsg, &buf[1], i);
4998                                 quarmsg[i] = '\0';
4999                                 break;
5000 
5001                           case 'B':     /* body type */
5002                                 if ((i = strlen(&buf[1])) >= sizeof(bodytype))
5003                                         i = sizeof(bodytype) - 1;
5004                                 memmove(bodytype, &buf[1], i);
5005                                 bodytype[i] = '\0';
5006                                 break;
5007 
5008                           case 'S':     /* sender name */
5009                                 if (Verbose)
5010                                 {
5011                                         (void) sm_io_fprintf(smioout,
5012                                                 SM_TIME_DEFAULT,
5013                                                 "%8ld %10ld%c%.12s ",
5014                                                 dfsize,
5015                                                 w->w_pri,
5016                                                 bitset(EF_WARNING, flags)
5017                                                         ? '+' : ' ',
5018                                                 ctime(&submittime) + 4);
5019                                         prtstr(&buf[1], 78);
5020                                 }
5021                                 else
5022                                 {
5023                                         (void) sm_io_fprintf(smioout,
5024                                                 SM_TIME_DEFAULT,
5025                                                 "%8ld %.16s ",
5026                                                 dfsize,
5027                                                 ctime(&submittime));
5028                                         prtstr(&buf[1], 39);
5029                                 }
5030 
5031                                 if (quarmsg[0] != '\0')
5032                                 {
5033                                         (void) sm_io_fprintf(smioout,
5034                                                              SM_TIME_DEFAULT,
5035                                                              "\n     QUARANTINE: %.*s",
5036                                                              Verbose ? 100 : 60,
5037                                                              quarmsg);
5038                                         quarmsg[0] = '\0';
5039                                 }
5040 
5041                                 if (statmsg[0] != '\0' || bodytype[0] != '\0')
5042                                 {
5043                                         (void) sm_io_fprintf(smioout,
5044                                                 SM_TIME_DEFAULT,
5045                                                 "\n    %10.10s",
5046                                                 bodytype);
5047                                         if (statmsg[0] != '\0')
5048                                                 (void) sm_io_fprintf(smioout,
5049                                                         SM_TIME_DEFAULT,
5050                                                         "   (%.*s)",
5051                                                         Verbose ? 100 : 60,
5052                                                         statmsg);
5053                                         statmsg[0] = '\0';
5054                                 }
5055                                 break;
5056 
5057                           case 'C':     /* controlling user */
5058                                 if (Verbose)
5059                                         (void) sm_io_fprintf(smioout,
5060                                                 SM_TIME_DEFAULT,
5061                                                 "\n\t\t\t\t\t\t(---%.64s---)",
5062                                                 &buf[1]);
5063                                 break;
5064 
5065                           case 'R':     /* recipient name */
5066                                 p = &buf[1];
5067                                 if (qfver >= 1)
5068                                 {
5069                                         p = strchr(p, ':');
5070                                         if (p == NULL)
5071                                                 break;
5072                                         p++;
5073                                 }
5074                                 if (Verbose)
5075                                 {
5076                                         (void) sm_io_fprintf(smioout,
5077                                                         SM_TIME_DEFAULT,
5078                                                         "\n\t\t\t\t\t\t");
5079                                         prtstr(p, 71);
5080                                 }
5081                                 else
5082                                 {
5083                                         (void) sm_io_fprintf(smioout,
5084                                                         SM_TIME_DEFAULT,
5085                                                         "\n\t\t\t\t\t ");
5086                                         prtstr(p, 38);
5087                                 }
5088                                 if (Verbose && statmsg[0] != '\0')
5089                                 {
5090                                         (void) sm_io_fprintf(smioout,
5091                                                         SM_TIME_DEFAULT,
5092                                                         "\n\t\t (%.100s)",
5093                                                         statmsg);
5094                                         statmsg[0] = '\0';
5095                                 }
5096                                 break;
5097 
5098                           case 'T':     /* creation time */
5099                                 submittime = atol(&buf[1]);
5100                                 break;
5101 
5102                           case 'F':     /* flag bits */
5103                                 for (p = &buf[1]; *p != '\0'; p++)
5104                                 {
5105                                         switch (*p)
5106                                         {
5107                                           case 'w':
5108                                                 flags |= EF_WARNING;
5109                                                 break;
5110                                         }
5111                                 }
5112                         }
5113                 }
5114                 if (submittime == (time_t) 0)
5115                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
5116                                              " (no control file)");
5117                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
5118                 (void) sm_io_close(f, SM_TIME_DEFAULT);
5119         }
5120         return nrequests;
5121 }
5122 
5123 /*
5124 **  QUEUE_LETTER -- get the proper queue letter for the current QueueMode.
5125 **
5126 **      Parameters:
5127 **              e -- envelope to build it in/from.
5128 **              type -- the file type, used as the first character
5129 **                      of the file name.
5130 **
5131 **      Returns:
5132 **              the letter to use
5133 */
5134 
5135 static char
5136 queue_letter(e, type)
5137         ENVELOPE *e;
5138         int type;
5139 {
5140         /* Change type according to QueueMode */
5141         if (type == ANYQFL_LETTER)
5142         {
5143                 if (e->e_quarmsg != NULL)
5144                         type = QUARQF_LETTER;
5145                 else
5146                 {
5147                         switch (QueueMode)
5148                         {
5149                           case QM_NORMAL:
5150                                 type = NORMQF_LETTER;
5151                                 break;
5152 
5153                           case QM_QUARANTINE:
5154                                 type = QUARQF_LETTER;
5155                                 break;
5156 
5157                           case QM_LOST:
5158                                 type = LOSEQF_LETTER;
5159                                 break;
5160 
5161                           default:
5162                                 /* should never happen */
5163                                 abort();
5164                                 /* NOTREACHED */
5165                         }
5166                 }
5167         }
5168         return type;
5169 }
5170 
5171 /*
5172 **  QUEUENAME -- build a file name in the queue directory for this envelope.
5173 **
5174 **      Parameters:
5175 **              e -- envelope to build it in/from.
5176 **              type -- the file type, used as the first character
5177 **                      of the file name.
5178 **
5179 **      Returns:
5180 **              a pointer to the queue name (in a static buffer).
5181 **
5182 **      Side Effects:
5183 **              If no id code is already assigned, queuename() will
5184 **              assign an id code with assign_queueid().  If no queue
5185 **              directory is assigned, one will be set with setnewqueue().
5186 */
5187 
5188 char *
5189 queuename(e, type)
5190         register ENVELOPE *e;
5191         int type;
5192 {
5193         int qd, qg;
5194         char *sub = "/";
5195         char pref[3];
5196         static char buf[MAXPATHLEN];
5197 
5198         /* Assign an ID if needed */
5199         if (e->e_id == NULL)
5200                 assign_queueid(e);
5201         type = queue_letter(e, type);
5202 
5203         /* begin of filename */
5204         pref[0] = (char) type;
5205         pref[1] = 'f';
5206         pref[2] = '\0';
5207 
5208         /* Assign a queue group/directory if needed */
5209         if (type == XSCRPT_LETTER)
5210         {
5211                 /*
5212                 **  We don't want to call setnewqueue() if we are fetching
5213                 **  the pathname of the transcript file, because setnewqueue
5214                 **  chooses a queue, and sometimes we need to write to the
5215                 **  transcript file before we have gathered enough information
5216                 **  to choose a queue.
5217                 */
5218 
5219                 if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5220                 {
5221                         if (e->e_qgrp != NOQGRP && e->e_qdir != NOQDIR)
5222                         {
5223                                 e->e_xfqgrp = e->e_qgrp;
5224                                 e->e_xfqdir = e->e_qdir;
5225                         }
5226                         else
5227                         {
5228                                 e->e_xfqgrp = 0;
5229                                 if (Queue[e->e_xfqgrp]->qg_numqueues <= 1)
5230                                         e->e_xfqdir = 0;
5231                                 else
5232                                 {
5233                                         e->e_xfqdir = get_rand_mod(
5234                                               Queue[e->e_xfqgrp]->qg_numqueues);
5235                                 }
5236                         }
5237                 }
5238                 qd = e->e_xfqdir;
5239                 qg = e->e_xfqgrp;
5240         }
5241         else
5242         {
5243                 if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
5244                         (void) setnewqueue(e);
5245                 if (type ==  DATAFL_LETTER)
5246                 {
5247                         qd = e->e_dfqdir;
5248                         qg = e->e_dfqgrp;
5249                 }
5250                 else
5251                 {
5252                         qd = e->e_qdir;
5253                         qg = e->e_qgrp;
5254                 }
5255         }
5256 
5257         /* xf files always have a valid qd and qg picked above */
5258         if ((qd == NOQDIR || qg == NOQGRP) && type != XSCRPT_LETTER)
5259                 (void) sm_strlcpyn(buf, sizeof(buf), 2, pref, e->e_id);
5260         else
5261         {
5262                 switch (type)
5263                 {
5264                   case DATAFL_LETTER:
5265                         if (bitset(QP_SUBDF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5266                                 sub = "/df/";
5267                         break;
5268 
5269                   case QUARQF_LETTER:
5270                   case TEMPQF_LETTER:
5271                   case NEWQFL_LETTER:
5272                   case LOSEQF_LETTER:
5273                   case NORMQF_LETTER:
5274                         if (bitset(QP_SUBQF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5275                                 sub = "/qf/";
5276                         break;
5277 
5278                   case XSCRPT_LETTER:
5279                         if (bitset(QP_SUBXF, Queue[qg]->qg_qpaths[qd].qp_subdirs))
5280                                 sub = "/xf/";
5281                         break;
5282 
5283                   default:
5284                         sm_abort("queuename: bad queue file type %d", type);
5285                 }
5286 
5287                 (void) sm_strlcpyn(buf, sizeof(buf), 4,
5288                                 Queue[qg]->qg_qpaths[qd].qp_name,
5289                                 sub, pref, e->e_id);
5290         }
5291 
5292         if (tTd(7, 2))
5293                 sm_dprintf("queuename: %s\n", buf);
5294         return buf;
5295 }
5296 
5297 /*
5298 **  INIT_QID_ALG -- Initialize the (static) parameters that are used to
5299 **      generate a queue ID.
5300 **
5301 **      This function is called by the daemon to reset
5302 **      LastQueueTime and LastQueuePid which are used by assign_queueid().
5303 **      Otherwise the algorithm may cause problems because
5304 **      LastQueueTime and LastQueuePid are set indirectly by main()
5305 **      before the daemon process is started, hence LastQueuePid is not
5306 **      the pid of the daemon and therefore a child of the daemon can
5307 **      actually have the same pid as LastQueuePid which means the section
5308 **      in  assign_queueid():
5309 **      * see if we need to get a new base time/pid *
5310 **      is NOT triggered which will cause the same queue id to be generated.
5311 **
5312 **      Parameters:
5313 **              none
5314 **
5315 **      Returns:
5316 **              none.
5317 */
5318 
5319 void
5320 init_qid_alg()
5321 {
5322         LastQueueTime = 0;
5323         LastQueuePid = -1;
5324 }
5325 
5326 /*
5327 **  ASSIGN_QUEUEID -- assign a queue ID for this envelope.
5328 **
5329 **      Assigns an id code if one does not already exist.
5330 **      This code assumes that nothing will remain in the queue for
5331 **      longer than 60 years.  It is critical that files with the given
5332 **      name do not already exist in the queue.
5333 **      [No longer initializes e_qdir to NOQDIR.]
5334 **
5335 **      Parameters:
5336 **              e -- envelope to set it in.
5337 **
5338 **      Returns:
5339 **              none.
5340 */
5341 
5342 static const char QueueIdChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
5343 # define QIC_LEN        60
5344 # define QIC_LEN_R      62
5345 
5346 /*
5347 **  Note: the length is "officially" 60 because minutes and seconds are
5348 **      usually only 0-59.  However (Linux):
5349 **       tm_sec The number of seconds after the minute, normally in
5350 **              the range 0 to 59, but can be up to 61 to allow for
5351 **              leap seconds.
5352 **      Hence the real length of the string is 62 to take this into account.
5353 **      Alternatively % QIC_LEN can (should) be used for access everywhere.
5354 */
5355 
5356 # define queuenextid() CurrentPid
5357 #define QIC_LEN_SQR     (QIC_LEN * QIC_LEN)
5358 
5359 void
5360 assign_queueid(e)
5361         register ENVELOPE *e;
5362 {
5363         pid_t pid = queuenextid();
5364         static unsigned int cX = 0;
5365         static unsigned int random_offset;
5366         struct tm *tm;
5367         char idbuf[MAXQFNAME - 2];
5368         unsigned int seq;
5369 
5370         if (e->e_id != NULL)
5371                 return;
5372 
5373         /* see if we need to get a new base time/pid */
5374         if (cX >= QIC_LEN_SQR || LastQueueTime == 0 || LastQueuePid != pid)
5375         {
5376                 time_t then = LastQueueTime;
5377 
5378                 /* if the first time through, pick a random offset */
5379                 if (LastQueueTime == 0)
5380                         random_offset = ((unsigned int)get_random())
5381                                         % QIC_LEN_SQR;
5382 
5383                 while ((LastQueueTime = curtime()) == then &&
5384                        LastQueuePid == pid)
5385                 {
5386                         (void) sleep(1);
5387                 }
5388                 LastQueuePid = queuenextid();
5389                 cX = 0;
5390         }
5391 
5392         /*
5393         **  Generate a new sequence number between 0 and QIC_LEN_SQR-1.
5394         **  This lets us generate up to QIC_LEN_SQR unique queue ids
5395         **  per second, per process.  With envelope splitting,
5396         **  a single message can consume many queue ids.
5397         */
5398 
5399         seq = (cX + random_offset) % QIC_LEN_SQR;
5400         ++cX;
5401         if (tTd(7, 50))
5402                 sm_dprintf("assign_queueid: random_offset=%u (%u)\n",
5403                         random_offset, seq);
5404 
5405         tm = gmtime(&LastQueueTime);
5406         idbuf[0] = QueueIdChars[tm->tm_year % QIC_LEN];
5407         idbuf[1] = QueueIdChars[tm->tm_mon];
5408         idbuf[2] = QueueIdChars[tm->tm_mday];
5409         idbuf[3] = QueueIdChars[tm->tm_hour];
5410         idbuf[4] = QueueIdChars[tm->tm_min % QIC_LEN_R];
5411         idbuf[5] = QueueIdChars[tm->tm_sec % QIC_LEN_R];
5412         idbuf[6] = QueueIdChars[seq / QIC_LEN];
5413         idbuf[7] = QueueIdChars[seq % QIC_LEN];
5414         (void) sm_snprintf(&idbuf[8], sizeof(idbuf) - 8, "%06d",
5415                            (int) LastQueuePid);
5416         e->e_id = sm_rpool_strdup_x(e->e_rpool, idbuf);
5417         macdefine(&e->e_macro, A_PERM, 'i', e->e_id);
5418 #if 0
5419         /* XXX: inherited from MainEnvelope */
5420         e->e_qgrp = NOQGRP;  /* too early to do anything else */
5421         e->e_qdir = NOQDIR;
5422         e->e_xfqgrp = NOQGRP;
5423 #endif /* 0 */
5424 
5425         /* New ID means it's not on disk yet */
5426         e->e_qfletter = '\0';
5427 
5428         if (tTd(7, 1))
5429                 sm_dprintf("assign_queueid: assigned id %s, e=%p\n",
5430                         e->e_id, e);
5431         if (LogLevel > 93)
5432                 sm_syslog(LOG_DEBUG, e->e_id, "assigned id");
5433 }
5434 /*
5435 **  SYNC_QUEUE_TIME -- Assure exclusive PID in any given second
5436 **
5437 **      Make sure one PID can't be used by two processes in any one second.
5438 **
5439 **              If the system rotates PIDs fast enough, may get the
5440 **              same pid in the same second for two distinct processes.
5441 **              This will interfere with the queue file naming system.
5442 **
5443 **      Parameters:
5444 **              none
5445 **
5446 **      Returns:
5447 **              none
5448 */
5449 
5450 void
5451 sync_queue_time()
5452 {
5453 #if FAST_PID_RECYCLE
5454         if (OpMode != MD_TEST &&
5455             OpMode != MD_CHECKCONFIG &&
5456             OpMode != MD_VERIFY &&
5457             LastQueueTime > 0 &&
5458             LastQueuePid == CurrentPid &&
5459             curtime() == LastQueueTime)
5460                 (void) sleep(1);
5461 #endif /* FAST_PID_RECYCLE */
5462 }
5463 /*
5464 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
5465 **
5466 **      Parameters:
5467 **              e -- the envelope to unlock.
5468 **
5469 **      Returns:
5470 **              none
5471 **
5472 **      Side Effects:
5473 **              unlocks the queue for `e'.
5474 */
5475 
5476 void
5477 unlockqueue(e)
5478         ENVELOPE *e;
5479 {
5480         if (tTd(51, 4))
5481                 sm_dprintf("unlockqueue(%s)\n",
5482                         e->e_id == NULL ? "NOQUEUE" : e->e_id);
5483 
5484 
5485         /* if there is a lock file in the envelope, close it */
5486         if (e->e_lockfp != NULL)
5487                 (void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
5488         e->e_lockfp = NULL;
5489 
5490         /* don't create a queue id if we don't already have one */
5491         if (e->e_id == NULL)
5492                 return;
5493 
5494         /* remove the transcript */
5495         if (LogLevel > 87)
5496                 sm_syslog(LOG_DEBUG, e->e_id, "unlock");
5497         if (!tTd(51, 104))
5498                 (void) xunlink(queuename(e, XSCRPT_LETTER));
5499 }
5500 /*
5501 **  SETCTLUSER -- create a controlling address
5502 **
5503 **      Create a fake "address" given only a local login name; this is
5504 **      used as a "controlling user" for future recipient addresses.
5505 **
5506 **      Parameters:
5507 **              user -- the user name of the controlling user.
5508 **              qfver -- the version stamp of this queue file.
5509 **              e -- envelope
5510 **
5511 **      Returns:
5512 **              An address descriptor for the controlling user,
5513 **              using storage allocated from e->e_rpool.
5514 **
5515 */
5516 
5517 static ADDRESS *
5518 setctluser(user, qfver, e)
5519         char *user;
5520         int qfver;
5521         ENVELOPE *e;
5522 {
5523         register ADDRESS *a;
5524         struct passwd *pw;
5525         char *p;
5526 
5527         /*
5528         **  See if this clears our concept of controlling user.
5529         */
5530 
5531         if (user == NULL || *user == '\0')
5532                 return NULL;
5533 
5534         /*
5535         **  Set up addr fields for controlling user.
5536         */
5537 
5538         a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
5539         memset((char *) a, '\0', sizeof(*a));
5540 
5541         if (*user == ':')
5542         {
5543                 p = &user[1];
5544                 a->q_user = sm_rpool_strdup_x(e->e_rpool, p);
5545         }
5546         else
5547         {
5548                 p = strtok(user, ":");
5549                 a->q_user = sm_rpool_strdup_x(e->e_rpool, user);
5550                 if (qfver >= 2)
5551                 {
5552                         if ((p = strtok(NULL, ":")) != NULL)
5553                                 a->q_uid = atoi(p);
5554                         if ((p = strtok(NULL, ":")) != NULL)
5555                                 a->q_gid = atoi(p);
5556                         if ((p = strtok(NULL, ":")) != NULL)
5557                         {
5558                                 char *o;
5559 
5560                                 a->q_flags |= QGOODUID;
5561 
5562                                 /* if there is another ':': restore it */
5563                                 if ((o = strtok(NULL, ":")) != NULL && o > p)
5564                                         o[-1] = ':';
5565                         }
5566                 }
5567                 else if ((pw = sm_getpwnam(user)) != NULL)
5568                 {
5569                         if (*pw->pw_dir == '\0')
5570                                 a->q_home = NULL;
5571                         else if (strcmp(pw->pw_dir, "/") == 0)
5572                                 a->q_home = "";
5573                         else
5574                                 a->q_home = sm_rpool_strdup_x(e->e_rpool, pw->pw_dir);
5575                         a->q_uid = pw->pw_uid;
5576                         a->q_gid = pw->pw_gid;
5577                         a->q_flags |= QGOODUID;
5578                 }
5579         }
5580 
5581         a->q_flags |= QPRIMARY;              /* flag as a "ctladdr" */
5582         a->q_mailer = LocalMailer;
5583         if (p == NULL)
5584                 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
5585         else
5586                 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, p);
5587         return a;
5588 }
5589 /*
5590 **  LOSEQFILE -- rename queue file with LOSEQF_LETTER & try to let someone know
5591 **
5592 **      Parameters:
5593 **              e -- the envelope (e->e_id will be used).
5594 **              why -- reported to whomever can hear.
5595 **
5596 **      Returns:
5597 **              none.
5598 */
5599 
5600 void
5601 loseqfile(e, why)
5602         register ENVELOPE *e;
5603         char *why;
5604 {
5605         bool loseit = true;
5606         char *p;
5607         char buf[MAXPATHLEN];
5608 
5609         if (e == NULL || e->e_id == NULL)
5610                 return;
5611         p = queuename(e, ANYQFL_LETTER);
5612         if (sm_strlcpy(buf, p, sizeof(buf)) >= sizeof(buf))
5613                 return;
5614         if (!bitset(EF_INQUEUE, e->e_flags))
5615                 queueup(e, false, true);
5616         else if (QueueMode == QM_LOST)
5617                 loseit = false;
5618 
5619         /* if already lost, no need to re-lose */
5620         if (loseit)
5621         {
5622                 p = queuename(e, LOSEQF_LETTER);
5623                 if (rename(buf, p) < 0)
5624                         syserr("cannot rename(%s, %s), uid=%d",
5625                                buf, p, (int) geteuid());
5626                 else if (LogLevel > 0)
5627                         sm_syslog(LOG_ALERT, e->e_id,
5628                                   "Losing %s: %s", buf, why);
5629         }
5630         if (e->e_dfp != NULL)
5631         {
5632                 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
5633                 e->e_dfp = NULL;
5634         }
5635         e->e_flags &= ~EF_HAS_DF;
5636 }
5637 /*
5638 **  NAME2QID -- translate a queue group name to a queue group id
5639 **
5640 **      Parameters:
5641 **              queuename -- name of queue group.
5642 **
5643 **      Returns:
5644 **              queue group id if found.
5645 **              NOQGRP otherwise.
5646 */
5647 
5648 int
5649 name2qid(queuename)
5650         char *queuename;
5651 {
5652         register STAB *s;
5653 
5654         s = stab(queuename, ST_QUEUE, ST_FIND);
5655         if (s == NULL)
5656                 return NOQGRP;
5657         return s->s_quegrp->qg_index;
5658 }
5659 /*
5660 **  QID_PRINTNAME -- create externally printable version of queue id
5661 **
5662 **      Parameters:
5663 **              e -- the envelope.
5664 **
5665 **      Returns:
5666 **              a printable version
5667 */
5668 
5669 char *
5670 qid_printname(e)
5671         ENVELOPE *e;
5672 {
5673         char *id;
5674         static char idbuf[MAXQFNAME + 34];
5675 
5676         if (e == NULL)
5677                 return "";
5678 
5679         if (e->e_id == NULL)
5680                 id = "";
5681         else
5682                 id = e->e_id;
5683 
5684         if (e->e_qdir == NOQDIR)
5685                 return id;
5686 
5687         (void) sm_snprintf(idbuf, sizeof(idbuf), "%.32s/%s",
5688                            Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_name,
5689                            id);
5690         return idbuf;
5691 }
5692 /*
5693 **  QID_PRINTQUEUE -- create full version of queue directory for data files
5694 **
5695 **      Parameters:
5696 **              qgrp -- index in queue group.
5697 **              qdir -- the short version of the queue directory
5698 **
5699 **      Returns:
5700 **              the full pathname to the queue (might point to a static var)
5701 */
5702 
5703 char *
5704 qid_printqueue(qgrp, qdir)
5705         int qgrp;
5706         int qdir;
5707 {
5708         char *subdir;
5709         static char dir[MAXPATHLEN];
5710 
5711         if (qdir == NOQDIR)
5712                 return Queue[qgrp]->qg_qdir;
5713 
5714         if (strcmp(Queue[qgrp]->qg_qpaths[qdir].qp_name, ".") == 0)
5715                 subdir = NULL;
5716         else
5717                 subdir = Queue[qgrp]->qg_qpaths[qdir].qp_name;
5718 
5719         (void) sm_strlcpyn(dir, sizeof(dir), 4,
5720                         Queue[qgrp]->qg_qdir,
5721                         subdir == NULL ? "" : "/",
5722                         subdir == NULL ? "" : subdir,
5723                         (bitset(QP_SUBDF,
5724                                 Queue[qgrp]->qg_qpaths[qdir].qp_subdirs)
5725                                         ? "/df" : ""));
5726         return dir;
5727 }
5728 
5729 /*
5730 **  PICKQDIR -- Pick a queue directory from a queue group
5731 **
5732 **      Parameters:
5733 **              qg -- queue group
5734 **              fsize -- file size in bytes
5735 **              e -- envelope, or NULL
5736 **
5737 **      Result:
5738 **              NOQDIR if no queue directory in qg has enough free space to
5739 **              hold a file of size 'fsize', otherwise the index of
5740 **              a randomly selected queue directory which resides on a
5741 **              file system with enough disk space.
5742 **              XXX This could be extended to select a queuedir with
5743 **                      a few (the fewest?) number of entries. That data
5744 **                      is available if shared memory is used.
5745 **
5746 **      Side Effects:
5747 **              If the request fails and e != NULL then sm_syslog is called.
5748 */
5749 
5750 int
5751 pickqdir(qg, fsize, e)
5752         QUEUEGRP *qg;
5753         long fsize;
5754         ENVELOPE *e;
5755 {
5756         int qdir;
5757         int i;
5758         long avail = 0;
5759 
5760         /* Pick a random directory, as a starting point. */
5761         if (qg->qg_numqueues <= 1)
5762                 qdir = 0;
5763         else
5764                 qdir = get_rand_mod(qg->qg_numqueues);
5765 
5766 #if _FFR_TESTS
5767         if (tTd(4, 101))
5768                 return NOQDIR;
5769 #endif /* _FFR_TESTS */
5770         if (MinBlocksFree <= 0 && fsize <= 0)
5771                 return qdir;
5772 
5773         /*
5774         **  Now iterate over the queue directories,
5775         **  looking for a directory with enough space for this message.
5776         */
5777 
5778         i = qdir;
5779         do
5780         {
5781                 QPATHS *qp = &qg->qg_qpaths[i];
5782                 long needed = 0;
5783                 long fsavail = 0;
5784 
5785                 if (fsize > 0)
5786                         needed += fsize / FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5787                                   + ((fsize % FILE_SYS_BLKSIZE(qp->qp_fsysidx)
5788                                       > 0) ? 1 : 0);
5789                 if (MinBlocksFree > 0)
5790                         needed += MinBlocksFree;
5791                 fsavail = FILE_SYS_AVAIL(qp->qp_fsysidx);
5792 #if SM_CONF_SHM
5793                 if (fsavail <= 0)
5794                 {
5795                         long blksize;
5796 
5797                         /*
5798                         **  might be not correctly updated,
5799                         **  let's try to get the info directly.
5800                         */
5801 
5802                         fsavail = freediskspace(FILE_SYS_NAME(qp->qp_fsysidx),
5803                                                 &blksize);
5804                         if (fsavail < 0)
5805                                 fsavail = 0;
5806                 }
5807 #endif /* SM_CONF_SHM */
5808                 if (needed <= fsavail)
5809                         return i;
5810                 if (avail < fsavail)
5811                         avail = fsavail;
5812 
5813                 if (qg->qg_numqueues > 0)
5814                         i = (i + 1) % qg->qg_numqueues;
5815         } while (i != qdir);
5816 
5817         if (e != NULL && LogLevel > 0)
5818                 sm_syslog(LOG_ALERT, e->e_id,
5819                         "low on space (%s needs %ld bytes + %ld blocks in %s), max avail: %ld",
5820                         CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
5821                         fsize, MinBlocksFree,
5822                         qg->qg_qdir, avail);
5823         return NOQDIR;
5824 }
5825 /*
5826 **  SETNEWQUEUE -- Sets a new queue group and directory
5827 **
5828 **      Assign a queue group and directory to an envelope and store the
5829 **      directory in e->e_qdir.
5830 **
5831 **      Parameters:
5832 **              e -- envelope to assign a queue for.
5833 **
5834 **      Returns:
5835 **              true if successful
5836 **              false otherwise
5837 **
5838 **      Side Effects:
5839 **              On success, e->e_qgrp and e->e_qdir are non-negative.
5840 **              On failure (not enough disk space),
5841 **              e->qgrp = NOQGRP, e->e_qdir = NOQDIR
5842 **              and usrerr() is invoked (which could raise an exception).
5843 */
5844 
5845 bool
5846 setnewqueue(e)
5847         ENVELOPE *e;
5848 {
5849         if (tTd(41, 20))
5850                 sm_dprintf("setnewqueue: called\n");
5851 
5852         /* not set somewhere else */
5853         if (e->e_qgrp == NOQGRP)
5854         {
5855                 ADDRESS *q;
5856 
5857                 /*
5858                 **  Use the queue group of the "first" recipient, as set by
5859                 **  the "queuegroup" rule set.  If that is not defined, then
5860                 **  use the queue group of the mailer of the first recipient.
5861                 **  If that is not defined either, then use the default
5862                 **  queue group.
5863                 **  Notice: "first" depends on the sorting of sendqueue
5864                 **  in recipient().
5865                 **  To avoid problems with "bad" recipients look
5866                 **  for a valid address first.
5867                 */
5868 
5869                 q = e->e_sendqueue;
5870                 while (q != NULL &&
5871                        (QS_IS_BADADDR(q->q_state) || QS_IS_DEAD(q->q_state)))
5872                 {
5873                         q = q->q_next;
5874                 }
5875                 if (q == NULL)
5876                         e->e_qgrp = 0;
5877                 else if (q->q_qgrp >= 0)
5878                         e->e_qgrp = q->q_qgrp;
5879                 else if (q->q_mailer != NULL &&
5880                          ISVALIDQGRP(q->q_mailer->m_qgrp))
5881                         e->e_qgrp = q->q_mailer->m_qgrp;
5882                 else
5883                         e->e_qgrp = 0;
5884                 e->e_dfqgrp = e->e_qgrp;
5885         }
5886 
5887         if (ISVALIDQDIR(e->e_qdir) && ISVALIDQDIR(e->e_dfqdir))
5888         {
5889                 if (tTd(41, 20))
5890                         sm_dprintf("setnewqueue: e_qdir already assigned (%s)\n",
5891                                 qid_printqueue(e->e_qgrp, e->e_qdir));
5892                 return true;
5893         }
5894 
5895         filesys_update();
5896         e->e_qdir = pickqdir(Queue[e->e_qgrp], e->e_msgsize, e);
5897         if (e->e_qdir == NOQDIR)
5898         {
5899                 e->e_qgrp = NOQGRP;
5900                 if (!bitset(EF_FATALERRS, e->e_flags))
5901                         usrerr("452 4.4.5 Insufficient disk space; try again later");
5902                 e->e_flags |= EF_FATALERRS;
5903                 return false;
5904         }
5905 
5906         if (tTd(41, 3))
5907                 sm_dprintf("setnewqueue: Assigned queue directory %s\n",
5908                         qid_printqueue(e->e_qgrp, e->e_qdir));
5909 
5910         if (e->e_xfqgrp == NOQGRP || e->e_xfqdir == NOQDIR)
5911         {
5912                 e->e_xfqgrp = e->e_qgrp;
5913                 e->e_xfqdir = e->e_qdir;
5914         }
5915         e->e_dfqdir = e->e_qdir;
5916         return true;
5917 }
5918 /*
5919 **  CHKQDIR -- check a queue directory
5920 **
5921 **      Parameters:
5922 **              name -- name of queue directory
5923 **              sff -- flags for safefile()
5924 **
5925 **      Returns:
5926 **              is it a queue directory?
5927 */
5928 
5929 static bool chkqdir __P((char *, long));
5930 
5931 static bool
5932 chkqdir(name, sff)
5933         char *name;
5934         long sff;
5935 {
5936         struct stat statb;
5937         int i;
5938 
5939         /* skip over . and .. directories */
5940         if (name[0] == '.' &&
5941             (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
5942                 return false;
5943 #if HASLSTAT
5944         if (lstat(name, &statb) < 0)
5945 #else /* HASLSTAT */
5946         if (stat(name, &statb) < 0)
5947 #endif /* HASLSTAT */
5948         {
5949                 if (tTd(41, 2))
5950                         sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5951                                    name, sm_errstring(errno));
5952                 return false;
5953         }
5954 #if HASLSTAT
5955         if (S_ISLNK(statb.st_mode))
5956         {
5957                 /*
5958                 **  For a symlink we need to make sure the
5959                 **  target is a directory
5960                 */
5961 
5962                 if (stat(name, &statb) < 0)
5963                 {
5964                         if (tTd(41, 2))
5965                                 sm_dprintf("chkqdir: stat(\"%s\"): %s\n",
5966                                            name, sm_errstring(errno));
5967                         return false;
5968                 }
5969         }
5970 #endif /* HASLSTAT */
5971 
5972         if (!S_ISDIR(statb.st_mode))
5973         {
5974                 if (tTd(41, 2))
5975                         sm_dprintf("chkqdir: \"%s\": Not a directory\n",
5976                                 name);
5977                 return false;
5978         }
5979 
5980         /* Print a warning if unsafe (but still use it) */
5981         /* XXX do this only if we want the warning? */
5982         i = safedirpath(name, RunAsUid, RunAsGid, NULL, sff, 0, 0);
5983         if (i != 0)
5984         {
5985                 if (tTd(41, 2))
5986                         sm_dprintf("chkqdir: \"%s\": Not safe: %s\n",
5987                                    name, sm_errstring(i));
5988 #if _FFR_CHK_QUEUE
5989                 if (LogLevel > 8)
5990                         sm_syslog(LOG_WARNING, NOQID,
5991                                   "queue directory \"%s\": Not safe: %s",
5992                                   name, sm_errstring(i));
5993 #endif /* _FFR_CHK_QUEUE */
5994         }
5995         return true;
5996 }
5997 /*
5998 **  MULTIQUEUE_CACHE -- cache a list of paths to queues.
5999 **
6000 **      Each potential queue is checked as the cache is built.
6001 **      Thereafter, each is blindly trusted.
6002 **      Note that we can be called again after a timeout to rebuild
6003 **      (although code for that is not ready yet).
6004 **
6005 **      Parameters:
6006 **              basedir -- base of all queue directories.
6007 **              blen -- strlen(basedir).
6008 **              qg -- queue group.
6009 **              qn -- number of queue directories already cached.
6010 **              phash -- pointer to hash value over queue dirs.
6011 #if SM_CONF_SHM
6012 **                      only used if shared memory is active.
6013 #endif * SM_CONF_SHM *
6014 **
6015 **      Returns:
6016 **              new number of queue directories.
6017 */
6018 
6019 #define INITIAL_SLOTS   20
6020 #define ADD_SLOTS       10
6021 
6022 static int
6023 multiqueue_cache(basedir, blen, qg, qn, phash)
6024         char *basedir;
6025         int blen;
6026         QUEUEGRP *qg;
6027         int qn;
6028         unsigned int *phash;
6029 {
6030         char *cp;
6031         int i, len;
6032         int slotsleft = 0;
6033         long sff = SFF_ANYFILE;
6034         char qpath[MAXPATHLEN];
6035         char subdir[MAXPATHLEN];
6036         char prefix[MAXPATHLEN];        /* dir relative to basedir */
6037 
6038         if (tTd(41, 20))
6039                 sm_dprintf("multiqueue_cache: called\n");
6040 
6041         /* Initialize to current directory */
6042         prefix[0] = '.';
6043         prefix[1] = '\0';
6044         if (qg->qg_numqueues != 0 && qg->qg_qpaths != NULL)
6045         {
6046                 for (i = 0; i < qg->qg_numqueues; i++)
6047                 {
6048                         if (qg->qg_qpaths[i].qp_name != NULL)
6049                                 (void) sm_free(qg->qg_qpaths[i].qp_name); /* XXX */
6050                 }
6051                 (void) sm_free((char *) qg->qg_qpaths); /* XXX */
6052                 qg->qg_qpaths = NULL;
6053                 qg->qg_numqueues = 0;
6054         }
6055 
6056         /* If running as root, allow safedirpath() checks to use privs */
6057         if (RunAsUid == 0)
6058                 sff |= SFF_ROOTOK;
6059 #if _FFR_CHK_QUEUE
6060         sff |= SFF_SAFEDIRPATH|SFF_NOWWFILES;
6061         if (!UseMSP)
6062                 sff |= SFF_NOGWFILES;
6063 #endif /* _FFR_CHK_QUEUE */
6064 
6065         if (!SM_IS_DIR_START(qg->qg_qdir))
6066         {
6067                 /*
6068                 **  XXX we could add basedir, but then we have to realloc()
6069                 **  the string... Maybe another time.
6070                 */
6071 
6072                 syserr("QueuePath %s not absolute", qg->qg_qdir);
6073                 ExitStat = EX_CONFIG;
6074                 return qn;
6075         }
6076 
6077         /* qpath: directory of current workgroup */
6078         len = sm_strlcpy(qpath, qg->qg_qdir, sizeof(qpath));
6079         if (len >= sizeof(qpath))
6080         {
6081                 syserr("QueuePath %.256s too long (%d max)",
6082                        qg->qg_qdir, (int) sizeof(qpath));
6083                 ExitStat = EX_CONFIG;
6084                 return qn;
6085         }
6086 
6087         /* begin of qpath must be same as basedir */
6088         if (strncmp(basedir, qpath, blen) != 0 &&
6089             (strncmp(basedir, qpath, blen - 1) != 0 || len != blen - 1))
6090         {
6091                 syserr("QueuePath %s not subpath of QueueDirectory %s",
6092                         qpath, basedir);
6093                 ExitStat = EX_CONFIG;
6094                 return qn;
6095         }
6096 
6097         /* Do we have a nested subdirectory? */
6098         if (blen < len && SM_FIRST_DIR_DELIM(qg->qg_qdir + blen) != NULL)
6099         {
6100 
6101                 /* Copy subdirectory into prefix for later use */
6102                 if (sm_strlcpy(prefix, qg->qg_qdir + blen, sizeof(prefix)) >=
6103                     sizeof(prefix))
6104                 {
6105                         syserr("QueuePath %.256s too long (%d max)",
6106                                 qg->qg_qdir, (int) sizeof(qpath));
6107                         ExitStat = EX_CONFIG;
6108                         return qn;
6109                 }
6110                 cp = SM_LAST_DIR_DELIM(prefix);
6111                 SM_ASSERT(cp != NULL);
6112                 *cp = '\0';     /* cut off trailing / */
6113         }
6114 
6115         /* This is guaranteed by the basedir check above */
6116         SM_ASSERT(len >= blen - 1);
6117         cp = &qpath[len - 1];
6118         if (*cp == '*')
6119         {
6120                 register DIR *dp;
6121                 register struct dirent *d;
6122                 int off;
6123                 char *delim;
6124                 char relpath[MAXPATHLEN];
6125 
6126                 *cp = '\0';     /* Overwrite wildcard */
6127                 if ((cp = SM_LAST_DIR_DELIM(qpath)) == NULL)
6128                 {
6129                         syserr("QueueDirectory: can not wildcard relative path");
6130                         if (tTd(41, 2))
6131                                 sm_dprintf("multiqueue_cache: \"%s*\": Can not wildcard relative path.\n",
6132                                         qpath);
6133                         ExitStat = EX_CONFIG;
6134                         return qn;
6135                 }
6136                 if (cp == qpath)
6137                 {
6138                         /*
6139                         **  Special case of top level wildcard, like /foo*
6140                         **      Change to //foo*
6141                         */
6142 
6143                         (void) sm_strlcpy(qpath + 1, qpath, sizeof(qpath) - 1);
6144                         ++cp;
6145                 }
6146                 delim = cp;
6147                 *(cp++) = '\0';         /* Replace / with \0 */
6148                 len = strlen(cp);       /* Last component of queue directory */
6149 
6150                 /*
6151                 **  Path relative to basedir, with trailing /
6152                 **  It will be modified below to specify the subdirectories
6153                 **  so they can be opened without chdir().
6154                 */
6155 
6156                 off = sm_strlcpyn(relpath, sizeof(relpath), 2, prefix, "/");
6157                 SM_ASSERT(off < sizeof(relpath));
6158 
6159                 if (tTd(41, 2))
6160                         sm_dprintf("multiqueue_cache: prefix=\"%s%s\"\n",
6161                                    relpath, cp);
6162 
6163                 /* It is always basedir: we don't need to store it per group */
6164                 /* XXX: optimize this! -> one more global? */
6165                 qg->qg_qdir = newstr(basedir);
6166                 qg->qg_qdir[blen - 1] = '\0';        /* cut off trailing / */
6167 
6168                 /*
6169                 **  XXX Should probably wrap this whole loop in a timeout
6170                 **  in case some wag decides to NFS mount the queues.
6171                 */
6172 
6173                 /* Test path to get warning messages. */
6174                 if (qn == 0)
6175                 {
6176                         /*  XXX qg_runasuid and qg_runasgid for specials? */
6177                         i = safedirpath(basedir, RunAsUid, RunAsGid, NULL,
6178                                         sff, 0, 0);
6179                         if (i != 0 && tTd(41, 2))
6180                                 sm_dprintf("multiqueue_cache: \"%s\": Not safe: %s\n",
6181                                            basedir, sm_errstring(i));
6182                 }
6183 
6184                 if ((dp = opendir(prefix)) == NULL)
6185                 {
6186                         syserr("can not opendir(%s/%s)", qg->qg_qdir, prefix);
6187                         if (tTd(41, 2))
6188                                 sm_dprintf("multiqueue_cache: opendir(\"%s/%s\"): %s\n",
6189                                            qg->qg_qdir, prefix,
6190                                            sm_errstring(errno));
6191                         ExitStat = EX_CONFIG;
6192                         return qn;
6193                 }
6194                 while ((d = readdir(dp)) != NULL)
6195                 {
6196                         /* Skip . and .. directories */
6197                         if (strcmp(d->d_name, ".") == 0 ||
6198                             strcmp(d->d_name, "..") == 0)
6199                                 continue;
6200 
6201                         i = strlen(d->d_name);
6202                         if (i < len || strncmp(d->d_name, cp, len) != 0)
6203                         {
6204                                 if (tTd(41, 5))
6205                                         sm_dprintf("multiqueue_cache: \"%s\", skipped\n",
6206                                                 d->d_name);
6207                                 continue;
6208                         }
6209 
6210                         /* Create relative pathname: prefix + local directory */
6211                         i = sizeof(relpath) - off;
6212                         if (sm_strlcpy(relpath + off, d->d_name, i) >= i)
6213                                 continue;       /* way too long */
6214 
6215                         if (!chkqdir(relpath, sff))
6216                                 continue;
6217 
6218                         if (qg->qg_qpaths == NULL)
6219                         {
6220                                 slotsleft = INITIAL_SLOTS;
6221                                 qg->qg_qpaths = (QPATHS *)xalloc((sizeof(*qg->qg_qpaths)) *
6222                                                                 slotsleft);
6223                                 qg->qg_numqueues = 0;
6224                         }
6225                         else if (slotsleft < 1)
6226                         {
6227                                 qg->qg_qpaths = (QPATHS *)sm_realloc((char *)qg->qg_qpaths,
6228                                                           (sizeof(*qg->qg_qpaths)) *
6229                                                           (qg->qg_numqueues +
6230                                                            ADD_SLOTS));
6231                                 if (qg->qg_qpaths == NULL)
6232                                 {
6233                                         (void) closedir(dp);
6234                                         return qn;
6235                                 }
6236                                 slotsleft += ADD_SLOTS;
6237                         }
6238 
6239                         /* check subdirs */
6240                         qg->qg_qpaths[qg->qg_numqueues].qp_subdirs = QP_NOSUB;
6241 
6242 #define CHKRSUBDIR(name, flag)  \
6243         (void) sm_strlcpyn(subdir, sizeof(subdir), 3, relpath, "/", name); \
6244         if (chkqdir(subdir, sff))       \
6245                 qg->qg_qpaths[qg->qg_numqueues].qp_subdirs |= flag;       \
6246         else
6247 
6248 
6249                         CHKRSUBDIR("qf", QP_SUBQF);
6250                         CHKRSUBDIR("df", QP_SUBDF);
6251                         CHKRSUBDIR("xf", QP_SUBXF);
6252 
6253                         /* assert(strlen(d->d_name) < MAXPATHLEN - 14) */
6254                         /* maybe even - 17 (subdirs) */
6255 
6256                         if (prefix[0] != '.')
6257                                 qg->qg_qpaths[qg->qg_numqueues].qp_name =
6258                                         newstr(relpath);
6259                         else
6260                                 qg->qg_qpaths[qg->qg_numqueues].qp_name =
6261                                         newstr(d->d_name);
6262 
6263                         if (tTd(41, 2))
6264                                 sm_dprintf("multiqueue_cache: %d: \"%s\" cached (%x).\n",
6265                                         qg->qg_numqueues, relpath,
6266                                         qg->qg_qpaths[qg->qg_numqueues].qp_subdirs);
6267 #if SM_CONF_SHM
6268                         qg->qg_qpaths[qg->qg_numqueues].qp_idx = qn;
6269                         *phash = hash_q(relpath, *phash);
6270 #endif /* SM_CONF_SHM */
6271                         qg->qg_numqueues++;
6272                         ++qn;
6273                         slotsleft--;
6274                 }
6275                 (void) closedir(dp);
6276 
6277                 /* undo damage */
6278                 *delim = '/';
6279         }
6280         if (qg->qg_numqueues == 0)
6281         {
6282                 qg->qg_qpaths = (QPATHS *) xalloc(sizeof(*qg->qg_qpaths));
6283 
6284                 /* test path to get warning messages */
6285                 i = safedirpath(qpath, RunAsUid, RunAsGid, NULL, sff, 0, 0);
6286                 if (i == ENOENT)
6287                 {
6288                         syserr("can not opendir(%s)", qpath);
6289                         if (tTd(41, 2))
6290                                 sm_dprintf("multiqueue_cache: opendir(\"%s\"): %s\n",
6291                                            qpath, sm_errstring(i));
6292                         ExitStat = EX_CONFIG;
6293                         return qn;
6294                 }
6295 
6296                 qg->qg_qpaths[0].qp_subdirs = QP_NOSUB;
6297                 qg->qg_numqueues = 1;
6298 
6299                 /* check subdirs */
6300 #define CHKSUBDIR(name, flag)   \
6301         (void) sm_strlcpyn(subdir, sizeof(subdir), 3, qg->qg_qdir, "/", name); \
6302         if (chkqdir(subdir, sff))       \
6303                 qg->qg_qpaths[0].qp_subdirs |= flag; \
6304         else
6305 
6306                 CHKSUBDIR("qf", QP_SUBQF);
6307                 CHKSUBDIR("df", QP_SUBDF);
6308                 CHKSUBDIR("xf", QP_SUBXF);
6309 
6310                 if (qg->qg_qdir[blen - 1] != '\0' &&
6311                     qg->qg_qdir[blen] != '\0')
6312                 {
6313                         /*
6314                         **  Copy the last component into qpaths and
6315                         **  cut off qdir
6316                         */
6317 
6318                         qg->qg_qpaths[0].qp_name = newstr(qg->qg_qdir + blen);
6319                         qg->qg_qdir[blen - 1] = '\0';
6320                 }
6321                 else
6322                         qg->qg_qpaths[0].qp_name = newstr(".");
6323 
6324 #if SM_CONF_SHM
6325                 qg->qg_qpaths[0].qp_idx = qn;
6326                 *phash = hash_q(qg->qg_qpaths[0].qp_name, *phash);
6327 #endif /* SM_CONF_SHM */
6328                 ++qn;
6329         }
6330         return qn;
6331 }
6332 
6333 /*
6334 **  FILESYS_FIND -- find entry in FileSys table, or add new one
6335 **
6336 **      Given the pathname of a directory, determine the file system
6337 **      in which that directory resides, and return a pointer to the
6338 **      entry in the FileSys table that describes the file system.
6339 **      A new entry is added if necessary (and requested).
6340 **      If the directory does not exist, -1 is returned.
6341 **
6342 **      Parameters:
6343 **              name -- name of directory (must be persistent!)
6344 **              path -- pathname of directory (name plus maybe "/df")
6345 **              add -- add to structure if not found.
6346 **
6347 **      Returns:
6348 **              >=0: found: index in file system table
6349 **              <0: some error, i.e.,
6350 **              FSF_TOO_MANY: too many filesystems (-> syserr())
6351 **              FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6352 **              FSF_NOT_FOUND: not in list
6353 */
6354 
6355 static short filesys_find __P((const char *, const char *, bool));
6356 
6357 #define FSF_NOT_FOUND   (-1)
6358 #define FSF_STAT_FAIL   (-2)
6359 #define FSF_TOO_MANY    (-3)
6360 
6361 static short
6362 filesys_find(name, path, add)
6363         const char *name;
6364         const char *path;
6365         bool add;
6366 {
6367         struct stat st;
6368         short i;
6369 
6370         if (stat(path, &st) < 0)
6371         {
6372                 syserr("cannot stat queue directory %s", path);
6373                 return FSF_STAT_FAIL;
6374         }
6375         for (i = 0; i < NumFileSys; ++i)
6376         {
6377                 if (FILE_SYS_DEV(i) == st.st_dev)
6378                 {
6379                         /*
6380                         **  Make sure the file system (FS) name is set:
6381                         **  even though the source code indicates that
6382                         **  FILE_SYS_DEV() is only set below, it could be
6383                         **  set via shared memory, hence we need to perform
6384                         **  this check/assignment here.
6385                         */
6386 
6387                         if (NULL == FILE_SYS_NAME(i))
6388                                 FILE_SYS_NAME(i) = name;
6389                         return i;
6390                 }
6391         }
6392         if (i >= MAXFILESYS)
6393         {
6394                 syserr("too many queue file systems (%d max)", MAXFILESYS);
6395                 return FSF_TOO_MANY;
6396         }
6397         if (!add)
6398                 return FSF_NOT_FOUND;
6399 
6400         ++NumFileSys;
6401         FILE_SYS_NAME(i) = name;
6402         FILE_SYS_DEV(i) = st.st_dev;
6403         FILE_SYS_AVAIL(i) = 0;
6404         FILE_SYS_BLKSIZE(i) = 1024; /* avoid divide by zero */
6405         return i;
6406 }
6407 
6408 /*
6409 **  FILESYS_SETUP -- set up mapping from queue directories to file systems
6410 **
6411 **      This data structure is used to efficiently check the amount of
6412 **      free space available in a set of queue directories.
6413 **
6414 **      Parameters:
6415 **              add -- initialize structure if necessary.
6416 **
6417 **      Returns:
6418 **              0: success
6419 **              <0: some error, i.e.,
6420 **              FSF_NOT_FOUND: not in list
6421 **              FSF_STAT_FAIL: can't stat() filesystem (-> syserr())
6422 **              FSF_TOO_MANY: too many filesystems (-> syserr())
6423 */
6424 
6425 static int filesys_setup __P((bool));
6426 
6427 static int
6428 filesys_setup(add)
6429         bool add;
6430 {
6431         int i, j;
6432         short fs;
6433         int ret;
6434 
6435         ret = 0;
6436         for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
6437         {
6438                 for (j = 0; j < Queue[i]->qg_numqueues; ++j)
6439                 {
6440                         QPATHS *qp = &Queue[i]->qg_qpaths[j];
6441                         char qddf[MAXPATHLEN];
6442 
6443                         (void) sm_strlcpyn(qddf, sizeof(qddf), 2, qp->qp_name,
6444                                         (bitset(QP_SUBDF, qp->qp_subdirs)
6445                                                 ? "/df" : ""));
6446                         fs = filesys_find(qp->qp_name, qddf, add);
6447                         if (fs >= 0)
6448                                 qp->qp_fsysidx = fs;
6449                         else
6450                                 qp->qp_fsysidx = 0;
6451                         if (fs < ret)
6452                                 ret = fs;
6453                 }
6454         }
6455         return ret;
6456 }
6457 
6458 /*
6459 **  FILESYS_UPDATE -- update amount of free space on all file systems
6460 **
6461 **      The FileSys table is used to cache the amount of free space
6462 **      available on all queue directory file systems.
6463 **      This function updates the cached information if it has expired.
6464 **
6465 **      Parameters:
6466 **              none.
6467 **
6468 **      Returns:
6469 **              none.
6470 **
6471 **      Side Effects:
6472 **              Updates FileSys table.
6473 */
6474 
6475 void
6476 filesys_update()
6477 {
6478         int i;
6479         long avail, blksize;
6480         time_t now;
6481         static time_t nextupdate = 0;
6482 
6483 #if SM_CONF_SHM
6484         /*
6485         **  Only the daemon updates the shared memory, i.e.,
6486         **  if shared memory is available but the pid is not the
6487         **  one of the daemon, then don't do anything.
6488         */
6489 
6490         if (ShmId != SM_SHM_NO_ID && DaemonPid != CurrentPid)
6491                 return;
6492 #endif /* SM_CONF_SHM */
6493         now = curtime();
6494         if (now < nextupdate)
6495                 return;
6496         nextupdate = now + FILESYS_UPDATE_INTERVAL;
6497         for (i = 0; i < NumFileSys; ++i)
6498         {
6499                 FILESYS *fs = &FILE_SYS(i);
6500 
6501                 avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6502                 if (avail < 0 || blksize <= 0)
6503                 {
6504                         if (LogLevel > 5)
6505                                 sm_syslog(LOG_ERR, NOQID,
6506                                         "filesys_update failed: %s, fs=%s, avail=%ld, blocksize=%ld",
6507                                         sm_errstring(errno),
6508                                         FILE_SYS_NAME(i), avail, blksize);
6509                         fs->fs_avail = 0;
6510                         fs->fs_blksize = 1024; /* avoid divide by zero */
6511                         nextupdate = now + 2; /* let's do this soon again */
6512                 }
6513                 else
6514                 {
6515                         fs->fs_avail = avail;
6516                         fs->fs_blksize = blksize;
6517                 }
6518         }
6519 }
6520 
6521 #if _FFR_ANY_FREE_FS
6522 /*
6523 **  FILESYS_FREE -- check whether there is at least one fs with enough space.
6524 **
6525 **      Parameters:
6526 **              fsize -- file size in bytes
6527 **
6528 **      Returns:
6529 **              true iff there is one fs with more than fsize bytes free.
6530 */
6531 
6532 bool
6533 filesys_free(fsize)
6534         long fsize;
6535 {
6536         int i;
6537 
6538         if (fsize <= 0)
6539                 return true;
6540         for (i = 0; i < NumFileSys; ++i)
6541         {
6542                 long needed = 0;
6543 
6544                 if (FILE_SYS_AVAIL(i) < 0 || FILE_SYS_BLKSIZE(i) <= 0)
6545                         continue;
6546                 needed += fsize / FILE_SYS_BLKSIZE(i)
6547                           + ((fsize % FILE_SYS_BLKSIZE(i)
6548                               > 0) ? 1 : 0)
6549                           + MinBlocksFree;
6550                 if (needed <= FILE_SYS_AVAIL(i))
6551                         return true;
6552         }
6553         return false;
6554 }
6555 #endif /* _FFR_ANY_FREE_FS */
6556 
6557 /*
6558 **  DISK_STATUS -- show amount of free space in queue directories
6559 **
6560 **      Parameters:
6561 **              out -- output file pointer.
6562 **              prefix -- string to output in front of each line.
6563 **
6564 **      Returns:
6565 **              none.
6566 */
6567 
6568 void
6569 disk_status(out, prefix)
6570         SM_FILE_T *out;
6571         char *prefix;
6572 {
6573         int i;
6574         long avail, blksize;
6575         long free;
6576 
6577         for (i = 0; i < NumFileSys; ++i)
6578         {
6579                 avail = freediskspace(FILE_SYS_NAME(i), &blksize);
6580                 if (avail >= 0 && blksize > 0)
6581                 {
6582                         free = (long)((double) avail *
6583                                 ((double) blksize / 1024));
6584                 }
6585                 else
6586                         free = -1;
6587                 (void) sm_io_fprintf(out, SM_TIME_DEFAULT,
6588                                 "%s%d/%s/%ld\r\n",
6589                                 prefix, i,
6590                                 FILE_SYS_NAME(i),
6591                                         free);
6592         }
6593 }
6594 
6595 #if SM_CONF_SHM
6596 
6597 /*
6598 **  INIT_SEM -- initialize semaphore system
6599 **
6600 **      Parameters:
6601 **              owner -- is this the owner of semaphores?
6602 **
6603 **      Returns:
6604 **              none.
6605 */
6606 
6607 #if _FFR_USE_SEM_LOCKING
6608 #if SM_CONF_SEM
6609 static int SemId = -1;          /* Semaphore Id */
6610 int SemKey = SM_SEM_KEY;
6611 #endif /* SM_CONF_SEM */
6612 #endif /* _FFR_USE_SEM_LOCKING */
6613 
6614 static void init_sem __P((bool));
6615 
6616 static void
6617 init_sem(owner)
6618         bool owner;
6619 {
6620 #if _FFR_USE_SEM_LOCKING
6621 #if SM_CONF_SEM
6622         SemId = sm_sem_start(SemKey, 1, 0, owner);
6623         if (SemId < 0)
6624         {
6625                 sm_syslog(LOG_ERR, NOQID,
6626                         "func=init_sem, sem_key=%ld, sm_sem_start=%d, error=%s",
6627                         (long) SemKey, SemId, sm_errstring(-SemId));
6628                 return;
6629         }
6630         if (owner && RunAsUid != 0)
6631         {
6632                 int r;
6633 
6634                 r = sm_semsetowner(SemId, RunAsUid, RunAsGid, 0660);
6635                 if (r != 0)
6636                         sm_syslog(LOG_ERR, NOQID,
6637                                 "key=%ld, sm_semsetowner=%d, RunAsUid=%d, RunAsGid=%d",
6638                                 (long) SemKey, r, RunAsUid, RunAsGid);
6639         }
6640 #endif /* SM_CONF_SEM */
6641 #endif /* _FFR_USE_SEM_LOCKING */
6642         return;
6643 }
6644 
6645 /*
6646 **  STOP_SEM -- stop semaphore system
6647 **
6648 **      Parameters:
6649 **              owner -- is this the owner of semaphores?
6650 **
6651 **      Returns:
6652 **              none.
6653 */
6654 
6655 static void stop_sem __P((bool));
6656 
6657 static void
6658 stop_sem(owner)
6659         bool owner;
6660 {
6661 #if _FFR_USE_SEM_LOCKING
6662 #if SM_CONF_SEM
6663         if (owner && SemId >= 0)
6664                 sm_sem_stop(SemId);
6665 #endif /* SM_CONF_SEM */
6666 #endif /* _FFR_USE_SEM_LOCKING */
6667         return;
6668 }
6669 
6670 /*
6671 **  UPD_QS -- update information about queue when adding/deleting an entry
6672 **
6673 **      Parameters:
6674 **              e -- envelope.
6675 **              count -- add/remove entry (+1/0/-1: add/no change/remove)
6676 **              space -- update the space available as well.
6677 **                      (>0/0/<0: add/no change/remove)
6678 **              where -- caller (for logging)
6679 **
6680 **      Returns:
6681 **              none.
6682 **
6683 **      Side Effects:
6684 **              Modifies available space in filesystem.
6685 **              Changes number of entries in queue directory.
6686 */
6687 
6688 void
6689 upd_qs(e, count, space, where)
6690         ENVELOPE *e;
6691         int count;
6692         int space;
6693         char *where;
6694 {
6695         short fidx;
6696         int idx;
6697 # if _FFR_USE_SEM_LOCKING
6698         int r;
6699 # endif /* _FFR_USE_SEM_LOCKING */
6700         long s;
6701 
6702         if (ShmId == SM_SHM_NO_ID || e == NULL)
6703                 return;
6704         if (e->e_qgrp == NOQGRP || e->e_qdir == NOQDIR)
6705                 return;
6706         idx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_idx;
6707         if (tTd(73,2))
6708                 sm_dprintf("func=upd_qs, count=%d, space=%d, where=%s, idx=%d, entries=%d\n",
6709                         count, space, where, idx, QSHM_ENTRIES(idx));
6710 
6711         /* XXX in theory this needs to be protected with a mutex */
6712         if (QSHM_ENTRIES(idx) >= 0 && count != 0)
6713         {
6714 # if _FFR_USE_SEM_LOCKING
6715                 r = sm_sem_acq(SemId, 0, 1);
6716 # endif /* _FFR_USE_SEM_LOCKING */
6717                 QSHM_ENTRIES(idx) += count;
6718 # if _FFR_USE_SEM_LOCKING
6719                 if (r >= 0)
6720                         r = sm_sem_rel(SemId, 0, 1);
6721 # endif /* _FFR_USE_SEM_LOCKING */
6722         }
6723 
6724         fidx = Queue[e->e_qgrp]->qg_qpaths[e->e_qdir].qp_fsysidx;
6725         if (fidx < 0)
6726                 return;
6727 
6728         /* update available space also?  (might be loseqfile) */
6729         if (space == 0)
6730                 return;
6731 
6732         /* convert size to blocks; this causes rounding errors */
6733         s = e->e_msgsize / FILE_SYS_BLKSIZE(fidx);
6734         if (s == 0)
6735                 return;
6736 
6737         /* XXX in theory this needs to be protected with a mutex */
6738         if (space > 0)
6739                 FILE_SYS_AVAIL(fidx) += s;
6740         else
6741                 FILE_SYS_AVAIL(fidx) -= s;
6742 
6743 }
6744 
6745 static bool write_key_file __P((char *, long));
6746 static long read_key_file __P((char *, long));
6747 
6748 /*
6749 **  WRITE_KEY_FILE -- record some key into a file.
6750 **
6751 **      Parameters:
6752 **              keypath -- file name.
6753 **              key -- key to write.
6754 **
6755 **      Returns:
6756 **              true iff file could be written.
6757 **
6758 **      Side Effects:
6759 **              writes file.
6760 */
6761 
6762 static bool
6763 write_key_file(keypath, key)
6764         char *keypath;
6765         long key;
6766 {
6767         bool ok;
6768         long sff;
6769         SM_FILE_T *keyf;
6770 
6771         ok = false;
6772         if (keypath == NULL || *keypath == '\0')
6773                 return ok;
6774         sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT;
6775         if (TrustedUid != 0 && RealUid == TrustedUid)
6776                 sff |= SFF_OPENASROOT;
6777         keyf = safefopen(keypath, O_WRONLY|O_TRUNC, FileMode, sff);
6778         if (keyf == NULL)
6779         {
6780                 sm_syslog(LOG_ERR, NOQID, "unable to write %s: %s",
6781                           keypath, sm_errstring(errno));
6782         }
6783         else
6784         {
6785                 if (geteuid() == 0 && RunAsUid != 0)
6786                 {
6787 #  if HASFCHOWN
6788                         int fd;
6789 
6790                         fd = keyf->f_file;
6791                         if (fd >= 0 && fchown(fd, RunAsUid, -1) < 0)
6792                         {
6793                                 int err = errno;
6794 
6795                                 sm_syslog(LOG_ALERT, NOQID,
6796                                           "ownership change on %s to %d failed: %s",
6797                                           keypath, RunAsUid, sm_errstring(err));
6798                         }
6799 #  endif /* HASFCHOWN */
6800                 }
6801                 ok = sm_io_fprintf(keyf, SM_TIME_DEFAULT, "%ld\n", key) !=
6802                      SM_IO_EOF;
6803                 ok = (sm_io_close(keyf, SM_TIME_DEFAULT) != SM_IO_EOF) && ok;
6804         }
6805         return ok;
6806 }
6807 
6808 /*
6809 **  READ_KEY_FILE -- read a key from a file.
6810 **
6811 **      Parameters:
6812 **              keypath -- file name.
6813 **              key -- default key.
6814 **
6815 **      Returns:
6816 **              key.
6817 */
6818 
6819 static long
6820 read_key_file(keypath, key)
6821         char *keypath;
6822         long key;
6823 {
6824         int r;
6825         long sff, n;
6826         SM_FILE_T *keyf;
6827 
6828         if (keypath == NULL || *keypath == '\0')
6829                 return key;
6830         sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY;
6831         if (RealUid == 0 || (TrustedUid != 0 && RealUid == TrustedUid))
6832                 sff |= SFF_OPENASROOT;
6833         keyf = safefopen(keypath, O_RDONLY, FileMode, sff);
6834         if (keyf == NULL)
6835         {
6836                 sm_syslog(LOG_ERR, NOQID, "unable to read %s: %s",
6837                           keypath, sm_errstring(errno));
6838         }
6839         else
6840         {
6841                 r = sm_io_fscanf(keyf, SM_TIME_DEFAULT, "%ld", &n);
6842                 if (r == 1)
6843                         key = n;
6844                 (void) sm_io_close(keyf, SM_TIME_DEFAULT);
6845         }
6846         return key;
6847 }
6848 
6849 /*
6850 **  INIT_SHM -- initialize shared memory structure
6851 **
6852 **      Initialize or attach to shared memory segment.
6853 **      Currently it is not a fatal error if this doesn't work.
6854 **      However, it causes us to have a "fallback" storage location
6855 **      for everything that is supposed to be in the shared memory,
6856 **      which makes the code slightly ugly.
6857 **
6858 **      Parameters:
6859 **              qn -- number of queue directories.
6860 **              owner -- owner of shared memory.
6861 **              hash -- identifies data that is stored in shared memory.
6862 **
6863 **      Returns:
6864 **              none.
6865 */
6866 
6867 static void init_shm __P((int, bool, unsigned int));
6868 
6869 static void
6870 init_shm(qn, owner, hash)
6871         int qn;
6872         bool owner;
6873         unsigned int hash;
6874 {
6875         int i;
6876         int count;
6877         int save_errno;
6878         bool keyselect;
6879 
6880         PtrFileSys = &FileSys[0];
6881         PNumFileSys = &Numfilesys;
6882 /* if this "key" is specified: select one yourself */
6883 #define SEL_SHM_KEY     ((key_t) -1)
6884 #define FIRST_SHM_KEY   25
6885 
6886         /* This allows us to disable shared memory at runtime. */
6887         if (ShmKey == 0)
6888                 return;
6889 
6890         count = 0;
6891         shms = SM_T_SIZE + qn * sizeof(QUEUE_SHM_T);
6892         keyselect = ShmKey == SEL_SHM_KEY;
6893         if (keyselect)
6894         {
6895                 if (owner)
6896                         ShmKey = FIRST_SHM_KEY;
6897                 else
6898                 {
6899                         errno = 0;
6900                         ShmKey = read_key_file(ShmKeyFile, ShmKey);
6901                         keyselect = false;
6902                         if (ShmKey == SEL_SHM_KEY)
6903                         {
6904                                 save_errno = (errno != 0) ? errno : EINVAL;
6905                                 goto error;
6906                         }
6907                 }
6908         }
6909         for (;;)
6910         {
6911                 /* allow read/write access for group? */
6912                 Pshm = sm_shmstart(ShmKey, shms,
6913                                 SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3),
6914                                 &ShmId, owner);
6915                 save_errno = errno;
6916                 if (Pshm != NULL || !sm_file_exists(save_errno))
6917                         break;
6918                 if (++count >= 3)
6919                 {
6920                         if (keyselect)
6921                         {
6922                                 ++ShmKey;
6923 
6924                                 /* back where we started? */
6925                                 if (ShmKey == SEL_SHM_KEY)
6926                                         break;
6927                                 continue;
6928                         }
6929                         break;
6930                 }
6931 
6932                 /* only sleep if we are at the first key */
6933                 if (!keyselect || ShmKey == SEL_SHM_KEY)
6934                         sleep(count);
6935         }
6936         if (Pshm != NULL)
6937         {
6938                 int *p;
6939 
6940                 if (keyselect)
6941                         (void) write_key_file(ShmKeyFile, (long) ShmKey);
6942                 if (owner && RunAsUid != 0)
6943                 {
6944                         i = sm_shmsetowner(ShmId, RunAsUid, RunAsGid, 0660);
6945                         if (i != 0)
6946                                 sm_syslog(LOG_ERR, NOQID,
6947                                         "key=%ld, sm_shmsetowner=%d, RunAsUid=%d, RunAsGid=%d",
6948                                         (long) ShmKey, i, RunAsUid, RunAsGid);
6949                 }
6950                 p = (int *) Pshm;
6951                 if (owner)
6952                 {
6953                         *p = (int) shms;
6954                         *((pid_t *) SHM_OFF_PID(Pshm)) = CurrentPid;
6955                         p = (int *) SHM_OFF_TAG(Pshm);
6956                         *p = hash;
6957                 }
6958                 else
6959                 {
6960                         if (*p != (int) shms)
6961                         {
6962                                 save_errno = EINVAL;
6963                                 cleanup_shm(false);
6964                                 goto error;
6965                         }
6966                         p = (int *) SHM_OFF_TAG(Pshm);
6967                         if (*p != (int) hash)
6968                         {
6969                                 save_errno = EINVAL;
6970                                 cleanup_shm(false);
6971                                 goto error;
6972                         }
6973 
6974                         /*
6975                         **  XXX how to check the pid?
6976                         **  Read it from the pid-file? That does
6977                         **  not need to exist.
6978                         **  We could disable shm if we can't confirm
6979                         **  that it is the right one.
6980                         */
6981                 }
6982 
6983                 PtrFileSys = (FILESYS *) OFF_FILE_SYS(Pshm);
6984                 PNumFileSys = (int *) OFF_NUM_FILE_SYS(Pshm);
6985                 QShm = (QUEUE_SHM_T *) OFF_QUEUE_SHM(Pshm);
6986                 PRSATmpCnt = (int *) OFF_RSA_TMP_CNT(Pshm);
6987                 *PRSATmpCnt = 0;
6988                 if (owner)
6989                 {
6990                         /* initialize values in shared memory */
6991                         NumFileSys = 0;
6992                         for (i = 0; i < qn; i++)
6993                                 QShm[i].qs_entries = -1;
6994                 }
6995                 init_sem(owner);
6996                 return;
6997         }
6998   error:
6999         if (LogLevel > (owner ? 8 : 11))
7000         {
7001                 sm_syslog(owner ? LOG_ERR : LOG_NOTICE, NOQID,
7002                           "can't %s shared memory, key=%ld: %s",
7003                           owner ? "initialize" : "attach to",
7004                           (long) ShmKey, sm_errstring(save_errno));
7005         }
7006 }
7007 #endif /* SM_CONF_SHM */
7008 
7009 
7010 /*
7011 **  SETUP_QUEUES -- set up all queue groups
7012 **
7013 **      Parameters:
7014 **              owner -- owner of shared memory?
7015 **
7016 **      Returns:
7017 **              none.
7018 **
7019 #if SM_CONF_SHM
7020 **      Side Effects:
7021 **              attaches shared memory.
7022 #endif * SM_CONF_SHM *
7023 */
7024 
7025 void
7026 setup_queues(owner)
7027         bool owner;
7028 {
7029         int i, qn, len;
7030         unsigned int hashval;
7031         time_t now;
7032         char basedir[MAXPATHLEN];
7033         struct stat st;
7034 
7035         /*
7036         **  Determine basedir for all queue directories.
7037         **  All queue directories must be (first level) subdirectories
7038         **  of the basedir.  The basedir is the QueueDir
7039         **  without wildcards, but with trailing /
7040         */
7041 
7042         hashval = 0;
7043         errno = 0;
7044         len = sm_strlcpy(basedir, QueueDir, sizeof(basedir));
7045 
7046         /* Provide space for trailing '/' */
7047         if (len >= sizeof(basedir) - 1)
7048         {
7049                 syserr("QueueDirectory: path too long: %d,  max %d",
7050                         len, (int) sizeof(basedir) - 1);
7051                 ExitStat = EX_CONFIG;
7052                 return;
7053         }
7054         SM_ASSERT(len > 0);
7055         if (basedir[len - 1] == '*')
7056         {
7057                 char *cp;
7058 
7059                 cp = SM_LAST_DIR_DELIM(basedir);
7060                 if (cp == NULL)
7061                 {
7062                         syserr("QueueDirectory: can not wildcard relative path \"%s\"",
7063                                 QueueDir);
7064                         if (tTd(41, 2))
7065                                 sm_dprintf("setup_queues: \"%s\": Can not wildcard relative path.\n",
7066                                         QueueDir);
7067                         ExitStat = EX_CONFIG;
7068                         return;
7069                 }
7070 
7071                 /* cut off wildcard pattern */
7072                 *++cp = '\0';
7073                 len = cp - basedir;
7074         }
7075         else if (!SM_IS_DIR_DELIM(basedir[len - 1]))
7076         {
7077                 /* append trailing slash since it is a directory */
7078                 basedir[len] = '/';
7079                 basedir[++len] = '\0';
7080         }
7081 
7082         /* len counts up to the last directory delimiter */
7083         SM_ASSERT(basedir[len - 1] == '/');
7084 
7085         if (chdir(basedir) < 0)
7086         {
7087                 int save_errno = errno;
7088 
7089                 syserr("can not chdir(%s)", basedir);
7090                 if (save_errno == EACCES)
7091                         (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
7092                                 "Program mode requires special privileges, e.g., root or TrustedUser.\n");
7093                 if (tTd(41, 2))
7094                         sm_dprintf("setup_queues: \"%s\": %s\n",
7095                                    basedir, sm_errstring(errno));
7096                 ExitStat = EX_CONFIG;
7097                 return;
7098         }
7099 #if SM_CONF_SHM
7100         hashval = hash_q(basedir, hashval);
7101 #endif /* SM_CONF_SHM */
7102 
7103         /* initialize for queue runs */
7104         DoQueueRun = false;
7105         now = curtime();
7106         for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
7107                 Queue[i]->qg_nextrun = now;
7108 
7109 
7110         if (UseMSP && OpMode != MD_TEST)
7111         {
7112                 long sff = SFF_CREAT;
7113 
7114                 if (stat(".", &st) < 0)
7115                 {
7116                         syserr("can not stat(%s)", basedir);
7117                         if (tTd(41, 2))
7118                                 sm_dprintf("setup_queues: \"%s\": %s\n",
7119                                            basedir, sm_errstring(errno));
7120                         ExitStat = EX_CONFIG;
7121                         return;
7122                 }
7123                 if (RunAsUid == 0)
7124                         sff |= SFF_ROOTOK;
7125 
7126                 /*
7127                 **  Check queue directory permissions.
7128                 **      Can we write to a group writable queue directory?
7129                 */
7130 
7131                 if (bitset(S_IWGRP, QueueFileMode) &&
7132                     bitset(S_IWGRP, st.st_mode) &&
7133                     safefile(" ", RunAsUid, RunAsGid, RunAsUserName, sff,
7134                              QueueFileMode, NULL) != 0)
7135                 {
7136                         syserr("can not write to queue directory %s (RunAsGid=%d, required=%d)",
7137                                 basedir, (int) RunAsGid, (int) st.st_gid);
7138                 }
7139                 if (bitset(S_IWOTH|S_IXOTH, st.st_mode))
7140                 {
7141 #if _FFR_MSP_PARANOIA
7142                         syserr("dangerous permissions=%o on queue directory %s",
7143                                 (int) st.st_mode, basedir);
7144 #else /* _FFR_MSP_PARANOIA */
7145                         if (LogLevel > 0)
7146                                 sm_syslog(LOG_ERR, NOQID,
7147                                           "dangerous permissions=%o on queue directory %s",
7148                                           (int) st.st_mode, basedir);
7149 #endif /* _FFR_MSP_PARANOIA */
7150                 }
7151 #if _FFR_MSP_PARANOIA
7152                 if (NumQueue > 1)
7153                         syserr("can not use multiple queues for MSP");
7154 #endif /* _FFR_MSP_PARANOIA */
7155         }
7156 
7157         /* initial number of queue directories */
7158         qn = 0;
7159         for (i = 0; i < NumQueue && Queue[i] != NULL; i++)
7160                 qn = multiqueue_cache(basedir, len, Queue[i], qn, &hashval);
7161 
7162 #if SM_CONF_SHM
7163         init_shm(qn, owner, hashval);
7164         i = filesys_setup(owner || ShmId == SM_SHM_NO_ID);
7165         if (i == FSF_NOT_FOUND)
7166         {
7167                 /*
7168                 **  We didn't get the right filesystem data
7169                 **  This may happen if we don't have the right shared memory.
7170                 **  So let's do this without shared memory.
7171                 */
7172 
7173                 SM_ASSERT(!owner);
7174                 cleanup_shm(false);     /* release shared memory */
7175                 i = filesys_setup(false);
7176                 if (i < 0)
7177                         syserr("filesys_setup failed twice, result=%d", i);
7178                 else if (LogLevel > 8)
7179                         sm_syslog(LOG_WARNING, NOQID,
7180                                   "shared memory does not contain expected data, ignored");
7181         }
7182 #else /* SM_CONF_SHM */
7183         i = filesys_setup(true);
7184 #endif /* SM_CONF_SHM */
7185         if (i < 0)
7186                 ExitStat = EX_CONFIG;
7187 }
7188 
7189 #if SM_CONF_SHM
7190 /*
7191 **  CLEANUP_SHM -- do some cleanup work for shared memory etc
7192 **
7193 **      Parameters:
7194 **              owner -- owner of shared memory?
7195 **
7196 **      Returns:
7197 **              none.
7198 **
7199 **      Side Effects:
7200 **              detaches shared memory.
7201 */
7202 
7203 void
7204 cleanup_shm(owner)
7205         bool owner;
7206 {
7207         if (ShmId != SM_SHM_NO_ID)
7208         {
7209                 if (sm_shmstop(Pshm, ShmId, owner) < 0 && LogLevel > 8)
7210                         sm_syslog(LOG_INFO, NOQID, "sm_shmstop failed=%s",
7211                                   sm_errstring(errno));
7212                 Pshm = NULL;
7213                 ShmId = SM_SHM_NO_ID;
7214         }
7215         stop_sem(owner);
7216 }
7217 #endif /* SM_CONF_SHM */
7218 
7219 /*
7220 **  CLEANUP_QUEUES -- do some cleanup work for queues
7221 **
7222 **      Parameters:
7223 **              none.
7224 **
7225 **      Returns:
7226 **              none.
7227 **
7228 */
7229 
7230 void
7231 cleanup_queues()
7232 {
7233         sync_queue_time();
7234 }
7235 /*
7236 **  SET_DEF_QUEUEVAL -- set default values for a queue group.
7237 **
7238 **      Parameters:
7239 **              qg -- queue group
7240 **              all -- set all values (true for default group)?
7241 **
7242 **      Returns:
7243 **              none.
7244 **
7245 **      Side Effects:
7246 **              sets default values for the queue group.
7247 */
7248 
7249 void
7250 set_def_queueval(qg, all)
7251         QUEUEGRP *qg;
7252         bool all;
7253 {
7254         if (bitnset(QD_DEFINED, qg->qg_flags))
7255                 return;
7256         if (all)
7257                 qg->qg_qdir = QueueDir;
7258 #if _FFR_QUEUE_GROUP_SORTORDER
7259         qg->qg_sortorder = QueueSortOrder;
7260 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7261         qg->qg_maxqrun = all ? MaxRunnersPerQueue : -1;
7262         qg->qg_nice = NiceQueueRun;
7263 }
7264 /*
7265 **  MAKEQUEUE -- define a new queue.
7266 **
7267 **      Parameters:
7268 **              line -- description of queue.  This is in labeled fields.
7269 **                      The fields are:
7270 **                         F -- the flags associated with the queue
7271 **                         I -- the interval between running the queue
7272 **                         J -- the maximum # of jobs in work list
7273 **                         [M -- the maximum # of jobs in a queue run]
7274 **                         N -- the niceness at which to run
7275 **                         P -- the path to the queue
7276 **                         S -- the queue sorting order
7277 **                         R -- number of parallel queue runners
7278 **                         r -- max recipients per envelope
7279 **                      The first word is the canonical name of the queue.
7280 **              qdef -- this is a 'Q' definition from .cf
7281 **
7282 **      Returns:
7283 **              none.
7284 **
7285 **      Side Effects:
7286 **              enters the queue into the queue table.
7287 */
7288 
7289 void
7290 makequeue(line, qdef)
7291         char *line;
7292         bool qdef;
7293 {
7294         register char *p;
7295         register QUEUEGRP *qg;
7296         register STAB *s;
7297         int i;
7298         char fcode;
7299 
7300         /* allocate a queue and set up defaults */
7301         qg = (QUEUEGRP *) xalloc(sizeof(*qg));
7302         memset((char *) qg, '\0', sizeof(*qg));
7303 
7304         if (line[0] == '\0')
7305         {
7306                 syserr("name required for queue");
7307                 return;
7308         }
7309 
7310         /* collect the queue name */
7311         for (p = line;
7312              *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p));
7313              p++)
7314                 continue;
7315         if (*p != '\0')
7316                 *p++ = '\0';
7317         qg->qg_name = newstr(line);
7318 
7319         /* set default values, can be overridden below */
7320         set_def_queueval(qg, false);
7321 
7322         /* now scan through and assign info from the fields */
7323         while (*p != '\0')
7324         {
7325                 auto char *delimptr;
7326 
7327                 while (*p != '\0' &&
7328                        (*p == ',' || (isascii(*p) && isspace(*p))))
7329                         p++;
7330 
7331                 /* p now points to field code */
7332                 fcode = *p;
7333                 while (*p != '\0' && *p != '=' && *p != ',')
7334                         p++;
7335                 if (*p++ != '=')
7336                 {
7337                         syserr("queue %s: `=' expected", qg->qg_name);
7338                         return;
7339                 }
7340                 while (isascii(*p) && isspace(*p))
7341                         p++;
7342 
7343                 /* p now points to the field body */
7344                 p = munchstring(p, &delimptr, ',');
7345 
7346                 /* install the field into the queue struct */
7347                 switch (fcode)
7348                 {
7349                   case 'P':             /* pathname */
7350                         if (*p == '\0')
7351                                 syserr("queue %s: empty path name",
7352                                         qg->qg_name);
7353                         else
7354                                 qg->qg_qdir = newstr(p);
7355                         break;
7356 
7357                   case 'F':             /* flags */
7358                         for (; *p != '\0'; p++)
7359                                 if (!(isascii(*p) && isspace(*p)))
7360                                         setbitn(*p, qg->qg_flags);
7361                         break;
7362 
7363                         /*
7364                         **  Do we need two intervals here:
7365                         **  One for persistent queue runners,
7366                         **  one for "normal" queue runs?
7367                         */
7368 
7369                   case 'I':     /* interval between running the queue */
7370                         qg->qg_queueintvl = convtime(p, 'm');
7371                         break;
7372 
7373                   case 'N':             /* run niceness */
7374                         qg->qg_nice = atoi(p);
7375                         break;
7376 
7377                   case 'R':             /* maximum # of runners for the group */
7378                         i = atoi(p);
7379 
7380                         /* can't have more runners than allowed total */
7381                         if (MaxQueueChildren > 0 && i > MaxQueueChildren)
7382                         {
7383                                 qg->qg_maxqrun = MaxQueueChildren;
7384                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7385                                                      "Q=%s: R=%d exceeds MaxQueueChildren=%d, set to MaxQueueChildren\n",
7386                                                      qg->qg_name, i,
7387                                                      MaxQueueChildren);
7388                         }
7389                         else
7390                                 qg->qg_maxqrun = i;
7391                         break;
7392 
7393                   case 'J':             /* maximum # of jobs in work list */
7394                         qg->qg_maxlist = atoi(p);
7395                         break;
7396 
7397                   case 'r':             /* max recipients per envelope */
7398                         qg->qg_maxrcpt = atoi(p);
7399                         break;
7400 
7401 #if _FFR_QUEUE_GROUP_SORTORDER
7402                   case 'S':             /* queue sorting order */
7403                         switch (*p)
7404                         {
7405                           case 'h':     /* Host first */
7406                           case 'H':
7407                                 qg->qg_sortorder = QSO_BYHOST;
7408                                 break;
7409 
7410                           case 'p':     /* Priority order */
7411                           case 'P':
7412                                 qg->qg_sortorder = QSO_BYPRIORITY;
7413                                 break;
7414 
7415                           case 't':     /* Submission time */
7416                           case 'T':
7417                                 qg->qg_sortorder = QSO_BYTIME;
7418                                 break;
7419 
7420                           case 'f':     /* File name */
7421                           case 'F':
7422                                 qg->qg_sortorder = QSO_BYFILENAME;
7423                                 break;
7424 
7425                           case 'm':     /* Modification time */
7426                           case 'M':
7427                                 qg->qg_sortorder = QSO_BYMODTIME;
7428                                 break;
7429 
7430                           case 'r':     /* Random */
7431                           case 'R':
7432                                 qg->qg_sortorder = QSO_RANDOM;
7433                                 break;
7434 
7435 # if _FFR_RHS
7436                           case 's':     /* Shuffled host name */
7437                           case 'S':
7438                                 qg->qg_sortorder = QSO_BYSHUFFLE;
7439                                 break;
7440 # endif /* _FFR_RHS */
7441 
7442                           case 'n':     /* none */
7443                           case 'N':
7444                                 qg->qg_sortorder = QSO_NONE;
7445                                 break;
7446 
7447                           default:
7448                                 syserr("Invalid queue sort order \"%s\"", p);
7449                         }
7450                         break;
7451 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
7452 
7453                   default:
7454                         syserr("Q%s: unknown queue equate %c=",
7455                                qg->qg_name, fcode);
7456                         break;
7457                 }
7458 
7459                 p = delimptr;
7460         }
7461 
7462 #if !HASNICE
7463         if (qg->qg_nice != NiceQueueRun)
7464         {
7465                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7466                                      "Q%s: Warning: N= set on system that doesn't support nice()\n",
7467                                      qg->qg_name);
7468         }
7469 #endif /* !HASNICE */
7470 
7471         /* do some rationality checking */
7472         if (NumQueue >= MAXQUEUEGROUPS)
7473         {
7474                 syserr("too many queue groups defined (%d max)",
7475                         MAXQUEUEGROUPS);
7476                 return;
7477         }
7478 
7479         if (qg->qg_qdir == NULL)
7480         {
7481                 if (QueueDir == NULL || *QueueDir == '\0')
7482                 {
7483                         syserr("QueueDir must be defined before queue groups");
7484                         return;
7485                 }
7486                 qg->qg_qdir = newstr(QueueDir);
7487         }
7488 
7489         if (qg->qg_maxqrun > 1 && !bitnset(QD_FORK, qg->qg_flags))
7490         {
7491                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
7492                                      "Warning: Q=%s: R=%d: multiple queue runners specified\n\tbut flag '%c' is not set\n",
7493                                      qg->qg_name, qg->qg_maxqrun, QD_FORK);
7494         }
7495 
7496         /* enter the queue into the symbol table */
7497         if (tTd(37, 8))
7498                 sm_syslog(LOG_INFO, NOQID,
7499                           "Adding %s to stab, path: %s", qg->qg_name,
7500                           qg->qg_qdir);
7501         s = stab(qg->qg_name, ST_QUEUE, ST_ENTER);
7502         if (s->s_quegrp != NULL)
7503         {
7504                 i = s->s_quegrp->qg_index;
7505 
7506                 /* XXX what about the pointers inside this struct? */
7507                 sm_free(s->s_quegrp); /* XXX */
7508         }
7509         else
7510                 i = NumQueue++;
7511         Queue[i] = s->s_quegrp = qg;
7512         qg->qg_index = i;
7513 
7514         /* set default value for max queue runners */
7515         if (qg->qg_maxqrun < 0)
7516         {
7517                 if (MaxRunnersPerQueue > 0)
7518                         qg->qg_maxqrun = MaxRunnersPerQueue;
7519                 else
7520                         qg->qg_maxqrun = 1;
7521         }
7522         if (qdef)
7523                 setbitn(QD_DEFINED, qg->qg_flags);
7524 }
7525 #if 0
7526 /*
7527 **  HASHFQN -- calculate a hash value for a fully qualified host name
7528 **
7529 **      Arguments:
7530 **              fqn -- an all lower-case host.domain string
7531 **              buckets -- the number of buckets (queue directories)
7532 **
7533 **      Returns:
7534 **              a bucket number (signed integer)
7535 **              -1 on error
7536 **
7537 **      Contributed by Exactis.com, Inc.
7538 */
7539 
7540 int
7541 hashfqn(fqn, buckets)
7542         register char *fqn;
7543         int buckets;
7544 {
7545         register char *p;
7546         register int h = 0, hash, cnt;
7547 
7548         if (fqn == NULL)
7549                 return -1;
7550 
7551         /*
7552         **  A variation on the gdb hash
7553         **  This is the best as of Feb 19, 1996 --bcx
7554         */
7555 
7556         p = fqn;
7557         h = 0x238F13AF * strlen(p);
7558         for (cnt = 0; *p != 0; ++p, cnt++)
7559         {
7560                 h = (h + (*p << (cnt * 5 % 24))) & 0x7FFFFFFF;
7561         }
7562         h = (1103515243 * h + 12345) & 0x7FFFFFFF;
7563         if (buckets < 2)
7564                 hash = 0;
7565         else
7566                 hash = (h % buckets);
7567 
7568         return hash;
7569 }
7570 #endif /* 0 */
7571 
7572 /*
7573 **  A structure for sorting Queue according to maxqrun without
7574 **      screwing up Queue itself.
7575 */
7576 
7577 struct sortqgrp
7578 {
7579         int sg_idx;             /* original index */
7580         int sg_maxqrun;         /* max queue runners */
7581 };
7582 typedef struct sortqgrp SORTQGRP_T;
7583 static int cmpidx __P((const void *, const void *));
7584 
7585 static int
7586 cmpidx(a, b)
7587         const void *a;
7588         const void *b;
7589 {
7590         /* The sort is highest to lowest, so the comparison is reversed */
7591         if (((SORTQGRP_T *)a)->sg_maxqrun < ((SORTQGRP_T *)b)->sg_maxqrun)
7592                 return 1;
7593         else if (((SORTQGRP_T *)a)->sg_maxqrun > ((SORTQGRP_T *)b)->sg_maxqrun)
7594                 return -1;
7595         else
7596                 return 0;
7597 }
7598 
7599 /*
7600 **  MAKEWORKGROUP -- balance queue groups into work groups per MaxQueueChildren
7601 **
7602 **  Take the now defined queue groups and assign them to work groups.
7603 **  This is done to balance out the number of concurrently active
7604 **  queue runners such that MaxQueueChildren is not exceeded. This may
7605 **  result in more than one queue group per work group. In such a case
7606 **  the number of running queue groups in that work group will have no
7607 **  more than the work group maximum number of runners (a "fair" portion
7608 **  of MaxQueueRunners). All queue groups within a work group will get a
7609 **  chance at running.
7610 **
7611 **      Parameters:
7612 **              none.
7613 **
7614 **      Returns:
7615 **              nothing.
7616 **
7617 **      Side Effects:
7618 **              Sets up WorkGrp structure.
7619 */
7620 
7621 void
7622 makeworkgroups()
7623 {
7624         int i, j, total_runners, dir, h;
7625         SORTQGRP_T si[MAXQUEUEGROUPS + 1];
7626 
7627         total_runners = 0;
7628         if (NumQueue == 1 && strcmp(Queue[0]->qg_name, "mqueue") == 0)
7629         {
7630                 /*
7631                 **  There is only the "mqueue" queue group (a default)
7632                 **  containing all of the queues. We want to provide to
7633                 **  this queue group the maximum allowable queue runners.
7634                 **  To match older behavior (8.10/8.11) we'll try for
7635                 **  1 runner per queue capping it at MaxQueueChildren.
7636                 **  So if there are N queues, then there will be N runners
7637                 **  for the "mqueue" queue group (where N is kept less than
7638                 **  MaxQueueChildren).
7639                 */
7640 
7641                 NumWorkGroups = 1;
7642                 WorkGrp[0].wg_numqgrp = 1;
7643                 WorkGrp[0].wg_qgs = (QUEUEGRP **) xalloc(sizeof(QUEUEGRP *));
7644                 WorkGrp[0].wg_qgs[0] = Queue[0];
7645                 if (MaxQueueChildren > 0 &&
7646                     Queue[0]->qg_numqueues > MaxQueueChildren)
7647                         WorkGrp[0].wg_runners = MaxQueueChildren;
7648                 else
7649                         WorkGrp[0].wg_runners = Queue[0]->qg_numqueues;
7650 
7651                 Queue[0]->qg_wgrp = 0;
7652 
7653                 /* can't have more runners than allowed total */
7654                 if (MaxQueueChildren > 0 &&
7655                     Queue[0]->qg_maxqrun > MaxQueueChildren)
7656                         Queue[0]->qg_maxqrun = MaxQueueChildren;
7657                 WorkGrp[0].wg_maxact = Queue[0]->qg_maxqrun;
7658                 WorkGrp[0].wg_lowqintvl = Queue[0]->qg_queueintvl;
7659                 return;
7660         }
7661 
7662         for (i = 0; i < NumQueue; i++)
7663         {
7664                 si[i].sg_maxqrun = Queue[i]->qg_maxqrun;
7665                 si[i].sg_idx = i;
7666         }
7667         qsort(si, NumQueue, sizeof(si[0]), cmpidx);
7668 
7669         NumWorkGroups = 0;
7670         for (i = 0; i < NumQueue; i++)
7671         {
7672                 total_runners += si[i].sg_maxqrun;
7673                 if (MaxQueueChildren <= 0 || total_runners <= MaxQueueChildren)
7674                         NumWorkGroups++;
7675                 else
7676                         break;
7677         }
7678 
7679         if (NumWorkGroups < 1)
7680                 NumWorkGroups = 1; /* gotta have one at least */
7681         else if (NumWorkGroups > MAXWORKGROUPS)
7682                 NumWorkGroups = MAXWORKGROUPS; /* the limit */
7683 
7684         /*
7685         **  We now know the number of work groups to pack the queue groups
7686         **  into. The queue groups in 'Queue' are sorted from highest
7687         **  to lowest for the number of runners per queue group.
7688         **  We put the queue groups with the largest number of runners
7689         **  into work groups first. Then the smaller ones are fitted in
7690         **  where it looks best.
7691         */
7692 
7693         j = 0;
7694         dir = 1;
7695         for (i = 0; i < NumQueue; i++)
7696         {
7697                 /* a to-and-fro packing scheme, continue from last position */
7698                 if (j >= NumWorkGroups)
7699                 {
7700                         dir = -1;
7701                         j = NumWorkGroups - 1;
7702                 }
7703                 else if (j < 0)
7704                 {
7705                         j = 0;
7706                         dir = 1;
7707                 }
7708 
7709                 if (WorkGrp[j].wg_qgs == NULL)
7710                         WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_malloc(sizeof(QUEUEGRP *) *
7711                                                         (WorkGrp[j].wg_numqgrp + 1));
7712                 else
7713                         WorkGrp[j].wg_qgs = (QUEUEGRP **)sm_realloc(WorkGrp[j].wg_qgs,
7714                                                         sizeof(QUEUEGRP *) *
7715                                                         (WorkGrp[j].wg_numqgrp + 1));
7716                 if (WorkGrp[j].wg_qgs == NULL)
7717                 {
7718                         syserr("!cannot allocate memory for work queues, need %d bytes",
7719                                (int) (sizeof(QUEUEGRP *) *
7720                                       (WorkGrp[j].wg_numqgrp + 1)));
7721                 }
7722 
7723                 h = si[i].sg_idx;
7724                 WorkGrp[j].wg_qgs[WorkGrp[j].wg_numqgrp] = Queue[h];
7725                 WorkGrp[j].wg_numqgrp++;
7726                 WorkGrp[j].wg_runners += Queue[h]->qg_maxqrun;
7727                 Queue[h]->qg_wgrp = j;
7728 
7729                 if (WorkGrp[j].wg_maxact == 0)
7730                 {
7731                         /* can't have more runners than allowed total */
7732                         if (MaxQueueChildren > 0 &&
7733                             Queue[h]->qg_maxqrun > MaxQueueChildren)
7734                                 Queue[h]->qg_maxqrun = MaxQueueChildren;
7735                         WorkGrp[j].wg_maxact = Queue[h]->qg_maxqrun;
7736                 }
7737 
7738                 /*
7739                 **  XXX: must wg_lowqintvl be the GCD?
7740                 **  qg1: 2m, qg2: 3m, minimum: 2m, when do queue runs for
7741                 **  qg2 occur?
7742                 */
7743 
7744                 /* keep track of the lowest interval for a persistent runner */
7745                 if (Queue[h]->qg_queueintvl > 0 &&
7746                     WorkGrp[j].wg_lowqintvl < Queue[h]->qg_queueintvl)
7747                         WorkGrp[j].wg_lowqintvl = Queue[h]->qg_queueintvl;
7748                 j += dir;
7749         }
7750         if (tTd(41, 9))
7751         {
7752                 for (i = 0; i < NumWorkGroups; i++)
7753                 {
7754                         sm_dprintf("Workgroup[%d]=", i);
7755                         for (j = 0; j < WorkGrp[i].wg_numqgrp; j++)
7756                         {
7757                                 sm_dprintf("%s, ",
7758                                         WorkGrp[i].wg_qgs[j]->qg_name);
7759                         }
7760                         sm_dprintf("\n");
7761                 }
7762         }
7763 }
7764 
7765 /*
7766 **  DUP_DF -- duplicate envelope data file
7767 **
7768 **      Copy the data file from the 'old' envelope to the 'new' envelope
7769 **      in the most efficient way possible.
7770 **
7771 **      Create a hard link from the 'old' data file to the 'new' data file.
7772 **      If the old and new queue directories are on different file systems,
7773 **      then the new data file link is created in the old queue directory,
7774 **      and the new queue file will contain a 'd' record pointing to the
7775 **      directory containing the new data file.
7776 **
7777 **      Parameters:
7778 **              old -- old envelope.
7779 **              new -- new envelope.
7780 **
7781 **      Results:
7782 **              Returns true on success, false on failure.
7783 **
7784 **      Side Effects:
7785 **              On success, the new data file is created.
7786 **              On fatal failure, EF_FATALERRS is set in old->e_flags.
7787 */
7788 
7789 static bool     dup_df __P((ENVELOPE *, ENVELOPE *));
7790 
7791 static bool
7792 dup_df(old, new)
7793         ENVELOPE *old;
7794         ENVELOPE *new;
7795 {
7796         int ofs, nfs, r;
7797         char opath[MAXPATHLEN];
7798         char npath[MAXPATHLEN];
7799 
7800         if (!bitset(EF_HAS_DF, old->e_flags))
7801         {
7802                 /*
7803                 **  this can happen if: SuperSafe != True
7804                 **  and a bounce mail is sent that is split.
7805                 */
7806 
7807                 queueup(old, false, true);
7808         }
7809         SM_REQUIRE(ISVALIDQGRP(old->e_qgrp) && ISVALIDQDIR(old->e_qdir));
7810         SM_REQUIRE(ISVALIDQGRP(new->e_qgrp) && ISVALIDQDIR(new->e_qdir));
7811 
7812         (void) sm_strlcpy(opath, queuename(old, DATAFL_LETTER), sizeof(opath));
7813         (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
7814 
7815         if (old->e_dfp != NULL)
7816         {
7817                 r = sm_io_setinfo(old->e_dfp, SM_BF_COMMIT, NULL);
7818                 if (r < 0 && errno != EINVAL)
7819                 {
7820                         syserr("@can't commit %s", opath);
7821                         old->e_flags |= EF_FATALERRS;
7822                         return false;
7823                 }
7824         }
7825 
7826         /*
7827         **  Attempt to create a hard link, if we think both old and new
7828         **  are on the same file system, otherwise copy the file.
7829         **
7830         **  Don't waste time attempting a hard link unless old and new
7831         **  are on the same file system.
7832         */
7833 
7834         SM_REQUIRE(ISVALIDQGRP(old->e_dfqgrp) && ISVALIDQDIR(old->e_dfqdir));
7835         SM_REQUIRE(ISVALIDQGRP(new->e_dfqgrp) && ISVALIDQDIR(new->e_dfqdir));
7836 
7837         ofs = Queue[old->e_dfqgrp]->qg_qpaths[old->e_dfqdir].qp_fsysidx;
7838         nfs = Queue[new->e_dfqgrp]->qg_qpaths[new->e_dfqdir].qp_fsysidx;
7839         if (FILE_SYS_DEV(ofs) == FILE_SYS_DEV(nfs))
7840         {
7841                 if (link(opath, npath) == 0)
7842                 {
7843                         new->e_flags |= EF_HAS_DF;
7844                         SYNC_DIR(npath, true);
7845                         return true;
7846                 }
7847                 goto error;
7848         }
7849 
7850         /*
7851         **  Can't link across queue directories, so try to create a hard
7852         **  link in the same queue directory as the old df file.
7853         **  The qf file will refer to the new df file using a 'd' record.
7854         */
7855 
7856         new->e_dfqgrp = old->e_dfqgrp;
7857         new->e_dfqdir = old->e_dfqdir;
7858         (void) sm_strlcpy(npath, queuename(new, DATAFL_LETTER), sizeof(npath));
7859         if (link(opath, npath) == 0)
7860         {
7861                 new->e_flags |= EF_HAS_DF;
7862                 SYNC_DIR(npath, true);
7863                 return true;
7864         }
7865 
7866   error:
7867         if (LogLevel > 0)
7868                 sm_syslog(LOG_ERR, old->e_id,
7869                           "dup_df: can't link %s to %s, error=%s, envelope splitting failed",
7870                           opath, npath, sm_errstring(errno));
7871         return false;
7872 }
7873 
7874 /*
7875 **  SPLIT_ENV -- Allocate a new envelope based on a given envelope.
7876 **
7877 **      Parameters:
7878 **              e -- envelope.
7879 **              sendqueue -- sendqueue for new envelope.
7880 **              qgrp -- index of queue group.
7881 **              qdir -- queue directory.
7882 **
7883 **      Results:
7884 **              new envelope.
7885 **
7886 */
7887 
7888 static ENVELOPE *split_env __P((ENVELOPE *, ADDRESS *, int, int));
7889 
7890 static ENVELOPE *
7891 split_env(e, sendqueue, qgrp, qdir)
7892         ENVELOPE *e;
7893         ADDRESS *sendqueue;
7894         int qgrp;
7895         int qdir;
7896 {
7897         ENVELOPE *ee;
7898 
7899         ee = (ENVELOPE *) sm_rpool_malloc_x(e->e_rpool, sizeof(*ee));
7900         STRUCTCOPY(*e, *ee);
7901         ee->e_message = NULL;        /* XXX use original message? */
7902         ee->e_id = NULL;
7903         assign_queueid(ee);
7904         ee->e_sendqueue = sendqueue;
7905         ee->e_flags &= ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS
7906                          |EF_SENDRECEIPT|EF_RET_PARAM|EF_HAS_DF);
7907         ee->e_flags |= EF_NORECEIPT; /* XXX really? */
7908         ee->e_from.q_state = QS_SENDER;
7909         ee->e_dfp = NULL;
7910         ee->e_lockfp = NULL;
7911         if (e->e_xfp != NULL)
7912                 ee->e_xfp = sm_io_dup(e->e_xfp);
7913 
7914         /* failed to dup e->e_xfp, start a new transcript */
7915         if (ee->e_xfp == NULL)
7916                 openxscript(ee);
7917 
7918         ee->e_qgrp = ee->e_dfqgrp = qgrp;
7919         ee->e_qdir = ee->e_dfqdir = qdir;
7920         ee->e_errormode = EM_MAIL;
7921         ee->e_statmsg = NULL;
7922         if (e->e_quarmsg != NULL)
7923                 ee->e_quarmsg = sm_rpool_strdup_x(ee->e_rpool,
7924                                                   e->e_quarmsg);
7925 
7926         /*
7927         **  XXX Not sure if this copying is necessary.
7928         **  sendall() does this copying, but I (dm) don't know if that is
7929         **  because of the storage management discipline we were using
7930         **  before rpools were introduced, or if it is because these lists
7931         **  can be modified later.
7932         */
7933 
7934         ee->e_header = copyheader(e->e_header, ee->e_rpool);
7935         ee->e_errorqueue = copyqueue(e->e_errorqueue, ee->e_rpool);
7936 
7937         return ee;
7938 }
7939 
7940 /* return values from split functions, check also below! */
7941 #define SM_SPLIT_FAIL   (0)
7942 #define SM_SPLIT_NONE   (1)
7943 #define SM_SPLIT_NEW(n) (1 + (n))
7944 
7945 /*
7946 **  SPLIT_ACROSS_QUEUE_GROUPS
7947 **
7948 **      This function splits an envelope across multiple queue groups
7949 **      based on the queue group of each recipient.
7950 **
7951 **      Parameters:
7952 **              e -- envelope.
7953 **
7954 **      Results:
7955 **              SM_SPLIT_FAIL on failure
7956 **              SM_SPLIT_NONE if no splitting occurred,
7957 **              or 1 + the number of additional envelopes created.
7958 **
7959 **      Side Effects:
7960 **              On success, e->e_sibling points to a list of zero or more
7961 **              additional envelopes, and the associated data files exist
7962 **              on disk.  But the queue files are not created.
7963 **
7964 **              On failure, e->e_sibling is not changed.
7965 **              The order of recipients in e->e_sendqueue is permuted.
7966 **              Abandoned data files for additional envelopes that failed
7967 **              to be created may exist on disk.
7968 */
7969 
7970 static int      q_qgrp_compare __P((const void *, const void *));
7971 static int      e_filesys_compare __P((const void *, const void *));
7972 
7973 static int
7974 q_qgrp_compare(p1, p2)
7975         const void *p1;
7976         const void *p2;
7977 {
7978         ADDRESS **pq1 = (ADDRESS **) p1;
7979         ADDRESS **pq2 = (ADDRESS **) p2;
7980 
7981         return (*pq1)->q_qgrp - (*pq2)->q_qgrp;
7982 }
7983 
7984 static int
7985 e_filesys_compare(p1, p2)
7986         const void *p1;
7987         const void *p2;
7988 {
7989         ENVELOPE **pe1 = (ENVELOPE **) p1;
7990         ENVELOPE **pe2 = (ENVELOPE **) p2;
7991         int fs1, fs2;
7992 
7993         fs1 = Queue[(*pe1)->e_qgrp]->qg_qpaths[(*pe1)->e_qdir].qp_fsysidx;
7994         fs2 = Queue[(*pe2)->e_qgrp]->qg_qpaths[(*pe2)->e_qdir].qp_fsysidx;
7995         if (FILE_SYS_DEV(fs1) < FILE_SYS_DEV(fs2))
7996                 return -1;
7997         if (FILE_SYS_DEV(fs1) > FILE_SYS_DEV(fs2))
7998                 return 1;
7999         return 0;
8000 }
8001 
8002 static int split_across_queue_groups __P((ENVELOPE *));
8003 static int
8004 split_across_queue_groups(e)
8005         ENVELOPE *e;
8006 {
8007         int naddrs, nsplits, i;
8008         bool changed;
8009         char **pvp;
8010         ADDRESS *q, **addrs;
8011         ENVELOPE *ee, *es;
8012         ENVELOPE *splits[MAXQUEUEGROUPS];
8013         char pvpbuf[PSBUFSIZE];
8014 
8015         SM_REQUIRE(ISVALIDQGRP(e->e_qgrp));
8016 
8017         /* Count addresses and assign queue groups. */
8018         naddrs = 0;
8019         changed = false;
8020         for (q = e->e_sendqueue; q != NULL; q = q->q_next)
8021         {
8022                 if (QS_IS_DEAD(q->q_state))
8023                         continue;
8024                 ++naddrs;
8025 
8026                 /* bad addresses and those already sent stay put */
8027                 if (QS_IS_BADADDR(q->q_state) ||
8028                     QS_IS_SENT(q->q_state))
8029                         q->q_qgrp = e->e_qgrp;
8030                 else if (!ISVALIDQGRP(q->q_qgrp))
8031                 {
8032                         /* call ruleset which should return a queue group */
8033                         i = rscap(RS_QUEUEGROUP, q->q_user, NULL, e, &pvp,
8034                                   pvpbuf, sizeof(pvpbuf));
8035                         if (i == EX_OK &&
8036                             pvp != NULL && pvp[0] != NULL &&
8037                             (pvp[0][0] & 0377) == CANONNET &&
8038                             pvp[1] != NULL && pvp[1][0] != '\0')
8039                         {
8040                                 i = name2qid(pvp[1]);
8041                                 if (ISVALIDQGRP(i))
8042                                 {
8043                                         q->q_qgrp = i;
8044                                         changed = true;
8045                                         if (tTd(20, 4))
8046                                                 sm_syslog(LOG_INFO, NOQID,
8047                                                         "queue group name %s -> %d",
8048                                                         pvp[1], i);
8049                                         continue;
8050                                 }
8051                                 else if (LogLevel > 10)
8052                                         sm_syslog(LOG_INFO, NOQID,
8053                                                 "can't find queue group name %s, selection ignored",
8054                                                 pvp[1]);
8055                         }
8056                         if (q->q_mailer != NULL &&
8057                             ISVALIDQGRP(q->q_mailer->m_qgrp))
8058                         {
8059                                 changed = true;
8060                                 q->q_qgrp = q->q_mailer->m_qgrp;
8061                         }
8062                         else if (ISVALIDQGRP(e->e_qgrp))
8063                                 q->q_qgrp = e->e_qgrp;
8064                         else
8065                                 q->q_qgrp = 0;
8066                 }
8067         }
8068 
8069         /* only one address? nothing to split. */
8070         if (naddrs <= 1 && !changed)
8071                 return SM_SPLIT_NONE;
8072 
8073         /* sort the addresses by queue group */
8074         addrs = sm_rpool_malloc_x(e->e_rpool, naddrs * sizeof(ADDRESS *));
8075         for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
8076         {
8077                 if (QS_IS_DEAD(q->q_state))
8078                         continue;
8079                 addrs[i++] = q;
8080         }
8081         qsort(addrs, naddrs, sizeof(ADDRESS *), q_qgrp_compare);
8082 
8083         /* split into multiple envelopes, by queue group */
8084         nsplits = 0;
8085         es = NULL;
8086         e->e_sendqueue = NULL;
8087         for (i = 0; i < naddrs; ++i)
8088         {
8089                 if (i == naddrs - 1 || addrs[i]->q_qgrp != addrs[i + 1]->q_qgrp)
8090                         addrs[i]->q_next = NULL;
8091                 else
8092                         addrs[i]->q_next = addrs[i + 1];
8093 
8094                 /* same queue group as original envelope? */
8095                 if (addrs[i]->q_qgrp == e->e_qgrp)
8096                 {
8097                         if (e->e_sendqueue == NULL)
8098                                 e->e_sendqueue = addrs[i];
8099                         continue;
8100                 }
8101 
8102                 /* different queue group than original envelope */
8103                 if (es == NULL || addrs[i]->q_qgrp != es->e_qgrp)
8104                 {
8105                         ee = split_env(e, addrs[i], addrs[i]->q_qgrp, NOQDIR);
8106                         es = ee;
8107                         splits[nsplits++] = ee;
8108                 }
8109         }
8110 
8111         /* no splits? return right now. */
8112         if (nsplits <= 0)
8113                 return SM_SPLIT_NONE;
8114 
8115         /* assign a queue directory to each additional envelope */
8116         for (i = 0; i < nsplits; ++i)
8117         {
8118                 es = splits[i];
8119 #if 0
8120                 es->e_qdir = pickqdir(Queue[es->e_qgrp], es->e_msgsize, es);
8121 #endif /* 0 */
8122                 if (!setnewqueue(es))
8123                         goto failure;
8124         }
8125 
8126         /* sort the additional envelopes by queue file system */
8127         qsort(splits, nsplits, sizeof(ENVELOPE *), e_filesys_compare);
8128 
8129         /* create data files for each additional envelope */
8130         if (!dup_df(e, splits[0]))
8131         {
8132                 i = 0;
8133                 goto failure;
8134         }
8135         for (i = 1; i < nsplits; ++i)
8136         {
8137                 /* copy or link to the previous data file */
8138                 if (!dup_df(splits[i - 1], splits[i]))
8139                         goto failure;
8140         }
8141 
8142         /* success: prepend the new envelopes to the e->e_sibling list */
8143         for (i = 0; i < nsplits; ++i)
8144         {
8145                 es = splits[i];
8146                 es->e_sibling = e->e_sibling;
8147                 e->e_sibling = es;
8148         }
8149         return SM_SPLIT_NEW(nsplits);
8150 
8151         /* failure: clean up */
8152   failure:
8153         if (i > 0)
8154         {
8155                 int j;
8156 
8157                 for (j = 0; j < i; j++)
8158                         (void) unlink(queuename(splits[j], DATAFL_LETTER));
8159         }
8160         e->e_sendqueue = addrs[0];
8161         for (i = 0; i < naddrs - 1; ++i)
8162                 addrs[i]->q_next = addrs[i + 1];
8163         addrs[naddrs - 1]->q_next = NULL;
8164         return SM_SPLIT_FAIL;
8165 }
8166 
8167 /*
8168 **  SPLIT_WITHIN_QUEUE
8169 **
8170 **      Split an envelope with multiple recipients into several
8171 **      envelopes within the same queue directory, if the number of
8172 **      recipients exceeds the limit for the queue group.
8173 **
8174 **      Parameters:
8175 **              e -- envelope.
8176 **
8177 **      Results:
8178 **              SM_SPLIT_FAIL on failure
8179 **              SM_SPLIT_NONE if no splitting occurred,
8180 **              or 1 + the number of additional envelopes created.
8181 */
8182 
8183 #define SPLIT_LOG_LEVEL 8
8184 
8185 static int      split_within_queue __P((ENVELOPE *));
8186 
8187 static int
8188 split_within_queue(e)
8189         ENVELOPE *e;
8190 {
8191         int maxrcpt, nrcpt, ndead, nsplit, i;
8192         int j, l;
8193         char *lsplits;
8194         ADDRESS *q, **addrs;
8195         ENVELOPE *ee, *firstsibling;
8196 
8197         if (!ISVALIDQGRP(e->e_qgrp) || bitset(EF_SPLIT, e->e_flags))
8198                 return SM_SPLIT_NONE;
8199 
8200         /* don't bother if there is no recipient limit */
8201         maxrcpt = Queue[e->e_qgrp]->qg_maxrcpt;
8202         if (maxrcpt <= 0)
8203                 return SM_SPLIT_NONE;
8204 
8205         /* count recipients */
8206         nrcpt = 0;
8207         for (q = e->e_sendqueue; q != NULL; q = q->q_next)
8208         {
8209                 if (QS_IS_DEAD(q->q_state))
8210                         continue;
8211                 ++nrcpt;
8212         }
8213         if (nrcpt <= maxrcpt)
8214                 return SM_SPLIT_NONE;
8215 
8216         /*
8217         **  Preserve the recipient list
8218         **  so that we can restore it in case of error.
8219         **  (But we discard dead addresses.)
8220         */
8221 
8222         addrs = sm_rpool_malloc_x(e->e_rpool, nrcpt * sizeof(ADDRESS *));
8223         for (i = 0, q = e->e_sendqueue; q != NULL; q = q->q_next)
8224         {
8225                 if (QS_IS_DEAD(q->q_state))
8226                         continue;
8227                 addrs[i++] = q;
8228         }
8229 
8230         /*
8231         **  Partition the recipient list so that bad and sent addresses
8232         **  come first. These will go with the original envelope, and
8233         **  do not count towards the maxrcpt limit.
8234         **  addrs[] does not contain QS_IS_DEAD() addresses.
8235         */
8236 
8237         ndead = 0;
8238         for (i = 0; i < nrcpt; ++i)
8239         {
8240                 if (QS_IS_BADADDR(addrs[i]->q_state) ||
8241                     QS_IS_SENT(addrs[i]->q_state) ||
8242                     QS_IS_DEAD(addrs[i]->q_state)) /* for paranoia's sake */
8243                 {
8244                         if (i > ndead)
8245                         {
8246                                 ADDRESS *tmp = addrs[i];
8247 
8248                                 addrs[i] = addrs[ndead];
8249                                 addrs[ndead] = tmp;
8250                         }
8251                         ++ndead;
8252                 }
8253         }
8254 
8255         /* Check if no splitting required. */
8256         if (nrcpt - ndead <= maxrcpt)
8257                 return SM_SPLIT_NONE;
8258 
8259         /* fix links */
8260         for (i = 0; i < nrcpt - 1; ++i)
8261                 addrs[i]->q_next = addrs[i + 1];
8262         addrs[nrcpt - 1]->q_next = NULL;
8263         e->e_sendqueue = addrs[0];
8264 
8265         /* prepare buffer for logging */
8266         if (LogLevel > SPLIT_LOG_LEVEL)
8267         {
8268                 l = MAXLINE;
8269                 lsplits = sm_malloc(l);
8270                 if (lsplits != NULL)
8271                         *lsplits = '\0';
8272                 j = 0;
8273         }
8274         else
8275         {
8276                 /* get rid of stupid compiler warnings */
8277                 lsplits = NULL;
8278                 j = l = 0;
8279         }
8280 
8281         /* split the envelope */
8282         firstsibling = e->e_sibling;
8283         i = maxrcpt + ndead;
8284         nsplit = 0;
8285         for (;;)
8286         {
8287                 addrs[i - 1]->q_next = NULL;
8288                 ee = split_env(e, addrs[i], e->e_qgrp, e->e_qdir);
8289                 if (!dup_df(e, ee))
8290                 {
8291 
8292                         ee = firstsibling;
8293                         while (ee != NULL)
8294                         {
8295                                 (void) unlink(queuename(ee, DATAFL_LETTER));
8296                                 ee = ee->e_sibling;
8297                         }
8298 
8299                         /* Error.  Restore e's sibling & recipient lists. */
8300                         e->e_sibling = firstsibling;
8301                         for (i = 0; i < nrcpt - 1; ++i)
8302                                 addrs[i]->q_next = addrs[i + 1];
8303                         if (lsplits != NULL)
8304                                 sm_free(lsplits);
8305                         return SM_SPLIT_FAIL;
8306                 }
8307 
8308                 /* prepend the new envelope to e->e_sibling */
8309                 ee->e_sibling = e->e_sibling;
8310                 e->e_sibling = ee;
8311                 ++nsplit;
8312                 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8313                 {
8314                         if (j >= l - strlen(ee->e_id) - 3)
8315                         {
8316                                 char *p;
8317 
8318                                 l += MAXLINE;
8319                                 p = sm_realloc(lsplits, l);
8320                                 if (p == NULL)
8321                                 {
8322                                         /* let's try to get this done */
8323                                         sm_free(lsplits);
8324                                         lsplits = NULL;
8325                                 }
8326                                 else
8327                                         lsplits = p;
8328                         }
8329                         if (lsplits != NULL)
8330                         {
8331                                 if (j == 0)
8332                                         j += sm_strlcat(lsplits + j,
8333                                                         ee->e_id,
8334                                                         l - j);
8335                                 else
8336                                         j += sm_strlcat2(lsplits + j,
8337                                                          "; ",
8338                                                          ee->e_id,
8339                                                          l - j);
8340                                 SM_ASSERT(j < l);
8341                         }
8342                 }
8343                 if (nrcpt - i <= maxrcpt)
8344                         break;
8345                 i += maxrcpt;
8346         }
8347         if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8348         {
8349                 if (nsplit > 0)
8350                 {
8351                         sm_syslog(LOG_NOTICE, e->e_id,
8352                                   "split: maxrcpts=%d, rcpts=%d, count=%d, id%s=%s",
8353                                   maxrcpt, nrcpt - ndead, nsplit,
8354                                   nsplit > 1 ? "s" : "", lsplits);
8355                 }
8356                 sm_free(lsplits);
8357         }
8358         return SM_SPLIT_NEW(nsplit);
8359 }
8360 /*
8361 **  SPLIT_BY_RECIPIENT
8362 **
8363 **      Split an envelope with multiple recipients into multiple
8364 **      envelopes as required by the sendmail configuration.
8365 **
8366 **      Parameters:
8367 **              e -- envelope.
8368 **
8369 **      Results:
8370 **              Returns true on success, false on failure.
8371 **
8372 **      Side Effects:
8373 **              see split_across_queue_groups(), split_within_queue(e)
8374 */
8375 
8376 bool
8377 split_by_recipient(e)
8378         ENVELOPE *e;
8379 {
8380         int split, n, i, j, l;
8381         char *lsplits;
8382         ENVELOPE *ee, *next, *firstsibling;
8383 
8384         if (OpMode == SM_VERIFY || !ISVALIDQGRP(e->e_qgrp) ||
8385             bitset(EF_SPLIT, e->e_flags))
8386                 return true;
8387         n = split_across_queue_groups(e);
8388         if (n == SM_SPLIT_FAIL)
8389                 return false;
8390         firstsibling = ee = e->e_sibling;
8391         if (n > 1 && LogLevel > SPLIT_LOG_LEVEL)
8392         {
8393                 l = MAXLINE;
8394                 lsplits = sm_malloc(l);
8395                 if (lsplits != NULL)
8396                         *lsplits = '\0';
8397                 j = 0;
8398         }
8399         else
8400         {
8401                 /* get rid of stupid compiler warnings */
8402                 lsplits = NULL;
8403                 j = l = 0;
8404         }
8405         for (i = 1; i < n; ++i)
8406         {
8407                 next = ee->e_sibling;
8408                 if (split_within_queue(ee) == SM_SPLIT_FAIL)
8409                 {
8410                         e->e_sibling = firstsibling;
8411                         return false;
8412                 }
8413                 ee->e_flags |= EF_SPLIT;
8414                 if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL)
8415                 {
8416                         if (j >= l - strlen(ee->e_id) - 3)
8417                         {
8418                                 char *p;
8419 
8420                                 l += MAXLINE;
8421                                 p = sm_realloc(lsplits, l);
8422                                 if (p == NULL)
8423                                 {
8424                                         /* let's try to get this done */
8425                                         sm_free(lsplits);
8426                                         lsplits = NULL;
8427                                 }
8428                                 else
8429                                         lsplits = p;
8430                         }
8431                         if (lsplits != NULL)
8432                         {
8433                                 if (j == 0)
8434                                         j += sm_strlcat(lsplits + j,
8435                                                         ee->e_id, l - j);
8436                                 else
8437                                         j += sm_strlcat2(lsplits + j, "; ",
8438                                                          ee->e_id, l - j);
8439                                 SM_ASSERT(j < l);
8440                         }
8441                 }
8442                 ee = next;
8443         }
8444         if (LogLevel > SPLIT_LOG_LEVEL && lsplits != NULL && n > 1)
8445         {
8446                 sm_syslog(LOG_NOTICE, e->e_id, "split: count=%d, id%s=%s",
8447                           n - 1, n > 2 ? "s" : "", lsplits);
8448                 sm_free(lsplits);
8449         }
8450         split = split_within_queue(e) != SM_SPLIT_FAIL;
8451         if (split)
8452                 e->e_flags |= EF_SPLIT;
8453         return split;
8454 }
8455 
8456 /*
8457 **  QUARANTINE_QUEUE_ITEM -- {un,}quarantine a single envelope
8458 **
8459 **      Add/remove quarantine reason and requeue appropriately.
8460 **
8461 **      Parameters:
8462 **              qgrp -- queue group for the item
8463 **              qdir -- queue directory in the given queue group
8464 **              e -- envelope information for the item
8465 **              reason -- quarantine reason, NULL means unquarantine.
8466 **
8467 **      Results:
8468 **              true if item changed, false otherwise
8469 **
8470 **      Side Effects:
8471 **              Changes quarantine tag in queue file and renames it.
8472 */
8473 
8474 static bool
8475 quarantine_queue_item(qgrp, qdir, e, reason)
8476         int qgrp;
8477         int qdir;
8478         ENVELOPE *e;
8479         char *reason;
8480 {
8481         bool dirty = false;
8482         bool failing = false;
8483         bool foundq = false;
8484         bool finished = false;
8485         int fd;
8486         int flags;
8487         int oldtype;
8488         int newtype;
8489         int save_errno;
8490         MODE_T oldumask = 0;
8491         SM_FILE_T *oldqfp, *tempqfp;
8492         char *bp;
8493         int bufsize;
8494         char oldqf[MAXPATHLEN];
8495         char tempqf[MAXPATHLEN];
8496         char newqf[MAXPATHLEN];
8497         char buf[MAXLINE];
8498 
8499         oldtype = queue_letter(e, ANYQFL_LETTER);
8500         (void) sm_strlcpy(oldqf, queuename(e, ANYQFL_LETTER), sizeof(oldqf));
8501         (void) sm_strlcpy(tempqf, queuename(e, NEWQFL_LETTER), sizeof(tempqf));
8502 
8503         /*
8504         **  Instead of duplicating all the open
8505         **  and lock code here, tell readqf() to
8506         **  do that work and return the open
8507         **  file pointer in e_lockfp.  Note that
8508         **  we must release the locks properly when
8509         **  we are done.
8510         */
8511 
8512         if (!readqf(e, true))
8513         {
8514                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8515                                      "Skipping %s\n", qid_printname(e));
8516                 return false;
8517         }
8518         oldqfp = e->e_lockfp;
8519 
8520         /* open the new queue file */
8521         flags = O_CREAT|O_WRONLY|O_EXCL;
8522         if (bitset(S_IWGRP, QueueFileMode))
8523                 oldumask = umask(002);
8524         fd = open(tempqf, flags, QueueFileMode);
8525         if (bitset(S_IWGRP, QueueFileMode))
8526                 (void) umask(oldumask);
8527         RELEASE_QUEUE;
8528 
8529         if (fd < 0)
8530         {
8531                 save_errno = errno;
8532                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8533                                      "Skipping %s: Could not open %s: %s\n",
8534                                      qid_printname(e), tempqf,
8535                                      sm_errstring(save_errno));
8536                 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8537                 return false;
8538         }
8539         if (!lockfile(fd, tempqf, NULL, LOCK_EX|LOCK_NB))
8540         {
8541                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8542                                      "Skipping %s: Could not lock %s\n",
8543                                      qid_printname(e), tempqf);
8544                 (void) close(fd);
8545                 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8546                 return false;
8547         }
8548 
8549         tempqfp = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT, (void *) &fd,
8550                              SM_IO_WRONLY_B, NULL);
8551         if (tempqfp == NULL)
8552         {
8553                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8554                                      "Skipping %s: Could not lock %s\n",
8555                                      qid_printname(e), tempqf);
8556                 (void) close(fd);
8557                 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8558                 return false;
8559         }
8560 
8561         /* Copy the data over, changing the quarantine reason */
8562         while (bufsize = sizeof(buf),
8563                (bp = fgetfolded(buf, &bufsize, oldqfp)) != NULL)
8564         {
8565                 if (tTd(40, 4))
8566                         sm_dprintf("+++++ %s\n", bp);
8567                 switch (bp[0])
8568                 {
8569                   case 'q':             /* quarantine reason */
8570                         foundq = true;
8571                         if (reason == NULL)
8572                         {
8573                                 if (Verbose)
8574                                 {
8575                                         (void) sm_io_fprintf(smioout,
8576                                                              SM_TIME_DEFAULT,
8577                                                              "%s: Removed quarantine of \"%s\"\n",
8578                                                              e->e_id, &bp[1]);
8579                                 }
8580                                 sm_syslog(LOG_INFO, e->e_id, "unquarantine");
8581                                 dirty = true;
8582                         }
8583                         else if (strcmp(reason, &bp[1]) == 0)
8584                         {
8585                                 if (Verbose)
8586                                 {
8587                                         (void) sm_io_fprintf(smioout,
8588                                                              SM_TIME_DEFAULT,
8589                                                              "%s: Already quarantined with \"%s\"\n",
8590                                                              e->e_id, reason);
8591                                 }
8592                                 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8593                                                      "q%s\n", reason);
8594                         }
8595                         else
8596                         {
8597                                 if (Verbose)
8598                                 {
8599                                         (void) sm_io_fprintf(smioout,
8600                                                              SM_TIME_DEFAULT,
8601                                                              "%s: Quarantine changed from \"%s\" to \"%s\"\n",
8602                                                              e->e_id, &bp[1],
8603                                                              reason);
8604                                 }
8605                                 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8606                                                      "q%s\n", reason);
8607                                 sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8608                                           reason);
8609                                 dirty = true;
8610                         }
8611                         break;
8612 
8613                   case 'S':
8614                         /*
8615                         **  If we are quarantining an unquarantined item,
8616                         **  need to put in a new 'q' line before it's
8617                         **  too late.
8618                         */
8619 
8620                         if (!foundq && reason != NULL)
8621                         {
8622                                 if (Verbose)
8623                                 {
8624                                         (void) sm_io_fprintf(smioout,
8625                                                              SM_TIME_DEFAULT,
8626                                                              "%s: Quarantined with \"%s\"\n",
8627                                                              e->e_id, reason);
8628                                 }
8629                                 (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8630                                                      "q%s\n", reason);
8631                                 sm_syslog(LOG_INFO, e->e_id, "quarantine=%s",
8632                                           reason);
8633                                 foundq = true;
8634                                 dirty = true;
8635                         }
8636 
8637                         /* Copy the line to the new file */
8638                         (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8639                                              "%s\n", bp);
8640                         break;
8641 
8642                   case '.':
8643                         finished = true;
8644                         /* FALLTHROUGH */
8645 
8646                   default:
8647                         /* Copy the line to the new file */
8648                         (void) sm_io_fprintf(tempqfp, SM_TIME_DEFAULT,
8649                                              "%s\n", bp);
8650                         break;
8651                 }
8652                 if (bp != buf)
8653                         sm_free(bp);
8654         }
8655 
8656         /* Make sure we read the whole old file */
8657         errno = sm_io_error(tempqfp);
8658         if (errno != 0 && errno != SM_IO_EOF)
8659         {
8660                 save_errno = errno;
8661                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8662                                      "Skipping %s: Error reading %s: %s\n",
8663                                      qid_printname(e), oldqf,
8664                                      sm_errstring(save_errno));
8665                 failing = true;
8666         }
8667 
8668         if (!failing && !finished)
8669         {
8670                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8671                                      "Skipping %s: Incomplete file: %s\n",
8672                                      qid_printname(e), oldqf);
8673                 failing = true;
8674         }
8675 
8676         /* Check if we actually changed anything or we can just bail now */
8677         if (!dirty)
8678         {
8679                 /* pretend we failed, even though we technically didn't */
8680                 failing = true;
8681         }
8682 
8683         /* Make sure we wrote things out safely */
8684         if (!failing &&
8685             (sm_io_flush(tempqfp, SM_TIME_DEFAULT) != 0 ||
8686              ((SuperSafe == SAFE_REALLY ||
8687                SuperSafe == SAFE_REALLY_POSTMILTER ||
8688                SuperSafe == SAFE_INTERACTIVE) &&
8689               fsync(sm_io_getinfo(tempqfp, SM_IO_WHAT_FD, NULL)) < 0) ||
8690              ((errno = sm_io_error(tempqfp)) != 0)))
8691         {
8692                 save_errno = errno;
8693                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8694                                      "Skipping %s: Error writing %s: %s\n",
8695                                      qid_printname(e), tempqf,
8696                                      sm_errstring(save_errno));
8697                 failing = true;
8698         }
8699 
8700 
8701         /* Figure out the new filename */
8702         newtype = (reason == NULL ? NORMQF_LETTER : QUARQF_LETTER);
8703         if (oldtype == newtype)
8704         {
8705                 /* going to rename tempqf to oldqf */
8706                 (void) sm_strlcpy(newqf, oldqf, sizeof(newqf));
8707         }
8708         else
8709         {
8710                 /* going to rename tempqf to new name based on newtype */
8711                 (void) sm_strlcpy(newqf, queuename(e, newtype), sizeof(newqf));
8712         }
8713 
8714         save_errno = 0;
8715 
8716         /* rename tempqf to newqf */
8717         if (!failing &&
8718             rename(tempqf, newqf) < 0)
8719                 save_errno = (errno == 0) ? EINVAL : errno;
8720 
8721         /* Check rename() success */
8722         if (!failing && save_errno != 0)
8723         {
8724                 sm_syslog(LOG_DEBUG, e->e_id,
8725                           "quarantine_queue_item: rename(%s, %s): %s",
8726                           tempqf, newqf, sm_errstring(save_errno));
8727 
8728                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8729                                      "Error renaming %s to %s: %s\n",
8730                                      tempqf, newqf,
8731                                      sm_errstring(save_errno));
8732                 if (oldtype == newtype)
8733                 {
8734                         /*
8735                         **  Bail here since we don't know the state of
8736                         **  the filesystem and may need to keep tempqf
8737                         **  for the user to rescue us.
8738                         */
8739 
8740                         RELEASE_QUEUE;
8741                         errno = save_errno;
8742                         syserr("!452 Error renaming control file %s", tempqf);
8743                         /* NOTREACHED */
8744                 }
8745                 else
8746                 {
8747                         /* remove new file (if rename() half completed) */
8748                         if (xunlink(newqf) < 0)
8749                         {
8750                                 save_errno = errno;
8751                                 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8752                                                      "Error removing %s: %s\n",
8753                                                      newqf,
8754                                                      sm_errstring(save_errno));
8755                         }
8756 
8757                         /* tempqf removed below */
8758                         failing = true;
8759                 }
8760 
8761         }
8762 
8763         /* If changing file types, need to remove old type */
8764         if (!failing && oldtype != newtype)
8765         {
8766                 if (xunlink(oldqf) < 0)
8767                 {
8768                         save_errno = errno;
8769                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8770                                              "Error removing %s: %s\n",
8771                                              oldqf, sm_errstring(save_errno));
8772                 }
8773         }
8774 
8775         /* see if anything above failed */
8776         if (failing)
8777         {
8778                 /* Something failed: remove new file, old file still there */
8779                 (void) xunlink(tempqf);
8780         }
8781 
8782         /*
8783         **  fsync() after file operations to make sure metadata is
8784         **  written to disk on filesystems in which renames are
8785         **  not guaranteed.  It's ok if they fail, mail won't be lost.
8786         */
8787 
8788         if (SuperSafe != SAFE_NO)
8789         {
8790                 /* for soft-updates */
8791                 (void) fsync(sm_io_getinfo(tempqfp,
8792                                            SM_IO_WHAT_FD, NULL));
8793 
8794                 if (!failing)
8795                 {
8796                         /* for soft-updates */
8797                         (void) fsync(sm_io_getinfo(oldqfp,
8798                                                    SM_IO_WHAT_FD, NULL));
8799                 }
8800 
8801                 /* for other odd filesystems */
8802                 SYNC_DIR(tempqf, false);
8803         }
8804 
8805         /* Close up shop */
8806         RELEASE_QUEUE;
8807         if (tempqfp != NULL)
8808                 (void) sm_io_close(tempqfp, SM_TIME_DEFAULT);
8809         if (oldqfp != NULL)
8810                 (void) sm_io_close(oldqfp, SM_TIME_DEFAULT);
8811 
8812         /* All went well */
8813         return !failing;
8814 }
8815 
8816 /*
8817 **  QUARANTINE_QUEUE -- {un,}quarantine matching items in the queue
8818 **
8819 **      Read all matching queue items, add/remove quarantine
8820 **      reason, and requeue appropriately.
8821 **
8822 **      Parameters:
8823 **              reason -- quarantine reason, "." means unquarantine.
8824 **              qgrplimit -- limit to single queue group unless NOQGRP
8825 **
8826 **      Results:
8827 **              none.
8828 **
8829 **      Side Effects:
8830 **              Lots of changes to the queue.
8831 */
8832 
8833 void
8834 quarantine_queue(reason, qgrplimit)
8835         char *reason;
8836         int qgrplimit;
8837 {
8838         int changed = 0;
8839         int qgrp;
8840 
8841         /* Convert internal representation of unquarantine */
8842         if (reason != NULL && reason[0] == '.' && reason[1] == '\0')
8843                 reason = NULL;
8844 
8845         if (reason != NULL)
8846         {
8847                 /* clean it */
8848                 reason = newstr(denlstring(reason, true, true));
8849         }
8850 
8851         for (qgrp = 0; qgrp < NumQueue && Queue[qgrp] != NULL; qgrp++)
8852         {
8853                 int qdir;
8854 
8855                 if (qgrplimit != NOQGRP && qgrplimit != qgrp)
8856                         continue;
8857 
8858                 for (qdir = 0; qdir < Queue[qgrp]->qg_numqueues; qdir++)
8859                 {
8860                         int i;
8861                         int nrequests;
8862 
8863                         if (StopRequest)
8864                                 stop_sendmail();
8865 
8866                         nrequests = gatherq(qgrp, qdir, true, NULL, NULL, NULL);
8867 
8868                         /* first see if there is anything */
8869                         if (nrequests <= 0)
8870                         {
8871                                 if (Verbose)
8872                                 {
8873                                         (void) sm_io_fprintf(smioout,
8874                                                              SM_TIME_DEFAULT, "%s: no matches\n",
8875                                                              qid_printqueue(qgrp, qdir));
8876                                 }
8877                                 continue;
8878                         }
8879 
8880                         if (Verbose)
8881                         {
8882                                 (void) sm_io_fprintf(smioout,
8883                                                      SM_TIME_DEFAULT, "Processing %s:\n",
8884                                                      qid_printqueue(qgrp, qdir));
8885                         }
8886 
8887                         for (i = 0; i < WorkListCount; i++)
8888                         {
8889                                 ENVELOPE e;
8890 
8891                                 if (StopRequest)
8892                                         stop_sendmail();
8893 
8894                                 /* setup envelope */
8895                                 clearenvelope(&e, true, sm_rpool_new_x(NULL));
8896                                 e.e_id = WorkList[i].w_name + 2;
8897                                 e.e_qgrp = qgrp;
8898                                 e.e_qdir = qdir;
8899 
8900                                 if (tTd(70, 101))
8901                                 {
8902                                         sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8903                                                       "Would do %s\n", e.e_id);
8904                                         changed++;
8905                                 }
8906                                 else if (quarantine_queue_item(qgrp, qdir,
8907                                                                &e, reason))
8908                                         changed++;
8909 
8910                                 /* clean up */
8911                                 sm_rpool_free(e.e_rpool);
8912                                 e.e_rpool = NULL;
8913                         }
8914                         if (WorkList != NULL)
8915                                 sm_free(WorkList); /* XXX */
8916                         WorkList = NULL;
8917                         WorkListSize = 0;
8918                         WorkListCount = 0;
8919                 }
8920         }
8921         if (Verbose)
8922         {
8923                 if (changed == 0)
8924                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8925                                              "No changes\n");
8926                 else
8927                         (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
8928                                              "%d change%s\n",
8929                                              changed,
8930                                              changed == 1 ? "" : "s");
8931         }
8932 }