Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/cron/lib/misc.c
/* Copyright 1988,1990,1993,1994 by Paul Vixie | /* Copyright 1988,1990,1993,1994 by Paul Vixie | ||||
* All rights reserved | * All rights reserved | ||||
*/ | |||||
/* | |||||
* Copyright (c) 1997 by Internet Software Consortium | |||||
* | * | ||||
* Distribute freely, except: don't remove my name from the source or | * Permission to use, copy, modify, and distribute this software for any | ||||
* documentation (don't take credit for my work), mark your changes (don't | * purpose with or without fee is hereby granted, provided that the above | ||||
* get me blamed for your possible bugs), don't alter or remove this | * copyright notice and this permission notice appear in all copies. | ||||
* 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 | |||||
* user. | |||||
* | * | ||||
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||||
* I'll try to keep a version up to date. I can be reached as follows: | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||||
* Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |||||
* SOFTWARE. | |||||
*/ | */ | ||||
#if !defined(lint) && !defined(LINT) | #if !defined(lint) && !defined(LINT) | ||||
static const char rcsid[] = | static const char rcsid[] = "$Id: misc.c,v 1.5 1998/08/14 00:32:40 vixie Exp $"; | ||||
"$FreeBSD$"; | |||||
#endif | #endif | ||||
/* vix 26jan87 [RCS has the rest of the log] | /* vix 26jan87 [RCS has the rest of the log] | ||||
* vix 30dec86 [written] | * vix 30dec86 [written] | ||||
*/ | */ | ||||
#include "cron.h" | #include "cron.h" | ||||
#if SYS_TIME_H | #if SYS_TIME_H | ||||
# include <sys/time.h> | # include <sys/time.h> | ||||
#else | #else | ||||
# include <time.h> | # include <time.h> | ||||
#endif | #endif | ||||
#include <sys/file.h> | #include <sys/file.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <err.h> | |||||
#include <errno.h> | #include <errno.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#if defined(SYSLOG) | #if defined(SYSLOG) | ||||
# include <syslog.h> | # include <syslog.h> | ||||
#endif | #endif | ||||
#if defined(LOG_CRON) && defined(LOG_FILE) | |||||
# undef LOG_FILE | |||||
#endif | |||||
#if defined(LOG_DAEMON) && !defined(LOG_CRON) | #if defined(LOG_DAEMON) && !defined(LOG_CRON) | ||||
#define LOG_CRON LOG_DAEMON | # define LOG_CRON LOG_DAEMON | ||||
#endif | #endif | ||||
static int LogFD = ERR; | static int LogFD = ERR; | ||||
int | int | ||||
strcmp_until(char *left, char *right, int until) | strcmp_until(const char *left, const char *right, int until) | ||||
{ | { | ||||
register int diff; | |||||
while (*left && *left != until && *left == *right) { | while (*left && *left != until && *left == *right) { | ||||
left++; | left++; | ||||
right++; | right++; | ||||
} | } | ||||
if ((*left=='\0' || *left == until) && | if ((*left=='\0' || *left == until) && | ||||
(*right=='\0' || *right == until)) { | (*right=='\0' || *right == until)) { | ||||
diff = 0; | return (0); | ||||
} else { | |||||
diff = *left - *right; | |||||
} | } | ||||
return (*left - *right); | |||||
return diff; | |||||
} | } | ||||
/* strdtb(s) - delete trailing blanks in string 's' and return new length | /* strdtb(s) - delete trailing blanks in string 's' and return new length | ||||
*/ | */ | ||||
int | int | ||||
strdtb(char *s) | strdtb(char *s) | ||||
{ | { | ||||
Show All 38 Lines | |||||
#else /* DEBUGGING */ | #else /* DEBUGGING */ | ||||
char *pc = flags; | char *pc = flags; | ||||
DebugFlags = 0; | DebugFlags = 0; | ||||
while (*pc) { | while (*pc) { | ||||
char **test; | const char **test; | ||||
int mask; | int mask; | ||||
/* try to find debug flag name in our list. | /* try to find debug flag name in our list. | ||||
*/ | */ | ||||
for ( test = DebugFlagNames, mask = 1; | for ( test = DebugFlagNames, mask = 1; | ||||
*test && strcmp_until(*test, pc, ','); | *test != NULL && strcmp_until(*test, pc, ','); | ||||
test++, mask <<= 1 | test++, mask <<= 1 | ||||
) | ) | ||||
; | ; | ||||
if (!*test) { | if (!*test) { | ||||
fprintf(stderr, | fprintf(stderr, | ||||
"unrecognized debug flag <%s> <%s>\n", | "unrecognized debug flag <%s> <%s>\n", | ||||
flags, pc); | flags, pc); | ||||
▲ Show 20 Lines • Show All 226 Lines • ▼ Show 20 Lines | out: if (allow) | ||||
fclose(allow); | fclose(allow); | ||||
if (deny) | if (deny) | ||||
fclose(deny); | fclose(deny); | ||||
return (isallowed); | return (isallowed); | ||||
} | } | ||||
void | void | ||||
log_it(char *username, int xpid, char *event, const char *detail) | log_it(const char *username, int xpid, const char *event, const char *detail) | ||||
{ | { | ||||
#if defined(LOG_FILE) || DEBUGGING | #if defined(LOG_FILE) || DEBUGGING | ||||
PID_T pid = xpid; | PID_T pid = xpid; | ||||
#endif | #endif | ||||
#if defined(LOG_FILE) | #if defined(LOG_FILE) | ||||
char *msg; | char *msg; | ||||
TIME_T now = time((TIME_T) 0); | TIME_T now = time((TIME_T) 0); | ||||
register struct tm *t = localtime(&now); | struct tm *t = localtime(&now); | ||||
#endif /*LOG_FILE*/ | #endif /*LOG_FILE*/ | ||||
#if defined(SYSLOG) | #if defined(SYSLOG) | ||||
static int syslog_open = 0; | static int syslog_open = 0; | ||||
#endif | #endif | ||||
#if defined(LOG_FILE) | #if defined(LOG_FILE) | ||||
/* we assume that MAX_TEMPSTR will hold the date, time, &punctuation. | /* we assume that MAX_TEMPSTR will hold the date, time, &punctuation. | ||||
*/ | */ | ||||
msg = malloc(strlen(username) | msg = malloc(strlen(username) | ||||
+ strlen(event) | + strlen(event) | ||||
+ strlen(detail) | + strlen(detail) | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | if (LogFD != ERR) { | ||||
LogFD = ERR; | LogFD = ERR; | ||||
} | } | ||||
} | } | ||||
/* two warnings: | /* two warnings: | ||||
* (1) this routine is fairly slow | * (1) this routine is fairly slow | ||||
* (2) it returns a pointer to static storage | * (2) it returns a pointer to static storage | ||||
* parameters: | |||||
* s: string we want the first word of | |||||
* t: terminators, implicitly including \0 | |||||
*/ | */ | ||||
char * | char * | ||||
first_word(char *s, char *t) | first_word(char *s, char *t) | ||||
{ | { | ||||
static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */ | static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */ | ||||
static int retsel = 0; | static int retsel = 0; | ||||
register char *rb, *rp; | char *rb, *rp; | ||||
/* select a return buffer */ | /* select a return buffer */ | ||||
retsel = 1-retsel; | retsel = 1-retsel; | ||||
rb = &retbuf[retsel][0]; | rb = &retbuf[retsel][0]; | ||||
rp = rb; | rp = rb; | ||||
/* skip any leading terminators */ | /* skip any leading terminators */ | ||||
while (*s && (NULL != strchr(t, *s))) { | while (*s && (NULL != strchr(t, *s))) { | ||||
Show All 10 Lines | first_word(char *s, char *t) | ||||
return rb; | return rb; | ||||
} | } | ||||
/* warning: | /* warning: | ||||
* heavily ascii-dependent. | * heavily ascii-dependent. | ||||
*/ | */ | ||||
static void | static void | ||||
mkprint(register char *dst, register unsigned char *src, register int len) | mkprint(char *dst, unsigned char *src, int len) | ||||
{ | { | ||||
/* | |||||
* XXX | |||||
* We know this routine can't overflow the dst buffer because mkprints() | |||||
* allocated enough space for the worst case. | |||||
*/ | |||||
while (len-- > 0) | while (len-- > 0) | ||||
{ | { | ||||
register unsigned char ch = *src++; | unsigned char ch = *src++; | ||||
if (ch < ' ') { /* control character */ | if (ch < ' ') { /* control character */ | ||||
*dst++ = '^'; | *dst++ = '^'; | ||||
*dst++ = ch + '@'; | *dst++ = ch + '@'; | ||||
} else if (ch < 0177) { /* printable */ | } else if (ch < 0177) { /* printable */ | ||||
*dst++ = ch; | *dst++ = ch; | ||||
} else if (ch == 0177) { /* delete/rubout */ | } else if (ch == 0177) { /* delete/rubout */ | ||||
*dst++ = '^'; | *dst++ = '^'; | ||||
*dst++ = '?'; | *dst++ = '?'; | ||||
} else { /* parity character */ | } else { /* parity character */ | ||||
sprintf(dst, "\\%03o", ch); | sprintf(dst, "\\%03o", ch); | ||||
dst += 4; | dst += 4; | ||||
} | } | ||||
} | } | ||||
*dst = '\0'; | *dst = '\0'; | ||||
} | } | ||||
/* warning: | /* warning: | ||||
* returns a pointer to malloc'd storage, you must call free yourself. | * returns a pointer to malloc'd storage, you must call free yourself. | ||||
*/ | */ | ||||
char * | char * | ||||
mkprints(unsigned char *src, unsigned int len) | mkprints(unsigned char *src, unsigned int len) | ||||
{ | { | ||||
register char *dst = malloc(len*4 + 1); | char *dst = malloc(len*4 + 1); | ||||
if (dst != NULL) | if (dst != NULL) | ||||
mkprint(dst, src, len); | mkprint(dst, src, len); | ||||
return dst; | return dst; | ||||
} | } | ||||
#ifdef MAIL_DATE | #ifdef MAIL_DATE | ||||
/* Sat, 27 Feb 93 11:44:51 CST | /* Sat, 27 Feb 93 11:44:51 CST | ||||
* 123456789012345678901234567 | * 123456789012345678901234567 | ||||
*/ | */ | ||||
char * | char * | ||||
arpadate(time_t *clock) | arpadate(time_t *clock) | ||||
{ | { | ||||
time_t t = clock ?*clock :time(0L); | time_t t = clock ?*clock :time(0L); | ||||
struct tm *tm = localtime(&t); | struct tm *tm = localtime(&t); | ||||
static char ret[32]; /* zone name might be >3 chars */ | static char ret[60]; /* zone name might be >3 chars */ | ||||
if (tm->tm_year >= 100) | if (tm->tm_year >= 100) | ||||
tm->tm_year += 1900; | tm->tm_year += 1900; | ||||
(void) snprintf(ret, sizeof(ret), "%s, %2d %s %d %02d:%02d:%02d %s", | (void) snprintf(ret, sizeof(ret), "%s, %2d %s %d %02d:%02d:%02d %s", | ||||
DowNames[tm->tm_wday], | DowNames[tm->tm_wday], | ||||
tm->tm_mday, | tm->tm_mday, | ||||
MonthNames[tm->tm_mon], | MonthNames[tm->tm_mon], | ||||
Show All 18 Lines |