]> granicus.if.org Git - shadow/blob - libmisc/chowntty.c
* libmisc/failure.c: Replace HAVE_UTMPX_H by USE_UTMPX.
[shadow] / libmisc / chowntty.c
1 /*
2  * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2001, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * Copyright (c) 2007 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <config.h>
34
35 #ident "$Id$"
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <grp.h>
42 #include "prototypes.h"
43 #include "defines.h"
44 #include <pwd.h>
45 #include "getdef.h"
46
47 /*
48  *      chown_tty() sets the login tty to be owned by the new user ID
49  *      with TTYPERM modes
50  */
51
52 void chown_tty (const struct passwd *info)
53 {
54         struct group *grent;
55         gid_t gid;
56
57         /*
58          * See if login.defs has some value configured for the port group
59          * ID.  Otherwise, use the user's primary group ID.
60          */
61
62         grent = getgr_nam_gid (getdef_str ("TTYGROUP"));
63         if (NULL != grent) {
64                 gid = grent->gr_gid;
65         } else {
66                 gid = info->pw_gid;
67         }
68
69         /*
70          * Change the permissions on the TTY to be owned by the user with
71          * the group as determined above.
72          */
73
74         if (   (fchown (STDIN_FILENO, info->pw_uid, gid) != 0)
75             || (fchmod (STDIN_FILENO, getdef_num ("TTYPERM", 0600)) != 0)) {
76                 int err = errno;
77
78                 fprintf (stderr,
79                          _("Unable to change owner or mode of tty stdin: %s"),
80                          strerror (err));
81                 SYSLOG ((LOG_WARN,
82                          "unable to change owner or mode of tty stdin for user `%s': %s\n",
83                          info->pw_name, strerror (err)));
84                 if (EROFS != err) {
85                         closelog ();
86                         exit (1);
87                 }
88         }
89 #ifdef __linux__
90         /*
91          * Please don't add code to chown /dev/vcs* to the user logging in -
92          * it's a potential security hole.  I wouldn't like the previous user
93          * to hold the file descriptor open and watch my screen.  We don't
94          * have the *BSD revoke() system call yet, and vhangup() only works
95          * for tty devices (which vcs* is not).  --marekm
96          */
97 #endif
98 }
99