Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 25 Sep 2017 17:39:53 +0000 (UTC)
From:      David Bright <dab@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r323986 - stable/11/libexec/getty
Message-ID:  <201709251739.v8PHdrvQ068508@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dab
Date: Mon Sep 25 17:39:53 2017
New Revision: 323986
URL: https://svnweb.freebsd.org/changeset/base/323986

Log:
  MFC r313107 (by danfe):
  Try to fix the old "he capability is stupid" bug in gettytab(5)/getty(8)
  
  There is one capability explicitly documented in gettytab(5) as stupid: he.
  And it is indeed.  It was meant to facilitate system hostname modification,
  but is hardly usable in practice because it allows very limited editing
  (e.g., it depends on a particular hostname length, making it non-generic).
  
  Replace it with simple implementation that treats ``he'' as POSIX extended
  regular expression which is matched against the hostname.  If there are no
  parenthesized subexpressions in the pattern, entire matched string is used
  as the final hostname.  Otherwise, use the first matched subexpression.
  If the pattern does not match, the original hostname is not modified.
  
  Using regex(3) gives more freedom, does not complicate the code very much,
  and makes a lot more sense, in turn making ``he'' less stupid and actually
  useful (e.g., it is now possible to obtain node or domain names from the
  original hostname string, without knowing it in advance).
  
  Approved by:	vangyzen (mentor)
  Sponsored by:	Dell EMC

Modified:
  stable/11/libexec/getty/gettytab.5
  stable/11/libexec/getty/subr.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/libexec/getty/gettytab.5
==============================================================================
--- stable/11/libexec/getty/gettytab.5	Mon Sep 25 15:03:27 2017	(r323985)
+++ stable/11/libexec/getty/gettytab.5	Mon Sep 25 17:39:53 2017	(r323986)
@@ -28,7 +28,7 @@
 .\"     from: @(#)gettytab.5	8.4 (Berkeley) 4/19/94
 .\" $FreeBSD$
 .\" "
-.Dd April 19, 1994
+.Dd February 2, 2017
 .Dt GETTYTAB 5
 .Os
 .Sh NAME
@@ -118,7 +118,7 @@ character
 .Em NOT
 hangup line on last close
 .It "he	str" Ta Dv NULL Ta
-.No "hostname editing string"
+.No "hostname editing regular expression"
 .It "hn	str	hostname	hostname"
 .It "ht	bool	false	terminal has real tabs"
 .It "hw	bool	false	do cts/rts hardware flow control"
@@ -302,18 +302,13 @@ but may also be overridden by the
 table entry.
 In either case it may be edited with the
 .Va \&he
-string.
-A '@' in the
-.Va \&he
-string causes one character from the real hostname to
-be copied to the final hostname.
-A '#' in the
-.Va \&he
-string causes the next character of the real hostname
-to be skipped.
-Each character that
-is neither '@' nor '#' is copied into the final hostname.
-Surplus '@' and '#' characters are ignored.
+POSIX
+.Dq extended
+regular expression, which is matched against the hostname.
+If there are no parenthesized subexpressions in the pattern,
+the entire matched string is used as the final hostname;
+otherwise, the first matched subexpression is used instead.
+If the pattern does not match, the original hostname is not modified.
 .It \&%t
 The tty name.
 .It "\&%m, \&%r, \&%s, \&%v"
@@ -526,10 +521,6 @@ The delay stuff is a real crock.
 Apart form its general lack of flexibility, some
 of the delay algorithms are not implemented.
 The terminal driver should support sane delay settings.
-.Pp
-The
-.Va \&he
-capability is stupid.
 .Pp
 The
 .Xr termcap 5

Modified: stable/11/libexec/getty/subr.c
==============================================================================
--- stable/11/libexec/getty/subr.c	Mon Sep 25 15:03:27 2017	(r323985)
+++ stable/11/libexec/getty/subr.c	Mon Sep 25 17:39:53 2017	(r323986)
@@ -43,6 +43,7 @@ static const char rcsid[] =
 #include <sys/time.h>
 
 #include <poll.h>
+#include <regex.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
@@ -53,8 +54,6 @@ static const char rcsid[] =
 #include "pathnames.h"
 #include "extern.h"
 
-
-
 /*
  * Get a table entry.
  */
@@ -469,42 +468,48 @@ adelay(int ms, struct delayval *dp)
 char	editedhost[MAXHOSTNAMELEN];
 
 void
-edithost(const char *pat)
+edithost(const char *pattern)
 {
-	const char *host = HN;
-	char *res = editedhost;
+	regex_t regex;
+	regmatch_t *match;
+	int found;
 
-	if (!pat)
-		pat = "";
-	while (*pat) {
-		switch (*pat) {
+	if (pattern == NULL || *pattern == '\0')
+		goto copyasis;
+	if (regcomp(&regex, pattern, REG_EXTENDED) != 0)
+		goto copyasis;
 
-		case '#':
-			if (*host)
-				host++;
-			break;
+	match = calloc(regex.re_nsub + 1, sizeof(*match));
+	if (match == NULL) {
+		regfree(&regex);
+		goto copyasis;
+	}
 
-		case '@':
-			if (*host)
-				*res++ = *host++;
-			break;
+	found = !regexec(&regex, HN, regex.re_nsub + 1, match, 0);
+	if (found) {
+		size_t subex, totalsize;
 
-		default:
-			*res++ = *pat;
-			break;
-
-		}
-		if (res == &editedhost[sizeof editedhost - 1]) {
-			*res = '\0';
-			return;
-		}
-		pat++;
+		/*
+		 * We found a match.  If there were no parenthesized
+		 * subexpressions in the pattern, use entire matched
+		 * string as ``editedhost''; otherwise use the first
+		 * matched subexpression.
+		 */
+		subex = !!regex.re_nsub;
+		totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
+		strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
+		    sizeof(editedhost) ? sizeof(editedhost) : totalsize);
 	}
-	if (*host)
-		strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
-	else
-		*res = '\0';
-	editedhost[sizeof editedhost - 1] = '\0';
+	free(match);
+	regfree(&regex);
+	if (found)
+		return;
+	/*
+	 * In case of any errors, or if the pattern did not match, pass
+	 * the original hostname as is.
+	 */
+ copyasis:
+	strlcpy(editedhost, HN, sizeof(editedhost));
 }
 
 static struct speedtab {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201709251739.v8PHdrvQ068508>