1 /* 2 * Copyright (c) 1980, 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #if defined(LIBC_SCCS) && !defined(lint) 33 static char sccsid[] = "@(#)rexec.c 8.1 (Berkeley) 6/4/93"; 34 #endif /* LIBC_SCCS and not lint */ 35 36 #include <sys/types.h> 37 #include <sys/uio.h> 38 #include <sys/socket.h> 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 42 #include <netinet/in.h> 43 44 #include <stdio.h> 45 #include <unistd.h> 46 #include <string.h> 47 #include <netdb.h> 48 #include <errno.h> 49 #include <ctype.h> 50 #include <err.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 54 int rexecoptions; 55 char *getpass(), *getlogin(); 56 57 /* 58 * Options and other state info. 59 */ 60 struct macel { 61 char mac_name[9]; /* macro name */ 62 char *mac_start; /* start of macro in macbuf */ 63 char *mac_end; /* end of macro in macbuf */ 64 }; 65 66 int macnum; /* number of defined macros */ 67 struct macel macros[16]; 68 char macbuf[4096]; 69 70 static FILE *cfile; 71 72 #define DEFAULT 1 73 #define LOGIN 2 74 #define PASSWD 3 75 #define ACCOUNT 4 76 #define MACDEF 5 77 #define ID 10 78 #define MACH 11 79 80 static char tokval[100]; 81 82 static struct toktab { 83 char *tokstr; 84 int tval; 85 } toktab[]= { 86 { "default", DEFAULT }, 87 { "login", LOGIN }, 88 { "password", PASSWD }, 89 { "passwd", PASSWD }, 90 { "account", ACCOUNT }, 91 { "machine", MACH }, 92 { "macdef", MACDEF }, 93 { NULL, 0 } 94 }; 95 96 static int 97 token() 98 { 99 char *cp; 100 int c; 101 struct toktab *t; 102 103 if (feof(cfile) || ferror(cfile)) 104 return (0); 105 while ((c = getc(cfile)) != EOF && 106 (c == '\n' || c == '\t' || c == ' ' || c == ',')) 107 continue; 108 if (c == EOF) 109 return (0); 110 cp = tokval; 111 if (c == '"') { 112 while ((c = getc(cfile)) != EOF && c != '"') { 113 if (c == '\\') 114 c = getc(cfile); 115 *cp++ = c; 116 } 117 } else { 118 *cp++ = c; 119 while ((c = getc(cfile)) != EOF 120 && c != '\n' && c != '\t' && c != ' ' && c != ',') { 121 if (c == '\\') 122 c = getc(cfile); 123 *cp++ = c; 124 } 125 } 126 *cp = 0; 127 if (tokval[0] == 0) 128 return (0); 129 for (t = toktab; t->tokstr; t++) 130 if (!strcmp(t->tokstr, tokval)) 131 return (t->tval); 132 return (ID); 133 } 134 135 static int 136 ruserpass(host, aname, apass, aacct) 137 char *host, **aname, **apass, **aacct; 138 { 139 char *hdir, buf[BUFSIZ], *tmp; 140 char myname[MAXHOSTNAMELEN], *mydomain; 141 int t, i, c, usedefault = 0; 142 struct stat stb; 143 144 hdir = getenv("HOME"); 145 if (hdir == NULL) 146 hdir = "."; 147 if (strlen(hdir) + 8 > sizeof(buf)) 148 return (0); 149 (void) sprintf(buf, "%s/.netrc", hdir); 150 cfile = fopen(buf, "r"); 151 if (cfile == NULL) { 152 if (errno != ENOENT) 153 warn("%s", buf); 154 return (0); 155 } 156 if (gethostname(myname, sizeof(myname)) < 0) 157 myname[0] = '\0'; 158 if ((mydomain = strchr(myname, '.')) == NULL) 159 mydomain = ""; 160 next: 161 while ((t = token())) switch(t) { 162 163 case DEFAULT: 164 usedefault = 1; 165 /* FALL THROUGH */ 166 167 case MACH: 168 if (!usedefault) { 169 if (token() != ID) 170 continue; 171 /* 172 * Allow match either for user's input host name 173 * or official hostname. Also allow match of 174 * incompletely-specified host in local domain. 175 */ 176 if (strcasecmp(host, tokval) == 0) 177 goto match; 178 if ((tmp = strchr(host, '.')) != NULL && 179 strcasecmp(tmp, mydomain) == 0 && 180 strncasecmp(host, tokval, tmp - host) == 0 && 181 tokval[tmp - host] == '\0') 182 goto match; 183 continue; 184 } 185 match: 186 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { 187 188 case LOGIN: 189 if (token()) 190 if (*aname == 0) { 191 *aname = malloc((unsigned) strlen(tokval) + 1); 192 (void) strcpy(*aname, tokval); 193 } else { 194 if (strcmp(*aname, tokval)) 195 goto next; 196 } 197 break; 198 case PASSWD: 199 if ((*aname == 0 || strcmp(*aname, "anonymous")) && 200 fstat(fileno(cfile), &stb) >= 0 && 201 (stb.st_mode & 077) != 0) { 202 warnx("Error: .netrc file is readable by others."); 203 warnx("Remove password or make file unreadable by others."); 204 goto bad; 205 } 206 if (token() && *apass == 0) { 207 *apass = malloc((unsigned) strlen(tokval) + 1); 208 (void) strcpy(*apass, tokval); 209 } 210 break; 211 case ACCOUNT: 212 if (fstat(fileno(cfile), &stb) >= 0 213 && (stb.st_mode & 077) != 0) { 214 warnx("Error: .netrc file is readable by others."); 215 warnx("Remove account or make file unreadable by others."); 216 goto bad; 217 } 218 if (token() && *aacct == 0) { 219 *aacct = malloc((unsigned) strlen(tokval) + 1); 220 (void) strcpy(*aacct, tokval); 221 } 222 break; 223 case MACDEF: 224 while ((c=getc(cfile)) != EOF && 225 (c == ' ' || c == '\t')) 226 ; 227 if (c == EOF || c == '\n') { 228 printf("Missing macdef name argument.\n"); 229 goto bad; 230 } 231 if (macnum == 16) { 232 printf("Limit of 16 macros have already been defined\n"); 233 goto bad; 234 } 235 tmp = macros[macnum].mac_name; 236 *tmp++ = c; 237 for (i=0; i < 8 && (c=getc(cfile)) != EOF && 238 !isspace(c); ++i) { 239 *tmp++ = c; 240 } 241 if (c == EOF) { 242 printf("Macro definition missing null line terminator.\n"); 243 goto bad; 244 } 245 *tmp = '\0'; 246 if (c != '\n') { 247 while ((c=getc(cfile)) != EOF && c != '\n'); 248 } 249 if (c == EOF) { 250 printf("Macro definition missing null line terminator.\n"); 251 goto bad; 252 } 253 if (macnum == 0) { 254 macros[macnum].mac_start = macbuf; 255 } 256 else { 257 macros[macnum].mac_start = macros[macnum-1].mac_end + 1; 258 } 259 tmp = macros[macnum].mac_start; 260 while (tmp != macbuf + 4096) { 261 if ((c=getc(cfile)) == EOF) { 262 printf("Macro definition missing null line terminator.\n"); 263 goto bad; 264 } 265 *tmp = c; 266 if (*tmp == '\n') { 267 if (*(tmp-1) == '\0') { 268 macros[macnum++].mac_end = tmp - 1; 269 break; 270 } 271 *tmp = '\0'; 272 } 273 tmp++; 274 } 275 if (tmp == macbuf + 4096) { 276 printf("4K macro buffer exceeded\n"); 277 goto bad; 278 } 279 break; 280 default: 281 warnx("Unknown .netrc keyword %s", tokval); 282 break; 283 } 284 goto done; 285 } 286 done: 287 (void) fclose(cfile); 288 return (0); 289 bad: 290 (void) fclose(cfile); 291 return (-1); 292 } 293 294 int 295 rexec(ahost, rport, name, pass, cmd, fd2p) 296 char **ahost; 297 int rport; 298 char *name, *pass, *cmd; 299 int *fd2p; 300 { 301 struct sockaddr_in sin, sin2, from; 302 struct hostent *hp; 303 u_short port; 304 int s, timo = 1, s3; 305 char c, *acct; 306 307 hp = gethostbyname(*ahost); 308 if (hp == 0) { 309 herror(*ahost); 310 return (-1); 311 } 312 *ahost = hp->h_name; 313 acct = NULL; 314 ruserpass(hp->h_name, &name, &pass, &acct); 315 free(acct); 316 retry: 317 s = socket(AF_INET, SOCK_STREAM, 0); 318 if (s < 0) { 319 perror("rexec: socket"); 320 return (-1); 321 } 322 sin.sin_family = hp->h_addrtype; 323 sin.sin_port = rport; 324 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 325 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 326 if (errno == ECONNREFUSED && timo <= 16) { 327 (void) close(s); 328 sleep(timo); 329 timo *= 2; 330 goto retry; 331 } 332 perror(hp->h_name); 333 return (-1); 334 } 335 port = 0; 336 if (fd2p == 0) 337 (void) write(s, "", 1); 338 else { 339 char num[8]; 340 int s2, sin2len; 341 342 s2 = socket(AF_INET, SOCK_STREAM, 0); 343 if (s2 < 0) { 344 (void) close(s); 345 return (-1); 346 } 347 listen(s2, 1); 348 sin2len = sizeof (sin2); 349 if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 || 350 sin2len != sizeof (sin2)) { 351 perror("getsockname"); 352 (void) close(s2); 353 goto bad; 354 } 355 port = ntohs((u_short)sin2.sin_port); 356 (void) sprintf(num, "%u", port); 357 (void) write(s, num, strlen(num)+1); 358 { int len = sizeof (from); 359 s3 = accept(s2, (struct sockaddr *)&from, &len); 360 close(s2); 361 if (s3 < 0) { 362 perror("accept"); 363 port = 0; 364 goto bad; 365 } 366 } 367 *fd2p = s3; 368 } 369 (void) write(s, name, strlen(name) + 1); 370 /* should public key encypt the password here */ 371 (void) write(s, pass, strlen(pass) + 1); 372 (void) write(s, cmd, strlen(cmd) + 1); 373 if (read(s, &c, 1) != 1) { 374 perror(*ahost); 375 goto bad; 376 } 377 if (c != 0) { 378 while (read(s, &c, 1) == 1) { 379 (void) write(2, &c, 1); 380 if (c == '\n') 381 break; 382 } 383 goto bad; 384 } 385 return (s); 386 bad: 387 if (port) 388 (void) close(*fd2p); 389 (void) close(s); 390 return (-1); 391 } 392