2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
14 static char sccsid[] = "@(#)rexecd.c 5.4 (Berkeley) 5/9/86";
17 #include <sys/ioctl.h>
18 #include <sys/param.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
31 struct passwd *getpwnam();
32 char *crypt(), *rindex(), *strncat(), *sprintf();
37 * remote execute server:
48 struct sockaddr_in from;
51 fromlen = sizeof (from);
52 if (getpeername(0, &from, &fromlen) < 0) {
53 fprintf(stderr, "%s: ", argv[0]);
54 perror("getpeername");
60 char username[20] = "USER=";
61 char homedir[64] = "HOME=";
62 char shell[64] = "SHELL=";
64 {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
67 struct sockaddr_in asin = { AF_INET };
71 struct sockaddr_in *fromp;
73 char cmdbuf[NCARGS+1], *cp, *namep;
74 char user[16], pass[16];
80 char buf[BUFSIZ], sig;
83 (void) signal(SIGINT, SIG_DFL);
84 (void) signal(SIGQUIT, SIG_DFL);
85 (void) signal(SIGTERM, SIG_DFL);
87 { int t = open("/dev/tty", 2);
89 ioctl(t, TIOCNOTTY, (char *)0);
101 if (read(f, &c, 1) != 1)
105 port = port * 10 + c - '0';
109 s = socket(AF_INET, SOCK_STREAM, 0);
112 if (bind(s, &asin, sizeof (asin)) < 0)
115 fromp->sin_port = htons((u_short)port);
116 if (connect(s, fromp, sizeof (*fromp)) < 0)
120 getstr(user, sizeof(user), "username");
121 getstr(pass, sizeof(pass), "password");
122 getstr(cmdbuf, sizeof(cmdbuf), "command");
124 pwd = getpwnam(user);
126 error("Login incorrect.\n");
130 if (*pwd->pw_passwd != '\0') {
131 namep = crypt(pass, pwd->pw_passwd);
132 if (strcmp(namep, pwd->pw_passwd)) {
133 error("Password incorrect.\n");
137 if (chdir(pwd->pw_dir) < 0) {
138 error("No remote directory.\n");
141 (void) write(2, "\0", 1);
146 error("Try again.\n");
150 (void) close(0); (void) close(1); (void) close(2);
151 (void) close(f); (void) close(pv[1]);
152 readfrom = (1L<<s) | (1L<<pv[0]);
153 ioctl(pv[1], FIONBIO, (char *)&one);
154 /* should set s nbio! */
157 (void) select(16, &ready, (fd_set *)0,
158 (fd_set *)0, (struct timeval *)0);
159 if (ready & (1L<<s)) {
160 if (read(s, &sig, 1) <= 0)
161 readfrom &= ~(1L<<s);
165 if (ready & (1L<<pv[0])) {
166 cc = read(pv[0], buf, sizeof (buf));
169 readfrom &= ~(1L<<pv[0]);
171 (void) write(s, buf, cc);
176 setpgrp(0, getpid());
177 (void) close(s); (void)close(pv[0]);
180 if (*pwd->pw_shell == '\0')
181 pwd->pw_shell = "/bin/sh";
184 (void) setgid((gid_t)pwd->pw_gid);
185 initgroups(pwd->pw_name, pwd->pw_gid);
186 (void) setuid((uid_t)pwd->pw_uid);
188 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
189 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
190 strncat(username, pwd->pw_name, sizeof(username)-6);
191 cp = rindex(pwd->pw_shell, '/');
196 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
197 perror(pwd->pw_shell);
202 error(fmt, a1, a2, a3)
209 (void) sprintf(buf+1, fmt, a1, a2, a3);
210 (void) write(2, buf, strlen(buf));
213 getstr(buf, cnt, err)
221 if (read(0, &c, 1) != 1)
225 error("%s too long\n", err);