kernel: new DEBUG_RACE option. try to provoke race conditions between processes.
[minix.git] / commands / rlogind / rlogind.c
blobf46b894a9fbacce6a05de6d494655cc0584422c6
1 /*
2 rlogind.c
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 */
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"
26 char pty_str[]= "/dev/ptyXX";
27 char tty_str[]= "/dev/ttyXX";
28 char hex_str[16]= "0123456789abcdef";
30 char PATH_UTMP[] = "/etc/utmp"; /* current logins */
31 char PATH_WTMP[] = "/usr/adm/wtmp"; /* login/logout history */
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];
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);
47 int main(int argc, char *argv[])
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;
58 prog_name= argv[0];
60 /* Check if the remote user is allowed in. */
61 authenticate();
63 write(1, "", 1); /* Send the '\0' */
64 confirmed= 1;
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.
71 tcp_urg(1, 1);
73 write(1, "\220", 1);
75 tcp_urg(1, 0);
77 /* Let's look for a pty. */
78 pty_fd= -1;
79 for (i= 'p'; i <= 'z'; i++)
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++)
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;
93 if (pty_fd != -1)
94 break;
96 if (pty_fd == -1)
98 printf("%s: out of ptys\r\n", prog_name);
99 exit(1);
101 tty_str[sizeof(pty_str)-3]= i;
102 tty_str[sizeof(pty_str)-2]= hex_str[j];
104 tty_fd= open(tty_str, O_RDWR);
105 if (tty_fd == -1)
107 printf("%s: unable to open '%s': %s\r\n", prog_name, tty_str,
108 strerror(errno));
109 exit(1);
112 slot= fttyslot(tty_fd);
114 login_pid= fork();
115 if (login_pid == -1)
117 printf("%s: unable to fork: %s\r\n", prog_name,
118 strerror(errno));
119 exit(1);
121 if (login_pid == 0)
123 close(pty_fd);
124 wtmp("", "", tty_str, login_pid, LOGIN_PROCESS, slot);
125 do_child(tty_fd, tty_str);
127 close(tty_fd);
129 write_pid= fork();
130 if (write_pid == -1)
132 printf("%s: unable to fork: %s\r\n", prog_name,
133 strerror(errno));
134 exit(1);
136 if (write_pid == 0)
138 dup2(pty_fd, 0);
139 count= 0;
140 for (;;)
142 if (!count)
144 count= read(0, line, sizeof(line));
145 if (count <= 0)
146 break;
147 lp= line;
149 bytes= write(1, lp, count);
150 if (bytes <= 0 || bytes > count)
151 break;
152 lp += bytes;
153 count -= bytes;
155 kill(getppid(), SIGKILL);
156 dealloc_term(slot, tty_str, login_pid);
157 _exit(1);
160 dup2(pty_fd, 1);
161 count= 0;
162 for (;;)
164 if (!count)
166 count= read(0, line, sizeof(line));
167 if (count <= 0)
168 break;
169 lp= line;
171 tmp_count= count;
172 cp= memchr(lp, 255, count);
173 if (cp)
175 tmp_count= cp-lp;
176 if (tmp_count == 0)
178 tmp_count= do_control(lp, count);
179 if (tmp_count)
181 lp += tmp_count;
182 count -= tmp_count;
183 continue;
187 bytes= write(1, lp, tmp_count);
188 if (bytes <= 0 || bytes > count)
189 break;
190 lp += bytes;
191 count -= bytes;
193 kill(write_pid, SIGKILL);
194 dealloc_term(slot, tty_str, login_pid);
195 return(0);
198 static void do_child(int tty_fd, char *tty_str)
200 int ctty_fd, tst_fd;
201 FILE *tty_file;
202 int sav_errno;
203 char **argp;
205 /* Set up the terminal attributes. */
206 setup_term(tty_fd);
208 /* Let's start the new session. */
209 setsid();
210 ctty_fd= open(tty_str, O_RDWR);
211 if (ctty_fd == -1)
213 printf("%s(do_child): unable to open '%s': %s\r\n",
214 prog_name, tty_str, strerror(errno));
215 exit(1);
217 /* Test if we really got a controlling tty. */
218 tst_fd= open("/dev/tty", O_RDWR);
219 if (tst_fd == -1)
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);
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;
237 /* We reached the point of no return. */
238 close(tst_fd);
239 close(tty_fd);
241 if (ctty_fd != 0)
243 dup2(ctty_fd, 0);
244 close(ctty_fd);
245 ctty_fd= 0;
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)
258 fprintf(tty_file, "%s(do_child): unable to exec login: %s\r\n",
259 prog_name, strerror(sav_errno));
260 fflush(tty_file);
262 _exit(1);
265 static void dealloc_term(int slot, char *tty_str, int pid)
267 wtmp("", "", tty_str, pid, DEAD_PROCESS, slot);
269 /* Finally we reset the owner and mode of the terminal. */
270 chown(tty_str, 0, 0);
271 chmod(tty_str, 0666);
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 */
282 /* Log an event into the UTMP and WTMP files. */
284 struct utmp utmp; /* UTMP/WTMP User Accounting */
285 int fd= -1;
286 int log = 1; /* log in wtmp */
287 char *p;
289 /* Strip the /dev part of the TTY name. */
290 p = strrchr(line, '/');
291 if (p != 0)
292 line= p+1;
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))
300 if (utmp.ut_type != INIT_PROCESS
301 && utmp.ut_type != USER_PROCESS)
302 log= 0;
304 close(fd);
306 if (type == LOGIN_PROCESS) log= 0; /* and don't log this one */
308 /* Clear the utmp record. */
309 memset((void *) &utmp, 0, sizeof(utmp));
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);
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);
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);
333 void fatal(int fd, char *msg, int err)
335 int len;
336 char buf[80], *bp;
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);
349 static void setup_term(int fd)
351 char *cp, *speed;
352 struct termios tt;
353 speed_t spd;
354 int num;
355 char *check;
357 cp= strchr(term, '/');
358 if (cp)
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')
370 cfsetospeed(&tt, spd);
371 cfsetispeed(&tt, spd);
373 tcsetattr(fd, TCSANOW, &tt);
375 env[0]= term;
376 env[1]= 0;
379 static speed_t num2speed(int num)
381 static struct
383 int num;
384 speed_t value;
385 } speed_table[]=
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 },
394 int i;
396 for (i= 0; speed_table[i].num != -1; i++)
398 if (speed_table[i].num == num)
399 return (speed_table[i].value);
401 return B0;
404 static int do_control(char *cp, int cnt)
406 char buf[20];
407 struct winsize winsize;
409 if (cnt > sizeof(buf))
410 cnt= sizeof(buf);
412 memcpy(buf, cp, cnt);
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;
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;
426 /* Let's fetch a winsize structure. */
427 if (cnt < 4 + sizeof(winsize))
428 readall(buf+cnt, 4 + sizeof(winsize) - cnt);
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);
443 static void readall(char *buf, int cnt)
445 int res;
447 while(cnt)
449 res= read(0, buf, cnt);
450 if (res <= 0)
451 return;
452 buf += cnt;
453 cnt -= res;