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 ************************************************************************/
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.3 2003/10/04 20:36:48 hmp Exp $ */
20 void login_fbtab(tty, uid, gid)
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:
32 Comments start with a # and extend to the end of the line.
34 Blank lines or lines with only a comment are ignored.
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 "..".
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
50 Problems are reported via the syslog daemon with severity
54 This module uses strtok(3), which may cause conflicts with other
55 uses of that same routine.
58 Wietse Venema (wietse@wzv.win.tue.nl)
59 Eindhoven University of Technology
63 #include <sys/types.h>
72 #include "pathnames.h"
74 void login_protect(char *, char *, int, uid_t
, gid_t
);
75 void login_fbtab(char *tty
, uid_t uid
, gid_t gid
);
77 #define WSPACE " \t\n"
79 /* login_fbtab - apply protections specified in /etc/fbtab or logindevperm */
82 login_fbtab(char *tty
, uid_t uid
, gid_t gid
)
91 if ((fp
= fopen(table
= _PATH_FBTAB
, "r")) == 0
92 && (fp
= fopen(table
= _PATH_LOGINDEVPERM
, "r")) == 0)
95 while (fgets(buf
, sizeof(buf
), fp
)) {
96 if ((cp
= strchr(buf
, '#')))
97 *cp
= 0; /* strip comment */
98 if ((cp
= devname
= strtok(buf
, WSPACE
)) == 0)
99 continue; /* empty or comment */
100 if (strncmp(devname
, _PATH_DEV
, sizeof _PATH_DEV
- 1) != 0
101 || (cp
= strtok((char *) 0, WSPACE
)) == 0
103 || sscanf(cp
, "%o", &prot
) == 0
105 || (prot
& 0777) != prot
106 || (cp
= strtok((char *) 0, WSPACE
)) == 0) {
107 syslog(LOG_ERR
, "%s: bad entry: %s", table
, cp
? cp
: "(null)");
110 if (strcmp(devname
+ 5, tty
) == 0) {
111 for (cp
= strtok(cp
, ":"); cp
; cp
= strtok((char *) 0, ":")) {
112 login_protect(table
, cp
, prot
, uid
, gid
);
119 /* login_protect - protect one device entry */
122 login_protect(char *table
, char *pattern
, int mask
, uid_t uid
, gid_t gid
)
128 if (glob(pattern
, GLOB_NOSORT
, NULL
, &gl
) != 0)
130 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
131 path
= gl
.gl_pathv
[i
];
132 /* clear flags of the device */
133 if (chflags(path
, 0) && errno
!= ENOENT
&& errno
!= EOPNOTSUPP
)
134 syslog(LOG_ERR
, "%s: chflags(%s): %m", table
, path
);
135 if (chmod(path
, mask
) && errno
!= ENOENT
)
136 syslog(LOG_ERR
, "%s: chmod(%s): %m", table
, path
);
137 if (chown(path
, uid
, gid
) && errno
!= ENOENT
)
138 syslog(LOG_ERR
, "%s: chown(%s): %m", table
, path
);