]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rexecd/rexecd.c
This commit was generated by cvs2svn to compensate for changes in r55924,
[FreeBSD/FreeBSD.git] / libexec / rexecd / rexecd.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)rexecd.c    8.1 (Berkeley) 6/4/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <sys/time.h>
52
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55
56 #include <err.h>
57 #include <netdb.h>
58 #include <libutil.h>
59 #include <paths.h>
60 #include <pwd.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <skey.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <syslog.h>
67 #include <unistd.h>
68
69 char    username[MAXLOGNAME + 5 + 1] = "USER=";
70 char    homedir[MAXPATHLEN + 5 + 1] = "HOME=";
71 char    shell[MAXPATHLEN + 6 + 1] = "SHELL=";
72 char    path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
73 char    *envinit[] =
74             {homedir, shell, path, username, 0};
75 char    **environ;
76 char    remote[MAXHOSTNAMELEN];
77
78 struct  sockaddr_in asin = { AF_INET };
79
80 void doit __P((int, struct sockaddr_in *));
81 void getstr __P((char *, int, char *));
82 /*VARARGS1*/
83 void error __P(());
84
85 /*
86  * remote execute server:
87  *      username\0
88  *      password\0
89  *      command\0
90  *      data
91  */
92 /*ARGSUSED*/
93 int
94 main(argc, argv)
95         int argc;
96         char **argv;
97 {
98         struct sockaddr_in from;
99         int fromlen;
100         struct hostent *hp;
101
102         openlog(argv[0], LOG_PID, LOG_AUTH);
103         fromlen = sizeof (from);
104         if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
105                 err(1, "getpeername");
106
107         realhostname(remote, sizeof(remote) - 1, &from.sin_addr);
108
109         doit(0, &from);
110         return(0);
111 }
112
113 void
114 doit(f, fromp)
115         int f;
116         struct sockaddr_in *fromp;
117 {
118         FILE *fp;
119         char cmdbuf[NCARGS+1], *cp, *namep;
120 #ifdef SKEY
121         char user[16], pass[100];
122 #else /* SKEY */
123         char user[16], pass[16];
124 #endif /* SKEY */
125         struct passwd *pwd;
126         int s;
127         u_short port;
128         int pv[2], pid, ready, readfrom, cc;
129         char buf[BUFSIZ], sig;
130         int one = 1;
131
132         (void) signal(SIGINT, SIG_DFL);
133         (void) signal(SIGQUIT, SIG_DFL);
134         (void) signal(SIGTERM, SIG_DFL);
135 #ifdef DEBUG
136         { int t = open(_PATH_TTY, 2);
137           if (t >= 0) {
138                 ioctl(t, TIOCNOTTY, (char *)0);
139                 (void) close(t);
140           }
141         }
142 #endif
143         dup2(f, 0);
144         dup2(f, 1);
145         dup2(f, 2);
146         (void) alarm(60);
147         port = 0;
148         for (;;) {
149                 char c;
150                 if (read(f, &c, 1) != 1)
151                         exit(1);
152                 if (c == 0)
153                         break;
154                 port = port * 10 + c - '0';
155         }
156         (void) alarm(0);
157         if (port != 0) {
158                 s = socket(AF_INET, SOCK_STREAM, 0);
159                 if (s < 0)
160                         exit(1);
161                 if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
162                         exit(1);
163                 (void) alarm(60);
164                 fromp->sin_port = htons(port);
165                 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
166                         exit(1);
167                 (void) alarm(0);
168         }
169         getstr(user, sizeof(user), "username");
170         getstr(pass, sizeof(pass), "password");
171         getstr(cmdbuf, sizeof(cmdbuf), "command");
172         setpwent();
173         pwd = getpwnam(user);
174         if (pwd == NULL) {
175                 error("Login incorrect.\n");
176                 exit(1);
177         }
178         endpwent();
179         if (*pwd->pw_passwd != '\0') {
180 #ifdef SKEY
181                 namep = skey_crypt(pass, pwd->pw_passwd, pwd,
182                                    skeyaccess(user, NULL, remote, NULL));
183 #else /* SKEY */
184                 namep = crypt(pass, pwd->pw_passwd);
185 #endif /* SKEY */
186                 if (strcmp(namep, pwd->pw_passwd)) {
187                         syslog(LOG_ERR, "LOGIN FAILURE from %s, %s",
188                                remote, user);
189                         error("Login incorrect.\n");
190                         exit(1);
191                 }
192         }
193
194         if (pwd->pw_uid == 0 || *pwd->pw_passwd == '\0' ||
195             (pwd->pw_expire && time(NULL) >= pwd->pw_expire)) {
196                 syslog(LOG_ERR, "%s LOGIN REFUSED from %s", user, remote);
197                 error("Login incorrect.\n");
198                 exit(1);
199         }
200
201         if ((fp = fopen(_PATH_FTPUSERS, "r")) != NULL) {
202                 while (fgets(buf, sizeof(buf), fp) != NULL) {
203                         if ((cp = index(buf, '\n')) != NULL)
204                                 *cp = '\0';
205                         if (strcmp(buf, pwd->pw_name) == 0) {
206                                 syslog(LOG_ERR, "%s LOGIN REFUSED from %s",
207                                        user, remote);
208                                 error("Login incorrect.\n");
209                                 exit(1);
210                         }
211                 }
212         }
213         (void) fclose(fp);
214
215         syslog(LOG_INFO, "login from %s as %s", remote, user);
216
217         (void) write(2, "\0", 1);
218         if (port) {
219                 (void) pipe(pv);
220                 pid = fork();
221                 if (pid == -1)  {
222                         error("Try again.\n");
223                         exit(1);
224                 }
225                 if (pid) {
226                         (void) close(0); (void) close(1); (void) close(2);
227                         (void) close(f); (void) close(pv[1]);
228                         readfrom = (1<<s) | (1<<pv[0]);
229                         ioctl(pv[1], FIONBIO, (char *)&one);
230                         /* should set s nbio! */
231                         do {
232                                 ready = readfrom;
233                                 (void) select(16, (fd_set *)&ready,
234                                     (fd_set *)NULL, (fd_set *)NULL,
235                                     (struct timeval *)NULL);
236                                 if (ready & (1<<s)) {
237                                         if (read(s, &sig, 1) <= 0)
238                                                 readfrom &= ~(1<<s);
239                                         else
240                                                 killpg(pid, sig);
241                                 }
242                                 if (ready & (1<<pv[0])) {
243                                         cc = read(pv[0], buf, sizeof (buf));
244                                         if (cc <= 0) {
245                                                 shutdown(s, 1+1);
246                                                 readfrom &= ~(1<<pv[0]);
247                                         } else
248                                                 (void) write(s, buf, cc);
249                                 }
250                         } while (readfrom);
251                         exit(0);
252                 }
253                 setpgrp(0, getpid());
254                 (void) close(s); (void)close(pv[0]);
255                 dup2(pv[1], 2);
256         }
257         if (*pwd->pw_shell == '\0')
258                 pwd->pw_shell = _PATH_BSHELL;
259         if (f > 2)
260                 (void) close(f);
261         if (setlogin(pwd->pw_name) < 0)
262                 syslog(LOG_ERR, "setlogin() failed: %m");
263         (void) setgid((gid_t)pwd->pw_gid);
264         initgroups(pwd->pw_name, pwd->pw_gid);
265         (void) setuid((uid_t)pwd->pw_uid);
266         (void)strcat(path, _PATH_DEFPATH);
267         environ = envinit;
268         strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
269         strncat(shell, pwd->pw_shell, sizeof(shell)-7);
270         strncat(username, pwd->pw_name, sizeof(username)-6);
271         cp = strrchr(pwd->pw_shell, '/');
272         if (cp)
273                 cp++;
274         else
275                 cp = pwd->pw_shell;
276         if (chdir(pwd->pw_dir) < 0) {
277                 error("No remote directory.\n");
278                 exit(1);
279         }
280         execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
281         err(1, "%s", pwd->pw_shell);
282 }
283
284 /*VARARGS1*/
285 void
286 error(fmt, a1, a2, a3)
287         char *fmt;
288         int a1, a2, a3;
289 {
290         char buf[BUFSIZ];
291
292         buf[0] = 1;
293         (void) snprintf(buf+1, sizeof(buf) - 1, fmt, a1, a2, a3);
294         (void) write(2, buf, strlen(buf));
295 }
296
297 void
298 getstr(buf, cnt, err)
299         char *buf;
300         int cnt;
301         char *err;
302 {
303         char c;
304
305         do {
306                 if (read(0, &c, 1) != 1)
307                         exit(1);
308                 *buf++ = c;
309                 if (--cnt == 0) {
310                         error("%s too long\n", err);
311                         exit(1);
312                 }
313         } while (c != 0);
314 }