Merge commit 'upstream/1.8.1p2'
[debian/sudo] / src / get_pty.c
1 /*
2  * Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/stat.h>
22 #include <sys/ioctl.h>
23 #ifdef HAVE_SYS_STROPTS_H
24 #include <sys/stropts.h>
25 #endif /* HAVE_SYS_STROPTS_H */
26 #include <stdio.h>
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 # include <stddef.h>
30 #else
31 # ifdef HAVE_STDLIB_H
32 #  include <stdlib.h>
33 # endif
34 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif /* HAVE_STRING_H */
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif /* HAVE_STRINGS_H */
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif /* HAVE_UNISTD_H */
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <grp.h>
47 #include <pwd.h>
48
49 #ifdef HAVE_UTIL_H
50 # include <util.h>
51 #endif
52 #ifdef HAVE_PTY_H
53 # include <pty.h>
54 #endif
55
56 #include "sudo.h"
57
58 #if defined(HAVE_OPENPTY)
59 int
60 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
61 {
62     struct group *gr;
63     gid_t ttygid = -1;
64
65     if ((gr = getgrnam("tty")) != NULL)
66         ttygid = gr->gr_gid;
67
68     if (openpty(master, slave, name, NULL, NULL) != 0)
69         return 0;
70     if (chown(name, ttyuid, ttygid) != 0)
71         return 0;
72     return 1;
73 }
74
75 #elif defined(HAVE__GETPTY)
76 int
77 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
78 {
79     char *line;
80
81     /* IRIX-style dynamic ptys (may fork) */
82     line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
83     if (line == NULL)
84         return 0;
85     *slave = open(line, O_RDWR|O_NOCTTY, 0);
86     if (*slave == -1) {
87         close(*master);
88         return 0;
89     }
90     (void) chown(line, ttyuid, -1);
91     strlcpy(name, line, namesz);
92     return 1;
93 }
94 #elif defined(HAVE_GRANTPT)
95 # ifndef HAVE_POSIX_OPENPT
96 static int
97 posix_openpt(int oflag)
98 {
99     int fd;
100
101 #  ifdef _AIX
102     fd = open("/dev/ptc", oflag);
103 #  else
104     fd = open("/dev/ptmx", oflag);
105 #  endif
106     return fd;
107 }
108 # endif /* HAVE_POSIX_OPENPT */
109
110 int
111 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
112 {
113     char *line;
114
115     *master = posix_openpt(O_RDWR|O_NOCTTY);
116     if (*master == -1)
117         return 0;
118
119     (void) grantpt(*master); /* may fork */
120     if (unlockpt(*master) != 0) {
121         close(*master);
122         return 0;
123     }
124     line = ptsname(*master);
125     if (line == NULL) {
126         close(*master);
127         return 0;
128     }
129     *slave = open(line, O_RDWR|O_NOCTTY, 0);
130     if (*slave == -1) {
131         close(*master);
132         return 0;
133     }
134 # if defined(I_PUSH) && !defined(_AIX)
135     ioctl(*slave, I_PUSH, "ptem");      /* pseudo tty emulation module */
136     ioctl(*slave, I_PUSH, "ldterm");    /* line discipline module */
137 # endif
138     (void) chown(line, ttyuid, -1);
139     strlcpy(name, line, namesz);
140     return 1;
141 }
142
143 #else /* Old-style BSD ptys */
144
145 static char line[] = "/dev/ptyXX";
146
147 int
148 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
149 {
150     char *bank, *cp;
151     struct group *gr;
152     gid_t ttygid = -1;
153
154     if ((gr = getgrnam("tty")) != NULL)
155         ttygid = gr->gr_gid;
156
157     for (bank = "pqrs"; *bank != '\0'; bank++) {
158         line[sizeof("/dev/ptyX") - 2] = *bank;
159         for (cp = "0123456789abcdef"; *cp != '\0'; cp++) {
160             line[sizeof("/dev/ptyXX") - 2] = *cp;
161             *master = open(line, O_RDWR|O_NOCTTY, 0);
162             if (*master == -1) {
163                 if (errno == ENOENT)
164                     return 0; /* out of ptys */
165                 continue; /* already in use */
166             }
167             line[sizeof("/dev/p") - 2] = 't';
168             (void) chown(line, ttyuid, ttygid);
169             (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
170 # ifdef HAVE_REVOKE
171             (void) revoke(line);
172 # endif
173             *slave = open(line, O_RDWR|O_NOCTTY, 0);
174             if (*slave != -1) {
175                     strlcpy(name, line, namesz);
176                     return 1; /* success */
177             }
178             (void) close(*master);
179         }
180     }
181     return 0;
182 }
183 #endif /* HAVE_OPENPTY */