6b656a1ca8d4559a28f3e422172cb66b99bac392
[oweals/cde.git] / cde / programs / dtlogin / usl_ptty.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 /* $XConsortium: usl_ptty.c /main/3 1995/10/27 16:16:16 rswiston $ */
24 /*                                                                      *
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
26  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
28  * (c) Copyright 1993, 1994 Novell, Inc.                                *
29  */
30 /* usl_ptty.c: routines to perform USL's password authentication  */
31
32 #include        <stdio.h>      /* just so file isn't empty        */
33
34 #if defined (USL)
35
36 #include <sys/byteorder.h>
37 #include <termio.h>
38 #include <stropts.h>
39 #include <sys/stream.h>
40 #include <fcntl.h>
41 #include <utmpx.h>
42 #include <sys/types.h>
43 #include <pwd.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <rx.h>
47
48
49 /* system calls */
50
51 extern  int     close();
52 extern  int     ioctl();
53 extern  int     getmsg();
54
55
56 /* externally defined routines */
57
58 extern  int     grantpt();
59 extern  int     unlockpt();
60 extern  char    *ptsname();
61 extern  char    *strncpy();
62
63
64
65 /* externally defined global variables */
66
67 int     Termios_received = 0; /* received termios structure flag */
68 struct termios  Termios;  /* termios buffer */
69
70 #define RX_SLAVENAMELEN 32
71
72
73 /* locally defined global variables */
74
75 int     Ptty_open;      /* ptty open flag */
76 int     Ptty_fd;        /* master ptty fd */
77 int     Slaveptty_fd;   /* slave ptty fd */
78 char    Slaveptty_name[RX_SLAVENAMELEN]; /* slave ptty device file name */
79
80
81 /*
82  * makepttypair()
83  *
84  * This function creates and opens a master/slave pair of pseudo ttys.
85  * It returns 0 for success, -1 for failure
86  *
87  */
88
89 int
90 makepttypair()
91 {
92         char    *ttyname;               /* file name of slave pseudo tty */
93         pid_t   mypid = getpid();       /* my process id */
94         struct passwd *pwp;             /* password file entry */
95
96         Debug("makettypair\n");
97         if ((Ptty_fd = open("/dev/ptmx", O_RDWR)) == -1) {
98                 Debug ("ptty: open ptmx failed, errno = %d\n", errno);
99                 return(-1);
100         }
101         if (grantpt(Ptty_fd) == -1) {
102                 Debug ("ptty: grantpt failed\n");
103                 return(-1);
104         }
105         if (unlockpt(Ptty_fd) == -1) {
106                 Debug ("ptty: unlockpt failed\n");
107                 return(-1);
108         }
109         if ((ttyname = ptsname(Ptty_fd)) == NULL) {
110                 Debug ("ptty: ptsname failed\n");
111                 return(-1);
112         }
113         (void) strncpy(Slaveptty_name, ttyname, RX_SLAVENAMELEN);
114         if ((Slaveptty_fd = open(Slaveptty_name, O_RDWR)) == -1) {
115                 Debug ("ptty: could not open pts %s, errno = %d\n",
116                          Slaveptty_name, errno);
117                 return(-1);
118         }
119
120         if (ioctl(Slaveptty_fd, I_PUSH, "ptem") == -1) {
121                 Debug ("ptty: push ptem failed\n");
122                 return(-1);
123         }
124         if (ioctl(Slaveptty_fd, I_PUSH, "ldterm") == -1) {
125                 Debug ("ptty: push ldterm failed\n");
126                 return(-1);
127         }
128
129         /*
130          *      Note that since the following ioctl() is performed
131          *      before the pckt module is pushed, it will not be sent back
132          *      to the client.  This is what we want.
133          *
134          */
135
136         if (Termios_received)
137                 if (ioctl(Slaveptty_fd, TCSETS, &Termios) == -1) {
138                         Debug ("ptty: TCSETS failed\n");
139                         return(-1);
140                 }
141
142         if (ioctl(Ptty_fd, I_PUSH, "pckt") == -1) {
143                 Debug ("ptty: push pckt failed\n");
144                 return(-1);
145         }
146
147         /* disable ldterm input processing on server end */
148         if (ioctl(Ptty_fd, TIOCREMOTE, 1) == -1) {
149                 Debug ("ptty: ioctl(TIOCREMOTE) failed\n");
150                 return(-1);
151         }
152
153         Ptty_open = 1;
154
155         Debug ("ptty: pttys created\n");
156
157         return(0);
158 }
159
160 #endif /* USL */