diff options
author | 2015-10-31 12:19:41 +0000 | |
---|---|---|
committer | 2015-10-31 12:19:41 +0000 | |
commit | 22e679e6cc3b44ab9217f1c68555a609ecaf1b5b (patch) | |
tree | a54d92251b4002825bd33c3b968003e67ed9aa2e | |
parent | open_socket() is only used by cron proper so move to cron.c (diff) | |
download | wireguard-openbsd-22e679e6cc3b44ab9217f1c68555a609ecaf1b5b.tar.xz wireguard-openbsd-22e679e6cc3b44ab9217f1c68555a609ecaf1b5b.zip |
Split client-only (at, crontab) functions out of misc.c and into
client.c. Move truly common functions into common.c. This avoids
dead code in the at and crontab commands.
-rw-r--r-- | usr.bin/at/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/crontab/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/cron/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/cron/client.c | 102 | ||||
-rw-r--r-- | usr.sbin/cron/common.c | 117 | ||||
-rw-r--r-- | usr.sbin/cron/misc.c | 176 |
6 files changed, 226 insertions, 181 deletions
diff --git a/usr.bin/at/Makefile b/usr.bin/at/Makefile index 3d3ea788144..74ba676627a 100644 --- a/usr.bin/at/Makefile +++ b/usr.bin/at/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.9 2015/01/23 01:58:20 tedu Exp $ +# $OpenBSD: Makefile,v 1.10 2015/10/31 12:19:41 millert Exp $ PROG= at -SRCS= at.c misc.c parsetime.c +SRCS= at.c client.c common.c parsetime.c CRONDIR=${.CURDIR}/../../usr.sbin/cron CFLAGS+=-I${CRONDIR} MAN= at.1 atrm.1 atq.1 diff --git a/usr.bin/crontab/Makefile b/usr.bin/crontab/Makefile index dff94dcdc8b..5fc8d3ecae8 100644 --- a/usr.bin/crontab/Makefile +++ b/usr.bin/crontab/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.6 2015/01/23 01:58:20 tedu Exp $ +# $OpenBSD: Makefile,v 1.7 2015/10/31 12:19:41 millert Exp $ PROG= crontab -SRCS= crontab.c misc.c entry.c env.c +SRCS= crontab.c entry.c env.c client.c common.c misc.c CFLAGS+=-I${.CURDIR} -I${.CURDIR}/../../usr.sbin/cron BINGRP =crontab BINMODE=2555 diff --git a/usr.sbin/cron/Makefile b/usr.sbin/cron/Makefile index 0441c097b31..377c9c3be7d 100644 --- a/usr.sbin/cron/Makefile +++ b/usr.sbin/cron/Makefile @@ -1,8 +1,8 @@ -# $OpenBSD: Makefile,v 1.4 2002/07/15 19:26:51 millert Exp $ +# $OpenBSD: Makefile,v 1.5 2015/10/31 12:19:41 millert Exp $ PROG= cron SRCS= cron.c database.c user.c entry.c job.c do_command.c \ - misc.c env.c popen.c atrun.c + misc.c env.c popen.c atrun.c common.c CFLAGS+=-I${.CURDIR} MAN= cron.8 diff --git a/usr.sbin/cron/client.c b/usr.sbin/cron/client.c new file mode 100644 index 00000000000..b9e0cd9554d --- /dev/null +++ b/usr.sbin/cron/client.c @@ -0,0 +1,102 @@ +/* $OpenBSD: client.c,v 1.1 2015/10/31 12:19:41 millert Exp $ */ + +/* Copyright 1988,1990,1993,1994 by Paul Vixie + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +#include "cron.h" + +/* int in_file(const char *string, FILE *file, int error) + * return TRUE if one of the lines in file matches string exactly, + * FALSE if no lines match, and error on error. + */ +static int +in_file(const char *string, FILE *file, int error) +{ + char line[MAX_TEMPSTR]; + char *endp; + + if (fseek(file, 0L, SEEK_SET)) + return (error); + while (fgets(line, MAX_TEMPSTR, file)) { + if (line[0] != '\0') { + endp = &line[strlen(line) - 1]; + if (*endp != '\n') + return (error); + *endp = '\0'; + if (0 == strcmp(line, string)) + return (TRUE); + } + } + if (ferror(file)) + return (error); + return (FALSE); +} + +/* int allowed(const char *username, const char *allow_file, const char *deny_file) + * returns TRUE if (allow_file exists and user is listed) + * or (deny_file exists and user is NOT listed). + * root is always allowed. + */ +int +allowed(const char *username, const char *allow_file, const char *deny_file) +{ + FILE *fp; + int isallowed; + + if (strcmp(username, "root") == 0) + return (TRUE); + isallowed = FALSE; + if ((fp = fopen(allow_file, "r")) != NULL) { + isallowed = in_file(username, fp, FALSE); + fclose(fp); + } else if ((fp = fopen(deny_file, "r")) != NULL) { + isallowed = !in_file(username, fp, FALSE); + fclose(fp); + } + return (isallowed); +} + +/* void poke_daemon(const char *spool_dir, unsigned char cookie) + * touches spool_dir and sends a poke to the cron daemon if running. + */ +void +poke_daemon(const char *spool_dir, unsigned char cookie) +{ + int sock = -1; + struct sockaddr_un s_un; + + (void) utime(spool_dir, NULL); /* old poke method */ + + bzero(&s_un, sizeof(s_un)); + if (snprintf(s_un.sun_path, sizeof s_un.sun_path, "%s/%s", + SPOOL_DIR, CRONSOCK) >= sizeof(s_un.sun_path)) { + fprintf(stderr, "%s: %s/%s: path too long\n", + ProgramName, SPOOL_DIR, CRONSOCK); + return; + } + s_un.sun_family = AF_UNIX; + (void) signal(SIGPIPE, SIG_IGN); + if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && + connect(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == 0) + write(sock, &cookie, 1); + else + fprintf(stderr, "%s: warning, cron does not appear to be " + "running.\n", ProgramName); + if (sock >= 0) + close(sock); + (void) signal(SIGPIPE, SIG_DFL); +} diff --git a/usr.sbin/cron/common.c b/usr.sbin/cron/common.c new file mode 100644 index 00000000000..0b44b811de8 --- /dev/null +++ b/usr.sbin/cron/common.c @@ -0,0 +1,117 @@ +/* $OpenBSD: common.c,v 1.1 2015/10/31 12:19:41 millert Exp $ */ + +/* Copyright 1988,1990,1993,1994 by Paul Vixie + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + +#include "cron.h" + +void +set_cron_cwd(void) +{ + struct stat sb; + struct group *grp = NULL; + + grp = getgrnam(CRON_GROUP); + /* first check for CRONDIR ("/var/cron" or some such) + */ + if (stat(CRONDIR, &sb) < 0 && errno == ENOENT) { + perror(CRONDIR); + if (0 == mkdir(CRONDIR, 0710)) { + fprintf(stderr, "%s: created\n", CRONDIR); + stat(CRONDIR, &sb); + } else { + fprintf(stderr, "%s: ", CRONDIR); + perror("mkdir"); + exit(EXIT_FAILURE); + } + } + if (!S_ISDIR(sb.st_mode)) { + fprintf(stderr, "'%s' is not a directory, bailing out.\n", + CRONDIR); + exit(EXIT_FAILURE); + } + if (chdir(CRONDIR) < 0) { + fprintf(stderr, "cannot chdir(%s), bailing out.\n", CRONDIR); + perror(CRONDIR); + exit(EXIT_FAILURE); + } + + /* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such) + */ + if (stat(SPOOL_DIR, &sb) < 0 && errno == ENOENT) { + perror(SPOOL_DIR); + if (0 == mkdir(SPOOL_DIR, 0700)) { + fprintf(stderr, "%s: created\n", SPOOL_DIR); + stat(SPOOL_DIR, &sb); + } else { + fprintf(stderr, "%s: ", SPOOL_DIR); + perror("mkdir"); + exit(EXIT_FAILURE); + } + } + if (!S_ISDIR(sb.st_mode)) { + fprintf(stderr, "'%s' is not a directory, bailing out.\n", + SPOOL_DIR); + exit(EXIT_FAILURE); + } + if (grp != NULL) { + if (sb.st_gid != grp->gr_gid) + chown(SPOOL_DIR, -1, grp->gr_gid); + if (sb.st_mode != 01730) + chmod(SPOOL_DIR, 01730); + } + + /* finally, look at AT_DIR ("atjobs" or some such) + */ + if (stat(AT_DIR, &sb) < 0 && errno == ENOENT) { + perror(AT_DIR); + if (0 == mkdir(AT_DIR, 0700)) { + fprintf(stderr, "%s: created\n", AT_DIR); + stat(AT_DIR, &sb); + } else { + fprintf(stderr, "%s: ", AT_DIR); + perror("mkdir"); + exit(EXIT_FAILURE); + } + } + if (!S_ISDIR(sb.st_mode)) { + fprintf(stderr, "'%s' is not a directory, bailing out.\n", + AT_DIR); + exit(EXIT_FAILURE); + } + if (grp != NULL) { + if (sb.st_gid != grp->gr_gid) + chown(AT_DIR, -1, grp->gr_gid); + if (sb.st_mode != 01770) + chmod(AT_DIR, 01770); + } +} + +int +strtot(const char *nptr, char **endptr, time_t *tp) +{ + long long ll; + + errno = 0; + ll = strtoll(nptr, endptr, 10); + if (*endptr == nptr) + return (-1); + if (ll < 0 || (errno == ERANGE && ll == LLONG_MAX) || (time_t)ll != ll) + return (-1); + *tp = (time_t)ll; + return (0); +} diff --git a/usr.sbin/cron/misc.c b/usr.sbin/cron/misc.c index a18689ab0b4..6b1fc7ad181 100644 --- a/usr.sbin/cron/misc.c +++ b/usr.sbin/cron/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.65 2015/10/31 12:14:16 millert Exp $ */ +/* $OpenBSD: misc.c,v 1.66 2015/10/31 12:19:41 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -22,88 +22,6 @@ static int LogFD = -1; static int syslog_open = FALSE; -void -set_cron_cwd(void) -{ - struct stat sb; - struct group *grp = NULL; - - grp = getgrnam(CRON_GROUP); - /* first check for CRONDIR ("/var/cron" or some such) - */ - if (stat(CRONDIR, &sb) < 0 && errno == ENOENT) { - perror(CRONDIR); - if (0 == mkdir(CRONDIR, 0710)) { - fprintf(stderr, "%s: created\n", CRONDIR); - stat(CRONDIR, &sb); - } else { - fprintf(stderr, "%s: ", CRONDIR); - perror("mkdir"); - exit(EXIT_FAILURE); - } - } - if (!S_ISDIR(sb.st_mode)) { - fprintf(stderr, "'%s' is not a directory, bailing out.\n", - CRONDIR); - exit(EXIT_FAILURE); - } - if (chdir(CRONDIR) < 0) { - fprintf(stderr, "cannot chdir(%s), bailing out.\n", CRONDIR); - perror(CRONDIR); - exit(EXIT_FAILURE); - } - - /* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such) - */ - if (stat(SPOOL_DIR, &sb) < 0 && errno == ENOENT) { - perror(SPOOL_DIR); - if (0 == mkdir(SPOOL_DIR, 0700)) { - fprintf(stderr, "%s: created\n", SPOOL_DIR); - stat(SPOOL_DIR, &sb); - } else { - fprintf(stderr, "%s: ", SPOOL_DIR); - perror("mkdir"); - exit(EXIT_FAILURE); - } - } - if (!S_ISDIR(sb.st_mode)) { - fprintf(stderr, "'%s' is not a directory, bailing out.\n", - SPOOL_DIR); - exit(EXIT_FAILURE); - } - if (grp != NULL) { - if (sb.st_gid != grp->gr_gid) - chown(SPOOL_DIR, -1, grp->gr_gid); - if (sb.st_mode != 01730) - chmod(SPOOL_DIR, 01730); - } - - /* finally, look at AT_DIR ("atjobs" or some such) - */ - if (stat(AT_DIR, &sb) < 0 && errno == ENOENT) { - perror(AT_DIR); - if (0 == mkdir(AT_DIR, 0700)) { - fprintf(stderr, "%s: created\n", AT_DIR); - stat(AT_DIR, &sb); - } else { - fprintf(stderr, "%s: ", AT_DIR); - perror("mkdir"); - exit(EXIT_FAILURE); - } - } - if (!S_ISDIR(sb.st_mode)) { - fprintf(stderr, "'%s' is not a directory, bailing out.\n", - AT_DIR); - exit(EXIT_FAILURE); - } - if (grp != NULL) { - if (sb.st_gid != grp->gr_gid) - chown(AT_DIR, -1, grp->gr_gid); - if (sb.st_mode != 01770) - chmod(AT_DIR, 01770); - } -} - /* get_char(file) : like getc() but increment LineNumber on newlines */ int @@ -189,57 +107,6 @@ skip_comments(FILE *file) unget_char(ch, file); } -/* int in_file(const char *string, FILE *file, int error) - * return TRUE if one of the lines in file matches string exactly, - * FALSE if no lines match, and error on error. - */ -static int -in_file(const char *string, FILE *file, int error) -{ - char line[MAX_TEMPSTR]; - char *endp; - - if (fseek(file, 0L, SEEK_SET)) - return (error); - while (fgets(line, MAX_TEMPSTR, file)) { - if (line[0] != '\0') { - endp = &line[strlen(line) - 1]; - if (*endp != '\n') - return (error); - *endp = '\0'; - if (0 == strcmp(line, string)) - return (TRUE); - } - } - if (ferror(file)) - return (error); - return (FALSE); -} - -/* int allowed(const char *username, const char *allow_file, const char *deny_file) - * returns TRUE if (allow_file exists and user is listed) - * or (deny_file exists and user is NOT listed). - * root is always allowed. - */ -int -allowed(const char *username, const char *allow_file, const char *deny_file) -{ - FILE *fp; - int isallowed; - - if (strcmp(username, "root") == 0) - return (TRUE); - isallowed = FALSE; - if ((fp = fopen(allow_file, "r")) != NULL) { - isallowed = in_file(username, fp, FALSE); - fclose(fp); - } else if ((fp = fopen(deny_file, "r")) != NULL) { - isallowed = !in_file(username, fp, FALSE); - fclose(fp); - } - return (isallowed); -} - void log_it(const char *username, pid_t xpid, const char *event, const char *detail) { @@ -306,44 +173,3 @@ first_word(char *s, char *t) *rp = '\0'; return (rb); } - -void -poke_daemon(const char *spool_dir, unsigned char cookie) -{ - int sock = -1; - struct sockaddr_un s_un; - - (void) utime(spool_dir, NULL); /* old poke method */ - - bzero(&s_un, sizeof(s_un)); - if (snprintf(s_un.sun_path, sizeof s_un.sun_path, "%s/%s", - SPOOL_DIR, CRONSOCK) >= sizeof(s_un.sun_path)) { - fprintf(stderr, "%s: %s/%s: path too long\n", - ProgramName, SPOOL_DIR, CRONSOCK); - return; - } - s_un.sun_family = AF_UNIX; - s_un.sun_len = SUN_LEN(&s_un); - (void) signal(SIGPIPE, SIG_IGN); - if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && - connect(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == 0) - write(sock, &cookie, 1); - else - fprintf(stderr, "%s: warning, cron does not appear to be " - "running.\n", ProgramName); - if (sock >= 0) - close(sock); - (void) signal(SIGPIPE, SIG_DFL); -} - -int -strtot(const char *nptr, char **endptr, time_t *tp) -{ - long long ll; - - ll = strtoll(nptr, endptr, 10); - if (ll < 0 || (time_t)ll != ll) - return (-1); - *tp = (time_t)ll; - return (0); -} |