/* Copyright 1988,1990,1993,1994 by Paul Vixie
* Distribute freely, except: don't remove my name from the source or
* documentation (don't take credit for my work), mark your changes (don't
* get me blamed for your possible bugs), don't alter or remove this
* notice. May be sold if buildable source is provided to buyer. No
* warrantee of any kind, express or implied, is included with this
* software; use at your own risk, responsibility for damages (if any) to
* anyone resulting from the use of this software rests entirely with the
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and
* I'll try to keep a version up to date. I can be reached as follows:
* Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
* From Id: misc.c,v 2.9 1994/01/15 20:43:43 vixie Exp
#if !defined(lint) && !defined(LINT)
static char rcsid
[] = "$Header: $";
/* vix 26jan87 [RCS has the rest of the log]
#if defined(LOG_DAEMON) && !defined(LOG_CRON)
#define LOG_CRON LOG_DAEMON
strcmp_until(left
, right
, until
)
while (*left
&& *left
!= until
&& *left
== *right
) {
if ((*left
=='\0' || *left
== until
) &&
(*right
=='\0' || *right
== until
)) {
/* strdtb(s) - delete trailing blanks in string 's' and return new length
/* scan forward to the null
/* scan backward to either the first character before the string,
* or the last non-blank in the string, whichever comes first.
while (x
>= s
&& isspace(*x
));
/* one character beyond where we stopped above is where the null
/* the difference between the position of the null character and
* the position of the first character of the string is the length.
/* debug flags are of the form flag[,flag ...]
* if an error occurs, print a message to stdout and return FALSE.
* otherwise return TRUE after setting ERROR_FLAGS.
printf("this program was compiled without debugging enabled\n");
/* try to find debug flag name in our list.
for ( test
= DebugFlagNames
, mask
= 1;
*test
&& strcmp_until(*test
, pc
, ',');
"unrecognized debug flag <%s> <%s>\n",
while (*pc
&& *pc
!= ',')
fprintf(stderr
, "debug flags enabled:");
for (flag
= 0; DebugFlagNames
[flag
]; flag
++)
if (DebugFlags
& (1 << flag
))
fprintf(stderr
, " %s", DebugFlagNames
[flag
]);
#if defined(BSD) || defined(POSIX)
if (seteuid(ROOT_UID
) < OK
) {
if (setuid(ROOT_UID
) < OK
) {
/* first check for CRONDIR ("/var/cron" or some such)
if (stat(CRONDIR
, &sb
) < OK
&& errno
== ENOENT
) {
if (OK
== mkdir(CRONDIR
, 0700)) {
fprintf(stderr
, "%s: created\n", CRONDIR
);
fprintf(stderr
, "%s: ", CRONDIR
);
if (!(sb
.st_mode
& S_IFDIR
)) {
fprintf(stderr
, "'%s' is not a directory, bailing out.\n",
if (chdir(CRONDIR
) < OK
) {
fprintf(stderr
, "cannot chdir(%s), bailing out.\n", CRONDIR
);
/* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such)
if (stat(SPOOL_DIR
, &sb
) < OK
&& errno
== ENOENT
) {
if (OK
== mkdir(SPOOL_DIR
, 0700)) {
fprintf(stderr
, "%s: created\n", SPOOL_DIR
);
fprintf(stderr
, "%s: ", SPOOL_DIR
);
if (!(sb
.st_mode
& S_IFDIR
)) {
fprintf(stderr
, "'%s' is not a directory, bailing out.\n",
/* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
* another daemon is already running, which we detect here.
* note: main() calls us twice; once before forking, once after.
* we maintain static storage of the file pointer so that we
* can rewrite our PID into the PIDFILE after the fork.
* it would be great if fflush() disassociated the file buffer.
acquire_daemonlock(closeflag
)
(void) sprintf(pidfile
, PIDFILE
, PIDDIR
);
if ((-1 == (fd
= open(pidfile
, O_RDWR
|O_CREAT
, 0644)))
|| (NULL
== (fp
= fdopen(fd
, "r+")))
sprintf(buf
, "can't open or create %s: %s",
pidfile
, strerror(errno
));
fprintf(stderr
, "%s: %s\n", ProgramName
, buf
);
log_it("CRON", getpid(), "DEATH", buf
);
if (flock(fd
, LOCK_EX
|LOCK_NB
) < OK
) {
fscanf(fp
, "%d", &otherpid
);
sprintf(buf
, "can't lock %s, otherpid may be %d: %s",
pidfile
, otherpid
, strerror(save_errno
));
fprintf(stderr
, "%s: %s\n", ProgramName
, buf
);
log_it("CRON", getpid(), "DEATH", buf
);
(void) fcntl(fd
, F_SETFD
, 1);
fprintf(fp
, "%d\n", getpid());
(void) ftruncate(fileno(fp
), ftell(fp
));
/* abandon fd and fp even though the file is open. we need to
* keep it open and locked, but we don't need the handles elsewhere.
/* get_char(file) : like getc() but increment LineNumber on newlines
Set_LineNum(LineNumber
+ 1)
/* unget_char(ch, file) : like ungetc but do LineNumber processing
Set_LineNum(LineNumber
- 1)
/* get_string(str, max, file, termstr) : like fgets() but
* (1) has terminator string which should include \n
* (2) will always leave room for the null
* (3) uses get_char() so LineNumber will be accurate
* (4) returns EOF or terminating character, whichever
get_string(string
, size
, file
, terms
)
while (EOF
!= (ch
= get_char(file
)) && !strchr(terms
, ch
)) {
/* skip_comments(file) : read past comment (if any)
while (EOF
!= (ch
= get_char(file
))) {
/* ch is now the first character of a line.
while (ch
== ' ' || ch
== '\t')
/* ch is now the first non-blank character of a line.
if (ch
!= '\n' && ch
!= '#')
/* ch must be a newline or comment as first non-blank
while (ch
!= '\n' && ch
!= EOF
)
/* ch is now the newline of a line which we're going to
/* int in_file(char *string, FILE *file)
* return TRUE if one of the lines in file matches string exactly,
while (fgets(line
, MAX_TEMPSTR
, file
)) {
line
[strlen(line
)-1] = '\0';
if (0 == strcmp(line
, string
))
/* int allowed(char *username)
* returns TRUE if (ALLOW_FILE exists and user is listed)
* or (DENY_FILE exists and user is NOT listed)
* or (neither file exists but user=="root" so it's okay)
static FILE *allow
, *deny
;
#if defined(ALLOW_FILE) && defined(DENY_FILE)
allow
= fopen(ALLOW_FILE
, "r");
deny
= fopen(DENY_FILE
, "r");
Debug(DMISC
, ("allow/deny enabled, %d/%d\n", !!allow
, !!deny
))
return (in_file(username
, allow
));
return (!in_file(username
, deny
));
#if defined(ALLOW_ONLY_ROOT)
return (strcmp(username
, ROOT_USER
) == 0);
log_it(username
, xpid
, event
, detail
)
TIME_T now
= time((TIME_T
) 0);
register struct tm
*t
= localtime(&now
);
static int syslog_open
= 0;
/* we assume that MAX_TEMPSTR will hold the date, time, &punctuation.
msg
= malloc(strlen(username
)
LogFD
= open(LOG_FILE
, O_WRONLY
|O_APPEND
|O_CREAT
, 0600);
fprintf(stderr
, "%s: can't open log file\n",
(void) fcntl(LogFD
, F_SETFD
, 1);
/* we have to sprintf() it because fprintf() doesn't always write
* everything out in one chunk and this has to be atomically appended
sprintf(msg
, "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)\n",
t
->tm_mon
+1, t
->tm_mday
, t
->tm_hour
, t
->tm_min
, t
->tm_sec
, pid
,
/* we have to run strlen() because sprintf() returns (char*) on old BSD
if (LogFD
< OK
|| write(LogFD
, msg
, strlen(msg
)) < OK
) {
fprintf(stderr
, "%s: can't write to log file\n", ProgramName
);
write(STDERR
, msg
, strlen(msg
));
/* we don't use LOG_PID since the pid passed to us by
* our client may not be our own. therefore we want to
* print the pid ourselves.
openlog(ProgramName
, LOG_PID
, LOG_CRON
);
openlog(ProgramName
, LOG_PID
);
syslog_open
= TRUE
; /* assume openlog success */
syslog(LOG_INFO
, "(%s) %s (%s)\n", username
, event
, detail
);
fprintf(stderr
, "log_it: (%s %d) %s (%s)\n",
username
, pid
, event
, detail
);
* (1) this routine is fairly slow
* (2) it returns a pointer to static storage
register char *s
; /* string we want the first word of */
register char *t
; /* terminators, implicitly including \0 */
static char retbuf
[2][MAX_TEMPSTR
+ 1]; /* sure wish C had GC */
/* select a return buffer */
/* skip any leading terminators */
while (*s
&& (NULL
!= strchr(t
, *s
))) {
/* copy until next terminator or full buffer */
while (*s
&& (NULL
== strchr(t
, *s
)) && (rp
< &rb
[MAX_TEMPSTR
])) {
/* finish the return-string and return it */
* heavily ascii-dependent.
register unsigned char *src
;
register unsigned char ch
= *src
++;
if (ch
< ' ') { /* control character */
} else if (ch
< 0177) { /* printable */
} else if (ch
== 0177) { /* delete/rubout */
} else { /* parity character */
sprintf(dst
, "\\%03o", ch
);
* returns a pointer to malloc'd storage, you must call free yourself.
register unsigned char *src
;
register unsigned int len
;
register char *dst
= malloc(len
*4 + 1);
/* Sat, 27 Feb 93 11:44:51 CST
* 123456789012345678901234567
time_t t
= clock
?*clock
:time(0L);
struct tm
*tm
= localtime(&t
);
static char ret
[30]; /* zone name might be >3 chars */
(void) sprintf(ret
, "%s, %2d %s %2d %02d:%02d:%02d %s",
int swap_uids() { save_euid
= geteuid(); return seteuid(getuid()); }
int swap_uids_back() { return seteuid(save_euid
); }
#else /*HAVE_SAVED_UIDS*/
int swap_uids() { return setreuid(geteuid(), getuid()); }
int swap_uids_back() { return swap_uids(); }
#endif /*HAVE_SAVED_UIDS*/