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