e0bfa6f37cca010c0e15deff8f89543c788289d9
[oweals/cde.git] / cde / lib / DtTerm / TermPrim / TermPrimGetPty-clone.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #ifndef lint
24 #ifdef  VERBOSE_REV_INFO
25 static char rcs_id[] = "$TOG: TermPrimGetPty-clone.c /main/7 1998/04/03 17:11:08 mgreess $";
26 #endif  /* VERBOSE_REV_INFO */
27 #endif  /* lint */
28
29 /*                                                                      *
30  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
31  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
32  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
33  * (c) Copyright 1993, 1994 Novell, Inc.                                *
34  */
35
36 #include "TermPrimDebug.h"
37 #include "TermHeader.h"
38 #include <fcntl.h>
39 #include <termios.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <Xm/Xm.h>
43 #ifdef  HP_ARCHITECTURE
44 # define X_INCLUDE_GRP_H
45 #endif  /* HP_ARCHITECTURE */
46 #define X_INCLUDE_UNISTD_H
47 #define XOS_USE_XT_LOCKING
48 #include <X11/Xos_r.h>
49
50 #if defined(__AIX)
51 # define        PTY_CLONE_DEVICE        "/dev/ptc"
52 #elif defined(__linux__)
53 # define        PTY_CLONE_DEVICE        "/dev/ptyc"
54 #endif  /* __AIX */
55
56 static int
57 GetPty(char **ptySlave, char **ptyMaster)
58 {
59     char *c;
60     int ptyFd;
61     int ttyFd;
62
63     *ptyMaster = malloc(strlen(PTY_CLONE_DEVICE) + 1);
64     (void) strcpy(*ptyMaster, PTY_CLONE_DEVICE);
65
66     if (isDebugFSet('p', 10)) {
67 #ifdef  BBA
68 #pragma BBA_IGNORE
69 #endif  /*BBA*/
70         return(-1);
71     }
72
73     if ((ptyFd = open(*ptyMaster, O_RDWR, 0))) {
74         _Xttynameparams tty_buf;
75 #if defined(__linux__)
76         if (c = _XTtyname(ptyFd)) {
77 #else
78         if (c = _XTtyname(ptyFd, tty_buf)) {
79 #endif
80             *ptySlave = malloc(strlen(c) + 1);
81             (void) strcpy(*ptySlave, c);
82             
83             if ((ttyFd = open(*ptySlave, O_RDWR | O_NOCTTY, 0)) < 0) {
84                 /* failure... */
85                 (void) perror(*ptySlave);
86                 (void) close(ptyFd);
87             } else {
88                 /* success...
89                  */
90                 /* close off the pty slave... */
91                 (void) close(ttyFd);
92
93                 /* fix the owner, mode, and group... */
94 #ifdef  HP_ARCHITECTURE
95                 {
96                     struct group *grp;
97                     gid_t gid;
98                     _Xgetgrparams grp_buf;
99
100                     if (grp = _XGetgrnam("tty", grp_buf)) {
101                         gid = grp->gr_gid;
102                     } else {
103                         gid = 0;
104                     }
105                     (void) endgrent();
106                     (void) chown(*ptySlave, getuid(), gid);
107                     (void) chmod(*ptySlave, 0620);
108                 }
109 #else   /* HP_ARCHITECTURE */
110                 (void) chown(*ptySlave, getuid(), getgid());
111                 (void) chmod(*ptySlave, 0622);
112 #endif  /* HP_ARCHITECTURE */
113
114                 /* pty master and slave names are already set.  Return
115                  * the file descriptor...
116                  */
117
118                 return(ptyFd);
119             }
120         } else {
121             /* ttyname on the pty master failed.  This should not happen!... */
122             (void) perror("ttyname");
123             (void) close(ptyFd);
124         }
125     } else {
126         (void) perror(*ptyMaster);
127     }
128     return(-1);
129 }
130
131 /* this is a public wrapper around the previous function that runs the
132  * previous function setuid root...
133  */
134 int
135 _DtTermPrimGetPty(char **ptySlave, char **ptyMaster)
136 {
137     int retValue;
138
139     /* this function needs to be suid root... */
140     (void) _DtTermPrimToggleSuidRoot(True);
141     retValue = GetPty(ptySlave, ptyMaster);
142     /* we now need to turn off setuid root... */
143     (void) _DtTermPrimToggleSuidRoot(False);
144
145     return(retValue);
146 }
147
148 static int
149 SetupPty(char *ptySlave, int ptyFd)
150 {
151 #ifdef  HP_ARCHITECTURE
152     {
153         struct group *grp;
154         gid_t gid;
155         _Xgetgrparams grp_buf;
156
157         if (grp = _XGetgrnam("tty", grp_buf)) {
158             gid = grp->gr_gid;
159         } else {
160             gid = 0;
161         }
162         (void) endgrent();
163         (void) chown(ptySlave, getuid(), gid);
164         (void) chmod(ptySlave, 0620);
165     }
166 #else   /* HP_ARCHITECTURE */
167     (void) chown(ptySlave, getuid(), getgid());
168     (void) chmod(ptySlave, 0622);
169 #endif  /* HP_ARCHITECTURE */
170 }
171     
172 int
173 _DtTermPrimSetupPty(char *ptySlave, int ptyFd)
174 {
175     int retValue;
176
177     /* this function needs to be suid root... */
178     (void) _DtTermPrimToggleSuidRoot(True);
179     retValue = SetupPty(ptySlave, ptyFd);
180     /* we now need to turn off setuid root... */
181     (void) _DtTermPrimToggleSuidRoot(False);
182
183     return(retValue);
184 }
185
186 static void
187 ReleasePty(char *ptySlave)
188 {
189     (void) chown(ptySlave, 0, 0);
190     (void) chmod(ptySlave, 0666);
191 }
192     
193 void
194 _DtTermPrimReleasePty(char *ptySlave)
195 {
196     /* this function needs to be suid root... */
197     (void) _DtTermPrimToggleSuidRoot(True);
198     (void) ReleasePty(ptySlave);
199     /* we now need to turn off setuid root... */
200     (void) _DtTermPrimToggleSuidRoot(False);
201 }
202
203 void
204 _DtTermPrimPtyCleanup()
205 {
206     return;
207 }