From cc3fd56f96f5726a445a8a6090bda47a94122df6 Mon Sep 17 00:00:00 2001 From: Garance A Drosehn Date: Fri, 15 Jun 2001 22:03:07 +0000 Subject: [PATCH] Rename a few global variables which hold hostname-related values to be more sensible/understandable. 'from'->'from_host' 'host'->'local_host' 'fromb'->'frombuf' 'fromhost'->'origin_host' and a local-variable named 'host'->'hostbuf'. This fixes some compile-time warnings about local variables shadowing global variables. Other than renaming variables, the only actual code changes are to call strlcpy() instead of strncpy() when setting those (renamed) variables, and that 'from_ip' is now a strdup()-created buffer instead of being a static buffer compiled in as 1025 bytes. Reviewed by: freebsd-print@bostonradio.org (an earlier version) MFC after: 1 week --- usr.sbin/lpr/common_source/common.c | 5 ++-- usr.sbin/lpr/common_source/displayq.c | 14 +++++----- usr.sbin/lpr/common_source/lp.h | 22 ++++++++++++--- usr.sbin/lpr/common_source/net.c | 12 ++++++--- usr.sbin/lpr/common_source/rmjob.c | 23 ++++++++-------- usr.sbin/lpr/lpd/extern.h | 1 - usr.sbin/lpr/lpd/lpd.c | 39 +++++++++++++-------------- usr.sbin/lpr/lpd/printjob.c | 21 +++++++-------- usr.sbin/lpr/lpd/recvjob.c | 7 ++--- usr.sbin/lpr/lpq/lpq.c | 2 +- usr.sbin/lpr/lpr/lpr.c | 11 ++++---- usr.sbin/lpr/lprm/lprm.c | 2 +- 12 files changed, 90 insertions(+), 69 deletions(-) diff --git a/usr.sbin/lpr/common_source/common.c b/usr.sbin/lpr/common_source/common.c index 8e705db4900..8c434b024d6 100644 --- a/usr.sbin/lpr/common_source/common.c +++ b/usr.sbin/lpr/common_source/common.c @@ -553,8 +553,9 @@ fatal(pp, msg, va_alist) #else va_start(ap); #endif - if (from != host) - (void)printf("%s: ", host); + /* this error message is being sent to the 'from_host' */ + if (from_host != local_host) + (void)printf("%s: ", local_host); (void)printf("%s: ", progname); if (pp && pp->printer) (void)printf("%s: ", pp->printer); diff --git a/usr.sbin/lpr/common_source/displayq.c b/usr.sbin/lpr/common_source/displayq.c index df82d56673b..7eef91e13d0 100644 --- a/usr.sbin/lpr/common_source/displayq.c +++ b/usr.sbin/lpr/common_source/displayq.c @@ -127,7 +127,7 @@ displayq(struct printer *pp, int format) if (ret >= 0) { if (statb.st_mode & LFM_PRINT_DIS) { if (pp->remote) - printf("%s: ", host); + printf("%s: ", local_host); printf("Warning: %s is down: ", pp->printer); seteuid(euid); fd = open(pp->status_file, O_RDONLY|O_SHLOCK); @@ -141,7 +141,7 @@ displayq(struct printer *pp, int format) } if (statb.st_mode & LFM_QUEUE_DIS) { if (pp->remote) - printf("%s: ", host); + printf("%s: ", local_host); printf("Warning: %s queue is turned off\n", pp->printer); } @@ -179,7 +179,7 @@ displayq(struct printer *pp, int format) * Print the status file. */ if (pp->remote) - printf("%s: ", host); + printf("%s: ", local_host); seteuid(euid); fd = open(pp->status_file, O_RDONLY|O_SHLOCK); seteuid(uid); @@ -238,8 +238,8 @@ displayq(struct printer *pp, int format) alarm(0); (void)signal(SIGALRM, savealrm); if (fd < 0) { - if (from != host) - printf("%s: ", host); + if (from_host != local_host) + printf("%s: ", local_host); printf("connection to %s is down\n", pp->remote_host); } else { @@ -259,7 +259,7 @@ static void warn(const struct printer *pp) { if (pp->remote) - printf("%s: ", host); + printf("%s: ", local_host); puts("Warning: no daemon present"); current[0] = '\0'; } @@ -396,7 +396,7 @@ inlist(char *uname, char *cfile) for (n = 0, cp = cfile+3; isdigit(*cp); ) n = n * 10 + (*cp++ - '0'); for (r = requ; r < &requ[requests]; r++) - if (*r == n && !strcmp(cp, from)) + if (*r == n && !strcmp(cp, from_host)) return(1); return(0); } diff --git a/usr.sbin/lpr/common_source/lp.h b/usr.sbin/lpr/common_source/lp.h index 3cee6957c97..9ca0612353c 100644 --- a/usr.sbin/lpr/common_source/lp.h +++ b/usr.sbin/lpr/common_source/lp.h @@ -154,10 +154,24 @@ struct request { */ extern char line[BUFSIZ]; extern const char *progname; /* program name (lpr, lpq, etc) */ - /* host machine name */ -extern char host[MAXHOSTNAMELEN]; -extern char *from; /* client's machine name */ -extern char from_ip[NI_MAXHOST]; /* client machine's IP address */ + + /* + * 'local_host' is the name of the machine that lpd (lpr, whatever) + * is actually running on. + * + * 'from_host' will point to the 'host' variable when receiving a job + * from a user on the same host, or "somewhere else" when receiving a + * job from a remote host. If 'from_host != local_host', then 'from_ip' + * is the character representation of the IP address of from_host (note + * that string could be an IPv6 address). + * + * Also note that when 'from_host' is not pointing at 'local_host', the + * string it is pointing at may be as long as NI_MAXHOST (which is very + * likely to be much longer than MAXHOSTNAMELEN). + */ +extern char local_host[MAXHOSTNAMELEN]; +extern const char *from_host; /* client's machine name */ +extern const char *from_ip; /* client machine's IP address */ extern int requ[]; /* job number of spool entries */ extern int requests; /* # of spool requests */ diff --git a/usr.sbin/lpr/common_source/net.c b/usr.sbin/lpr/common_source/net.c index 619c253cbbb..df70ef46613 100644 --- a/usr.sbin/lpr/common_source/net.c +++ b/usr.sbin/lpr/common_source/net.c @@ -65,9 +65,15 @@ static const char rcsid[] = #include "lp.local.h" #include "pathnames.h" -char host[MAXHOSTNAMELEN]; /* host machine name */ -char *from = host; /* client's machine name */ -char from_ip[NI_MAXHOST] = ""; /* client machine's IP address */ +/* + * 'local_host' is always the hostname of the machine which is running + * lpr (lpd, whatever), while 'from_host' either points at 'local_host' + * or points at a different buffer when receiving a job from a remote + * machine (and that buffer has the hostname of that remote machine). + */ +char local_host[MAXHOSTNAMELEN]; /* host running lpd/lpr */ +const char *from_host = local_host; /* client's machine name */ +const char *from_ip = ""; /* client machine's IP address */ #ifdef INET6 u_char family = PF_UNSPEC; diff --git a/usr.sbin/lpr/common_source/rmjob.c b/usr.sbin/lpr/common_source/rmjob.c index 39a7d0f132b..3a60efb14eb 100644 --- a/usr.sbin/lpr/common_source/rmjob.c +++ b/usr.sbin/lpr/common_source/rmjob.c @@ -106,9 +106,9 @@ rmjob(const char *printer) } } if (!strcmp(person, "-all")) { - if (from == host) + if (from_host == local_host) fatal(pp, "The login name \"-all\" is reserved"); - all = 1; /* all those from 'from' */ + all = 1; /* all those from 'from_host' */ person = root; } @@ -218,8 +218,8 @@ do_unlink(char *file) { int ret; - if (from != host) - printf("%s: ", host); + if (from_host != local_host) + printf("%s: ", local_host); seteuid(euid); ret = unlink(file); seteuid(uid); @@ -242,7 +242,7 @@ chk(char *file) if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') return(0); - if (all && (from == host || !strcmp(from, file+6))) + if (all && (from_host == local_host || !strcmp(from_host, file+6))) return(1); /* @@ -288,12 +288,13 @@ chk(char *file) int isowner(char *owner, char *file) { - if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) + if (!strcmp(person, root) && (from_host == local_host || + !strcmp(from_host, file+6))) return (1); - if (!strcmp(person, owner) && !strcmp(from, file+6)) + if (!strcmp(person, owner) && !strcmp(from_host, file+6)) return (1); - if (from != host) - printf("%s: ", host); + if (from_host != local_host) + printf("%s: ", local_host); printf("%s: Permission denied\n", file); return(0); } @@ -362,8 +363,8 @@ rmremote(const struct printer *pp) rem = getport(pp, pp->remote_host, 0); (void)signal(SIGALRM, savealrm); if (rem < 0) { - if (from != host) - printf("%s: ", host); + if (from_host != local_host) + printf("%s: ", local_host); printf("connection to %s is down\n", pp->remote_host); } else { if (writev(rem, iov, niov) != totlen) diff --git a/usr.sbin/lpr/lpd/extern.h b/usr.sbin/lpr/lpd/extern.h index dc9b1094067..497594ec727 100644 --- a/usr.sbin/lpr/lpd/extern.h +++ b/usr.sbin/lpr/lpd/extern.h @@ -37,7 +37,6 @@ #include extern char scnkey[][HEIGHT]; /* in lpdchar.c */ -extern char fromb[]; extern int lflag; /* in lpd.c */ struct printer; diff --git a/usr.sbin/lpr/lpd/lpd.c b/usr.sbin/lpr/lpd/lpd.c index d7de7492ec7..ed912c98e15 100644 --- a/usr.sbin/lpr/lpd/lpd.c +++ b/usr.sbin/lpr/lpd/lpd.c @@ -138,7 +138,7 @@ main(int argc, char **argv) euid = geteuid(); /* these shouldn't be different */ uid = getuid(); socket_debug = 0; - gethostname(host, sizeof(host)); + gethostname(local_host, sizeof(local_host)); progname = "lpd"; @@ -413,7 +413,8 @@ int requ[MAXREQUESTS]; /* job number of spool entries */ int requests; /* # of spool requests */ char *person; /* name of person doing lprm */ -char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */ + /* buffer to hold the client's machine-name */ +static char frombuf[MAXHOSTNAMELEN]; char cbuf[BUFSIZ]; /* command line buffer */ const char *cmdnames[] = { "null", @@ -450,10 +451,10 @@ doit(void) if (lflag) { if (*cp >= '\1' && *cp <= '\5') syslog(LOG_INFO, "%s requests %s %s", - from, cmdnames[(u_char)*cp], cp+1); + from_host, cmdnames[(u_char)*cp], cp+1); else syslog(LOG_INFO, "bad request (%d) from %s", - *cp, from); + *cp, from_host); } switch (*cp++) { case CMD_CHECK_QUE: /* check the queue, print any jobs there */ @@ -611,7 +612,7 @@ chkhost(struct sockaddr *f) register FILE *hostf; int first = 1; int good = 0; - char host[NI_MAXHOST], ip[NI_MAXHOST]; + char hostbuf[NI_MAXHOST], ip[NI_MAXHOST]; char serv[NI_MAXSERV]; int error, addrlen; caddr_t addr; @@ -622,45 +623,43 @@ chkhost(struct sockaddr *f) fatal(0, "Malformed from address"); /* Need real hostname for temporary filenames */ - error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0, - NI_NAMEREQD); + error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0, + NI_NAMEREQD); if (error) { - error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0, - NI_NUMERICHOST | NI_WITHSCOPEID); + error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), + NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID); if (error) fatal(0, "Host name for your address unknown"); else fatal(0, "Host name for your address (%s) unknown", - host); + hostbuf); } - (void)strncpy(fromb, host, sizeof(fromb) - 1); - fromb[sizeof(fromb) - 1] = '\0'; - from = fromb; + strlcpy(frombuf, hostbuf, sizeof(frombuf)); + from_host = frombuf; /* Need address in stringform for comparison (no DNS lookup here) */ - error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0, - NI_NUMERICHOST | NI_WITHSCOPEID); + error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0, + NI_NUMERICHOST | NI_WITHSCOPEID); if (error) fatal(0, "Cannot print address"); - strncpy(from_ip, host, NI_MAXHOST); - from_ip[sizeof(from_ip) - 1] = '\0'; + from_ip = strdup(hostbuf); /* Reject numeric addresses */ memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_DGRAM; /*dummy*/ hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; - if (getaddrinfo(fromb, NULL, &hints, &res) == 0) { + if (getaddrinfo(from_host, NULL, &hints, &res) == 0) { freeaddrinfo(res); - fatal(0, "reverse lookup results in non-FQDN %s", fromb); + fatal(0, "reverse lookup results in non-FQDN %s", from_host); } /* Check for spoof, ala rlogind */ memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_DGRAM; /*dummy*/ - error = getaddrinfo(fromb, NULL, &hints, &res); + error = getaddrinfo(from_host, NULL, &hints, &res); if (error) { fatal(0, "hostname for your address (%s) unknown: %s", from_ip, gai_strerror(error)); diff --git a/usr.sbin/lpr/lpd/printjob.c b/usr.sbin/lpr/lpd/printjob.c index 14dc49c6873..b46d2782f30 100644 --- a/usr.sbin/lpr/lpd/printjob.c +++ b/usr.sbin/lpr/lpd/printjob.c @@ -107,7 +107,7 @@ static char title[80]; /* ``pr'' title */ static char locale[80]; /* ``pr'' locale */ static char class[32]; /* classification field */ -static char fromhost[MAXHOSTNAMELEN]; /* user's host machine */ +static char origin_host[MAXHOSTNAMELEN]; /* user's host machine */ /* indentation size in static characters */ static char indent[10] = "-i0"; static char jobname[100]; /* job or file name */ @@ -417,8 +417,7 @@ printit(struct printer *pp, char *file) while (getline(cfp)) switch (line[0]) { case 'H': - strncpy(fromhost, line+1, sizeof(fromhost) - 1); - fromhost[sizeof(fromhost) - 1] = '\0'; + strlcpy(origin_host, line + 1, sizeof(origin_host)); if (class[0] == '\0') { strncpy(class, line+1, sizeof(class) - 1); class[sizeof(class) - 1] = '\0'; @@ -747,7 +746,7 @@ print(struct printer *pp, int format, char *file) av[n++] = "-n"; av[n++] = logname; av[n++] = "-h"; - av[n++] = fromhost; + av[n++] = origin_host; av[n++] = pp->acct_file; av[n] = 0; fo = pfd; @@ -874,8 +873,7 @@ sendit(struct printer *pp, char *file) i = i * 10 + (*cp++ - '0'); fino = i; } else if (line[0] == 'H') { - strncpy(fromhost, line+1, sizeof(fromhost) - 1); - fromhost[sizeof(fromhost) - 1] = '\0'; + strlcpy(origin_host, line + 1, sizeof(origin_host)); if (class[0] == '\0') { strncpy(class, line+1, sizeof(class) - 1); class[sizeof(class) - 1] = '\0'; @@ -1005,7 +1003,7 @@ sendfile(struct printer *pp, int type, char *file, char format) av[++n] = "-n"; av[++n] = logname; av[++n] = "-h"; - av[++n] = fromhost; + av[++n] = origin_host; av[++n] = pp->acct_file; av[++n] = 0; if ((ifilter = dofork(pp, DORETURN)) == 0) { /* child */ @@ -1166,7 +1164,7 @@ sendfile(struct printer *pp, int type, char *file, char format) openpr(pp); if (type == '\3') trstat_write(pp, TR_SENDING, stb.st_size, logname, - pp->remote_host, fromhost); + pp->remote_host, origin_host); return(OK); } @@ -1321,10 +1319,10 @@ sendmail(struct printer *pp, char *user, int bombed) _exit(0); } else if (s > 0) { /* parent */ dup2(p[1], 1); - printf("To: %s@%s\n", user, fromhost); + printf("To: %s@%s\n", user, origin_host); printf("Subject: %s printer job \"%s\"\n", pp->printer, *jobname ? jobname : ""); - printf("Reply-To: root@%s\n\n", host); + printf("Reply-To: root@%s\n\n", local_host); printf("Your printer job "); if (*jobname) printf("(%s) ", jobname); @@ -1341,7 +1339,8 @@ sendmail(struct printer *pp, char *user, int bombed) cp = "FATALERR"; break; case NOACCT: - printf("\ncould not be printed without an account on %s\n", host); + printf("\ncould not be printed without an account on %s\n", + local_host); cp = "NOACCT"; break; case FILTERERR: diff --git a/usr.sbin/lpr/lpd/recvjob.c b/usr.sbin/lpr/lpd/recvjob.c index 661ad053d1c..99e38dee06d 100644 --- a/usr.sbin/lpr/lpd/recvjob.c +++ b/usr.sbin/lpr/lpd/recvjob.c @@ -186,7 +186,8 @@ readjob(struct printer *pp) * something different than what gethostbyaddr() * returns */ - strncpy(cp + 6, from, sizeof(line) + line - cp - 7); + strncpy(cp + 6, from_host, sizeof(line) + line - cp + - 7); line[sizeof(line) - 1 ] = '\0'; strncpy(tfname, cp, sizeof(tfname) - 1); tfname[sizeof (tfname) - 1] = '\0'; @@ -231,7 +232,7 @@ readjob(struct printer *pp) dfcnt++; trstat_init(pp, dfname, dfcnt); (void) readfile(pp, dfname, size); - trstat_write(pp, TR_RECVING, size, givenid, from, + trstat_write(pp, TR_RECVING, size, givenid, from_host, givenhost); continue; } @@ -386,7 +387,7 @@ frecverr(msg, va_alist) #else va_start(ap); #endif - syslog(LOG_ERR, "Error receiving job from %s:", fromb); + syslog(LOG_ERR, "Error receiving job from %s:", from_host); vsyslog(LOG_ERR, msg, ap); va_end(ap); /* diff --git a/usr.sbin/lpr/lpq/lpq.c b/usr.sbin/lpr/lpq/lpq.c index 600286eddff..96f5d8ec50c 100644 --- a/usr.sbin/lpr/lpq/lpq.c +++ b/usr.sbin/lpr/lpq/lpq.c @@ -93,7 +93,7 @@ main(int argc, char **argv) uid = getuid(); seteuid(uid); progname = *argv; - if (gethostname(host, sizeof(host))) + if (gethostname(local_host, sizeof(local_host))) err(1, "gethostname"); openlog("lpd", 0, LOG_LPR); diff --git a/usr.sbin/lpr/lpr/lpr.c b/usr.sbin/lpr/lpr/lpr.c index a3b2e37a973..143d98a779a 100644 --- a/usr.sbin/lpr/lpr/lpr.c +++ b/usr.sbin/lpr/lpr/lpr.c @@ -80,7 +80,7 @@ static const char rcsid[] = #include "pathnames.h" static char *cfname; /* daemon control files, linked from tf's */ -static char *class = host; /* class title on header page */ +static char *class = local_host; /* class title on header page */ static char *dfname; /* data files */ static char *fonts[4]; /* troff font names */ static char format = 'f'; /* format char for printing files */ @@ -152,7 +152,7 @@ main(int argc, char *argv[]) signal(SIGTERM, cleanup); progname = argv[0]; - gethostname(host, sizeof(host)); + gethostname(local_host, sizeof(local_host)); openlog("lpd", 0, LOG_LPR); errs = 0; @@ -326,7 +326,7 @@ main(int argc, char *argv[]) (void) fchown(tfd, pp->daemon_user, -1); /* owned by daemon for protection */ seteuid(uid); - card('H', host); + card('H', local_host); card('P', person); card('C', class); if (hdr && !pp->no_header) { @@ -854,7 +854,7 @@ mktemps(const struct printer *pp) n = n * 10 + (*cp++ - '0'); } } - len = strlen(pp->spool_dir) + strlen(host) + 8; + len = strlen(pp->spool_dir) + strlen(local_host) + 8; tfname = lmktemp(pp, "tf", n, len); cfname = lmktemp(pp, "cf", n, len); dfname = lmktemp(pp, "df", n, len); @@ -876,6 +876,7 @@ lmktemp(const struct printer *pp, const char *id, int num, int len) if ((s = malloc(len)) == NULL) errx(1, "out of memory"); - (void) snprintf(s, len, "%s/%sA%03d%s", pp->spool_dir, id, num, host); + (void) snprintf(s, len, "%s/%sA%03d%s", pp->spool_dir, id, num, + local_host); return(s); } diff --git a/usr.sbin/lpr/lprm/lprm.c b/usr.sbin/lpr/lprm/lprm.c index b163369485f..7c27a263f60 100644 --- a/usr.sbin/lpr/lprm/lprm.c +++ b/usr.sbin/lpr/lprm/lprm.c @@ -98,7 +98,7 @@ main(int argc, char *argv[]) euid = geteuid(); seteuid(uid); /* be safe */ progname = argv[0]; - gethostname(host, sizeof(host)); + gethostname(local_host, sizeof(local_host)); openlog("lpd", 0, LOG_LPR); /*