1 /* Copyright 1988,1990,1993 by Paul Vixie
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static char rcsid
[] = "$Id: misc.c,v 1.1 1994/01/05 20:40:15 jtc Exp $";
22 /* vix 26jan87 [RCS has the rest of the log]
23 * vix 30dec86 [written]
30 # include <sys/time.h>
44 #if defined(LOG_DAEMON) && !defined(LOG_CRON)
45 #define LOG_CRON LOG_DAEMON
50 strcmp_until(left
, right
, until
)
57 while (*left
&& *left
!= until
&& *left
== *right
) {
62 if ((*left
=='\0' || *left
== until
) &&
63 (*right
=='\0' || *right
== until
)) {
66 diff
= *left
- *right
;
73 /* strdtb(s) - delete trailing blanks in string 's' and return new length
81 /* scan forward to the null
86 /* scan backward to either the first character before the string,
87 * or the last non-blank in the string, whichever comes first.
90 while (x
>= s
&& isspace(*x
));
92 /* one character beyond where we stopped above is where the null
97 /* the difference between the position of the null character and
98 * the position of the first character of the string is the length.
105 set_debug_flags(flags
)
108 /* debug flags are of the form flag[,flag ...]
110 * if an error occurs, print a message to stdout and return FALSE.
111 * otherwise return TRUE after setting ERROR_FLAGS.
116 printf("this program was compiled without debugging enabled\n");
119 #else /* DEBUGGING */
129 /* try to find debug flag name in our list.
131 for ( test
= DebugFlagNames
, mask
= 1;
132 *test
&& strcmp_until(*test
, pc
, ',');
139 "unrecognized debug flag <%s> <%s>\n",
146 /* skip to the next flag
148 while (*pc
&& *pc
!= ',')
157 fprintf(stderr
, "debug flags enabled:");
159 for (flag
= 0; DebugFlagNames
[flag
]; flag
++)
160 if (DebugFlags
& (1 << flag
))
161 fprintf(stderr
, " %s", DebugFlagNames
[flag
]);
162 fprintf(stderr
, "\n");
167 #endif /* DEBUGGING */
174 #if defined(BSD) || defined(POSIX)
175 if (seteuid(ROOT_UID
) < OK
) {
180 if (setuid(ROOT_UID
) < OK
) {
193 /* first check for CRONDIR ("/var/cron" or some such)
195 if (stat(CRONDIR
, &sb
) < OK
&& errno
== ENOENT
) {
197 if (OK
== mkdir(CRONDIR
, 0700)) {
198 fprintf(stderr
, "%s: created\n", CRONDIR
);
201 fprintf(stderr
, "%s: ", CRONDIR
);
206 if (!(sb
.st_mode
& S_IFDIR
)) {
207 fprintf(stderr
, "'%s' is not a directory, bailing out.\n",
211 if (chdir(CRONDIR
) < OK
) {
212 fprintf(stderr
, "cannot chdir(%s), bailing out.\n", CRONDIR
);
217 /* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such)
219 if (stat(SPOOL_DIR
, &sb
) < OK
&& errno
== ENOENT
) {
221 if (OK
== mkdir(SPOOL_DIR
, 0700)) {
222 fprintf(stderr
, "%s: created\n", SPOOL_DIR
);
223 stat(SPOOL_DIR
, &sb
);
225 fprintf(stderr
, "%s: ", SPOOL_DIR
);
230 if (!(sb
.st_mode
& S_IFDIR
)) {
231 fprintf(stderr
, "'%s' is not a directory, bailing out.\n",
238 /* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
239 * another daemon is already running, which we detect here.
241 * note: main() calls us twice; once before forking, once after.
242 * we maintain static storage of the file pointer so that we
243 * can rewrite our PID into the PIDFILE after the fork.
245 * it would be great if fflush() disassociated the file buffer.
248 acquire_daemonlock(closeflag
)
251 static FILE *fp
= NULL
;
253 if (closeflag
&& fp
) {
260 char pidfile
[MAX_FNAME
];
261 char buf
[MAX_TEMPSTR
];
264 (void) sprintf(pidfile
, PIDFILE
, PIDDIR
);
265 if ((-1 == (fd
= open(pidfile
, O_RDWR
|O_CREAT
, 0644)))
266 || (NULL
== (fp
= fdopen(fd
, "r+")))
268 sprintf(buf
, "can't open or create %s: %s",
269 pidfile
, strerror(errno
));
270 fprintf(stderr
, "%s: %s\n", ProgramName
, buf
);
271 log_it("CRON", getpid(), "DEATH", buf
);
275 if (flock(fd
, LOCK_EX
|LOCK_NB
) < OK
) {
276 int save_errno
= errno
;
278 fscanf(fp
, "%d", &otherpid
);
279 sprintf(buf
, "can't lock %s, otherpid may be %d: %s",
280 pidfile
, otherpid
, strerror(save_errno
));
281 fprintf(stderr
, "%s: %s\n", ProgramName
, buf
);
282 log_it("CRON", getpid(), "DEATH", buf
);
286 (void) fcntl(fd
, F_SETFD
, 1);
290 fprintf(fp
, "%d\n", getpid());
292 (void) ftruncate(fileno(fp
), ftell(fp
));
294 /* abandon fd and fp even though the file is open. we need to
295 * keep it open and locked, but we don't need the handles elsewhere.
299 /* get_char(file) : like getc() but increment LineNumber on newlines
309 Set_LineNum(LineNumber
+ 1)
314 /* unget_char(ch, file) : like ungetc but do LineNumber processing
323 Set_LineNum(LineNumber
- 1)
327 /* get_string(str, max, file, termstr) : like fgets() but
328 * (1) has terminator string which should include \n
329 * (2) will always leave room for the null
330 * (3) uses get_char() so LineNumber will be accurate
331 * (4) returns EOF or terminating character, whichever
334 get_string(string
, size
, file
, terms
)
342 while (EOF
!= (ch
= get_char(file
)) && !strchr(terms
, ch
)) {
344 *string
++ = (char) ch
;
356 /* skip_comments(file) : read past comment (if any)
364 while (EOF
!= (ch
= get_char(file
))) {
365 /* ch is now the first character of a line.
368 while (ch
== ' ' || ch
== '\t')
374 /* ch is now the first non-blank character of a line.
377 if (ch
!= '\n' && ch
!= '#')
380 /* ch must be a newline or comment as first non-blank
381 * character on a line.
384 while (ch
!= '\n' && ch
!= EOF
)
387 /* ch is now the newline of a line which we're going to
392 unget_char(ch
, file
);
396 /* int in_file(char *string, FILE *file)
397 * return TRUE if one of the lines in file matches string exactly,
401 in_file(string
, file
)
405 char line
[MAX_TEMPSTR
];
408 while (fgets(line
, MAX_TEMPSTR
, file
)) {
410 line
[strlen(line
)-1] = '\0';
411 if (0 == strcmp(line
, string
))
418 /* int allowed(char *username)
419 * returns TRUE if (ALLOW_FILE exists and user is listed)
420 * or (DENY_FILE exists and user is NOT listed)
421 * or (neither file exists but user=="root" so it's okay)
427 static int init
= FALSE
;
428 static FILE *allow
, *deny
;
432 #if defined(ALLOW_FILE) && defined(DENY_FILE)
433 allow
= fopen(ALLOW_FILE
, "r");
434 deny
= fopen(DENY_FILE
, "r");
435 Debug(DMISC
, ("allow/deny enabled, %d/%d\n", !!allow
, !!deny
))
443 return (in_file(username
, allow
));
445 return (!in_file(username
, deny
));
447 #if defined(ALLOW_ONLY_ROOT)
448 return (strcmp(username
, ROOT_USER
) == 0);
456 log_it(username
, pid
, event
, detail
)
462 #if defined(LOG_FILE)
464 TIME_T now
= time((TIME_T
) 0);
465 register struct tm
*t
= localtime(&now
);
466 static int log_fd
= -1;
470 static int syslog_open
= 0;
473 #if defined(LOG_FILE)
474 /* we assume that MAX_TEMPSTR will hold the date, time, &punctuation.
476 msg
= malloc(strlen(username
)
482 log_fd
= open(LOG_FILE
, O_WRONLY
|O_APPEND
|O_CREAT
, 0600);
484 fprintf(stderr
, "%s: can't open log file\n",
488 (void) fcntl(log_fd
, F_SETFD
, 1);
492 /* we have to sprintf() it because fprintf() doesn't always write
493 * everything out in one chunk and this has to be atomically appended
496 sprintf(msg
, "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)\n",
498 t
->tm_mon
+1, t
->tm_mday
, t
->tm_hour
, t
->tm_min
, t
->tm_sec
, pid
,
501 /* we have to run strlen() because sprintf() returns (char*) on old BSD
503 if (log_fd
< OK
|| write(log_fd
, msg
, strlen(msg
)) < OK
) {
504 fprintf(stderr
, "%s: can't write to log file\n", ProgramName
);
507 write(STDERR
, msg
, strlen(msg
));
515 /* we don't use LOG_PID since the pid passed to us by
516 * our client may not be our own. therefore we want to
517 * print the pid ourselves.
520 openlog(ProgramName
, LOG_PID
, LOG_CRON
);
522 openlog(ProgramName
, LOG_PID
);
524 syslog_open
= TRUE
; /* assume openlog success */
527 syslog(LOG_INFO
, "(%s) %s (%s)\n", username
, event
, detail
);
533 fprintf(stderr
, "log_it: (%s %d) %s (%s)\n",
534 username
, pid
, event
, detail
);
541 * (1) this routine is fairly slow
542 * (2) it returns a pointer to static storage
546 register char *s
; /* string we want the first word of */
547 register char *t
; /* terminators, implicitly including \0 */
549 static char retbuf
[2][MAX_TEMPSTR
+ 1]; /* sure wish C had GC */
550 static int retsel
= 0;
551 register char *rb
, *rp
;
553 /* select a return buffer */
555 rb
= &retbuf
[retsel
][0];
558 /* skip any leading terminators */
559 while (*s
&& (NULL
!= strchr(t
, *s
))) {
563 /* copy until next terminator or full buffer */
564 while (*s
&& (NULL
== strchr(t
, *s
)) && (rp
< &rb
[MAX_TEMPSTR
])) {
568 /* finish the return-string and return it */
575 * heavily ascii-dependent.
578 mkprint(dst
, src
, len
)
580 register unsigned char *src
;
585 register unsigned char ch
= *src
++;
587 if (ch
< ' ') { /* control character */
590 } else if (ch
< 0177) { /* printable */
592 } else if (ch
== 0177) { /* delete/rubout */
595 } else { /* parity character */
596 sprintf(dst
, "\\%03o", ch
);
605 * returns a pointer to malloc'd storage, you must call free yourself.
609 register unsigned char *src
;
610 register unsigned int len
;
612 register char *dst
= malloc(len
*4 + 1);
614 mkprint(dst
, src
, len
);
621 /* Sat, 27 Feb 93 11:44:51 CST
622 * 123456789012345678901234567
628 time_t t
= clock
?*clock
:time(0L);
629 struct tm
*tm
= localtime(&t
);
630 static char ret
[30]; /* zone name might be >3 chars */
632 (void) sprintf(ret
, "%s, %2d %s %2d %02d:%02d:%02d %s",
633 DowNames
[tm
->tm_wday
],
635 MonthNames
[tm
->tm_mon
],
646 #ifdef HAVE_SAVED_SUIDS
647 static int save_euid
;
648 int swap_uids() { save_euid
= geteuid(); return seteuid(getuid()); }
649 int swap_uids_back() { return seteuid(save_euid
); }
650 #else /*HAVE_SAVED_UIDS*/
651 int swap_uids() { return setreuid(geteuid(), getuid()); }
652 int swap_uids_back() { return swap_uids(); }
653 #endif /*HAVE_SAVED_UIDS*/