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