1 1.26 lukem /* $NetBSD: rexecd.c,v 1.26 2008/07/20 01:09:07 lukem Exp $ */ 2 1.4 mrg 3 1.1 cgd /* 4 1.4 mrg * Copyright (c) 1983, 1993 5 1.4 mrg * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.17 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 cgd * may be used to endorse or promote products derived from this software 17 1.1 cgd * without specific prior written permission. 18 1.1 cgd * 19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cgd * SUCH DAMAGE. 30 1.1 cgd */ 31 1.1 cgd 32 1.4 mrg #include <sys/cdefs.h> 33 1.1 cgd #ifndef lint 34 1.26 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ 35 1.26 lukem The Regents of the University of California. All rights reserved."); 36 1.4 mrg #if 0 37 1.4 mrg static char sccsid[] = "from: @(#)rexecd.c 8.1 (Berkeley) 6/4/93"; 38 1.4 mrg #else 39 1.26 lukem __RCSID("$NetBSD: rexecd.c,v 1.26 2008/07/20 01:09:07 lukem Exp $"); 40 1.4 mrg #endif 41 1.1 cgd #endif /* not lint */ 42 1.1 cgd 43 1.22 christos #include <sys/ioctl.h> 44 1.1 cgd #include <sys/param.h> 45 1.1 cgd #include <sys/socket.h> 46 1.5 mrg #include <sys/syslog.h> 47 1.1 cgd #include <sys/time.h> 48 1.4 mrg 49 1.1 cgd #include <netinet/in.h> 50 1.4 mrg 51 1.5 mrg #include <err.h> 52 1.4 mrg #include <errno.h> 53 1.1 cgd #include <netdb.h> 54 1.4 mrg #include <paths.h> 55 1.22 christos #include <poll.h> 56 1.1 cgd #include <pwd.h> 57 1.4 mrg #include <signal.h> 58 1.10 wiz #include <stdarg.h> 59 1.1 cgd #include <stdio.h> 60 1.1 cgd #include <stdlib.h> 61 1.1 cgd #include <string.h> 62 1.4 mrg #include <unistd.h> 63 1.1 cgd 64 1.21 christos #ifdef USE_PAM 65 1.21 christos #include <security/pam_appl.h> 66 1.21 christos #include <security/openpam.h> 67 1.21 christos #endif 68 1.21 christos 69 1.19 ginsbach int main(int, char *[]); 70 1.21 christos static void rexecd_errx(int, const char *, ...) 71 1.21 christos __attribute__((__noreturn__, __format__(__printf__, 2, 3))); 72 1.25 perry static void doit(struct sockaddr *) __dead; 73 1.21 christos static void getstr(char *, int, const char *); 74 1.25 perry static void usage(void) __dead; 75 1.21 christos 76 1.21 christos #ifdef USE_PAM 77 1.21 christos static pam_handle_t *pamh; 78 1.21 christos static struct pam_conv pamc = { 79 1.21 christos openpam_nullconv, 80 1.21 christos NULL 81 1.21 christos }; 82 1.21 christos static int pam_flags = PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK; 83 1.21 christos static int pam_err; 84 1.21 christos #define pam_ok(err) ((pam_err = (err)) == PAM_SUCCESS) 85 1.21 christos #endif 86 1.1 cgd 87 1.22 christos extern char **environ; 88 1.22 christos static int dolog; 89 1.21 christos #ifndef USE_PAM 90 1.21 christos static char username[32 + 1] = "USER="; 91 1.21 christos static char logname[32 + 3 + 1] = "LOGNAME="; 92 1.21 christos static char homedir[PATH_MAX + 1] = "HOME="; 93 1.21 christos static char shell[PATH_MAX + 1] = "SHELL="; 94 1.21 christos static char path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH="; 95 1.21 christos static char *envinit[] = { homedir, shell, path, username, logname, 0 }; 96 1.21 christos #endif 97 1.5 mrg 98 1.1 cgd /* 99 1.1 cgd * remote execute server: 100 1.1 cgd * username\0 101 1.1 cgd * password\0 102 1.1 cgd * command\0 103 1.1 cgd * data 104 1.1 cgd */ 105 1.4 mrg int 106 1.19 ginsbach main(int argc, char *argv[]) 107 1.1 cgd { 108 1.11 itojun struct sockaddr_storage from; 109 1.21 christos socklen_t fromlen; 110 1.21 christos int ch; 111 1.5 mrg 112 1.5 mrg while ((ch = getopt(argc, argv, "l")) != -1) 113 1.5 mrg switch (ch) { 114 1.5 mrg case 'l': 115 1.14 thorpej dolog = 1; 116 1.5 mrg openlog("rexecd", LOG_PID, LOG_DAEMON); 117 1.5 mrg break; 118 1.5 mrg default: 119 1.21 christos usage(); 120 1.5 mrg } 121 1.1 cgd 122 1.22 christos fromlen = sizeof(from); 123 1.21 christos if (getpeername(STDIN_FILENO, (struct sockaddr *)&from, &fromlen) < 0) 124 1.22 christos err(EXIT_FAILURE, "getpeername"); 125 1.5 mrg 126 1.21 christos if (((struct sockaddr *)&from)->sa_family == AF_INET6 && 127 1.21 christos IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) { 128 1.21 christos char hbuf[NI_MAXHOST]; 129 1.21 christos if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf, 130 1.21 christos sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) { 131 1.21 christos (void)strlcpy(hbuf, "invalid", sizeof(hbuf)); 132 1.21 christos } 133 1.21 christos if (dolog) 134 1.21 christos syslog(LOG_ERR, 135 1.21 christos "malformed \"from\" address (v4 mapped, %s)", 136 1.21 christos hbuf); 137 1.22 christos return EXIT_FAILURE; 138 1.21 christos } 139 1.21 christos 140 1.21 christos doit((struct sockaddr *)&from); 141 1.1 cgd } 142 1.1 cgd 143 1.4 mrg void 144 1.21 christos doit(struct sockaddr *fromp) 145 1.1 cgd { 146 1.5 mrg struct pollfd fds[2]; 147 1.21 christos char cmdbuf[NCARGS + 1]; 148 1.6 mycroft const char *cp; 149 1.1 cgd char user[16], pass[16]; 150 1.5 mrg char buf[BUFSIZ], sig; 151 1.22 christos struct passwd *pwd, pwres; 152 1.4 mrg int s = -1; /* XXX gcc */ 153 1.5 mrg int pv[2], pid, cc; 154 1.1 cgd int one = 1; 155 1.5 mrg in_port_t port; 156 1.21 christos char hostname[2 * MAXHOSTNAMELEN + 1]; 157 1.21 christos char pbuf[NI_MAXSERV]; 158 1.21 christos const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; 159 1.21 christos #ifndef USE_PAM 160 1.21 christos char *namep; 161 1.21 christos #endif 162 1.22 christos char pwbuf[1024]; 163 1.1 cgd 164 1.5 mrg (void)signal(SIGINT, SIG_DFL); 165 1.5 mrg (void)signal(SIGQUIT, SIG_DFL); 166 1.5 mrg (void)signal(SIGTERM, SIG_DFL); 167 1.21 christos (void)dup2(STDIN_FILENO, STDOUT_FILENO); 168 1.21 christos (void)dup2(STDIN_FILENO, STDERR_FILENO); 169 1.21 christos 170 1.21 christos if (getnameinfo(fromp, fromp->sa_len, hostname, sizeof(hostname), 171 1.21 christos pbuf, sizeof(pbuf), niflags) != 0) { 172 1.21 christos if (dolog) 173 1.21 christos syslog(LOG_ERR, "malformed \"from\" address (af %d)", 174 1.21 christos fromp->sa_family); 175 1.22 christos exit(EXIT_FAILURE); 176 1.21 christos } 177 1.21 christos 178 1.5 mrg (void)alarm(60); 179 1.1 cgd port = 0; 180 1.1 cgd for (;;) { 181 1.1 cgd char c; 182 1.21 christos if (read(STDIN_FILENO, &c, 1) != 1) { 183 1.14 thorpej if (dolog) 184 1.21 christos syslog(LOG_ERR, "initial read failed"); 185 1.22 christos exit(EXIT_FAILURE); 186 1.5 mrg } 187 1.1 cgd if (c == 0) 188 1.1 cgd break; 189 1.1 cgd port = port * 10 + c - '0'; 190 1.1 cgd } 191 1.1 cgd if (port != 0) { 192 1.11 itojun s = socket(fromp->sa_family, SOCK_STREAM, 0); 193 1.5 mrg if (s < 0) { 194 1.14 thorpej if (dolog) 195 1.5 mrg syslog(LOG_ERR, "socket: %m"); 196 1.22 christos exit(EXIT_FAILURE); 197 1.5 mrg } 198 1.11 itojun (void)alarm(60); 199 1.11 itojun switch (fromp->sa_family) { 200 1.11 itojun case AF_INET: 201 1.11 itojun ((struct sockaddr_in *)fromp)->sin_port = htons(port); 202 1.11 itojun break; 203 1.11 itojun case AF_INET6: 204 1.11 itojun ((struct sockaddr_in6 *)fromp)->sin6_port = htons(port); 205 1.11 itojun break; 206 1.11 itojun default: 207 1.11 itojun syslog(LOG_ERR, "unsupported address family"); 208 1.22 christos exit(EXIT_FAILURE); 209 1.5 mrg } 210 1.11 itojun if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) { 211 1.14 thorpej if (dolog) 212 1.5 mrg syslog(LOG_ERR, "connect: %m"); 213 1.22 christos exit(EXIT_FAILURE); 214 1.5 mrg } 215 1.5 mrg (void)alarm(0); 216 1.1 cgd } 217 1.21 christos (void)alarm(60); 218 1.1 cgd getstr(user, sizeof(user), "username"); 219 1.1 cgd getstr(pass, sizeof(pass), "password"); 220 1.1 cgd getstr(cmdbuf, sizeof(cmdbuf), "command"); 221 1.21 christos (void)alarm(0); 222 1.23 christos if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 || 223 1.23 christos pwd == NULL) { 224 1.14 thorpej if (dolog) 225 1.5 mrg syslog(LOG_ERR, "no such user %s", user); 226 1.22 christos rexecd_errx(EXIT_FAILURE, "Login incorrect."); 227 1.1 cgd } 228 1.21 christos #ifdef USE_PAM 229 1.21 christos if (!pam_ok(pam_start("rexecd", user, &pamc, &pamh)) || 230 1.21 christos !pam_ok(pam_set_item(pamh, PAM_RHOST, hostname)) || 231 1.21 christos !pam_ok(pam_set_item(pamh, PAM_AUTHTOK, pass))) { 232 1.21 christos if (dolog) 233 1.21 christos syslog(LOG_ERR, "PAM ERROR %s@%s (%s)", user, 234 1.21 christos hostname, pam_strerror(pamh, pam_err)); 235 1.22 christos rexecd_errx(EXIT_FAILURE, "Try again."); 236 1.21 christos } 237 1.21 christos if (!pam_ok(pam_authenticate(pamh, pam_flags)) || 238 1.21 christos !pam_ok(pam_acct_mgmt(pamh, pam_flags))) { 239 1.21 christos if (dolog) 240 1.21 christos syslog(LOG_ERR, "LOGIN REFUSED for %s@%s (%s)", user, 241 1.21 christos hostname, pam_strerror(pamh, pam_err)); 242 1.22 christos rexecd_errx(EXIT_FAILURE, "Password incorrect."); 243 1.21 christos } 244 1.21 christos #else 245 1.1 cgd if (*pwd->pw_passwd != '\0') { 246 1.1 cgd namep = crypt(pass, pwd->pw_passwd); 247 1.21 christos if (strcmp(namep, pwd->pw_passwd) != 0) { 248 1.14 thorpej if (dolog) 249 1.5 mrg syslog(LOG_ERR, "incorrect password for %s", 250 1.5 mrg user); 251 1.22 christos rexecd_errx(EXIT_FAILURE, 252 1.22 christos "Password incorrect.");/* XXX: wrong! */ 253 1.1 cgd } 254 1.5 mrg } else 255 1.21 christos (void)crypt("dummy password", "PA"); /* must always crypt */ 256 1.21 christos #endif 257 1.1 cgd if (chdir(pwd->pw_dir) < 0) { 258 1.14 thorpej if (dolog) 259 1.5 mrg syslog(LOG_ERR, "%s does not exist for %s", pwd->pw_dir, 260 1.21 christos user); 261 1.22 christos rexecd_errx(EXIT_FAILURE, "No remote directory."); 262 1.1 cgd } 263 1.21 christos 264 1.21 christos if (dolog) 265 1.21 christos syslog(LOG_INFO, "login from %s as %s", hostname, user); 266 1.19 ginsbach (void)write(STDERR_FILENO, "\0", 1); 267 1.1 cgd if (port) { 268 1.5 mrg if (pipe(pv) < 0 || (pid = fork()) == -1) { 269 1.14 thorpej if (dolog) 270 1.5 mrg syslog(LOG_ERR,"pipe or fork failed for %s: %m", 271 1.5 mrg user); 272 1.22 christos rexecd_errx(EXIT_FAILURE, "Try again."); 273 1.1 cgd } 274 1.1 cgd if (pid) { 275 1.21 christos /* parent */ 276 1.21 christos #ifdef USE_PAM 277 1.21 christos (void)pam_end(pamh, pam_err); 278 1.21 christos #endif 279 1.19 ginsbach (void)close(STDIN_FILENO); 280 1.19 ginsbach (void)close(STDOUT_FILENO); 281 1.19 ginsbach (void)close(STDERR_FILENO); 282 1.5 mrg (void)close(pv[1]); 283 1.5 mrg fds[0].fd = s; 284 1.5 mrg fds[1].fd = pv[0]; 285 1.5 mrg fds[0].events = fds[1].events = POLLIN; 286 1.5 mrg if (ioctl(pv[1], FIONBIO, (char *)&one) < 0) 287 1.8 wiz _exit(1); 288 1.1 cgd /* should set s nbio! */ 289 1.1 cgd do { 290 1.5 mrg if (poll(fds, 2, 0) < 0) { 291 1.22 christos (void)close(s); 292 1.22 christos (void)close(pv[0]); 293 1.8 wiz _exit(1); 294 1.5 mrg } 295 1.5 mrg if (fds[0].revents & POLLIN) { 296 1.1 cgd if (read(s, &sig, 1) <= 0) 297 1.5 mrg fds[0].events = 0; 298 1.1 cgd else 299 1.22 christos (void)killpg(pid, sig); 300 1.1 cgd } 301 1.5 mrg if (fds[1].revents & POLLIN) { 302 1.1 cgd cc = read(pv[0], buf, sizeof (buf)); 303 1.1 cgd if (cc <= 0) { 304 1.22 christos (void)shutdown(s, SHUT_RDWR); 305 1.5 mrg fds[1].events = 0; 306 1.1 cgd } else 307 1.5 mrg (void)write(s, buf, cc); 308 1.1 cgd } 309 1.5 mrg } while ((fds[0].events | fds[1].events) & POLLIN); 310 1.5 mrg _exit(0); 311 1.5 mrg } 312 1.21 christos /* child */ 313 1.5 mrg (void)close(s); 314 1.5 mrg (void)close(pv[0]); 315 1.21 christos if (dup2(pv[1], STDERR_FILENO) < 0) { 316 1.14 thorpej if (dolog) 317 1.5 mrg syslog(LOG_ERR, "dup2 failed for %s", user); 318 1.22 christos rexecd_errx(EXIT_FAILURE, "Try again."); 319 1.1 cgd } 320 1.1 cgd } 321 1.1 cgd if (*pwd->pw_shell == '\0') 322 1.21 christos pwd->pw_shell = __UNCONST(_PATH_BSHELL); 323 1.15 dsl if (setsid() < 0 || 324 1.15 dsl setlogin(pwd->pw_name) < 0 || 325 1.5 mrg initgroups(pwd->pw_name, pwd->pw_gid) < 0 || 326 1.21 christos #ifdef USE_PAM 327 1.21 christos setgid((gid_t)pwd->pw_gid) < 0) { 328 1.21 christos #else 329 1.19 ginsbach setgid((gid_t)pwd->pw_gid) < 0 || 330 1.5 mrg setuid((uid_t)pwd->pw_uid) < 0) { 331 1.21 christos #endif 332 1.22 christos rexecd_errx(EXIT_FAILURE, "Try again."); 333 1.14 thorpej if (dolog) 334 1.5 mrg syslog(LOG_ERR, "could not set permissions for %s: %m", 335 1.5 mrg user); 336 1.22 christos exit(EXIT_FAILURE); 337 1.5 mrg } 338 1.21 christos #ifdef USE_PAM 339 1.21 christos if (!pam_ok(pam_setcred(pamh, PAM_ESTABLISH_CRED))) 340 1.21 christos syslog(LOG_ERR, "pam_setcred() failed: %s", 341 1.21 christos pam_strerror(pamh, pam_err)); 342 1.21 christos (void)pam_setenv(pamh, "HOME", pwd->pw_dir, 1); 343 1.21 christos (void)pam_setenv(pamh, "SHELL", pwd->pw_shell, 1); 344 1.21 christos (void)pam_setenv(pamh, "USER", pwd->pw_name, 1); 345 1.21 christos (void)pam_setenv(pamh, "LOGNAME", pwd->pw_name, 1); 346 1.21 christos (void)pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1); 347 1.21 christos environ = pam_getenvlist(pamh); 348 1.21 christos (void)pam_end(pamh, pam_err); 349 1.21 christos if (setuid((uid_t)pwd->pw_uid) < 0) { 350 1.21 christos if (dolog) 351 1.21 christos syslog(LOG_ERR, "could not set uid for %s: %m", 352 1.21 christos user); 353 1.22 christos rexecd_errx(EXIT_FAILURE, "Try again."); 354 1.21 christos } 355 1.21 christos #else 356 1.16 itojun (void)strlcat(path, _PATH_DEFPATH, sizeof(path)); 357 1.1 cgd environ = envinit; 358 1.21 christos (void)strlcat(homedir, pwd->pw_dir, sizeof(homedir)); 359 1.21 christos (void)strlcat(shell, pwd->pw_shell, sizeof(shell)); 360 1.21 christos (void)strlcat(username, pwd->pw_name, sizeof(username)); 361 1.21 christos (void)strlcat(logname, pwd->pw_name, sizeof(logname)); 362 1.21 christos #endif 363 1.21 christos 364 1.4 mrg cp = strrchr(pwd->pw_shell, '/'); 365 1.1 cgd if (cp) 366 1.1 cgd cp++; 367 1.1 cgd else 368 1.1 cgd cp = pwd->pw_shell; 369 1.14 thorpej if (dolog) 370 1.5 mrg syslog(LOG_INFO, "running command for %s: %s", user, cmdbuf); 371 1.24 mrg (void)execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL); 372 1.14 thorpej if (dolog) 373 1.5 mrg syslog(LOG_ERR, "execl failed for %s: %m", user); 374 1.22 christos err(EXIT_FAILURE, "%s", pwd->pw_shell); 375 1.1 cgd } 376 1.1 cgd 377 1.4 mrg void 378 1.21 christos rexecd_errx(int ex, const char *fmt, ...) 379 1.1 cgd { 380 1.1 cgd char buf[BUFSIZ]; 381 1.4 mrg va_list ap; 382 1.21 christos ssize_t len; 383 1.4 mrg 384 1.4 mrg va_start(ap, fmt); 385 1.1 cgd buf[0] = 1; 386 1.21 christos len = vsnprintf(buf + 1, sizeof(buf) - 1, fmt, ap) + 1; 387 1.21 christos buf[len++] = '\n'; 388 1.21 christos (void)write(STDERR_FILENO, buf, len); 389 1.9 wiz va_end(ap); 390 1.21 christos exit(ex); 391 1.1 cgd } 392 1.1 cgd 393 1.4 mrg void 394 1.21 christos getstr(char *buf, int cnt, const char *emsg) 395 1.1 cgd { 396 1.1 cgd char c; 397 1.1 cgd 398 1.1 cgd do { 399 1.19 ginsbach if (read(STDIN_FILENO, &c, 1) != 1) 400 1.22 christos exit(EXIT_FAILURE); 401 1.1 cgd *buf++ = c; 402 1.21 christos if (--cnt == 0) 403 1.22 christos rexecd_errx(EXIT_FAILURE, "%s too long", emsg); 404 1.1 cgd } while (c != 0); 405 1.1 cgd } 406 1.21 christos 407 1.21 christos static void 408 1.21 christos usage(void) 409 1.21 christos { 410 1.21 christos (void)fprintf(stderr, "Usage: %s [-l]\n", getprogname()); 411 1.22 christos exit(EXIT_FAILURE); 412 1.21 christos } 413