root/src/misc.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. trylock_file
  2. glue_strings
  3. strcmp_until
  4. strdtb
  5. set_debug_flags
  6. set_cron_uid
  7. check_spool_dir
  8. acquire_daemonlock
  9. get_char
  10. unget_char
  11. get_string
  12. skip_comments
  13. in_file
  14. allowed
  15. log_it
  16. log_close
  17. first_word
  18. mkprint
  19. mkprints
  20. arpadate
  21. swap_uids
  22. swap_uids_back
  23. swap_uids
  24. swap_uids_back
  25. strlens
  26. get_gmtoff

   1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
   2  * All rights reserved
   3  */
   4 
   5 /*
   6  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
   7  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
   8  *
   9  * Permission to use, copy, modify, and distribute this software for any
  10  * purpose with or without fee is hereby granted, provided that the above
  11  * copyright notice and this permission notice appear in all copies.
  12  *
  13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
  16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20  */
  21 
  22 /* vix 26jan87 [RCS has the rest of the log]
  23  * vix 30dec86 [written]
  24  */
  25 
  26 #include "config.h"
  27 
  28 #include "globals.h"
  29 
  30 #include <ctype.h>
  31 #include <errno.h>
  32 #include <signal.h>
  33 #include <stdarg.h>
  34 #include <stdio.h>
  35 #include <stdlib.h>
  36 #include <string.h>
  37 #include <sys/stat.h>
  38 
  39 #if defined(SYSLOG)
  40 # include <syslog.h>
  41 #endif
  42 
  43 #ifdef WITH_AUDIT
  44 # include <libaudit.h>
  45 #endif
  46 
  47 #ifdef HAVE_FCNTL_H     /* fcntl(2) */
  48 # include <fcntl.h>
  49 #endif
  50 #ifdef HAVE_UNISTD_H    /* lockf(3) */
  51 # include <unistd.h>
  52 #endif
  53 #ifdef HAVE_FLOCK       /* flock(2) */
  54 # include <sys/file.h>
  55 #endif
  56 
  57 #include "funcs.h"
  58 #include "macros.h"
  59 #include "pathnames.h"
  60 
  61 #if defined(SYSLOG) && defined(LOG_FILE)
  62 # undef LOG_FILE
  63 #endif
  64 
  65 #if defined(LOG_DAEMON) && !defined(LOG_CRON)
  66 # define LOG_CRON LOG_DAEMON
  67 #endif
  68 
  69 #ifndef FACILITY
  70 # define FACILITY LOG_CRON
  71 #endif
  72 
  73 static int LogFD = ERR;
  74 
  75 #if defined(SYSLOG)
  76 static int syslog_open = FALSE;
  77 #endif
  78 
  79 #if defined(HAVE_FLOCK)
  80 # define trylock_file(fd)      flock((fd), LOCK_EX|LOCK_NB)
  81 #elif defined(HAVE_FCNTL) && defined(F_SETLK)
  82 static int trylock_file(int fd) {
     /* [previous][next][first][last][top][bottom][index][help]  */
  83         struct flock fl;
  84 
  85         memset(&fl, '\0', sizeof (fl));
  86         fl.l_type = F_WRLCK;
  87         fl.l_whence = SEEK_SET;
  88         fl.l_start = 0;
  89         fl.l_len = 0;
  90 
  91         return fcntl(fd, F_SETLK, &fl);
  92 }
  93 #elif defined(HAVE_LOCKF)
  94 # define trylock_file(fd)      lockf((fd), F_TLOCK, 0)
  95 #endif
  96 
  97 /*
  98  * glue_strings is the overflow-safe equivalent of
  99  *              sprintf(buffer, "%s%c%s", a, separator, b);
 100  *
 101  * returns 1 on success, 0 on failure.  'buffer' MUST NOT be used if
 102  * glue_strings fails.
 103  */
 104 int
 105 glue_strings(char *buffer, size_t buffer_size, const char *a, const char *b,
     /* [previous][next][first][last][top][bottom][index][help]  */
 106         char separator) {
 107         char *buf;
 108         char *buf_end;
 109 
 110         if (buffer_size <= 0)
 111                 return (0);
 112         buf_end = buffer + buffer_size;
 113         buf = buffer;
 114 
 115         for ( /* nothing */ ; buf < buf_end && *a != '\0'; buf++, a++)
 116                 *buf = *a;
 117         if (buf == buf_end)
 118                 return (0);
 119         if (separator != '/' || buf == buffer || buf[-1] != '/')
 120                 *buf++ = separator;
 121         if (buf == buf_end)
 122                 return (0);
 123         for ( /* nothing */ ; buf < buf_end && *b != '\0'; buf++, b++)
 124                 *buf = *b;
 125         if (buf == buf_end)
 126                 return (0);
 127         *buf = '\0';
 128         return (1);
 129 }
 130 
 131 int strcmp_until(const char *left, const char *right, char until) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 132         while (*left && *left != until && *left == *right) {
 133                 left++;
 134                 right++;
 135         }
 136 
 137         if ((*left == '\0' || *left == until) && (*right == '\0' ||
 138                         *right == until)) {
 139                 return (0);
 140         }
 141         return (*left - *right);
 142 }
 143 
 144 /* strdtb(s) - delete trailing blanks in string 's' and return new length
 145  */
 146 size_t strdtb(char *s) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 147         char *x = s;
 148 
 149         /* scan forward to the null
 150          */
 151         while (*x)
 152                 x++;
 153 
 154         /* scan backward to either the first character before the string,
 155          * or the last non-blank in the string, whichever comes first.
 156          */
 157         do {
 158                 x--;
 159         } while (x >= s && isspace((unsigned char) *x));
 160 
 161         /* one character beyond where we stopped above is where the null
 162          * goes.
 163          */
 164         *++x = '\0';
 165 
 166         /* the difference between the position of the null character and
 167          * the position of the first character of the string is the length.
 168          */
 169         return ((size_t)(x - s));
 170 }
 171 
 172 int set_debug_flags(const char *flags) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 173         /* debug flags are of the form    flag[,flag ...]
 174          *
 175          * if an error occurs, print a message to stdout and return FALSE.
 176          * otherwise return TRUE after setting ERROR_FLAGS.
 177          */
 178 
 179 #if !DEBUGGING
 180 
 181         printf("this program was compiled without debugging enabled\n");
 182         return (FALSE);
 183 
 184 #else /* DEBUGGING */
 185 
 186         const char *pc = flags;
 187 
 188         DebugFlags = 0;
 189 
 190         while (*pc) {
 191                 const char **test;
 192                 int mask;
 193 
 194                 /* try to find debug flag name in our list.
 195                  */
 196                 for (test = DebugFlagNames, mask = 1;
 197                         *test != NULL && strcmp_until(*test, pc, ','); test++, mask <<= 1) ;
 198 
 199                 if (!*test) {
 200                         fprintf(stderr, "unrecognized debug flag <%s> <%s>\n", flags, pc);
 201                         return (FALSE);
 202                 }
 203 
 204                 DebugFlags |= mask;
 205 
 206                 /* skip to the next flag
 207                  */
 208                 while (*pc && *pc != ',')
 209                         pc++;
 210                 if (*pc == ',')
 211                         pc++;
 212         }
 213 
 214         if (DebugFlags) {
 215                 int flag;
 216 
 217                 fprintf(stderr, "debug flags enabled:");
 218 
 219                 for (flag = 0; DebugFlagNames[flag]; flag++)
 220                         if (DebugFlags & (1 << flag))
 221                                 fprintf(stderr, " %s", DebugFlagNames[flag]);
 222                 fprintf(stderr, "\n");
 223         }
 224 
 225         return (TRUE);
 226 
 227 #endif /* DEBUGGING */
 228 }
 229 
 230 void set_cron_uid(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 231 #if defined(BSD) || defined(POSIX)
 232         if (seteuid(ROOT_UID) < OK) {
 233                 perror("seteuid");
 234                 exit(ERROR_EXIT);
 235         }
 236 #else
 237         if (setuid(ROOT_UID) < OK) {
 238                 perror("setuid");
 239                 exit(ERROR_EXIT);
 240         }
 241 #endif
 242 }
 243 
 244 void check_spool_dir(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 245         struct stat sb;
 246 #ifdef CRON_GROUP
 247         struct group *grp = NULL;
 248 
 249         grp = getgrnam(CRON_GROUP);
 250 #endif
 251         /* check SPOOL_DIR existence
 252          */
 253         if (stat(SPOOL_DIR, &sb) < OK && errno == ENOENT) {
 254                 perror(SPOOL_DIR);
 255                 if (OK == mkdir(SPOOL_DIR, 0700)) {
 256                         fprintf(stderr, "%s: created\n", SPOOL_DIR);
 257                         if (stat(SPOOL_DIR, &sb) < OK) {
 258                                 perror("stat retry");
 259                                 exit(ERROR_EXIT);
 260                         }
 261                 }
 262                 else {
 263                         fprintf(stderr, "%s: ", SPOOL_DIR);
 264                         perror("mkdir");
 265                         exit(ERROR_EXIT);
 266                 }
 267         }
 268         if (!S_ISDIR(sb.st_mode)) {
 269                 fprintf(stderr, "'%s' is not a directory, bailing out.\n", SPOOL_DIR);
 270                 exit(ERROR_EXIT);
 271         }
 272 #ifdef CRON_GROUP
 273         if (grp != NULL) {
 274                 if (sb.st_gid != grp->gr_gid)
 275                         if (chown(SPOOL_DIR, -1, grp->gr_gid) == -1) {
 276                                 fprintf(stderr, "chown %s failed: %s\n", SPOOL_DIR,
 277                                         strerror(errno));
 278                                 exit(ERROR_EXIT);
 279                         }
 280                 if (sb.st_mode != 01730)
 281                         if (chmod(SPOOL_DIR, 01730) == -1) {
 282                                 fprintf(stderr, "chmod 01730 %s failed: %s\n", SPOOL_DIR,
 283                                         strerror(errno));
 284                                 exit(ERROR_EXIT);
 285                         }
 286         }
 287 #endif
 288 }
 289 
 290 /* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
 291  *      another daemon is already running, which we detect here.
 292  *
 293  * note: main() calls us twice; once before forking, once after.
 294  *      we maintain static storage of the file pointer so that we
 295  *      can rewrite our PID into _PATH_CRON_PID after the fork.
 296  */
 297 void acquire_daemonlock(int closeflag) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 298         static int fd = -1;
 299         char buf[3 * MAX_FNAME];
 300         const char *pidfile;
 301         char *ep;
 302         long otherpid = -1;
 303         ssize_t num, len;
 304         pid_t pid = getpid();
 305 
 306         if (closeflag) {
 307                 /* close stashed fd for child so we don't leak it. */
 308                 if (fd != -1) {
 309                         close(fd);
 310                         fd = -1;
 311                 }
 312                 /* and restore default sig handlers so we don't remove pid file if killed */
 313                 signal(SIGINT,SIG_DFL);
 314                 signal(SIGTERM,SIG_DFL);
 315                 return;
 316         }
 317 
 318         if (NoFork == 1)
 319                 return; //move along, nothing to do here...
 320 
 321         if (fd == -1) {
 322                 pidfile = _PATH_CRON_PID;
 323                 /* Initial mode is 0600 to prevent flock() race/DoS. */
 324                 if ((fd = open(pidfile, O_RDWR | O_CREAT, 0600)) == -1) {
 325                         int save_errno = errno;
 326                         sprintf(buf, "can't open or create %s", pidfile);
 327                         fprintf(stderr, "%s: %s: %s\n", ProgramName, buf,
 328                                 strerror(save_errno));
 329                         log_it("CRON", pid, "DEATH", buf, save_errno);
 330                         exit(ERROR_EXIT);
 331                 }
 332 
 333                 if (trylock_file(fd) < OK) {
 334                         int save_errno = errno;
 335 
 336                         memset(buf, 0, sizeof (buf));
 337                         if ((num = read(fd, buf, sizeof (buf) - 1)) > 0 &&
 338                                 (otherpid = strtol(buf, &ep, 10)) > 0 &&
 339                                 ep != buf && *ep == '\n' && otherpid != LONG_MAX) {
 340                                 snprintf(buf, sizeof (buf),
 341                                         "can't lock %s, otherpid may be %ld", pidfile, otherpid);
 342                         }
 343                         else {
 344                                 snprintf(buf, sizeof (buf),
 345                                         "can't lock %s, otherpid unknown", pidfile);
 346                         }
 347                         fprintf(stderr, "%s: %s: %s\n", ProgramName, buf,
 348                                 strerror(save_errno));
 349                         log_it("CRON", pid, "DEATH", buf, save_errno);
 350                         exit(ERROR_EXIT);
 351                 }
 352                 (void) fchmod(fd, 0644);
 353                 (void) fcntl(fd, F_SETFD, 1);
 354         }
 355 #if !defined(HAVE_FLOCK)
 356         else {
 357                 /* Racy but better than nothing, just hope the parent exits */
 358                 sleep(0);
 359                 trylock_file(fd);       
 360         }
 361 #endif
 362 
 363         sprintf(buf, "%ld\n", (long) pid);
 364         (void) lseek(fd, (off_t) 0, SEEK_SET);
 365         len = (ssize_t)strlen(buf);
 366         if ((num = write(fd, buf, (size_t)len)) != len)
 367                 log_it("CRON", pid, "ERROR", "write() failed", errno);
 368         else {
 369                 if (ftruncate(fd, num) == -1)
 370                         log_it("CRON", pid, "ERROR", "ftruncate() failed", errno);
 371         }
 372 
 373         /* abandon fd even though the file is open. we need to keep
 374          * it open and locked, but we don't need the handles elsewhere.
 375          */
 376 }
 377 
 378 /* get_char(file) : like getc() but increment LineNumber on newlines
 379  */
 380 int get_char(FILE * file) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 381         int ch;
 382 
 383         ch = getc(file);
 384         if (ch == '\n')
 385                 Set_LineNum(LineNumber + 1)
 386         return (ch);
 387 }
 388 
 389 /* unget_char(ch, file) : like ungetc but do LineNumber processing
 390  */
 391 void unget_char(int ch, FILE * file) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 392         ungetc(ch, file);
 393         if (ch == '\n')
 394                 Set_LineNum(LineNumber - 1)
 395 }
 396 
 397 /* get_string(str, max, file, termstr) : like fgets() but
 398  *      (1) has terminator string which should include \n
 399  *      (2) will always leave room for the null
 400  *      (3) uses get_char() so LineNumber will be accurate
 401  *      (4) returns EOF or terminating character, whichever
 402  */
 403 int get_string(char *string, int size, FILE * file, const char *terms) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 404         int ch;
 405 
 406         while (EOF != (ch = get_char(file)) && !strchr(terms, ch)) {
 407                 if (size > 1) {
 408                         *string++ = (char) ch;
 409                         size--;
 410                 }
 411         }
 412 
 413         if (size > 0)
 414                 *string = '\0';
 415 
 416         return (ch);
 417 }
 418 
 419 /* skip_comments(file) : read past comment (if any)
 420  */
 421 void skip_comments(FILE * file) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 422         int ch;
 423 
 424         while (EOF != (ch = get_char(file))) {
 425                 /* ch is now the first character of a line.
 426                  */
 427                 while (ch == ' ' || ch == '\t')
 428                         ch = get_char(file);
 429 
 430                 if (ch == EOF)
 431                         break;
 432 
 433                 /* ch is now the first non-blank character of a line.
 434                  */
 435 
 436                 if (ch != '\n' && ch != '#')
 437                         break;
 438 
 439                 /* ch must be a newline or comment as first non-blank
 440                  * character on a line.
 441                  */
 442 
 443                 while (ch != '\n' && ch != EOF)
 444                         ch = get_char(file);
 445 
 446                 /* ch is now the newline of a line which we're going to
 447                  * ignore.
 448                  */
 449         }
 450         if (ch != EOF)
 451                 unget_char(ch, file);
 452 }
 453 
 454 /* int in_file(const char *string, FILE *file, int error)
 455  *      return TRUE if one of the lines in file matches string exactly,
 456  *      FALSE if no lines match, and error on error.
 457  */
 458 static int in_file(const char *string, FILE * file, int error) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 459         char line[MAX_TEMPSTR];
 460         char *endp;
 461 
 462         if (fseek(file, 0L, SEEK_SET))
 463                 return (error);
 464         while (fgets(line, MAX_TEMPSTR, file)) {
 465                 if (line[0] != '\0') {
 466                         endp = &line[strlen(line) - 1];
 467                         if (*endp != '\n')
 468                                 return (error);
 469                         *endp = '\0';
 470                         if (0 == strcmp(line, string))
 471                                 return (TRUE);
 472                 }
 473         }
 474         if (ferror(file))
 475                 return (error);
 476         return (FALSE);
 477 }
 478 
 479 /* int allowed(const char *username, const char *allow_file, const char *deny_file)
 480  *      returns TRUE if (allow_file exists and user is listed)
 481  *      or (deny_file exists and user is NOT listed).
 482  *      root is always allowed.
 483  */
 484 int allowed(const char *username, const char *allow_file,
     /* [previous][next][first][last][top][bottom][index][help]  */
 485         const char *deny_file) {
 486         FILE *fp;
 487         int isallowed;
 488         char buf[128];
 489 
 490         if (getuid() == 0)
 491                 return TRUE;
 492         isallowed = FALSE;
 493         if ((fp = fopen(allow_file, "r")) != NULL) {
 494                 isallowed = in_file(username, fp, FALSE);
 495                 fclose(fp);
 496                 if ((getuid() == 0) && (!isallowed)) {
 497                         snprintf(buf, sizeof (buf),
 498                                 "root used -u for user %s not in cron.allow", username);
 499                         log_it("crontab", getpid(), "warning", buf, 0);
 500                         isallowed = TRUE;
 501                 }
 502         }
 503         else if ((fp = fopen(deny_file, "r")) != NULL) {
 504                 isallowed = !in_file(username, fp, FALSE);
 505                 fclose(fp);
 506                 if ((getuid() == 0) && (!isallowed)) {
 507                         snprintf(buf, sizeof (buf),
 508                                 "root used -u for user %s in cron.deny", username);
 509                         log_it("crontab", getpid(), "warning", buf, 0);
 510                         isallowed = TRUE;
 511                 }
 512         }
 513 #ifdef WITH_AUDIT
 514         if (isallowed == FALSE) {
 515                 int audit_fd = audit_open();
 516                 audit_log_user_message(audit_fd, AUDIT_USER_START, "cron deny",
 517                         NULL, NULL, NULL, 0);
 518                 close(audit_fd);
 519         }
 520 #endif
 521         return (isallowed);
 522 }
 523 
 524 void log_it(const char *username, PID_T xpid, const char *event,
     /* [previous][next][first][last][top][bottom][index][help]  */
 525         const char *detail, int err) {
 526 #if defined(LOG_FILE) || DEBUGGING
 527         PID_T pid = xpid;
 528 #endif
 529 #if defined(LOG_FILE)
 530         char *msg;
 531         TIME_T now = time((TIME_T) 0);
 532         struct tm *t = localtime(&now);
 533         int msg_size;
 534 #endif
 535 
 536 #if defined(LOG_FILE)
 537         /* we assume that MAX_TEMPSTR will hold the date, time, &punctuation.
 538          */
 539         msg = malloc(msg_size = (strlen(username)
 540                         + strlen(event)
 541                         + strlen(detail)
 542                         + MAX_TEMPSTR)
 543                 );
 544         if (msg == NULL) {      /* damn, out of mem and we did not test that before... */
 545                 fprintf(stderr, "%s: Run OUT OF MEMORY while %s\n",
 546                         ProgramName, __FUNCTION__);
 547                 return;
 548         }
 549         if (LogFD < OK) {
 550                 LogFD = open(LOG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0600);
 551                 if (LogFD < OK) {
 552                         fprintf(stderr, "%s: can't open log file\n", ProgramName);
 553                         perror(LOG_FILE);
 554                 }
 555                 else {
 556                         (void) fcntl(LogFD, F_SETFD, 1);
 557                 }
 558         }
 559 
 560         /* we have to snprintf() it because fprintf() doesn't always write
 561          * everything out in one chunk and this has to be atomically appended
 562          * to the log file.
 563          */
 564         snprintf(msg, msg_size,
 565                 "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)%s%s\n", username,
 566                 t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, pid,
 567                 event, detail, err != 0 ? ": " : "", err != 0 ? strerror(err) : "");
 568 
 569         /* we have to run strlen() because sprintf() returns (char*) on old BSD
 570          */
 571         if (LogFD < OK || write(LogFD, msg, strlen(msg)) < OK) {
 572                 if (LogFD >= OK)
 573                         perror(LOG_FILE);
 574                 fprintf(stderr, "%s: can't write to log file\n", ProgramName);
 575                 write(STDERR, msg, strlen(msg));
 576         }
 577 
 578         free(msg);
 579 #endif /*LOG_FILE */
 580 
 581 #if defined(SYSLOG)
 582         if (!syslog_open) {
 583 # ifdef LOG_DAEMON
 584                 openlog(ProgramName, LOG_PID, FACILITY);
 585 # else
 586                 openlog(ProgramName, LOG_PID);
 587 # endif
 588                 syslog_open = TRUE;     /* assume openlog success */
 589         }
 590 
 591         syslog(err != 0 ? LOG_ERR : LOG_INFO,
 592                 "(%s) %s (%s)%s%s", username, event, detail,
 593                 err != 0 ? ": " : "", err != 0 ? strerror(err) : "");
 594 
 595 
 596 #endif   /*SYSLOG*/
 597 #if DEBUGGING
 598         if (DebugFlags) {
 599                 fprintf(stderr, "log_it: (%s %ld) %s (%s)%s%s\n",
 600                         username, (long) pid, event, detail,
 601                         err != 0 ? ": " : "", err != 0 ? strerror(err) : "");
 602         }
 603 #endif
 604 }
 605 
 606 void log_close(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 607         if (LogFD != ERR) {
 608                 close(LogFD);
 609                 LogFD = ERR;
 610         }
 611 #if defined(SYSLOG)
 612         closelog();
 613         syslog_open = FALSE;
 614 #endif   /*SYSLOG*/
 615 }
 616 
 617 /* char *first_word(const char *s, const char *t)
 618  *      return pointer to first word
 619  * parameters:
 620  *      s - string we want the first word of
 621  *      t - terminators, implicitly including \0
 622  * warnings:
 623  *      (1) this routine is fairly slow
 624  *      (2) it returns a pointer to static storage
 625  */
 626 char *first_word(const char *s, const char *t) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 627         static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */
 628         static int retsel = 0;
 629         char *rb, *rp;
 630 
 631         /* select a return buffer */
 632         retsel = 1 - retsel;
 633         rb = &retbuf[retsel][0];
 634         rp = rb;
 635 
 636         /* skip any leading terminators */
 637         while (*s && (NULL != strchr(t, *s))) {
 638                 s++;
 639         }
 640 
 641         /* copy until next terminator or full buffer */
 642         while (*s && (NULL == strchr(t, *s)) && (rp < &rb[MAX_TEMPSTR])) {
 643                 *rp++ = *s++;
 644         }
 645 
 646         /* finish the return-string and return it */
 647         *rp = '\0';
 648         return (rb);
 649 }
 650 
 651 /* warning:
 652  *      heavily ascii-dependent.
 653  */
 654 static void mkprint(char *dst, unsigned char *src, size_t len) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 655 /*
 656  * XXX
 657  * We know this routine can't overflow the dst buffer because mkprints()
 658  * allocated enough space for the worst case.
 659 */
 660         while (len-- > 0) {
 661                 unsigned char ch = *src++;
 662 
 663                 if (ch < ' ') { /* control character */
 664                         *dst++ = '^';
 665                         *dst++ = (char)(ch + '@');
 666                 }
 667                 else if (ch < 0177) {   /* printable */
 668                         *dst++ = (char)ch;
 669                 }
 670                 else if (ch == 0177) {  /* delete/rubout */
 671                         *dst++ = '^';
 672                         *dst++ = '?';
 673                 }
 674                 else {  /* parity character */
 675                         sprintf(dst, "\\%03o", ch);
 676                         dst += 4;
 677                 }
 678         }
 679         *dst = '\0';
 680 }
 681 
 682 /* warning:
 683  *      returns a pointer to malloc'd storage, you must call free yourself.
 684  */
 685 char *mkprints(unsigned char *src, size_t len) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 686         char *dst = malloc(len * 4 + 1);
 687 
 688         if (dst)
 689                 mkprint(dst, src, len);
 690 
 691         return (dst);
 692 }
 693 
 694 #ifdef MAIL_DATE
 695 /* Sat, 27 Feb 1993 11:44:51 -0800 (CST)
 696  * 1234567890123456789012345678901234567
 697  */
 698 char *arpadate(time_t *clock) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 699         time_t t = clock ? *clock : time((TIME_T) 0);
 700         struct tm tm = *localtime(&t);
 701         long gmtoff = get_gmtoff(&t, &tm);
 702         int hours = gmtoff / SECONDS_PER_HOUR;
 703         int minutes =
 704                         (gmtoff - (hours * SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE;
 705         static char ret[64];    /* zone name might be >3 chars */
 706 
 707         (void) sprintf(ret, "%s, %2d %s %2d %02d:%02d:%02d %.2d%.2d (%s)",
 708                 DowNames[tm.tm_wday],
 709                 tm.tm_mday,
 710                 MonthNames[tm.tm_mon],
 711                 tm.tm_year + 1900,
 712                 tm.tm_hour, tm.tm_min, tm.tm_sec, hours, minutes, TZONE(tm));
 713         return (ret);
 714 }
 715 #endif /*MAIL_DATE */
 716 
 717 #ifdef HAVE_SAVED_UIDS
 718 static uid_t save_euid;
 719 static gid_t save_egid;
 720 
 721 int swap_uids(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 722         save_egid = getegid();
 723         save_euid = geteuid();
 724         return ((setegid(getgid()) || seteuid(getuid()))? -1 : 0);
 725 }
 726 
 727 int swap_uids_back(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 728         return ((setegid(save_egid) || seteuid(save_euid)) ? -1 : 0);
 729 }
 730 
 731 #else /*HAVE_SAVED_UIDS */
 732 
 733 int swap_uids(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 734         return ((setregid(getegid(), getgid())
 735                         || setreuid(geteuid(), getuid())) ? -1 : 0);
 736 }
 737 
 738 int swap_uids_back(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 739         return (swap_uids());
 740 }
 741 #endif /*HAVE_SAVED_UIDS */
 742 
 743 size_t strlens(const char *last, ...) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 744         va_list ap;
 745         size_t ret = 0;
 746         const char *str;
 747 
 748         va_start(ap, last);
 749         for (str = last; str != NULL; str = va_arg(ap, const char *))
 750                      ret += strlen(str);
 751         va_end(ap);
 752         return (ret);
 753 }
 754 
 755 /* Return the offset from GMT in seconds (algorithm taken from sendmail).
 756  *
 757  * warning:
 758  *      clobbers the static storage space used by localtime() and gmtime().
 759  *      If the local pointer is non-NULL it *must* point to a local copy.
 760  */
 761 #ifndef HAVE_STRUCT_TM_TM_GMTOFF
 762 long get_gmtoff(time_t * clock, struct tm *local) {
     /* [previous][next][first][last][top][bottom][index][help]  */
 763         struct tm gmt;
 764         long offset;
 765 
 766         gmt = *gmtime(clock);
 767         if (local == NULL)
 768                 local = localtime(clock);
 769 
 770         offset = (local->tm_sec - gmt.tm_sec) +
 771                 ((local->tm_min - gmt.tm_min) * 60) +
 772                 ((local->tm_hour - gmt.tm_hour) * 3600);
 773 
 774         /* Timezone may cause year rollover to happen on a different day. */
 775         if (local->tm_year < gmt.tm_year)
 776                 offset -= 24 * 3600;
 777         else if (local->tm_year > gmt.tm_year)
 778                 offset += 24 * 3600;
 779         else if (local->tm_yday < gmt.tm_yday)
 780                 offset -= 24 * 3600;
 781         else if (local->tm_yday > gmt.tm_yday)
 782                 offset += 24 * 3600;
 783 
 784         return (offset);
 785 }
 786 #endif /* HAVE_STRUCT_TM_TM_GMTOFF */

/* [previous][next][first][last][top][bottom][index][help]  */