1*7d0c2e54Skrw /* $OpenBSD: client.c,v 1.10 2018/07/13 08:39:33 krw Exp $ */
222e679e6Smillert
322e679e6Smillert /* Copyright 1988,1990,1993,1994 by Paul Vixie
422e679e6Smillert * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
522e679e6Smillert * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
622e679e6Smillert *
722e679e6Smillert * Permission to use, copy, modify, and distribute this software for any
822e679e6Smillert * purpose with or without fee is hereby granted, provided that the above
922e679e6Smillert * copyright notice and this permission notice appear in all copies.
1022e679e6Smillert *
1122e679e6Smillert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1222e679e6Smillert * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1322e679e6Smillert * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
1422e679e6Smillert * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1522e679e6Smillert * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1622e679e6Smillert * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1722e679e6Smillert * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1822e679e6Smillert */
1922e679e6Smillert
20fa575ea2Smillert #include <sys/types.h>
21fa575ea2Smillert #include <sys/socket.h>
226b36bf07Smillert #include <sys/stat.h>
23fa575ea2Smillert #include <sys/un.h>
24fa575ea2Smillert
25fa575ea2Smillert #include <bitstring.h> /* for structs.h */
266b36bf07Smillert #include <err.h>
276b36bf07Smillert #include <errno.h>
28fa575ea2Smillert #include <stdio.h>
29fa575ea2Smillert #include <stdlib.h>
30fa575ea2Smillert #include <string.h>
31fa575ea2Smillert #include <time.h> /* for structs.h */
32fa575ea2Smillert #include <unistd.h>
33fa575ea2Smillert
34fa575ea2Smillert #include "pathnames.h"
35fa575ea2Smillert #include "macros.h"
36fa575ea2Smillert #include "structs.h"
37fa575ea2Smillert #include "funcs.h"
38fa575ea2Smillert #include "globals.h"
3922e679e6Smillert
4022e679e6Smillert /* int in_file(const char *string, FILE *file, int error)
4122e679e6Smillert * return TRUE if one of the lines in file matches string exactly,
4222e679e6Smillert * FALSE if no lines match, and error on error.
4322e679e6Smillert */
4422e679e6Smillert static int
in_file(const char * string,FILE * file,int error)4522e679e6Smillert in_file(const char *string, FILE *file, int error)
4622e679e6Smillert {
4722e679e6Smillert char line[MAX_TEMPSTR];
4822e679e6Smillert char *endp;
4922e679e6Smillert
5022e679e6Smillert if (fseek(file, 0L, SEEK_SET))
5122e679e6Smillert return (error);
5222e679e6Smillert while (fgets(line, MAX_TEMPSTR, file)) {
5322e679e6Smillert if (line[0] != '\0') {
5422e679e6Smillert endp = &line[strlen(line) - 1];
5522e679e6Smillert if (*endp != '\n')
5622e679e6Smillert return (error);
5722e679e6Smillert *endp = '\0';
5822e679e6Smillert if (0 == strcmp(line, string))
5922e679e6Smillert return (TRUE);
6022e679e6Smillert }
6122e679e6Smillert }
6222e679e6Smillert if (ferror(file))
6322e679e6Smillert return (error);
6422e679e6Smillert return (FALSE);
6522e679e6Smillert }
6622e679e6Smillert
6722e679e6Smillert /* int allowed(const char *username, const char *allow_file, const char *deny_file)
6822e679e6Smillert * returns TRUE if (allow_file exists and user is listed)
6922e679e6Smillert * or (deny_file exists and user is NOT listed).
7022e679e6Smillert * root is always allowed.
7122e679e6Smillert */
7222e679e6Smillert int
allowed(const char * username,const char * allow_file,const char * deny_file)7322e679e6Smillert allowed(const char *username, const char *allow_file, const char *deny_file)
7422e679e6Smillert {
7522e679e6Smillert FILE *fp;
7622e679e6Smillert int isallowed;
7722e679e6Smillert
7822e679e6Smillert if (strcmp(username, "root") == 0)
7922e679e6Smillert return (TRUE);
8022e679e6Smillert isallowed = FALSE;
8122e679e6Smillert if ((fp = fopen(allow_file, "r")) != NULL) {
8222e679e6Smillert isallowed = in_file(username, fp, FALSE);
8322e679e6Smillert fclose(fp);
8422e679e6Smillert } else if ((fp = fopen(deny_file, "r")) != NULL) {
8522e679e6Smillert isallowed = !in_file(username, fp, FALSE);
8622e679e6Smillert fclose(fp);
8722e679e6Smillert }
8822e679e6Smillert return (isallowed);
8922e679e6Smillert }
9022e679e6Smillert
9188959323Smillert /* void poke_daemon(unsigned char cookie)
9222e679e6Smillert * touches spool_dir and sends a poke to the cron daemon if running.
9322e679e6Smillert */
9422e679e6Smillert void
poke_daemon(unsigned char cookie)9588959323Smillert poke_daemon(unsigned char cookie)
9622e679e6Smillert {
9722e679e6Smillert int sock = -1;
9888959323Smillert const char *cronsock = _PATH_CRON_SOCK;
9922e679e6Smillert struct sockaddr_un s_un;
10022e679e6Smillert
10122e679e6Smillert bzero(&s_un, sizeof(s_un));
1026b36bf07Smillert if (strlcpy(s_un.sun_path, cronsock, sizeof(s_un.sun_path)) >=
1036b36bf07Smillert sizeof(s_un.sun_path)) {
1046b36bf07Smillert warnc(ENAMETOOLONG, "%s", cronsock);
10522e679e6Smillert return;
10622e679e6Smillert }
10722e679e6Smillert s_un.sun_family = AF_UNIX;
10822e679e6Smillert if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
10922e679e6Smillert connect(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == 0)
1105ca6582cSguenther send(sock, &cookie, 1, MSG_NOSIGNAL);
11122e679e6Smillert else
1126b36bf07Smillert warnx("warning, cron does not appear to be running");
11322e679e6Smillert if (sock >= 0)
11422e679e6Smillert close(sock);
11522e679e6Smillert }
116