Imported Upstream version 1.7.6p1
[debian/sudo] / get_pty.c
1 /*
2  * Copyright (c) 2009-2010 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(master, slave, name, namesz, ttyuid)
61     int *master;
62     int *slave;
63     char *name;
64     size_t namesz;
65     uid_t ttyuid;
66 {
67     struct group *gr;
68     gid_t ttygid = -1;
69
70     if ((gr = sudo_getgrnam("tty")) != NULL) {
71         ttygid = gr->gr_gid;
72         gr_delref(gr);
73     }
74
75     if (openpty(master, slave, name, NULL, NULL) != 0)
76         return 0;
77     if (chown(name, ttyuid, ttygid) != 0)
78         return 0;
79     return 1;
80 }
81
82 #elif defined(HAVE__GETPTY)
83 int
84 get_pty(master, slave, name, namesz, ttyuid)
85     int *master;
86     int *slave;
87     char *name;
88     size_t namesz;
89     uid_t ttyuid;
90 {
91     char *line;
92
93     /* IRIX-style dynamic ptys (may fork) */
94     line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
95     if (line == NULL)
96         return 0;
97     *slave = open(line, O_RDWR|O_NOCTTY, 0);
98     if (*slave == -1) {
99         close(*master);
100         return 0;
101     }
102     (void) chown(line, ttyuid, -1);
103     strlcpy(name, line, namesz);
104     return 1;
105 }
106 #elif defined(HAVE_GRANTPT)
107 # ifndef HAVE_POSIX_OPENPT
108 static int
109 posix_openpt(oflag)
110     int oflag;
111 {
112     int fd;
113
114 #  ifdef _AIX
115     fd = open("/dev/ptc", oflag);
116 #  else
117     fd = open("/dev/ptmx", oflag);
118 #  endif
119     return fd;
120 }
121 # endif /* HAVE_POSIX_OPENPT */
122
123 int
124 get_pty(master, slave, name, namesz, ttyuid)
125     int *master;
126     int *slave;
127     char *name;
128     size_t namesz;
129     uid_t ttyuid;
130 {
131     char *line;
132
133     *master = posix_openpt(O_RDWR|O_NOCTTY);
134     if (*master == -1)
135         return 0;
136
137     (void) grantpt(*master); /* may fork */
138     if (unlockpt(*master) != 0) {
139         close(*master);
140         return 0;
141     }
142     line = ptsname(*master);
143     if (line == NULL) {
144         close(*master);
145         return 0;
146     }
147     *slave = open(line, O_RDWR|O_NOCTTY, 0);
148     if (*slave == -1) {
149         close(*master);
150         return 0;
151     }
152 # if defined(I_PUSH) && !defined(_AIX)
153     ioctl(*slave, I_PUSH, "ptem");      /* pseudo tty emulation module */
154     ioctl(*slave, I_PUSH, "ldterm");    /* line discipline module */
155 # endif
156     (void) chown(line, ttyuid, -1);
157     strlcpy(name, line, namesz);
158     return 1;
159 }
160
161 #else /* Old-style BSD ptys */
162
163 static char line[] = "/dev/ptyXX";
164
165 int
166 get_pty(master, slave, name, namesz, ttyuid)
167     int *master;
168     int *slave;
169     char *name;
170     size_t namesz;
171     uid_t ttyuid;
172 {
173     char *bank, *cp;
174     struct group *gr;
175     gid_t ttygid = -1;
176
177     if ((gr = sudo_getgrnam("tty")) != NULL)
178         ttygid = gr->gr_gid;
179
180     for (bank = "pqrs"; *bank != '\0'; bank++) {
181         line[sizeof("/dev/ptyX") - 2] = *bank;
182         for (cp = "0123456789abcdef"; *cp != '\0'; cp++) {
183             line[sizeof("/dev/ptyXX") - 2] = *cp;
184             *master = open(line, O_RDWR|O_NOCTTY, 0);
185             if (*master == -1) {
186                 if (errno == ENOENT)
187                     return 0; /* out of ptys */
188                 continue; /* already in use */
189             }
190             line[sizeof("/dev/p") - 2] = 't';
191             (void) chown(line, ttyuid, ttygid);
192             (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
193 # ifdef HAVE_REVOKE
194             (void) revoke(line);
195 # endif
196             *slave = open(line, O_RDWR|O_NOCTTY, 0);
197             if (*slave != -1) {
198                     strlcpy(name, line, namesz);
199                     return 1; /* success */
200             }
201             (void) close(*master);
202         }
203     }
204     return 0;
205 }
206 #endif /* HAVE_OPENPTY */