From b39d776fc1137a481d3439a0663b9683e7b9654b Mon Sep 17 00:00:00 2001 From: Richard Basch Date: Wed, 19 Feb 1997 01:35:26 +0000 Subject: [PATCH] kcmd.c (getport): Let the OS pick the best port rather than scanning. krlogin.c: Fixed 8bit flow control (Solaris) krlogind.c: Whitespace cleanup krshd.c: No need to set lport before calling getport(&lport) Also, changed all occurrences of krb5_xfree to use something else. [kcmd.c still needs one free() fixed when a routine exists to replace the realm component] git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@9908 dc483132-0cff-0310-8789-dd5450dbe970 --- src/appl/bsd/ChangeLog | 3 + src/appl/bsd/kcmd.c | 49 ++++++----- src/appl/bsd/krlogin.c | 185 +++++++++++++++++++--------------------- src/appl/bsd/krlogind.c | 6 +- src/appl/bsd/krshd.c | 8 +- 5 files changed, 120 insertions(+), 131 deletions(-) diff --git a/src/appl/bsd/ChangeLog b/src/appl/bsd/ChangeLog index 32082f028..70ce9c0af 100644 --- a/src/appl/bsd/ChangeLog +++ b/src/appl/bsd/ChangeLog @@ -2,6 +2,9 @@ Tue Feb 18 18:03:55 1997 Richard Basch * krcp.c: Replace krb5_xfree with krb5_free_data_contents * kcmd.c krlogind.c krshd.c: Use free instead of internal krb5_xfree + * krlogin.c: Fixed 8bit character flow under Solaris + * kcmd.c: getport() lets the OS pick the port rather than scanning + * krshd.c: Don't bother to set lport before calling getport(&lport) Sat Dec 28 21:06:43 1996 Sam Hartman diff --git a/src/appl/bsd/kcmd.c b/src/appl/bsd/kcmd.c index 73b2ec6b9..1f196002d 100644 --- a/src/appl/bsd/kcmd.c +++ b/src/appl/bsd/kcmd.c @@ -92,7 +92,7 @@ kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, service, realm, struct sockaddr_in sin, from, local_laddr; krb5_creds *get_cred, *ret_cred = 0; char c; - int lport = START_PORT; + int lport; struct hostent *hp; int rc; char *host_save; @@ -167,12 +167,12 @@ kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, service, realm, #endif /* POSIX_SIGNALS */ for (;;) { - s = getport(&lport); + s = getport(0); if (s < 0) { if (errno == EAGAIN) - fprintf(stderr, "socket: All ports in use\n"); + fprintf(stderr, "socket: All ports in use\n"); else - perror("kcmd: socket"); + perror("kcmd: socket"); #ifdef POSIX_SIGNALS sigprocmask(SIG_SETMASK, &oldmask, (sigset_t*)0); #else @@ -185,12 +185,10 @@ kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, service, realm, memcpy((caddr_t)&sin.sin_addr,hp->h_addr, sizeof(sin.sin_addr)); sin.sin_port = rport; if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0) - break; + break; (void) close(s); - if (errno == EADDRINUSE) { - lport--; + if (errno == EADDRINUSE) continue; - } #if !(defined(tek) || defined(ultrix) || defined(sun) || defined(SYSV)) if (hp->h_addr_list[1] != NULL) { @@ -217,7 +215,6 @@ kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, service, realm, krb5_free_creds(bsd_context, get_cred); return (-1); } - lport--; if (fd2p == 0) { write(s, "", 1); lport = 0; @@ -401,28 +398,30 @@ getport(alport) { struct sockaddr_in sin; int s; + int len = sizeof(sin); + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) + return (-1); + memset((char *) &sin, 0,sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; - s = socket(AF_INET, SOCK_STREAM, 0); - if (s < 0) - return (-1); - for (;;) { - sin.sin_port = htons((u_short)*alport); - if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0) - return (s); - if (errno != EADDRINUSE) { - (void) close(s); - return (-1); - } - (*alport)--; - if (*alport == IPPORT_RESERVED) { - (void) close(s); - errno = EAGAIN; /* close */ - return (-1); + + if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0) { + if (alport) { + if (getsockname(s, (struct sockaddr *)&sin, &len) < 0) { + (void) close(s); + return -1; + } else { + *alport = ntohs(sin.sin_port); + } } + return s; } + + (void) close(s); + return -1; } diff --git a/src/appl/bsd/krlogin.c b/src/appl/bsd/krlogin.c index 0e51620ff..d3c745142 100644 --- a/src/appl/bsd/krlogin.c +++ b/src/appl/bsd/krlogin.c @@ -362,7 +362,6 @@ main(argc, argv) argv++; argc--; goto another; } - if (argc > 0 && !strcmp(*argv, "-d")) { argv++, argc--; options |= SO_DEBUG; @@ -523,14 +522,6 @@ main(argc, argv) sprintf (term + strlen (term), "%d", ospeed); else { (void) strcat(term, speeds[ospeed]); -#if 0 - /* XXX - Not used, since the above code was - * not ifdef'd and it relied on cfget... */ - - /* some "posix" systems don't have cfget... - * so used CBAUD if it's there */ - (void) strcat(term, speeds[ttyb.c_cflag & CBAUD]); -#endif } } #else @@ -980,7 +971,7 @@ int signo; */ writer() { - char c; + unsigned char c; register n; register bol = 1; /* beginning of line */ register local = 0; @@ -1220,7 +1211,7 @@ void oob() #endif mark = 0; - recv(rem, &mark, 1, MSG_OOB); + recv(rem, &mark, 1, MSG_OOB); if (mark & TIOCPKT_WINDOW) { /* * Let server know about window size changes @@ -1329,7 +1320,7 @@ fd_set readset, excset, writeset; #endif ppid = getppid(); -FD_ZERO(&readset); + FD_ZERO(&readset); FD_ZERO(&excset); FD_ZERO(&writeset); #ifdef POSIX_SIGNALS @@ -1351,32 +1342,33 @@ FD_ZERO(&readset); bufp = rcvbuf; rcvcnt = 0; - rcvstate = READING; -FD_SET(rem,&readset); + rcvstate = READING; + FD_SET(rem,&readset); FD_CLR(1,&writeset); } FD_SET(rem,&excset); if (select(rem+1, &readset, &writeset, &excset, 0) > 0 ) { if (FD_ISSET(rem, &excset)) - oob(); + oob(); if (FD_ISSET(1,&writeset)) { - n = write(1, bufp, remaining); - if (n < 0) { - if (errno != EINTR) - return (-1); - continue; + n = write(1, bufp, remaining); + if (n < 0) { + if (errno != EINTR) + return (-1); + continue; + } + bufp += n; } - bufp += n; -} -if (FD_ISSET(rem, &readset)) { + if (FD_ISSET(rem, &readset)) { rcvcnt = des_read(rem, rcvbuf, sizeof (rcvbuf)); - if (rcvcnt == 0) - return (0); - if (rcvcnt < 0) - goto error; -} + if (rcvcnt == 0) + return (0); + if (rcvcnt < 0) + goto error; + } } else - error: { +error: + { if (errno == EINTR) continue; perror("read"); @@ -1390,67 +1382,64 @@ if (FD_ISSET(rem, &readset)) { mode(f) { #ifdef POSIX_TERMIOS - struct termios newtty; + struct termios newtty; - switch(f) { - case 0: + switch(f) { + case 0: #ifdef TIOCGLTC -#ifndef solaris20 - (void) ioctl(0, TIOCSLTC, (char *)&defltc); +#if !defined(sun) + (void) ioctl(0, TIOCSLTC, (char *)&defltc); #endif #endif - (void) tcsetattr(0, TCSADRAIN, &deftty); - break; - case 1: - (void) tcgetattr(0, &newtty); -/* was __svr4__ */ + (void) tcsetattr(0, TCSADRAIN, &deftty); + break; + case 1: + (void) tcgetattr(0, &newtty); + /* was __svr4__ */ #ifdef VLNEXT /* there's a POSIX way of doing this, but do we need it general? */ - newtty.c_cc[VLNEXT] = 0; + newtty.c_cc[VLNEXT] = 0; #endif - newtty.c_lflag &= ~(ICANON|ISIG|ECHO); - if (!flow) - { - newtty.c_lflag &= ~(ICANON|ISIG|ECHO); - newtty.c_iflag &= ~(BRKINT|INLCR|ICRNL|ISTRIP); - /* newtty.c_iflag |= (IXON|IXANY); */ - newtty.c_iflag &= ~(IXON|IXANY); - newtty.c_oflag &= ~(OPOST); - } else { - newtty.c_lflag &= ~(ICANON|ISIG|ECHO); - newtty.c_iflag &= ~(INLCR|ICRNL); - /* newtty.c_iflag |= (BRKINT|ISTRIP|IXON|IXANY); */ - newtty.c_iflag &= ~(IXON|IXANY); - newtty.c_iflag |= (BRKINT|ISTRIP); - newtty.c_oflag &= ~(ONLCR|ONOCR); - newtty.c_oflag |= (OPOST); - } + newtty.c_lflag &= ~(ICANON|ISIG|ECHO); + newtty.c_iflag &= ~(ISTRIP|INLCR|ICRNL); + + if (!flow) { + newtty.c_iflag &= ~(BRKINT|IXON|IXANY); + newtty.c_oflag &= ~(OPOST); + } else { + /* XXX - should we set ixon ? */ + newtty.c_iflag &= ~(IXON|IXANY); + newtty.c_iflag |= (BRKINT); + newtty.c_oflag &= ~(ONLCR|ONOCR); + newtty.c_oflag |= (OPOST); + } #ifdef TABDLY - /* preserve tab delays, but turn off XTABS */ - if ((newtty.c_oflag & TABDLY) == TAB3) - newtty.c_oflag &= ~TABDLY; + /* preserve tab delays, but turn off XTABS */ + if ((newtty.c_oflag & TABDLY) == TAB3) + newtty.c_oflag &= ~TABDLY; #endif + if (!eight) + newtty.c_iflag |= ISTRIP; + if (litout) + newtty.c_oflag &= ~OPOST; - if (litout) - newtty.c_oflag &= ~OPOST; - - newtty.c_cc[VMIN] = 1; - newtty.c_cc[VTIME] = 0; - (void) tcsetattr(0, TCSADRAIN, &newtty); + newtty.c_cc[VMIN] = 1; + newtty.c_cc[VTIME] = 0; + (void) tcsetattr(0, TCSADRAIN, &newtty); #ifdef TIOCGLTC - /* Do this after the tcsetattr() in case this version - * of termio supports the VSUSP or VDSUSP characters */ -#ifndef solaris20 - /* this forces ICRNL under Solaris... */ - (void) ioctl(0, TIOCSLTC, (char *)&noltc); + /* Do this after the tcsetattr() in case this version + * of termio supports the VSUSP or VDSUSP characters */ +#if !defined(sun) + /* this forces ICRNL under Solaris... */ + (void) ioctl(0, TIOCSLTC, (char *)&noltc); #endif #endif - break; - default: - return; - /* NOTREACHED */ - } + break; + default: + return; + /* NOTREACHED */ + } #else struct ltchars *ltc; #ifdef USE_TERMIO @@ -1465,17 +1454,17 @@ mode(f) (void) ioctl(0, TIOCGETP, (char *)&sb); switch (f) { - case 0: + case 0: #ifdef USE_TERMIO /* - ** remember whether IXON was set, so it can be restored - ** when mode(1) is next done - */ + ** remember whether IXON was set, so it can be restored + ** when mode(1) is next done + */ (void) ioctl(fileno(stdin), TIOCGETP, &ixon_state); /* - ** copy the initial modes we saved into sb; this is - ** for restoring to the initial state - */ + ** copy the initial modes we saved into sb; this is + ** for restoring to the initial state + */ (void)memcpy(&sb, &defmodes, sizeof(defmodes)); #else @@ -1489,30 +1478,30 @@ mode(f) ltc = &defltc; break; - case 1: + case 1: #ifdef USE_TERMIO /* - ** turn off output mappings - */ + ** turn off output mappings + */ sb.c_oflag &= ~(ONLCR|OCRNL); /* - ** turn off canonical processing and character echo; - ** also turn off signal checking -- ICANON might be - ** enough to do this, but we're being careful - */ + ** turn off canonical processing and character echo; + ** also turn off signal checking -- ICANON might be + ** enough to do this, but we're being careful + */ sb.c_lflag &= ~(ECHO|ICANON|ISIG); sb.c_cc[VTIME] = 1; sb.c_cc[VMIN] = 1; if (eight) - sb.c_iflag &= ~(ISTRIP); + sb.c_iflag &= ~(ISTRIP); #ifdef TABDLY /* preserve tab delays, but turn off tab-to-space expansion */ if ((sb.c_oflag & TABDLY) == TAB3) - sb.c_oflag &= ~TAB3; + sb.c_oflag &= ~TAB3; #endif /* - ** restore current flow control state - */ + ** restore current flow control state + */ if ((ixon_state.c_iflag & IXON) && flow ) { sb.c_iflag |= IXON; } else { @@ -1523,15 +1512,15 @@ mode(f) sb.sg_flags |= (!flow ? RAW : CBREAK); /* preserve tab delays, but turn off XTABS */ if ((sb.sg_flags & TBDELAY) == XTABS) - sb.sg_flags &= ~TBDELAY; + sb.sg_flags &= ~TBDELAY; sb.sg_kill = sb.sg_erase = -1; #ifdef LLITOUT if (litout) - lflags |= LLITOUT; + lflags |= LLITOUT; #endif #ifdef LPASS8 if (eight) - lflags |= LPASS8; + lflags |= LPASS8; #endif /* LPASS8 */ tc = ¬c; sb.sg_flags &= ~defflags; @@ -1540,7 +1529,7 @@ mode(f) ltc = &noltc; break; - default: + default: return; } (void) ioctl(0, TIOCSLTC, (char *)ltc); diff --git a/src/appl/bsd/krlogind.c b/src/appl/bsd/krlogind.c index 29bd8a418..5937acc8f 100644 --- a/src/appl/bsd/krlogind.c +++ b/src/appl/bsd/krlogind.c @@ -614,12 +614,12 @@ int syncpipe[2]; #if defined(POSIX_TERMIOS) && !defined(ultrix) tcgetattr(t,&new_termio); #if !defined(USE_LOGIN_F) - new_termio.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN); + new_termio.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN); new_termio.c_iflag &= ~(IXON|IXANY|BRKINT|INLCR|ICRNL); #else new_termio.c_lflag |= (ICANON|ECHO|ISIG|IEXTEN); new_termio.c_oflag |= (ONLCR|OPOST); - new_termio.c_iflag|= (IXON|IXANY|BRKINT|INLCR|ICRNL); + new_termio.c_iflag |= (IXON|IXANY|BRKINT|INLCR|ICRNL); #endif /*Do we need binary stream?*/ new_termio.c_iflag &= ~(ISTRIP); /* new_termio.c_iflag = 0; */ @@ -775,7 +775,7 @@ int syncpipe[2]; #endif -#if!defined(USE_LOGIN_F) +#if !defined(USE_LOGIN_F) /* Pass down rusername and lusername to login. */ (void) write(p, rusername, strlen(rusername) +1); (void) write(p, lusername, strlen(lusername) +1); diff --git a/src/appl/bsd/krshd.c b/src/appl/bsd/krshd.c index 43727a7d4..1b1d6a304 100644 --- a/src/appl/bsd/krshd.c +++ b/src/appl/bsd/krshd.c @@ -701,12 +701,10 @@ void doit(f, fromp) } (void) alarm(0); if (port != 0) { - int lport; if (anyport) { - lport = 5120; /* arbitrary */ - s = getport(&lport); + s = getport(0); } else { - lport = IPPORT_RESERVED - 1; + int lport = IPPORT_RESERVED - 1; s = rresvport(&lport); } if (s < 0) { @@ -716,7 +714,7 @@ void doit(f, fromp) } #ifdef KERBEROS if ( port >= IPPORT_RESERVED) - non_privileged = 1; + non_privileged = 1; #else if (port >= IPPORT_RESERVED) { syslog(LOG_ERR , "2nd port not reserved\n"); -- 2.26.2