From 2d9ec4dac7872c3f93f4bee63b763b6fae15be97 Mon Sep 17 00:00:00 2001 From: itojun Date: Mon, 20 Dec 1999 16:22:41 +0000 Subject: [PATCH] void unbounded sprintf(). fix proc title. --- usr.sbin/faithd/faithd.c | 16 +++++++------ usr.sbin/faithd/ftp.c | 52 ++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/usr.sbin/faithd/faithd.c b/usr.sbin/faithd/faithd.c index 884120eca819..e37f6e8cd648 100644 --- a/usr.sbin/faithd/faithd.c +++ b/usr.sbin/faithd/faithd.c @@ -1,4 +1,4 @@ -/* $NetBSD: faithd.c,v 1.6 1999/12/20 16:03:19 itojun Exp $ */ +/* $NetBSD: faithd.c,v 1.7 1999/12/20 16:22:41 itojun Exp $ */ /* * Copyright (C) 1997 and 1998 WIDE Project. @@ -85,6 +85,7 @@ char *serverpath = NULL; char *serverarg[MAXARGV + 1]; static char *faithdname = NULL; char logname[BUFSIZ]; +char procname[BUFSIZ]; struct myaddrs { struct myaddrs *next; struct sockaddr *addr; @@ -169,7 +170,7 @@ main(int argc, char *argv[]) memset(&ss, 0, sizeof(ss)); memset(&hints, 0, sizeof(hints)); - sprintf(serv, "%u", NAMESERVER_PORT); + snprintf(serv, sizeof(serv), "%u", NAMESERVER_PORT); hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(ns, serv, &hints, &res) == 0) { res_init(); @@ -272,7 +273,8 @@ main(int argc, char *argv[]) start_daemon(); - sprintf(logname, "accepting port %s", service); + snprintf(logname, sizeof(logname), "faithd %s", service); + snprintf(procname, sizeof(procname), "accepting port %s", service); openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON); syslog(LOG_INFO, "Staring faith daemon for %s port", service); @@ -296,7 +298,7 @@ play_service(int s_wld) * Wait, accept, fork, faith.... */ again: - setproctitle(logname); + setproctitle(procname); FD_ZERO(&rfds); FD_SET(s_wld, &rfds); @@ -642,7 +644,7 @@ exit_error(const char *fmt, ...) char buf[BUFSIZ]; va_start(ap, fmt); - vsprintf(buf, fmt, ap); + vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); fprintf(stderr, "%s\n", buf); exit(EXIT_FAILURE); @@ -655,7 +657,7 @@ exit_failure(const char *fmt, ...) char buf[BUFSIZ]; va_start(ap, fmt); - vsprintf(buf, fmt, ap); + vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); syslog(LOG_ERR, buf); exit(EXIT_FAILURE); @@ -668,7 +670,7 @@ exit_success(const char *fmt, ...) char buf[BUFSIZ]; va_start(ap, fmt); - vsprintf(buf, fmt, ap); + vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); syslog(LOG_INFO, buf); exit(EXIT_SUCCESS); diff --git a/usr.sbin/faithd/ftp.c b/usr.sbin/faithd/ftp.c index b2f2941baa01..641dcbeb3bfc 100644 --- a/usr.sbin/faithd/ftp.c +++ b/usr.sbin/faithd/ftp.c @@ -1,4 +1,4 @@ -/* $NetBSD: ftp.c,v 1.3 1999/12/20 15:35:55 itojun Exp $ */ +/* $NetBSD: ftp.c,v 1.4 1999/12/20 16:22:41 itojun Exp $ */ /* * Copyright (C) 1997 and 1998 WIDE Project. @@ -404,7 +404,7 @@ ftp_copyresult(int src, int dst, enum state state) case NONE: if (!passivemode && rbuf[0] == '1') { if (ftp_activeconn() < 0) { - n = sprintf(rbuf, + n = snprintf(rbuf, sizeof(rbuf), "425 Cannot open data connetion\r\n"); } } @@ -497,7 +497,7 @@ passivefail0: wport6 = socket(sin6->sin6_family, SOCK_STREAM, 0); if (wport6 == -1) { passivefail: - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "500 could not translate from PASV\r\n"); write(src, sbuf, n); return n; @@ -553,7 +553,7 @@ passivefail: a = (char *)&sin6->sin6_addr; p = (char *)&sin6->sin6_port; - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "228 Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)\r\n", 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), @@ -564,7 +564,7 @@ passivefail: passivemode = 1; return n; } else { - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "229 Entering Extended Passive Mode (|||%d|)\r\n", ntohs(sin6->sin6_port)); write(dst, sbuf, n); @@ -618,7 +618,7 @@ passivefail1: wport6 = socket(sin->sin_family, SOCK_STREAM, 0); if (wport6 == -1) { passivefail2: - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "500 could not translate from EPSV\r\n"); write(src, sbuf, n); return n; @@ -674,7 +674,7 @@ passivefail2: a = (char *)&sin->sin_addr; p = (char *)&sin->sin_port; - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); @@ -778,7 +778,7 @@ ftp_copycommand(int src, int dst, enum state *state) wport4 = wport6 = port4 = port6 = -1; if (epsvall) { - n = sprintf(sbuf, "501 %s disallowed in EPSV ALL\r\n", + n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n", cmd); write(src, sbuf, n); return n; @@ -792,7 +792,7 @@ ftp_copycommand(int src, int dst, enum state *state) &ho[12], &ho[13], &ho[14], &ho[15], &pal, &po[0], &po[1]); if (n != 21 || af != 6 || hal != 16|| pal != 2) { - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "501 illegal parameter to LPRT\r\n"); write(src, sbuf, n); return n; @@ -813,7 +813,7 @@ sendport: error = getsockname(dst, (struct sockaddr *)&data4, &n); if (error == -1) { lprtfail: - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "500 could not translate to PORT\r\n"); write(src, sbuf, n); return n; @@ -854,7 +854,7 @@ lprtfail: sin = (struct sockaddr_in *)&data4; a = (char *)&sin->sin_addr; p = (char *)&sin->sin_port; - n = sprintf(sbuf, "PORT %d,%d,%d,%d,%d,%d\r\n", + n = snprintf(sbuf, sizeof(sbuf), "PORT %d,%d,%d,%d,%d,%d\r\n", UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); write(dst, sbuf, n); @@ -877,7 +877,7 @@ lprtfail: wport4 = wport6 = port4 = port6 = -1; if (epsvall) { - n = sprintf(sbuf, "501 %s disallowed in EPSV ALL\r\n", + n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n", cmd); write(src, sbuf, n); return n; @@ -890,7 +890,7 @@ lprtfail: p++; if (!*p) { eprtparamfail: - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "501 illegal parameter to EPRT\r\n"); write(src, sbuf, n); return n; @@ -911,7 +911,7 @@ eprtparamfail: n = sscanf(afp, "%d", &af); if (n != 1 || af != 2) { - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "501 unsupported address family to EPRT\r\n"); write(src, sbuf, n); return n; @@ -920,13 +920,13 @@ eprtparamfail: hints.ai_family = AF_UNSPEC; error = getaddrinfo(hostp, portp, &hints, &res); if (error) { - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "501 EPRT: %s\r\n", gai_strerror(error)); write(src, sbuf, n); return n; } if (res->ai_next) { - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "501 EPRT: %s resolved to multiple addresses\r\n", hostp); write(src, sbuf, n); return n; @@ -948,14 +948,14 @@ eprtparamfail: wport4 = wport6 = port4 = port6 = -1; if (epsvall) { - n = sprintf(sbuf, "501 %s disallowed in EPSV ALL\r\n", + n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n", cmd); write(src, sbuf, n); return n; } /* transmit PASV */ - n = sprintf(sbuf, "PASV\r\n"); + n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n"); write(dst, sbuf, n); *state = LPSV; passivemode = 0; /* to be set to 1 later */ @@ -970,7 +970,7 @@ eprtparamfail: close(port6); wport4 = wport6 = port4 = port6 = -1; - n = sprintf(sbuf, "PASV\r\n"); + n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n"); write(dst, sbuf, n); *state = EPSV; passivemode = 0; /* to be set to 1 later */ @@ -981,7 +981,7 @@ eprtparamfail: * EPSV ALL */ epsvall = 1; - n = sprintf(sbuf, "200 EPSV ALL command successful.\r\n"); + n = snprintf(sbuf, sizeof(sbuf), "200 EPSV ALL command successful.\r\n"); write(src, sbuf, n); return n; #ifdef FAITH4 @@ -1003,7 +1003,7 @@ eprtparamfail: n = sscanf(p, "%u,%u,%u,%u,%u,%u", &ho[0], &ho[1], &ho[2], &ho[3], &po[0], &po[1]); if (n != 6) { - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "501 illegal parameter to PORT\r\n"); write(src, sbuf, n); return n; @@ -1023,7 +1023,7 @@ eprtparamfail: error = getsockname(dst, (struct sockaddr *)&data4, &n); if (error == -1) { portfail: - n = sprintf(sbuf, + n = snprintf(sbuf, sizeof(sbuf), "500 could not translate to EPRT\r\n"); write(src, sbuf, n); return n; @@ -1065,7 +1065,7 @@ portfail: wport4 = -1; goto portfail; } - n = sprintf(sbuf, "EPRT |%d|%s|%s|\r\n", af, host, serv); + n = snprintf(sbuf, sizeof(sbuf), "EPRT |%d|%s|%s|\r\n", af, host, serv); write(dst, sbuf, n); *state = nstate; passivemode = 0; @@ -1084,7 +1084,7 @@ portfail: wport4 = wport6 = port4 = port6 = -1; /* transmit EPSV */ - n = sprintf(sbuf, "EPSV\r\n"); + n = snprintf(sbuf, sizeof(sbuf), "EPSV\r\n"); write(dst, sbuf, n); *state = PASV; passivemode = 0; /* to be set to 1 later */ @@ -1094,7 +1094,7 @@ portfail: /* * reject PORT/PASV */ - n = sprintf(sbuf, "502 %s not implemented.\r\n", cmd); + n = snprintf(sbuf, sizeof(sbuf), "502 %s not implemented.\r\n", cmd); write(src, sbuf, n); return n; #endif /* FAITH4 */ @@ -1111,7 +1111,7 @@ portfail: */ if (ftp_passiveconn() < 0) { - n = sprintf(sbuf, "425 Cannot open data connetion\r\n"); + n = snprintf(sbuf, sizeof(sbuf), "425 Cannot open data connetion\r\n"); write(src, sbuf, n); } else { /* simply relay the command */