xref: /illumos-gate/usr/src/cmd/cron/permit.c (revision d1419d5a02eaedd520f925cd465fd62f3950ae43)
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)
50 uid_t uid;
51 {
52 	struct passwd *nptr;
53 
54 	if ((nptr = getpwuid(uid)) == NULL) {
55 		per_errno = 1;
56 		return (NULL);
57 	}
58 	if ((strcmp(nptr->pw_shell, SHELL) != 0) &&
59 	    (strcmp(nptr->pw_shell, "") != 0)) {
60 		per_errno = 2;
61 		/*
62 		 * return NULL if you want crontab and at to abort
63 		 * when the users login shell is not /usr/bin/sh otherwise
64 		 * return pw_name
65 		 */
66 		return (nptr->pw_name);
67 	}
68 	return (nptr->pw_name);
69 }
70 
71 int
72 allowed(user, allow, deny)
73 char *user, *allow, *deny;
74 {
75 	if (exists(allow)) {
76 		if (within(user, allow)) {
77 			return (1);
78 		} else {
79 			return (0);
80 		}
81 	} else if (exists(deny)) {
82 		if (within(user, deny)) {
83 			return (0);
84 		} else {
85 			return (1);
86 		}
87 	} else if (chkauthattr(CRONUSER_AUTH, user)) {
88 		return (1);
89 	} else {
90 		return (0);
91 	}
92 }
93 
94 static int
95 within(username, filename)
96 char *username, *filename;
97 {
98 	char line[UNAMESIZE];
99 	FILE *cap;
100 	int i;
101 
102 	if ((cap = fopen(filename, "r")) == NULL)
103 		return (0);
104 	while (fgets(line, UNAMESIZE, cap) != NULL) {
105 		for (i = 0; line[i] != '\0'; i++) {
106 			if (isspace(line[i])) {
107 				line[i] = '\0';
108 				break; }
109 		}
110 		if (strcmp(line, username) == 0) {
111 			fclose(cap);
112 			return (1);
113 		}
114 	}
115 	fclose(cap);
116 	return (0);
117 }
118 
119 int
120 cron_admin(const char *name)
121 {
122 	return (chkauthattr(CRONADMIN_AUTH, name));
123 }
124