diff options
author | Michael Shalayeff <mickey@cvs.openbsd.org> | 2001-06-11 15:18:54 +0000 |
---|---|---|
committer | Michael Shalayeff <mickey@cvs.openbsd.org> | 2001-06-11 15:18:54 +0000 |
commit | 17fdbbbbd36dd0f57381719477a4df3a9771df26 (patch) | |
tree | f5156d1d8e47c08bc274d75009b0aa67541e2fa1 | |
parent | bbbf292a68225de030d9d9ced38e0bce00decab0 (diff) |
strncpy vs strlcpy, some strlen missuse.
some other related minor cleanups, and spaces.
millert@ ok
-rw-r--r-- | libexec/ftpd/ftpd.c | 20 | ||||
-rw-r--r-- | libexec/getty/subr.c | 11 | ||||
-rw-r--r-- | libexec/rlogind/rlogind.c | 20 | ||||
-rw-r--r-- | libexec/rpc.rquotad/rquotad.c | 5 | ||||
-rw-r--r-- | libexec/rshd/rshd.c | 16 | ||||
-rw-r--r-- | libexec/telnetd/sys_term.c | 10 | ||||
-rw-r--r-- | libexec/telnetd/telnetd.c | 28 | ||||
-rw-r--r-- | libexec/telnetd/utility.c | 9 | ||||
-rw-r--r-- | libexec/tftpd/tftpd.c | 13 |
9 files changed, 58 insertions, 74 deletions
diff --git a/libexec/ftpd/ftpd.c b/libexec/ftpd/ftpd.c index db3cec1d4d5..f4cc78a77dd 100644 --- a/libexec/ftpd/ftpd.c +++ b/libexec/ftpd/ftpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ftpd.c,v 1.97 2001/05/29 21:35:16 millert Exp $ */ +/* $OpenBSD: ftpd.c,v 1.98 2001/06/11 15:18:49 mickey Exp $ */ /* $NetBSD: ftpd.c,v 1.15 1995/06/03 22:46:47 mycroft Exp $ */ /* @@ -73,7 +73,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; #else -static char rcsid[] = "$OpenBSD: ftpd.c,v 1.97 2001/05/29 21:35:16 millert Exp $"; +static char rcsid[] = "$OpenBSD: ftpd.c,v 1.98 2001/06/11 15:18:49 mickey Exp $"; #endif #endif /* not lint */ @@ -2510,8 +2510,7 @@ guniquefd(local, nam) } if (cp) *cp = '/'; - (void) strlcpy(new, local, sizeof(new)); - len = strlen(new); + len = strlcpy(new, local, sizeof(new)); if (len+2+1 >= sizeof(new)-1) return (-1); cp = new + len; @@ -2703,11 +2702,12 @@ logxfer(name, size, start) char vremotehost[MAXHOSTNAMELEN*4], vpath[MAXPATHLEN*4]; char *vpw; time_t now; + int len; if ((statfd >= 0) && (getcwd(dir, sizeof(dir)) != NULL)) { time(&now); - vpw = (char *)malloc(strlen((guest) ? guestpw : pw->pw_name)*4+1); + vpw = malloc(strlen(guest ? guestpw : pw->pw_name) * 4 + 1); if (vpw == NULL) return; @@ -2717,9 +2717,9 @@ logxfer(name, size, start) strvis(vpath, rpath, VIS_SAFE|VIS_NOSLASH); strvis(vremotehost, remotehost, VIS_SAFE|VIS_NOSLASH); - strvis(vpw, (guest) ? guestpw : pw->pw_name, VIS_SAFE|VIS_NOSLASH); + strvis(vpw, guest? guestpw : pw->pw_name, VIS_SAFE|VIS_NOSLASH); - snprintf(buf, sizeof(buf), + len = snprintf(buf, sizeof(buf), "%.24s %d %s %qd %s %c %s %c %c %s ftp %d %s %s\n", ctime(&now), now - start + (now == start), vremotehost, (long long) size, vpath, @@ -2727,7 +2727,11 @@ logxfer(name, size, start) 'o', ((guest) ? 'a' : 'r'), vpw, 0 /* none yet */, ((guest) ? "*" : pw->pw_name), dhostname); - write(statfd, buf, strlen(buf)); + if (len >= sizeof(buf)) { + len = sizeof(buf); + buf[sizeof(buf) - 1] = '\n'; + } + write(statfd, buf, len); free(vpw); } } diff --git a/libexec/getty/subr.c b/libexec/getty/subr.c index a8a562abe00..21efd017a17 100644 --- a/libexec/getty/subr.c +++ b/libexec/getty/subr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: subr.c,v 1.11 2001/01/28 19:34:28 niklas Exp $ */ +/* $OpenBSD: subr.c,v 1.12 2001/06/11 15:18:50 mickey Exp $ */ /* * Copyright (c) 1983, 1993 @@ -35,7 +35,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)subr.c 8.1 (Berkeley) 6/4/93";*/ -static char rcsid[] = "$OpenBSD: subr.c,v 1.11 2001/01/28 19:34:28 niklas Exp $"; +static char rcsid[] = "$OpenBSD: subr.c,v 1.12 2001/06/11 15:18:50 mickey Exp $"; #endif /* not lint */ /* @@ -99,8 +99,8 @@ gettable(name, buf) for (np = gettynums; np->field; np++) printf("cgetnum: %s=%d\n", np->field, np->value); for (fp = gettyflags; fp->field; fp++) - printf("cgetflags: %s='%c' set='%c'\n", fp->field, - fp->value + '0', fp->set + '0'); + printf("cgetflags: %s='%c' set='%c'\n", fp->field, + fp->value + '0', fp->set + '0'); exit(1); #endif /* DEBUG */ } @@ -603,10 +603,9 @@ edithost(pat) pat++; } if (*host) - strncpy(res, host, sizeof editedhost - (res - editedhost) - 1); + strlcpy(res, host, sizeof editedhost - (res - editedhost)); else *res = '\0'; - editedhost[sizeof editedhost - 1] = '\0'; } void diff --git a/libexec/rlogind/rlogind.c b/libexec/rlogind/rlogind.c index 4ea1f079b0c..a9b5c3a5e3e 100644 --- a/libexec/rlogind/rlogind.c +++ b/libexec/rlogind/rlogind.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rlogind.c,v 1.27 2001/01/28 19:34:31 niklas Exp $ */ +/* $OpenBSD: rlogind.c,v 1.28 2001/06/11 15:18:50 mickey Exp $ */ /*- * Copyright (c) 1983, 1988, 1989, 1993 @@ -41,7 +41,7 @@ static char copyright[] = #ifndef lint /* from: static char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93"; */ -static char *rcsid = "$OpenBSD: rlogind.c,v 1.27 2001/01/28 19:34:31 niklas Exp $"; +static char *rcsid = "$OpenBSD: rlogind.c,v 1.28 2001/06/11 15:18:50 mickey Exp $"; #endif /* not lint */ /* @@ -204,7 +204,7 @@ doit(f, fromp) int master, pid, on = 1; int authenticated = 0; char hostname[MAXHOSTNAMELEN]; - int good = 0; + int good; char c; char naddr[NI_MAXHOST]; char saddr[NI_MAXHOST]; @@ -248,18 +248,18 @@ doit(f, fromp) exit(1); } + good = 0; if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len, saddr, sizeof(saddr), NULL, 0, NI_NAMEREQD) == 0) { - strncpy(hostname, saddr, sizeof(hostname)-1); + strlcpy(hostname, saddr, sizeof(hostname)); if (check_all) { - good = 0; memset(&hints, 0, sizeof(hints)); hints.ai_family = fromp->sa_family; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME; res0 = NULL; gaierror = getaddrinfo(hostname, "0", &hints, &res0); - for (res = res0; good == 0 && res; res = res->ai_next) { + for (res = res0; !good && res; res = res->ai_next) { if (res->ai_family != fromp->sa_family) continue; if (res->ai_addrlen != fromp->sa_len) @@ -273,12 +273,10 @@ doit(f, fromp) freeaddrinfo(res0); } else good = 1; - } else - good = 0; + } /* aha, the DNS looks spoofed */ - if (good == 0) - strncpy(hostname, naddr, sizeof(hostname)-1); - hostname[sizeof(hostname)-1] = '\0'; + if (!good) + strlcpy(hostname, naddr, sizeof(hostname)); #ifdef KERBEROS diff --git a/libexec/rpc.rquotad/rquotad.c b/libexec/rpc.rquotad/rquotad.c index e354ab37f12..b40080848e4 100644 --- a/libexec/rpc.rquotad/rquotad.c +++ b/libexec/rpc.rquotad/rquotad.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rquotad.c,v 1.9 2001/02/12 07:15:46 deraadt Exp $ */ +/* $OpenBSD: rquotad.c,v 1.10 2001/06/11 15:18:51 mickey Exp $ */ /* * by Manuel Bouyer (bouyer@ensta.fr) * @@ -323,8 +323,7 @@ hasquota(fs, qfnamep) qfextension[USRQUOTA], QUOTAFILENAME); initname = 1; } - strncpy(buf, fs->fs_mntops, sizeof buf); - buf[sizeof(buf) - 1] = '\0'; + strlcpy(buf, fs->fs_mntops, sizeof buf); for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { if ((cp = strchr(opt, '='))) *cp++ = '\0'; diff --git a/libexec/rshd/rshd.c b/libexec/rshd/rshd.c index 1676ed460a9..72f7958a46d 100644 --- a/libexec/rshd/rshd.c +++ b/libexec/rshd/rshd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rshd.c,v 1.37 2001/05/29 21:37:16 millert Exp $ */ +/* $OpenBSD: rshd.c,v 1.38 2001/06/11 15:18:51 mickey Exp $ */ /*- * Copyright (c) 1988, 1989, 1992, 1993, 1994 @@ -41,7 +41,7 @@ static char copyright[] = #ifndef lint /* from: static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; */ -static char *rcsid = "$OpenBSD: rshd.c,v 1.37 2001/05/29 21:37:16 millert Exp $"; +static char *rcsid = "$OpenBSD: rshd.c,v 1.38 2001/06/11 15:18:51 mickey Exp $"; #endif /* not lint */ /* @@ -385,8 +385,7 @@ doit(fromp) if (!use_kerberos) #endif if (check_all || local_domain(saddr)) { - strncpy(remotehost, saddr, sizeof(remotehost) - 1); - remotehost[sizeof(remotehost) - 1] = 0; + strlcpy(remotehost, saddr, sizeof(remotehost)); errorhost = remotehost; memset(&hints, 0, sizeof(hints)); hints.ai_family = fromp->sa_family; @@ -429,15 +428,14 @@ doit(fromp) } } } - hostname = strncpy(hostnamebuf, hostname, - sizeof(hostnamebuf) - 1); + strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf)); + hostname = hostnamebuf; if (res0) freeaddrinfo(res0); } else - errorhost = hostname = strncpy(hostnamebuf, - naddr, sizeof(hostnamebuf) - 1); + strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf)); + errorhost = hostname = hostnamebuf; - hostnamebuf[sizeof(hostnamebuf) - 1] = '\0'; #ifdef KERBEROS if (use_kerberos) { kdata = (AUTH_DAT *) authbuf; diff --git a/libexec/telnetd/sys_term.c b/libexec/telnetd/sys_term.c index 83d5f5ab417..5cee73127ac 100644 --- a/libexec/telnetd/sys_term.c +++ b/libexec/telnetd/sys_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_term.c,v 1.22 2001/01/17 23:51:54 deraadt Exp $ */ +/* $OpenBSD: sys_term.c,v 1.23 2001/06/11 15:18:52 mickey Exp $ */ /* $NetBSD: sys_term.c,v 1.9 1996/03/20 04:25:53 tls Exp $ */ /* @@ -39,7 +39,7 @@ static char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95"; static char rcsid[] = "$NetBSD: sys_term.c,v 1.8 1996/02/28 20:38:21 thorpej Exp $"; #else -static char rcsid[] = "$OpenBSD: sys_term.c,v 1.22 2001/01/17 23:51:54 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: sys_term.c,v 1.23 2001/06/11 15:18:52 mickey Exp $"; #endif #endif /* not lint */ @@ -2138,10 +2138,8 @@ jobend(jid, path, user) } if (path) { - strncpy(saved_path, path, sizeof(wtmp.ut_tpath)); - strncpy(saved_user, user, sizeof(wtmp.ut_user)); - saved_path[sizeof(saved_path)] = '\0'; - saved_user[sizeof(saved_user)] = '\0'; + strlcpy(saved_path, path, sizeof(saved_path)); + strlcpy(saved_user, user, sizeof(saved_user)); } if (saved_jid == 0) { saved_jid = jid; diff --git a/libexec/telnetd/telnetd.c b/libexec/telnetd/telnetd.c index c7ce8900470..5da6b9a4164 100644 --- a/libexec/telnetd/telnetd.c +++ b/libexec/telnetd/telnetd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: telnetd.c,v 1.28 2001/05/25 10:25:22 hin Exp $ */ +/* $OpenBSD: telnetd.c,v 1.29 2001/06/11 15:18:52 mickey Exp $ */ /* $NetBSD: telnetd.c,v 1.6 1996/03/20 04:25:57 tls Exp $ */ /* @@ -45,7 +45,7 @@ static char copyright[] = static char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95"; static char rcsid[] = "$NetBSD: telnetd.c,v 1.5 1996/02/28 20:38:23 thorpej Exp $"; #else -static char rcsid[] = "$OpenBSD: telnetd.c,v 1.28 2001/05/25 10:25:22 hin Exp $"; +static char rcsid[] = "$OpenBSD: telnetd.c,v 1.29 2001/06/11 15:18:52 mickey Exp $"; #endif #endif /* not lint */ @@ -705,14 +705,12 @@ getterminaltype(name, name_sz) * we have to just go with what we (might) have already gotten. */ if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) { - (void) strncpy(first, terminaltype, sizeof(first)-1); - first[sizeof(first)-1] = '\0'; + strlcpy(first, terminaltype, sizeof(first)); for(;;) { /* * Save the unknown name, and request the next name. */ - (void) strncpy(last, terminaltype, sizeof(last)-1); - last[sizeof(last)-1] = '\0'; + strlcpy(last, terminaltype, sizeof(last)); _gettermname(); if (terminaltypeok(terminaltype)) break; @@ -730,10 +728,8 @@ getterminaltype(name, name_sz) * the start of the list. */ _gettermname(); - if (strncmp(first, terminaltype, sizeof(first)) != 0) { - (void) strncpy(terminaltype, first, sizeof(terminaltype)-1); - terminaltype[sizeof(terminaltype)-1] = '\0'; - } + if (strncmp(first, terminaltype, sizeof(first)) != 0) + strlcpy(terminaltype, first, sizeof(terminaltype)); break; } } @@ -823,8 +819,7 @@ doit(who) fatal(net, "Out of ptys"); if ((pty = open(lp, O_RDWR)) >= 0) { - strncpy(line, lp, sizeof line -1); - line[sizeof line -1] = '\0'; + strlcpy(line, lp, sizeof line); line[5] = 't'; break; } @@ -884,8 +879,7 @@ doit(who) * We must make a copy because Kerberos is probably going * to also do a gethost* and overwrite the static data... */ - strncpy(remote_host_name, host, sizeof(remote_host_name)-1); - remote_host_name[sizeof(remote_host_name)-1] = 0; + strlcpy(remote_host_name, host, sizeof(remote_host_name)); host = remote_host_name; (void) gethostname(host_name, sizeof (host_name)); @@ -1161,10 +1155,8 @@ telnet(f, p, host) HE = gtgetstr("he", &cp); HN = gtgetstr("hn", &cp); IM = gtgetstr("im", &cp); - if (HN && *HN) { - strncpy(host_name, HN, sizeof host_name - 1); - host_name[sizeof host_name -1] = '\0'; - } + if (HN && *HN) + strlcpy(host_name, HN, sizeof host_name); if (IM == 0) IM = ""; } else { diff --git a/libexec/telnetd/utility.c b/libexec/telnetd/utility.c index 69e16fb4ac9..6f3cc3e1983 100644 --- a/libexec/telnetd/utility.c +++ b/libexec/telnetd/utility.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utility.c,v 1.15 1998/07/10 08:06:24 deraadt Exp $ */ +/* $OpenBSD: utility.c,v 1.16 2001/06/11 15:18:53 mickey Exp $ */ /* $NetBSD: utility.c,v 1.9 1996/02/28 20:38:29 thorpej Exp $ */ /* @@ -39,7 +39,7 @@ static char sccsid[] = "@(#)utility.c 8.4 (Berkeley) 5/30/95"; static char rcsid[] = "$NetBSD: utility.c,v 1.9 1996/02/28 20:38:29 thorpej Exp $"; #else -static char rcsid[] = "$OpenBSD: utility.c,v 1.15 1998/07/10 08:06:24 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: utility.c,v 1.16 2001/06/11 15:18:53 mickey Exp $"; #endif #endif /* not lint */ @@ -396,7 +396,6 @@ edithost(pat, host) register char *host; { register char *res = editedhost; - char *strncpy(); if (!pat) pat = ""; @@ -424,11 +423,9 @@ edithost(pat, host) pat++; } if (*host) - (void) strncpy(res, host, - sizeof editedhost - (res - editedhost) -1); + strlcpy(res, host, sizeof editedhost - (res - editedhost)); else *res = '\0'; - editedhost[sizeof editedhost - 1] = '\0'; } static char *putlocation; diff --git a/libexec/tftpd/tftpd.c b/libexec/tftpd/tftpd.c index cdeb387dcd7..85faa6fa5f0 100644 --- a/libexec/tftpd/tftpd.c +++ b/libexec/tftpd/tftpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tftpd.c,v 1.17 2001/05/09 18:06:35 deraadt Exp $ */ +/* $OpenBSD: tftpd.c,v 1.18 2001/06/11 15:18:53 mickey Exp $ */ /* * Copyright (c) 1983 Regents of the University of California. @@ -41,7 +41,7 @@ char copyright[] = #ifndef lint /*static char sccsid[] = "from: @(#)tftpd.c 5.13 (Berkeley) 2/26/91";*/ -static char rcsid[] = "$OpenBSD: tftpd.c,v 1.17 2001/05/09 18:06:35 deraadt Exp $: tftpd.c,v 1.6 1997/02/16 23:49:21 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: tftpd.c,v 1.18 2001/06/11 15:18:53 mickey Exp $: tftpd.c,v 1.6 1997/02/16 23:49:21 deraadt Exp $"; #endif /* not lint */ /* @@ -617,10 +617,9 @@ nak(error) pe->e_msg = strerror(error - 100); tp->th_code = EUNDEF; /* set 'undef' errorcode */ } - strcpy(tp->th_msg, pe->e_msg); - length = strlen(pe->e_msg); - tp->th_msg[length] = '\0'; - length += 5; - if (send(peer, buf, length, 0) != length) + length = strlcpy(tp->th_msg, pe->e_msg, sizeof(buf) - 4); + if (length >= sizeof(buf) - 4) + length = sizeof(buf) - 5; + if (send(peer, buf, length + 5, 0) != length) syslog(LOG_ERR, "nak: %m"); } |