1 /************************************************************************
2 * Copyright 1995 by Wietse Venema. All rights reserved.
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.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that this entire copyright notice is duplicated in all such
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 ************************************************************************/
18 void login_fbtab(tty, uid, gid)
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:
30 Comments start with a # and extend to the end of the line.
32 Blank lines or lines with only a comment are ignored.
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 "..".
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
48 Problems are reported via the syslog daemon with severity
54 Wietse Venema (wietse@wzv.win.tue.nl)
55 Eindhoven University of Technology
61 RCSID("$Id: login_fbtab.c,v 1.14 1999/09/16 20:37:24 assar Exp $");
63 void login_protect (char *, char *, int, uid_t, gid_t);
64 void login_fbtab (char *tty, uid_t uid, gid_t gid);
66 #define WSPACE " \t\n"
68 /* login_fbtab - apply protections specified in /etc/fbtab or logindevperm */
71 login_fbtab(char *tty, uid_t uid, gid_t gid)
81 if ((fp = fopen(table = _PATH_FBTAB, "r")) == 0
82 && (fp = fopen(table = _PATH_LOGINDEVPERM, "r")) == 0)
85 while (fgets(buf, sizeof(buf), fp)) {
86 if ((cp = strchr(buf, '#')) != 0)
87 *cp = 0; /* strip comment */
89 if ((cp = devname = strtok_r(buf, WSPACE, &foo)) == 0)
90 continue; /* empty or comment */
91 if (strncmp(devname, "/dev/", 5) != 0
92 || (cp = strtok_r(NULL, WSPACE, &foo)) == 0
94 || sscanf(cp, "%o", &prot) == 0
96 || (prot & 0777) != prot
97 || (cp = strtok_r(NULL, WSPACE, &foo)) == 0) {
98 syslog(LOG_ERR, "%s: bad entry: %s", table, cp ? cp : "(null)");
101 if (strcmp(devname + 5, tty) == 0) {
103 for (cp = strtok_r(cp, ":", &foo);
105 cp = strtok_r(NULL, ":", &foo)) {
106 login_protect(table, cp, prot, uid, gid);
113 /* login_protect - protect one device entry */
116 login_protect(char *table, char *path, int mask, uid_t uid, gid_t gid)
119 int pathlen = strlen(path);
123 if (strcmp("/*", path + pathlen - 2) != 0) {
124 if (chmod(path, mask) && errno != ENOENT)
125 syslog(LOG_ERR, "%s: chmod(%s): %m", table, path);
126 if (chown(path, uid, gid) && errno != ENOENT)
127 syslog(LOG_ERR, "%s: chown(%s): %m", table, path);
129 strlcpy (buf, path, sizeof(buf));
130 if (sizeof(buf) > pathlen)
131 buf[pathlen - 2] = '\0';
132 /* Solaris evidently operates on the directory as well */
133 login_protect(table, buf, mask | ((mask & 0444) >> 2), uid, gid);
134 if ((dir = opendir(buf)) == 0) {
135 syslog(LOG_ERR, "%s: opendir(%s): %m", table, path);
137 if (sizeof(buf) > pathlen) {
138 buf[pathlen - 2] = '/';
139 buf[pathlen - 1] = '\0';
142 while ((ent = readdir(dir)) != 0) {
143 if (strcmp(ent->d_name, ".") != 0
144 && strcmp(ent->d_name, "..") != 0) {
145 strlcpy (buf + pathlen - 1,
147 sizeof(buf) - (pathlen + 1));
148 login_protect(table, buf, mask, uid, gid);