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