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