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