xref: /dragonfly/usr.bin/login/login_fbtab.c (revision 6e285212)
1 /************************************************************************
2 * Copyright 1995 by Wietse Venema.  All rights reserved.
3 *
4 * This material was originally written and compiled by Wietse Venema at
5 * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
6 * 1992, 1993, 1994 and 1995.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that this entire copyright notice is duplicated in all such
10 * copies.
11 *
12 * This software is provided "as is" and without any expressed or implied
13 * warranties, including, without limitation, the implied warranties of
14 * merchantibility and fitness for any particular purpose.
15 ************************************************************************/
16 /* $FreeBSD: src/usr.bin/login/login_fbtab.c,v 1.6.2.4 2001/12/14 10:58:18 rwatson Exp $ */
17 /* $DragonFly: src/usr.bin/login/login_fbtab.c,v 1.2 2003/06/17 04:29:28 dillon Exp $ */
18 /*
19     SYNOPSIS
20 	void login_fbtab(tty, uid, gid)
21 	char *tty;
22 	uid_t uid;
23 	gid_t gid;
24 
25     DESCRIPTION
26 	This module implements device security as described in the
27 	SunOS 4.1.x fbtab(5) and SunOS 5.x logindevperm(4) manual
28 	pages. The program first looks for /etc/fbtab. If that file
29 	cannot be opened it attempts to process /etc/logindevperm.
30 	We expect entries with the folowing format:
31 
32 	    Comments start with a # and extend to the end of the line.
33 
34 	    Blank lines or lines with only a comment are ignored.
35 
36 	    All other lines consist of three fields delimited by
37 	    whitespace: a login device (/dev/console), an octal
38 	    permission number (0600), and a ":"-delimited list of
39 	    devices (/dev/kbd:/dev/mouse). All device names are
40 	    absolute paths. A path that ends in "*" refers to all
41 	    directory entries except "." and "..".
42 
43 	    If the tty argument (relative path) matches a login device
44 	    name (absolute path), the permissions of the devices in the
45 	    ":"-delimited list are set as specified in the second
46 	    field, and their ownership is changed to that of the uid
47 	    and gid arguments.
48 
49     DIAGNOSTICS
50 	Problems are reported via the syslog daemon with severity
51 	LOG_ERR.
52 
53     BUGS
54 	This module uses strtok(3), which may cause conflicts with other
55 	uses of that same routine.
56 
57     AUTHOR
58 	Wietse Venema (wietse@wzv.win.tue.nl)
59 	Eindhoven University of Technology
60 	The Netherlands
61  */
62 
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <stdio.h>
66 #include <syslog.h>
67 #include <string.h>
68 #include <errno.h>
69 #include <glob.h>
70 #include <paths.h>
71 #include <unistd.h>
72 #include "pathnames.h"
73 
74 void	login_protect	__P((char *, char *, int, uid_t, gid_t));
75 void	login_fbtab	__P((char *tty, uid_t uid, gid_t gid));
76 
77 #define	WSPACE		" \t\n"
78 
79 /* login_fbtab - apply protections specified in /etc/fbtab or logindevperm */
80 
81 void
82 login_fbtab(tty, uid, gid)
83 char   *tty;
84 uid_t   uid;
85 gid_t   gid;
86 {
87     FILE   *fp;
88     char    buf[BUFSIZ];
89     char   *devname;
90     char   *cp;
91     int     prot;
92     char *table;
93 
94     if ((fp = fopen(table = _PATH_FBTAB, "r")) == 0
95     && (fp = fopen(table = _PATH_LOGINDEVPERM, "r")) == 0)
96 	return;
97 
98     while (fgets(buf, sizeof(buf), fp)) {
99 	if ((cp = strchr(buf, '#')))
100 	    *cp = 0;				/* strip comment */
101 	if ((cp = devname = strtok(buf, WSPACE)) == 0)
102 	    continue;				/* empty or comment */
103 	if (strncmp(devname, _PATH_DEV, sizeof _PATH_DEV - 1) != 0
104 	       || (cp = strtok((char *) 0, WSPACE)) == 0
105 	       || *cp != '0'
106 	       || sscanf(cp, "%o", &prot) == 0
107 	       || prot == 0
108 	       || (prot & 0777) != prot
109 	       || (cp = strtok((char *) 0, WSPACE)) == 0) {
110 	    syslog(LOG_ERR, "%s: bad entry: %s", table, cp ? cp : "(null)");
111 	    continue;
112 	}
113 	if (strcmp(devname + 5, tty) == 0) {
114 	    for (cp = strtok(cp, ":"); cp; cp = strtok((char *) 0, ":")) {
115 		login_protect(table, cp, prot, uid, gid);
116 	    }
117 	}
118     }
119     fclose(fp);
120 }
121 
122 /* login_protect - protect one device entry */
123 
124 void
125 login_protect(table, pattern, mask, uid, gid)
126 	char	*table;
127 	char	*pattern;
128 	int	mask;
129 	uid_t	uid;
130 	gid_t	gid;
131 {
132 	glob_t  gl;
133 	char	*path;
134 	int     i;
135 
136 	if (glob(pattern, GLOB_NOSORT, NULL, &gl) != 0)
137 		return;
138 	for (i = 0; i < gl.gl_pathc; i++) {
139 		path = gl.gl_pathv[i];
140 		/* clear flags of the device */
141 		if (chflags(path, 0) && errno != ENOENT && errno != EOPNOTSUPP)
142 			syslog(LOG_ERR, "%s: chflags(%s): %m", table, path);
143 		if (chmod(path, mask) && errno != ENOENT)
144 			syslog(LOG_ERR, "%s: chmod(%s): %m", table, path);
145 		if (chown(path, uid, gid) && errno != ENOENT)
146 			syslog(LOG_ERR, "%s: chown(%s): %m", table, path);
147 	}
148 	globfree(&gl);
149 }
150