xref: /illumos-gate/usr/src/cmd/cron/permit.c (revision 618372bc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d1419d5aSNobutomo Nakano  * Common Development and Distribution License (the "License").
6d1419d5aSNobutomo Nakano  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22*618372bcSSebastian Wiedenroth /*	  All Rights Reserved  */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
25d1419d5aSNobutomo Nakano  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26d1419d5aSNobutomo Nakano  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <pwd.h>
35d1419d5aSNobutomo Nakano #include <auth_attr.h>
36d1419d5aSNobutomo Nakano #include <auth_list.h>
37d1419d5aSNobutomo Nakano 
387c478bd9Sstevel@tonic-gate #include "cron.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate struct stat globstat;
417c478bd9Sstevel@tonic-gate #define	exists(file)	(stat(file, &globstat) == 0)
427c478bd9Sstevel@tonic-gate #define	ROOT	"root"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate int per_errno;	/* status info from getuser */
457c478bd9Sstevel@tonic-gate static int within(char *, char *);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate char *
getuser(uid_t uid)49*618372bcSSebastian Wiedenroth getuser(uid_t uid)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	struct passwd *nptr;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if ((nptr = getpwuid(uid)) == NULL) {
547c478bd9Sstevel@tonic-gate 		per_errno = 1;
557c478bd9Sstevel@tonic-gate 		return (NULL);
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 	if ((strcmp(nptr->pw_shell, SHELL) != 0) &&
587c478bd9Sstevel@tonic-gate 	    (strcmp(nptr->pw_shell, "") != 0)) {
597c478bd9Sstevel@tonic-gate 		per_errno = 2;
607c478bd9Sstevel@tonic-gate 		/*
617c478bd9Sstevel@tonic-gate 		 * return NULL if you want crontab and at to abort
627c478bd9Sstevel@tonic-gate 		 * when the users login shell is not /usr/bin/sh otherwise
637c478bd9Sstevel@tonic-gate 		 * return pw_name
647c478bd9Sstevel@tonic-gate 		 */
657c478bd9Sstevel@tonic-gate 		return (nptr->pw_name);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	return (nptr->pw_name);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
allowed(char * user,char * allow,char * deny)71*618372bcSSebastian Wiedenroth allowed(char *user, char *allow, char *deny)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	if (exists(allow)) {
747c478bd9Sstevel@tonic-gate 		if (within(user, allow)) {
757c478bd9Sstevel@tonic-gate 			return (1);
767c478bd9Sstevel@tonic-gate 		} else {
777c478bd9Sstevel@tonic-gate 			return (0);
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 	} else if (exists(deny)) {
807c478bd9Sstevel@tonic-gate 		if (within(user, deny)) {
817c478bd9Sstevel@tonic-gate 			return (0);
827c478bd9Sstevel@tonic-gate 		} else {
837c478bd9Sstevel@tonic-gate 			return (1);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 	} else if (chkauthattr(CRONUSER_AUTH, user)) {
867c478bd9Sstevel@tonic-gate 		return (1);
877c478bd9Sstevel@tonic-gate 	} else {
887c478bd9Sstevel@tonic-gate 		return (0);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static int
within(char * username,char * filename)93*618372bcSSebastian Wiedenroth within(char *username, char *filename)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	char line[UNAMESIZE];
967c478bd9Sstevel@tonic-gate 	FILE *cap;
977c478bd9Sstevel@tonic-gate 	int i;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ((cap = fopen(filename, "r")) == NULL)
1007c478bd9Sstevel@tonic-gate 		return (0);
1017c478bd9Sstevel@tonic-gate 	while (fgets(line, UNAMESIZE, cap) != NULL) {
1027c478bd9Sstevel@tonic-gate 		for (i = 0; line[i] != '\0'; i++) {
1037c478bd9Sstevel@tonic-gate 			if (isspace(line[i])) {
1047c478bd9Sstevel@tonic-gate 				line[i] = '\0';
1057c478bd9Sstevel@tonic-gate 				break; }
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 		if (strcmp(line, username) == 0) {
1087c478bd9Sstevel@tonic-gate 			fclose(cap);
1097c478bd9Sstevel@tonic-gate 			return (1);
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	fclose(cap);
1137c478bd9Sstevel@tonic-gate 	return (0);
1147c478bd9Sstevel@tonic-gate }
115d1419d5aSNobutomo Nakano 
116d1419d5aSNobutomo Nakano int
cron_admin(const char * name)117d1419d5aSNobutomo Nakano cron_admin(const char *name)
118d1419d5aSNobutomo Nakano {
119d1419d5aSNobutomo Nakano 	return (chkauthattr(CRONADMIN_AUTH, name));
120d1419d5aSNobutomo Nakano }
121