Initial import from FreeBSD RELENG_4:
[dragonfly.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: src/libexec/rexecd/rexecd.c,v 1.18.2.3 2002/05/14 22:27:21 des Exp $";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51
52 #include <netinet/in.h>
53
54 #include <err.h>
55 #ifdef DEBUG
56 #include <fcntl.h>
57 #endif
58 #include <libutil.h>
59 #include <paths.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <skey.h>
63 #include <string.h>
64 #include <syslog.h>
65 #include <unistd.h>
66
67 char    username[MAXLOGNAME + 5 + 1] = "USER=";
68 char    homedir[MAXPATHLEN + 5 + 1] = "HOME=";
69 char    shell[MAXPATHLEN + 6 + 1] = "SHELL=";
70 char    path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
71 char    *envinit[] =
72             {homedir, shell, path, username, 0};
73 char    **environ;
74 char    remote[MAXHOSTNAMELEN];
75
76 struct  sockaddr_in asin = { AF_INET };
77
78 void doit __P((int, struct sockaddr_in *));
79 void getstr __P((char *, int, char *));
80 /*VARARGS1*/
81 void error __P(());
82
83 int no_uid_0 = 1;
84
85 void
86 usage(void)
87 {
88         syslog(LOG_ERR, "usage: rexecd [-i]");
89         exit(1);
90 }
91
92 /*
93  * remote execute server:
94  *      username\0
95  *      password\0
96  *      command\0
97  *      data
98  */
99 /*ARGSUSED*/
100 int
101 main(argc, argv)
102         int argc;
103         char **argv;
104 {
105         struct sockaddr_in from;
106         int fromlen;
107         int ch;
108
109         openlog("rexecd", LOG_PID, LOG_AUTH);
110
111         while ((ch = getopt(argc, argv, "i")) != -1)
112           switch (ch) {
113           case 'i':
114                 no_uid_0 = 0;
115                 break;
116           default:
117                 usage();
118           }
119         argc -= optind;
120         argv += optind;
121
122         fromlen = sizeof (from);
123         if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
124                 err(1, "getpeername");
125
126         realhostname(remote, sizeof(remote) - 1, &from.sin_addr);
127
128         doit(0, &from);
129         return(0);
130 }
131
132 void
133 doit(f, fromp)
134         int f;
135         struct sockaddr_in *fromp;
136 {
137         FILE *fp;
138         char cmdbuf[NCARGS+1], *cp, *namep;
139 #ifdef SKEY
140         char user[16], pass[100];
141 #else /* SKEY */
142         char user[16], pass[16];
143 #endif /* SKEY */
144         struct passwd *pwd;
145         int s;
146         u_short port;
147         int pv[2], pid, ready, readfrom, cc;
148         char buf[BUFSIZ], sig;
149         int one = 1;
150
151         (void) signal(SIGINT, SIG_DFL);
152         (void) signal(SIGQUIT, SIG_DFL);
153         (void) signal(SIGTERM, SIG_DFL);
154 #ifdef DEBUG
155         { int t = open(_PATH_TTY, 2);
156           if (t >= 0) {
157                 ioctl(t, TIOCNOTTY, (char *)0);
158                 (void) close(t);
159           }
160         }
161 #endif
162         dup2(f, 0);
163         dup2(f, 1);
164         dup2(f, 2);
165         (void) alarm(60);
166         port = 0;
167         for (;;) {
168                 char c;
169                 if (read(f, &c, 1) != 1)
170                         exit(1);
171                 if (c == 0)
172                         break;
173                 port = port * 10 + c - '0';
174         }
175         (void) alarm(0);
176         if (port != 0) {
177                 s = socket(AF_INET, SOCK_STREAM, 0);
178                 if (s < 0)
179                         exit(1);
180                 if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
181                         exit(1);
182                 (void) alarm(60);
183                 fromp->sin_port = htons(port);
184                 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
185                         exit(1);
186                 (void) alarm(0);
187         }
188         (void) alarm(60);
189         getstr(user, sizeof(user), "username");
190         getstr(pass, sizeof(pass), "password");
191         getstr(cmdbuf, sizeof(cmdbuf), "command");
192         (void) alarm(0);
193         setpwent();
194         pwd = getpwnam(user);
195         if (pwd == NULL) {
196                 error("Login incorrect.\n");
197                 exit(1);
198         }
199         endpwent();
200         if (*pwd->pw_passwd != '\0') {
201 #ifdef SKEY
202                 namep = skey_crypt(pass, pwd->pw_passwd, pwd,
203                                    skeyaccess(user, NULL, remote, NULL));
204 #else /* SKEY */
205                 namep = crypt(pass, pwd->pw_passwd);
206 #endif /* SKEY */
207                 if (strcmp(namep, pwd->pw_passwd)) {
208                         syslog(LOG_ERR, "LOGIN FAILURE from %s, %s",
209                                remote, user);
210                         error("Login incorrect.\n");
211                         exit(1);
212                 }
213         }
214
215         if ((pwd->pw_uid == 0 && no_uid_0) || *pwd->pw_passwd == '\0' ||
216             (pwd->pw_expire && time(NULL) >= pwd->pw_expire)) {
217                 syslog(LOG_ERR, "%s LOGIN REFUSED from %s", user, remote);
218                 error("Login incorrect.\n");
219                 exit(1);
220         }
221
222         if ((fp = fopen(_PATH_FTPUSERS, "r")) != NULL) {
223                 while (fgets(buf, sizeof(buf), fp) != NULL) {
224                         if ((cp = index(buf, '\n')) != NULL)
225                                 *cp = '\0';
226                         if (strcmp(buf, pwd->pw_name) == 0) {
227                                 syslog(LOG_ERR, "%s LOGIN REFUSED from %s",
228                                        user, remote);
229                                 error("Login incorrect.\n");
230                                 exit(1);
231                         }
232                 }
233         }
234         (void) fclose(fp);
235
236         syslog(LOG_INFO, "login from %s as %s", remote, user);
237
238         (void) write(2, "\0", 1);
239         if (port) {
240                 (void) pipe(pv);
241                 pid = fork();
242                 if (pid == -1)  {
243                         error("Try again.\n");
244                         exit(1);
245                 }
246                 if (pid) {
247                         (void) close(0); (void) close(1); (void) close(2);
248                         (void) close(f); (void) close(pv[1]);
249                         readfrom = (1<<s) | (1<<pv[0]);
250                         ioctl(pv[1], FIONBIO, (char *)&one);
251                         /* should set s nbio! */
252                         do {
253                                 ready = readfrom;
254                                 (void) select(16, (fd_set *)&ready,
255                                     (fd_set *)NULL, (fd_set *)NULL,
256                                     (struct timeval *)NULL);
257                                 if (ready & (1<<s)) {
258                                         if (read(s, &sig, 1) <= 0)
259                                                 readfrom &= ~(1<<s);
260                                         else
261                                                 killpg(pid, sig);
262                                 }
263                                 if (ready & (1<<pv[0])) {
264                                         cc = read(pv[0], buf, sizeof (buf));
265                                         if (cc <= 0) {
266                                                 shutdown(s, 1+1);
267                                                 readfrom &= ~(1<<pv[0]);
268                                         } else
269                                                 (void) write(s, buf, cc);
270                                 }
271                         } while (readfrom);
272                         exit(0);
273                 }
274                 setpgrp(0, getpid());
275                 (void) close(s); (void)close(pv[0]);
276                 dup2(pv[1], 2);
277         }
278         if (*pwd->pw_shell == '\0')
279                 pwd->pw_shell = _PATH_BSHELL;
280         if (f > 2)
281                 (void) close(f);
282         if (setlogin(pwd->pw_name) < 0)
283                 syslog(LOG_ERR, "setlogin() failed: %m");
284         (void) setgid((gid_t)pwd->pw_gid);
285         initgroups(pwd->pw_name, pwd->pw_gid);
286         (void) setuid((uid_t)pwd->pw_uid);
287         (void)strcat(path, _PATH_DEFPATH);
288         environ = envinit;
289         strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
290         strncat(shell, pwd->pw_shell, sizeof(shell)-7);
291         strncat(username, pwd->pw_name, sizeof(username)-6);
292         cp = strrchr(pwd->pw_shell, '/');
293         if (cp)
294                 cp++;
295         else
296                 cp = pwd->pw_shell;
297         if (chdir(pwd->pw_dir) < 0) {
298                 error("No remote directory.\n");
299                 exit(1);
300         }
301         execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
302         err(1, "%s", pwd->pw_shell);
303 }
304
305 /*VARARGS1*/
306 void
307 error(fmt, a1, a2, a3)
308         char *fmt;
309         int a1, a2, a3;
310 {
311         char buf[BUFSIZ];
312
313         buf[0] = 1;
314         (void) snprintf(buf+1, sizeof(buf) - 1, fmt, a1, a2, a3);
315         (void) write(2, buf, strlen(buf));
316 }
317
318 void
319 getstr(buf, cnt, err)
320         char *buf;
321         int cnt;
322         char *err;
323 {
324         char c;
325
326         do {
327                 if (read(0, &c, 1) != 1)
328                         exit(1);
329                 *buf++ = c;
330                 if (--cnt == 0) {
331                         error("%s too long\n", err);
332                         exit(1);
333                 }
334         } while (c != 0);
335 }