xref: /illumos-gate/usr/src/cmd/cron/permit.c (revision 15f90b02bdacbf0ae47fa105944f15b6596f9748)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 /*	  All Rights Reserved  */
23 
24 /*
25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <pwd.h>
35 #include <auth_attr.h>
36 #include <auth_list.h>
37 
38 #include "cron.h"
39 
40 struct stat globstat;
41 #define	exists(file)	(stat(file, &globstat) == 0)
42 #define	ROOT	"root"
43 
44 int per_errno;	/* status info from getuser */
45 static int within(char *, char *);
46 
47 
48 char *
49 getuser(uid_t uid)
50 {
51 	struct passwd *nptr;
52 
53 	if ((nptr = getpwuid(uid)) == NULL) {
54 		per_errno = 1;
55 		return (NULL);
56 	}
57 	if ((strcmp(nptr->pw_shell, SHELL) != 0) &&
58 	    (strcmp(nptr->pw_shell, "") != 0)) {
59 		per_errno = 2;
60 		/*
61 		 * return NULL if you want crontab and at to abort
62 		 * when the users login shell is not /usr/bin/sh otherwise
63 		 * return pw_name
64 		 */
65 		return (nptr->pw_name);
66 	}
67 	return (nptr->pw_name);
68 }
69 
70 int
71 allowed(char *user, char *allow, char *deny)
72 {
73 	if (exists(allow)) {
74 		if (within(user, allow)) {
75 			return (1);
76 		} else {
77 			return (0);
78 		}
79 	} else if (exists(deny)) {
80 		if (within(user, deny)) {
81 			return (0);
82 		} else {
83 			return (1);
84 		}
85 	} else if (chkauthattr(CRONUSER_AUTH, user)) {
86 		return (1);
87 	} else {
88 		return (0);
89 	}
90 }
91 
92 static int
93 within(char *username, char *filename)
94 {
95 	char line[UNAMESIZE];
96 	FILE *cap;
97 	int i;
98 
99 	if ((cap = fopen(filename, "r")) == NULL)
100 		return (0);
101 	while (fgets(line, UNAMESIZE, cap) != NULL) {
102 		for (i = 0; line[i] != '\0'; i++) {
103 			if (isspace(line[i])) {
104 				line[i] = '\0';
105 				break; }
106 		}
107 		if (strcmp(line, username) == 0) {
108 			fclose(cap);
109 			return (1);
110 		}
111 	}
112 	fclose(cap);
113 	return (0);
114 }
115 
116 int
117 cron_admin(const char *name)
118 {
119 	return (chkauthattr(CRONADMIN_AUTH, name));
120 }
121