1 | /*
|
---|
2 | rlogind.c
|
---|
3 |
|
---|
4 | Created: by Philip Homburg <philip@cs.vu.nl>
|
---|
5 | Log: Utmp improvement by Kees Bot <kjb@cs.vu.nl>
|
---|
6 | Split to compile easier on i86 by kjb
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include <sys/types.h>
|
---|
10 | #include <sys/stat.h>
|
---|
11 | #include <sys/ioctl.h>
|
---|
12 | #include <errno.h>
|
---|
13 | #include <fcntl.h>
|
---|
14 | #include <signal.h>
|
---|
15 | #include <stdio.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include <termios.h>
|
---|
19 | #include <time.h>
|
---|
20 | #include <unistd.h>
|
---|
21 | #include <utmp.h>
|
---|
22 | #include <net/hton.h>
|
---|
23 | #define EXTERN
|
---|
24 | #include "rlogind.h"
|
---|
25 |
|
---|
26 | char pty_str[]= "/dev/ptyXX";
|
---|
27 | char tty_str[]= "/dev/ttyXX";
|
---|
28 | char hex_str[16]= "0123456789abcdef";
|
---|
29 |
|
---|
30 | char PATH_UTMP[] = "/etc/utmp"; /* current logins */
|
---|
31 | char PATH_WTMP[] = "/usr/adm/wtmp"; /* login/logout history */
|
---|
32 |
|
---|
33 | char term[64]= "TERM=";
|
---|
34 | #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation. */
|
---|
35 | int confirmed= 0;
|
---|
36 | char *env[2];
|
---|
37 | char *args[10];
|
---|
38 |
|
---|
39 | static void do_child(int tty_fd, char *tty_str);
|
---|
40 | static void dealloc_term(int slot, char *tty_str, int pid);
|
---|
41 | static void wtmp(char *user, char *id, char *line, int pid, int type, int slot);
|
---|
42 | static void setup_term(int fd);
|
---|
43 | static speed_t num2speed(int num);
|
---|
44 | static int do_control(char *buf, int cnt);
|
---|
45 | static void readall(char *buf, int cnt);
|
---|
46 |
|
---|
47 | int main(int argc, char *argv[])
|
---|
48 | {
|
---|
49 | int error;
|
---|
50 | int i, j= 0;
|
---|
51 | int tty_fd, pty_fd;
|
---|
52 | int login_pid, write_pid;
|
---|
53 | int count, bytes, tmp_count;
|
---|
54 | char *lp= 0, *cp;
|
---|
55 | struct stat struct_stat;
|
---|
56 | int slot;
|
---|
57 |
|
---|
58 | prog_name= argv[0];
|
---|
59 |
|
---|
60 | /* Check if the remote user is allowed in. */
|
---|
61 | authenticate();
|
---|
62 |
|
---|
63 | write(1, "", 1); /* Send the '\0' */
|
---|
64 | confirmed= 1;
|
---|
65 |
|
---|
66 | /* We try to convince the other side not the do ^S/^Q, the rlogin
|
---|
67 | * protocol indicates the we only send this when XOFF is turned off
|
---|
68 | * but we don't know when this happens so we tell the other side that
|
---|
69 | * it is turned off.
|
---|
70 | */
|
---|
71 | tcp_urg(1, 1);
|
---|
72 |
|
---|
73 | write(1, "\220", 1);
|
---|
74 |
|
---|
75 | tcp_urg(1, 0);
|
---|
76 |
|
---|
77 | /* Let's look for a pty. */
|
---|
78 | pty_fd= -1;
|
---|
79 | for (i= 'p'; i <= 'z'; i++)
|
---|
80 | {
|
---|
81 | pty_str[sizeof(pty_str)-3]= i;
|
---|
82 | pty_str[sizeof(pty_str)-2]= '0';
|
---|
83 | error= stat(pty_str, &struct_stat);
|
---|
84 | if (error == -1)
|
---|
85 | continue;
|
---|
86 | for (j= 0; j < 16; j++)
|
---|
87 | {
|
---|
88 | pty_str[sizeof(pty_str)-2]= hex_str[j];
|
---|
89 | pty_fd= open(pty_str, O_RDWR);
|
---|
90 | if (pty_fd != -1)
|
---|
91 | break;
|
---|
92 | }
|
---|
93 | if (pty_fd != -1)
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | if (pty_fd == -1)
|
---|
97 | {
|
---|
98 | printf("%s: out of ptys\r\n", prog_name);
|
---|
99 | exit(1);
|
---|
100 | }
|
---|
101 | tty_str[sizeof(pty_str)-3]= i;
|
---|
102 | tty_str[sizeof(pty_str)-2]= hex_str[j];
|
---|
103 |
|
---|
104 | tty_fd= open(tty_str, O_RDWR);
|
---|
105 | if (tty_fd == -1)
|
---|
106 | {
|
---|
107 | printf("%s: unable to open '%s': %s\r\n", prog_name, tty_str,
|
---|
108 | strerror(errno));
|
---|
109 | exit(1);
|
---|
110 | }
|
---|
111 |
|
---|
112 | slot= fttyslot(tty_fd);
|
---|
113 |
|
---|
114 | login_pid= fork();
|
---|
115 | if (login_pid == -1)
|
---|
116 | {
|
---|
117 | printf("%s: unable to fork: %s\r\n", prog_name,
|
---|
118 | strerror(errno));
|
---|
119 | exit(1);
|
---|
120 | }
|
---|
121 | if (login_pid == 0)
|
---|
122 | {
|
---|
123 | close(pty_fd);
|
---|
124 | wtmp("", "", tty_str, login_pid, LOGIN_PROCESS, slot);
|
---|
125 | do_child(tty_fd, tty_str);
|
---|
126 | }
|
---|
127 | close(tty_fd);
|
---|
128 |
|
---|
129 | write_pid= fork();
|
---|
130 | if (write_pid == -1)
|
---|
131 | {
|
---|
132 | printf("%s: unable to fork: %s\r\n", prog_name,
|
---|
133 | strerror(errno));
|
---|
134 | exit(1);
|
---|
135 | }
|
---|
136 | if (write_pid == 0)
|
---|
137 | {
|
---|
138 | dup2(pty_fd, 0);
|
---|
139 | count= 0;
|
---|
140 | for (;;)
|
---|
141 | {
|
---|
142 | if (!count)
|
---|
143 | {
|
---|
144 | count= read(0, line, sizeof(line));
|
---|
145 | if (count <= 0)
|
---|
146 | break;
|
---|
147 | lp= line;
|
---|
148 | }
|
---|
149 | bytes= write(1, lp, count);
|
---|
150 | if (bytes <= 0 || bytes > count)
|
---|
151 | break;
|
---|
152 | lp += bytes;
|
---|
153 | count -= bytes;
|
---|
154 | }
|
---|
155 | kill(getppid(), SIGKILL);
|
---|
156 | dealloc_term(slot, tty_str, login_pid);
|
---|
157 | _exit(1);
|
---|
158 | }
|
---|
159 |
|
---|
160 | dup2(pty_fd, 1);
|
---|
161 | count= 0;
|
---|
162 | for (;;)
|
---|
163 | {
|
---|
164 | if (!count)
|
---|
165 | {
|
---|
166 | count= read(0, line, sizeof(line));
|
---|
167 | if (count <= 0)
|
---|
168 | break;
|
---|
169 | lp= line;
|
---|
170 | }
|
---|
171 | tmp_count= count;
|
---|
172 | cp= memchr(lp, 255, count);
|
---|
173 | if (cp)
|
---|
174 | {
|
---|
175 | tmp_count= cp-lp;
|
---|
176 | if (tmp_count == 0)
|
---|
177 | {
|
---|
178 | tmp_count= do_control(lp, count);
|
---|
179 | if (tmp_count)
|
---|
180 | {
|
---|
181 | lp += tmp_count;
|
---|
182 | count -= tmp_count;
|
---|
183 | continue;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | bytes= write(1, lp, tmp_count);
|
---|
188 | if (bytes <= 0 || bytes > count)
|
---|
189 | break;
|
---|
190 | lp += bytes;
|
---|
191 | count -= bytes;
|
---|
192 | }
|
---|
193 | kill(write_pid, SIGKILL);
|
---|
194 | dealloc_term(slot, tty_str, login_pid);
|
---|
195 | return(0);
|
---|
196 | }
|
---|
197 |
|
---|
198 | static void do_child(int tty_fd, char *tty_str)
|
---|
199 | {
|
---|
200 | int ctty_fd, tst_fd;
|
---|
201 | FILE *tty_file;
|
---|
202 | int sav_errno;
|
---|
203 | char **argp;
|
---|
204 |
|
---|
205 | /* Set up the terminal attributes. */
|
---|
206 | setup_term(tty_fd);
|
---|
207 |
|
---|
208 | /* Let's start the new session. */
|
---|
209 | setsid();
|
---|
210 | ctty_fd= open(tty_str, O_RDWR);
|
---|
211 | if (ctty_fd == -1)
|
---|
212 | {
|
---|
213 | printf("%s(do_child): unable to open '%s': %s\r\n",
|
---|
214 | prog_name, tty_str, strerror(errno));
|
---|
215 | exit(1);
|
---|
216 | }
|
---|
217 | /* Test if we really got a controlling tty. */
|
---|
218 | tst_fd= open("/dev/tty", O_RDWR);
|
---|
219 | if (tst_fd == -1)
|
---|
220 | {
|
---|
221 | printf(
|
---|
222 | "%s(do_child): '%s' didn't result in a controlling tty (%s)\r\n",
|
---|
223 | prog_name, tty_str, strerror(errno));
|
---|
224 | exit(1);
|
---|
225 | }
|
---|
226 |
|
---|
227 | argp= args;
|
---|
228 | *argp++= "login";
|
---|
229 | *argp++= "-p";
|
---|
230 | *argp++= "-h";
|
---|
231 | *argp++= hostname;
|
---|
232 | if (authenticated)
|
---|
233 | *argp++= "-f";
|
---|
234 | if (lusername[0] != '\0')
|
---|
235 | *argp++= lusername;
|
---|
236 |
|
---|
237 | /* We reached the point of no return. */
|
---|
238 | close(tst_fd);
|
---|
239 | close(tty_fd);
|
---|
240 |
|
---|
241 | if (ctty_fd != 0)
|
---|
242 | {
|
---|
243 | dup2(ctty_fd, 0);
|
---|
244 | close(ctty_fd);
|
---|
245 | ctty_fd= 0;
|
---|
246 | }
|
---|
247 | dup2(ctty_fd, 1);
|
---|
248 | #if DEBUG
|
---|
249 | fprintf(stderr, "execing login\r\n");
|
---|
250 | #endif
|
---|
251 | dup2(ctty_fd, 2);
|
---|
252 | execve("/bin/login", args, env);
|
---|
253 | if (errno == ENOENT) execve("/usr/bin/login", args, env);
|
---|
254 | sav_errno= errno;
|
---|
255 | tty_file= fdopen(2, "w");
|
---|
256 | if (tty_file)
|
---|
257 | {
|
---|
258 | fprintf(tty_file, "%s(do_child): unable to exec login: %s\r\n",
|
---|
259 | prog_name, strerror(sav_errno));
|
---|
260 | fflush(tty_file);
|
---|
261 | }
|
---|
262 | _exit(1);
|
---|
263 | }
|
---|
264 |
|
---|
265 | static void dealloc_term(int slot, char *tty_str, int pid)
|
---|
266 | {
|
---|
267 | wtmp("", "", tty_str, pid, DEAD_PROCESS, slot);
|
---|
268 |
|
---|
269 | /* Finally we reset the owner and mode of the terminal. */
|
---|
270 | chown(tty_str, 0, 0);
|
---|
271 | chmod(tty_str, 0666);
|
---|
272 | }
|
---|
273 |
|
---|
274 | static void wtmp(
|
---|
275 | char *user, /* name of user */
|
---|
276 | char *id, /* inittab ID */
|
---|
277 | char *line, /* TTY name */
|
---|
278 | int pid, /* PID of process */
|
---|
279 | int type, /* TYPE of entry */
|
---|
280 | int slot) /* slot number in UTMP */
|
---|
281 | {
|
---|
282 | /* Log an event into the UTMP and WTMP files. */
|
---|
283 |
|
---|
284 | struct utmp utmp; /* UTMP/WTMP User Accounting */
|
---|
285 | int fd= -1;
|
---|
286 | int log = 1; /* log in wtmp */
|
---|
287 | char *p;
|
---|
288 |
|
---|
289 | /* Strip the /dev part of the TTY name. */
|
---|
290 | p = strrchr(line, '/');
|
---|
291 | if (p != 0)
|
---|
292 | line= p+1;
|
---|
293 |
|
---|
294 | if (type == DEAD_PROCESS) {
|
---|
295 | /* Don't add a logout entry for just a dying login. */
|
---|
296 | if ((fd = open(PATH_UTMP, O_RDONLY)) < 0) return;
|
---|
297 | if (lseek(fd, (off_t) slot * sizeof(utmp), SEEK_SET) != -1
|
---|
298 | && read(fd, (void *) &utmp, sizeof(utmp)) == sizeof(utmp))
|
---|
299 | {
|
---|
300 | if (utmp.ut_type != INIT_PROCESS
|
---|
301 | && utmp.ut_type != USER_PROCESS)
|
---|
302 | log= 0;
|
---|
303 | }
|
---|
304 | close(fd);
|
---|
305 | }
|
---|
306 | if (type == LOGIN_PROCESS) log= 0; /* and don't log this one */
|
---|
307 |
|
---|
308 | /* Clear the utmp record. */
|
---|
309 | memset((void *) &utmp, 0, sizeof(utmp));
|
---|
310 |
|
---|
311 | /* Enter new values. */
|
---|
312 | strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
|
---|
313 | strncpy(utmp.ut_id, id, sizeof(utmp.ut_id));
|
---|
314 | strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
|
---|
315 | strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
|
---|
316 | utmp.ut_pid = pid;
|
---|
317 | utmp.ut_type = type;
|
---|
318 | utmp.ut_time = time((time_t *)0);
|
---|
319 |
|
---|
320 | if (log) {
|
---|
321 | if ((fd = open(PATH_WTMP, O_WRONLY | O_APPEND)) < 0) return;
|
---|
322 | write(fd, (char *) &utmp, sizeof(struct utmp));
|
---|
323 | close(fd);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* write entry to utmp */
|
---|
327 | if ((fd = open(PATH_UTMP, O_WRONLY)) < 0) return;
|
---|
328 | if (lseek(fd, (off_t) slot * sizeof(utmp), SEEK_SET) != -1)
|
---|
329 | write(fd, (char *) &utmp, sizeof(struct utmp));
|
---|
330 | close(fd);
|
---|
331 | }
|
---|
332 |
|
---|
333 | void fatal(int fd, char *msg, int err)
|
---|
334 | {
|
---|
335 | int len;
|
---|
336 | char buf[80], *bp;
|
---|
337 |
|
---|
338 | bp= buf;
|
---|
339 | if (!confirmed)
|
---|
340 | *bp++= '\1';
|
---|
341 | if (err)
|
---|
342 | len= sprintf(bp, "rlogind: %s: %s.\r\n", msg, strerror(err));
|
---|
343 | else
|
---|
344 | len= sprintf(bp, "rlogind: %s.\r\n", msg);
|
---|
345 | write(fd, buf, bp+len-buf);
|
---|
346 | exit(1);
|
---|
347 | }
|
---|
348 |
|
---|
349 | static void setup_term(int fd)
|
---|
350 | {
|
---|
351 | char *cp, *speed;
|
---|
352 | struct termios tt;
|
---|
353 | speed_t spd;
|
---|
354 | int num;
|
---|
355 | char *check;
|
---|
356 |
|
---|
357 | cp= strchr(term, '/');
|
---|
358 | if (cp)
|
---|
359 | {
|
---|
360 | tcgetattr(fd, &tt);
|
---|
361 | *cp++= '\0';
|
---|
362 | speed= cp;
|
---|
363 | cp= strchr(speed, '/');
|
---|
364 | if (cp)
|
---|
365 | *cp++= '\0';
|
---|
366 | num= strtol(speed, &check, 0);
|
---|
367 | spd= num2speed(num);
|
---|
368 | if (spd != B0 && check[0] == '\0')
|
---|
369 | {
|
---|
370 | cfsetospeed(&tt, spd);
|
---|
371 | cfsetispeed(&tt, spd);
|
---|
372 | }
|
---|
373 | tcsetattr(fd, TCSANOW, &tt);
|
---|
374 | }
|
---|
375 | env[0]= term;
|
---|
376 | env[1]= 0;
|
---|
377 | }
|
---|
378 |
|
---|
379 | static speed_t num2speed(int num)
|
---|
380 | {
|
---|
381 | static struct
|
---|
382 | {
|
---|
383 | int num;
|
---|
384 | speed_t value;
|
---|
385 | } speed_table[]=
|
---|
386 | {
|
---|
387 | { 0, B0, }, { 50, B50, }, { 75, B75, }, { 110, B110, },
|
---|
388 | { 134, B134, }, { 150, B150, }, { 200, B200, }, { 300, B300, },
|
---|
389 | { 600, B600, }, { 1200, B1200, }, { 1800, B1800, },
|
---|
390 | { 2400, B2400, }, { 4800, B4800, }, { 9600, B9600, },
|
---|
391 | { 19200, B19200, }, { 38400, B38400, },
|
---|
392 | { -1, -1 },
|
---|
393 | };
|
---|
394 | int i;
|
---|
395 |
|
---|
396 | for (i= 0; speed_table[i].num != -1; i++)
|
---|
397 | {
|
---|
398 | if (speed_table[i].num == num)
|
---|
399 | return (speed_table[i].value);
|
---|
400 | }
|
---|
401 | return B0;
|
---|
402 | }
|
---|
403 |
|
---|
404 | static int do_control(char *cp, int cnt)
|
---|
405 | {
|
---|
406 | char buf[20];
|
---|
407 | struct winsize winsize;
|
---|
408 |
|
---|
409 | if (cnt > sizeof(buf))
|
---|
410 | cnt= sizeof(buf);
|
---|
411 |
|
---|
412 | memcpy(buf, cp, cnt);
|
---|
413 |
|
---|
414 | /* Let's fetch the first 2 bytes. */
|
---|
415 | if (cnt < 2)
|
---|
416 | readall(buf+cnt, 2-cnt);
|
---|
417 | if ((unsigned char)buf[1] != 255)
|
---|
418 | return 0;
|
---|
419 |
|
---|
420 | /* Let's fetch the first 4 bytes. */
|
---|
421 | if (cnt < 4)
|
---|
422 | readall(buf+cnt, 4-cnt);
|
---|
423 | if (buf[2] != 's' || buf[3] != 's')
|
---|
424 | return 0;
|
---|
425 |
|
---|
426 | /* Let's fetch a winsize structure. */
|
---|
427 | if (cnt < 4 + sizeof(winsize))
|
---|
428 | readall(buf+cnt, 4 + sizeof(winsize) - cnt);
|
---|
429 |
|
---|
430 | memcpy(&winsize, buf+4, sizeof(winsize));
|
---|
431 | winsize.ws_row= ntohs(winsize.ws_row);
|
---|
432 | winsize.ws_col= ntohs(winsize.ws_col);
|
---|
433 | winsize.ws_xpixel= ntohs(winsize.ws_xpixel);
|
---|
434 | winsize.ws_ypixel= ntohs(winsize.ws_ypixel);
|
---|
435 | #if DEBUG
|
---|
436 | fprintf(stderr, "setting window size to %d, %d\r\n", winsize.ws_row,
|
---|
437 | winsize.ws_col);
|
---|
438 | #endif
|
---|
439 | ioctl(1, TIOCSWINSZ, &winsize);
|
---|
440 | return 4 + sizeof(winsize);
|
---|
441 | }
|
---|
442 |
|
---|
443 | static void readall(char *buf, int cnt)
|
---|
444 | {
|
---|
445 | int res;
|
---|
446 |
|
---|
447 | while(cnt)
|
---|
448 | {
|
---|
449 | res= read(0, buf, cnt);
|
---|
450 | if (res <= 0)
|
---|
451 | return;
|
---|
452 | buf += cnt;
|
---|
453 | cnt -= res;
|
---|
454 | }
|
---|
455 | }
|
---|