xref: /dflybsd-src/usr.bin/at/perm.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino  *  perm.c - check user permission for at(1)
3*86d7f5d3SJohn Marino  *  Copyright (C) 1994  Thomas Koenig
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. The name of the author(s) may not be used to endorse or promote
11*86d7f5d3SJohn Marino  *    products derived from this software without specific prior written
12*86d7f5d3SJohn Marino  *    permission.
13*86d7f5d3SJohn Marino  *
14*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*86d7f5d3SJohn Marino  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*86d7f5d3SJohn Marino  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*86d7f5d3SJohn Marino  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*86d7f5d3SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*86d7f5d3SJohn Marino  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*86d7f5d3SJohn Marino  * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*86d7f5d3SJohn Marino  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*86d7f5d3SJohn Marino  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*86d7f5d3SJohn Marino  *
25*86d7f5d3SJohn Marino  * $FreeBSD: src/usr.bin/at/perm.c,v 1.11 2001/12/02 12:26:18 markm Exp $
26*86d7f5d3SJohn Marino  * $DragonFly: src/usr.bin/at/perm.c,v 1.5 2007/08/26 16:12:27 pavalos Exp $
27*86d7f5d3SJohn Marino  */
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino /* System Headers */
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino #include <sys/types.h>
32*86d7f5d3SJohn Marino #include <err.h>
33*86d7f5d3SJohn Marino #include <errno.h>
34*86d7f5d3SJohn Marino #include <pwd.h>
35*86d7f5d3SJohn Marino #include <stddef.h>
36*86d7f5d3SJohn Marino #include <stdio.h>
37*86d7f5d3SJohn Marino #include <stdlib.h>
38*86d7f5d3SJohn Marino #include <string.h>
39*86d7f5d3SJohn Marino #include <unistd.h>
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino /* Local headers */
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino #include "at.h"
44*86d7f5d3SJohn Marino #include "perm.h"
45*86d7f5d3SJohn Marino #include "privs.h"
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino /* Macros */
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino #define MAXUSERID 10
50*86d7f5d3SJohn Marino 
51*86d7f5d3SJohn Marino /* Structures and unions */
52*86d7f5d3SJohn Marino 
53*86d7f5d3SJohn Marino /* Function declarations */
54*86d7f5d3SJohn Marino 
55*86d7f5d3SJohn Marino static int check_for_user(FILE *fp, const char *name);
56*86d7f5d3SJohn Marino 
57*86d7f5d3SJohn Marino /* Local functions */
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino static int
check_for_user(FILE * fp,const char * name)60*86d7f5d3SJohn Marino check_for_user(FILE *fp, const char *name)
61*86d7f5d3SJohn Marino {
62*86d7f5d3SJohn Marino     char *buffer;
63*86d7f5d3SJohn Marino     size_t len;
64*86d7f5d3SJohn Marino     int found = 0;
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino     len = strlen(name);
67*86d7f5d3SJohn Marino     if ((buffer = malloc(len+2)) == NULL)
68*86d7f5d3SJohn Marino 	errx(EXIT_FAILURE, "virtual memory exhausted");
69*86d7f5d3SJohn Marino 
70*86d7f5d3SJohn Marino     while(fgets(buffer, len+2, fp) != NULL)
71*86d7f5d3SJohn Marino     {
72*86d7f5d3SJohn Marino 	if ((strncmp(name, buffer, len) == 0) &&
73*86d7f5d3SJohn Marino 	    (buffer[len] == '\n'))
74*86d7f5d3SJohn Marino 	{
75*86d7f5d3SJohn Marino 	    found = 1;
76*86d7f5d3SJohn Marino 	    break;
77*86d7f5d3SJohn Marino 	}
78*86d7f5d3SJohn Marino     }
79*86d7f5d3SJohn Marino     fclose(fp);
80*86d7f5d3SJohn Marino     free(buffer);
81*86d7f5d3SJohn Marino     return found;
82*86d7f5d3SJohn Marino }
83*86d7f5d3SJohn Marino 
84*86d7f5d3SJohn Marino /* Global functions */
85*86d7f5d3SJohn Marino int
check_permission(void)86*86d7f5d3SJohn Marino check_permission(void)
87*86d7f5d3SJohn Marino {
88*86d7f5d3SJohn Marino     FILE *fp;
89*86d7f5d3SJohn Marino     uid_t uid = geteuid();
90*86d7f5d3SJohn Marino     struct passwd *pentry;
91*86d7f5d3SJohn Marino 
92*86d7f5d3SJohn Marino     if (uid==0)
93*86d7f5d3SJohn Marino 	return 1;
94*86d7f5d3SJohn Marino 
95*86d7f5d3SJohn Marino     if ((pentry = getpwuid(uid)) == NULL)
96*86d7f5d3SJohn Marino 	err(EXIT_FAILURE, "cannot access user database");
97*86d7f5d3SJohn Marino 
98*86d7f5d3SJohn Marino     PRIV_START
99*86d7f5d3SJohn Marino 
100*86d7f5d3SJohn Marino     fp = fopen(PERM_PATH "at.allow","r");
101*86d7f5d3SJohn Marino 
102*86d7f5d3SJohn Marino     PRIV_END
103*86d7f5d3SJohn Marino 
104*86d7f5d3SJohn Marino     if (fp != NULL)
105*86d7f5d3SJohn Marino     {
106*86d7f5d3SJohn Marino 	return check_for_user(fp, pentry->pw_name);
107*86d7f5d3SJohn Marino     }
108*86d7f5d3SJohn Marino     else if (errno == ENOENT)
109*86d7f5d3SJohn Marino     {
110*86d7f5d3SJohn Marino 
111*86d7f5d3SJohn Marino 	PRIV_START
112*86d7f5d3SJohn Marino 
113*86d7f5d3SJohn Marino 	fp = fopen(PERM_PATH "at.deny", "r");
114*86d7f5d3SJohn Marino 
115*86d7f5d3SJohn Marino 	PRIV_END
116*86d7f5d3SJohn Marino 
117*86d7f5d3SJohn Marino 	if (fp != NULL)
118*86d7f5d3SJohn Marino 	{
119*86d7f5d3SJohn Marino 	    return !check_for_user(fp, pentry->pw_name);
120*86d7f5d3SJohn Marino 	}
121*86d7f5d3SJohn Marino 	else if (errno != ENOENT)
122*86d7f5d3SJohn Marino 	    warn("at.deny");
123*86d7f5d3SJohn Marino     }
124*86d7f5d3SJohn Marino     else
125*86d7f5d3SJohn Marino 	warn("at.allow");
126*86d7f5d3SJohn Marino     return 0;
127*86d7f5d3SJohn Marino }
128