138678Skfall /* 242763Sbostic * Copyright (c) 1983 Regents of the University of California. 342763Sbostic * All rights reserved. 442763Sbostic * 542763Sbostic * %sccs.include.redist.c% 638678Skfall */ 738678Skfall 838678Skfall #ifndef lint 942763Sbostic static char Xsccsid[] = "derived from @(#)rcmd.c 5.17 (Berkeley) 6/27/88"; 10*58529Sleres static char sccsid[] = "@(#)kcmd.c 5.8 (Berkeley) 03/06/93"; 1142763Sbostic #endif /* not lint */ 1238678Skfall 1338678Skfall #include <sys/param.h> 1438678Skfall #include <sys/file.h> 1538678Skfall #include <sys/socket.h> 1638678Skfall #include <sys/stat.h> 1738678Skfall 1838678Skfall #include <netinet/in.h> 1958470Sbostic #include <arpa/inet.h> 2038678Skfall 2141760Skfall #include <kerberosIV/des.h> 2240683Sbostic #include <kerberosIV/krb.h> 2340683Sbostic #include <kerberosIV/kparse.h> 2458470Sbostic 2558470Sbostic #include <ctype.h> 2658470Sbostic #include <errno.h> 2758470Sbostic #include <netdb.h> 2840683Sbostic #include <pwd.h> 2958470Sbostic #include <signal.h> 3040683Sbostic #include <stdio.h> 3158470Sbostic #include <stdlib.h> 3258470Sbostic #include <string.h> 3358470Sbostic #include <unistd.h> 3438678Skfall 3558470Sbostic #include "krb.h" 3658470Sbostic 3758470Sbostic #ifndef MAXHOSTNAMELEN 3838678Skfall #define MAXHOSTNAMELEN 64 3938678Skfall #endif 4038678Skfall 4138678Skfall #define START_PORT 5120 /* arbitrary */ 4238678Skfall 4358470Sbostic int getport __P((int *)); 4458470Sbostic 4558470Sbostic int 4638678Skfall kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm, 4758470Sbostic cred, schedule, msg_data, laddr, faddr, authopts) 4858470Sbostic int *sock; 4958470Sbostic char **ahost; 5058470Sbostic u_short rport; 5158470Sbostic char *locuser, *remuser, *cmd; 5258470Sbostic int *fd2p; 5358470Sbostic KTEXT ticket; 5458470Sbostic char *service; 5558470Sbostic char *realm; 5658470Sbostic CREDENTIALS *cred; 5758470Sbostic Key_schedule schedule; 5858470Sbostic MSG_DAT *msg_data; 5958470Sbostic struct sockaddr_in *laddr, *faddr; 6058470Sbostic long authopts; 6138678Skfall { 6238678Skfall int s, timo = 1, pid; 6338678Skfall long oldmask; 6438678Skfall struct sockaddr_in sin, from; 6538678Skfall char c; 6638678Skfall #ifdef ATHENA_COMPAT 6738678Skfall int lport = IPPORT_RESERVED - 1; 6838678Skfall #else 6938678Skfall int lport = START_PORT; 7058470Sbostic #endif 7138678Skfall struct hostent *hp; 7238678Skfall int rc; 7338678Skfall char *host_save; 7438678Skfall int status; 7538678Skfall 7638678Skfall pid = getpid(); 7738678Skfall hp = gethostbyname(*ahost); 7858470Sbostic if (hp == NULL) { 7938678Skfall /* fprintf(stderr, "%s: unknown host\n", *ahost); */ 8038678Skfall return (-1); 8138678Skfall } 8238678Skfall 8358470Sbostic host_save = malloc(strlen(hp->h_name) + 1); 8458470Sbostic strcpy(host_save, hp->h_name); 8558470Sbostic *ahost = host_save; 8638678Skfall 8758470Sbostic /* If realm is null, look up from table */ 8858470Sbostic if (realm == NULL || realm[0] == '\0') 8958470Sbostic realm = krb_realmofhost(host_save); 9038678Skfall 9138678Skfall oldmask = sigblock(sigmask(SIGURG)); 9238678Skfall for (;;) { 93*58529Sleres s = getport(&lport); 9438678Skfall if (s < 0) { 9538678Skfall if (errno == EAGAIN) 9638842Skfall fprintf(stderr, 9738842Skfall "kcmd(socket): All ports in use\n"); 9838678Skfall else 9938842Skfall perror("kcmd: socket"); 10038678Skfall sigsetmask(oldmask); 10138678Skfall return (-1); 10238678Skfall } 10338678Skfall fcntl(s, F_SETOWN, pid); 10438678Skfall sin.sin_family = hp->h_addrtype; 10538678Skfall #if defined(ultrix) || defined(sun) 10638678Skfall bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 10738678Skfall #else 10838678Skfall bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length); 10958470Sbostic #endif 11038678Skfall sin.sin_port = rport; 11158470Sbostic if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 11238678Skfall break; 11338678Skfall (void) close(s); 11438678Skfall if (errno == EADDRINUSE) { 11538678Skfall lport--; 11638678Skfall continue; 11738678Skfall } 118*58529Sleres /* 119*58529Sleres * don't wait very long for Kerberos rcmd. 120*58529Sleres */ 121*58529Sleres if (errno == ECONNREFUSED && timo <= 4) { 12238842Skfall /* sleep(timo); don't wait at all here */ 12338678Skfall timo *= 2; 12438678Skfall continue; 12538678Skfall } 12638678Skfall #if !(defined(ultrix) || defined(sun)) 12738678Skfall if (hp->h_addr_list[1] != NULL) { 12838678Skfall int oerrno = errno; 12938678Skfall 13038678Skfall fprintf(stderr, 13138842Skfall "kcmd: connect to address %s: ", 13238842Skfall inet_ntoa(sin.sin_addr)); 13338678Skfall errno = oerrno; 13458470Sbostic perror(NULL); 13538678Skfall hp->h_addr_list++; 13638678Skfall bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, 13738678Skfall hp->h_length); 13838678Skfall fprintf(stderr, "Trying %s...\n", 13938678Skfall inet_ntoa(sin.sin_addr)); 14038678Skfall continue; 14138678Skfall } 14238678Skfall #endif /* !(defined(ultrix) || defined(sun)) */ 14338678Skfall if (errno != ECONNREFUSED) 14438678Skfall perror(hp->h_name); 14538678Skfall sigsetmask(oldmask); 14638678Skfall return (-1); 14738678Skfall } 14838678Skfall lport--; 14938678Skfall if (fd2p == 0) { 15038678Skfall write(s, "", 1); 15138678Skfall lport = 0; 15238678Skfall } else { 15338678Skfall char num[8]; 15458470Sbostic int s2 = getport(&lport), s3; 15558470Sbostic int len = sizeof(from); 15638678Skfall 15758470Sbostic if (s2 < 0) { 15858470Sbostic status = -1; 15938678Skfall goto bad; 16058470Sbostic } 16138678Skfall listen(s2, 1); 16238678Skfall (void) sprintf(num, "%d", lport); 16358470Sbostic if (write(s, num, strlen(num) + 1) != strlen(num) + 1) { 16438842Skfall perror("kcmd(write): setting up stderr"); 16538678Skfall (void) close(s2); 16658470Sbostic status = -1; 16738678Skfall goto bad; 16838678Skfall } 16938678Skfall s3 = accept(s2, (struct sockaddr *)&from, &len); 17038678Skfall (void) close(s2); 17138678Skfall if (s3 < 0) { 17238842Skfall perror("kcmd:accept"); 17338678Skfall lport = 0; 17438678Skfall status = -1; 17538678Skfall goto bad; 17638678Skfall } 17738678Skfall *fd2p = s3; 17838678Skfall from.sin_port = ntohs((u_short)from.sin_port); 17938678Skfall if (from.sin_family != AF_INET || 18038678Skfall from.sin_port >= IPPORT_RESERVED) { 18138678Skfall fprintf(stderr, 18238842Skfall "kcmd(socket): protocol failure in circuit setup.\n"); 18358470Sbostic status = -1; 18438678Skfall goto bad2; 18538678Skfall } 18638678Skfall } 18758470Sbostic /* 18858470Sbostic * Kerberos-authenticated service. Don't have to send locuser, 18958470Sbostic * since its already in the ticket, and we'll extract it on 19058470Sbostic * the other side. 19158470Sbostic */ 19258470Sbostic /* (void) write(s, locuser, strlen(locuser)+1); */ 19338678Skfall 19458470Sbostic /* set up the needed stuff for mutual auth, but only if necessary */ 19558470Sbostic if (authopts & KOPT_DO_MUTUAL) { 19658470Sbostic int sin_len; 19758470Sbostic *faddr = sin; 19838678Skfall 19958470Sbostic sin_len = sizeof(struct sockaddr_in); 20058470Sbostic if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) { 20158470Sbostic perror("kcmd(getsockname)"); 20258470Sbostic status = -1; 20358470Sbostic goto bad2; 20458470Sbostic } 20538678Skfall } 20658470Sbostic if ((status = krb_sendauth(authopts, s, ticket, service, *ahost, 20738678Skfall realm, (unsigned long) getpid(), msg_data, 20838678Skfall cred, schedule, 20938678Skfall laddr, 21038678Skfall faddr, 21138678Skfall "KCMDV0.1")) != KSUCCESS) 21258470Sbostic goto bad2; 21338678Skfall 21438678Skfall (void) write(s, remuser, strlen(remuser)+1); 21538678Skfall (void) write(s, cmd, strlen(cmd)+1); 21638678Skfall 21758470Sbostic if ((rc = read(s, &c, 1)) != 1) { 21858470Sbostic if (rc == -1) 21958470Sbostic perror(*ahost); 22058470Sbostic else 22158470Sbostic fprintf(stderr,"kcmd: bad connection with remote host\n"); 22258470Sbostic status = -1; 22338678Skfall goto bad2; 22438678Skfall } 22558470Sbostic if (c != '\0') { 22638678Skfall while (read(s, &c, 1) == 1) { 22738678Skfall (void) write(2, &c, 1); 22838678Skfall if (c == '\n') 22938678Skfall break; 23038678Skfall } 23158470Sbostic status = -1; 23238678Skfall goto bad2; 23338678Skfall } 23438678Skfall sigsetmask(oldmask); 23558470Sbostic *sock = s; 23658470Sbostic return (KSUCCESS); 23738678Skfall bad2: 23838678Skfall if (lport) 23938678Skfall (void) close(*fd2p); 24038678Skfall bad: 24138678Skfall (void) close(s); 24238678Skfall sigsetmask(oldmask); 24358470Sbostic return (status); 24438678Skfall } 24538678Skfall 24658470Sbostic int 24738678Skfall getport(alport) 24838678Skfall int *alport; 24938678Skfall { 25038678Skfall struct sockaddr_in sin; 25138678Skfall int s; 25238678Skfall 25338678Skfall sin.sin_family = AF_INET; 25438678Skfall sin.sin_addr.s_addr = INADDR_ANY; 25538678Skfall s = socket(AF_INET, SOCK_STREAM, 0); 25638678Skfall if (s < 0) 25738678Skfall return (-1); 25838678Skfall for (;;) { 25938678Skfall sin.sin_port = htons((u_short)*alport); 26058470Sbostic if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 26138678Skfall return (s); 26238678Skfall if (errno != EADDRINUSE) { 26338678Skfall (void) close(s); 26438678Skfall return (-1); 26538678Skfall } 26638678Skfall (*alport)--; 26738678Skfall #ifdef ATHENA_COMPAT 26838678Skfall if (*alport == IPPORT_RESERVED/2) { 26938678Skfall #else 27038678Skfall if (*alport == IPPORT_RESERVED) { 27158470Sbostic #endif 27238678Skfall (void) close(s); 27338678Skfall errno = EAGAIN; /* close */ 27438678Skfall return (-1); 27538678Skfall } 27638678Skfall } 27738678Skfall } 278