From Arne.Dag.Fidjestol@idi.ntnu.no  Tue Mar 13 19:04:43 2001
Return-Path: <Arne.Dag.Fidjestol@idi.ntnu.no>
Received: from zevs.idi.ntnu.no (zevs.idi.ntnu.no [129.241.164.12])
	by hub.freebsd.org (Postfix) with ESMTP id 637D337B718
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 13 Mar 2001 19:04:42 -0800 (PST)
	(envelope-from Arne.Dag.Fidjestol@idi.ntnu.no)
Received: from anfield.idi.ntnu.no (IDENT:0@anfield.idi.ntnu.no [129.241.111.72])
	by zevs.idi.ntnu.no (8.9.3/8.9.3) with ESMTP id EAA16278
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 14 Mar 2001 04:04:39 +0100 (MET)
Received: (from adf@localhost)
	by anfield.idi.ntnu.no (8.11.2/8.9.3) id f2E34d695971;
	Wed, 14 Mar 2001 04:04:39 +0100 (CET)
Message-Id: <200103140304.f2E34d695971@anfield.idi.ntnu.no>
Date: Wed, 14 Mar 2001 04:04:39 +0100 (CET)
From: Arne Dag Fidjestl <Arne.Dag.Fidjestol@idi.ntnu.no>
Reply-To: Arne.Dag.Fidjestol@idi.ntnu.no
To: FreeBSD-gnats-submit@freebsd.org
Subject: inetd/auth: -i option to return uid instead of uname
X-Send-Pr-Version: 3.2

>Number:         25787
>Category:       bin
>Synopsis:       inetd/auth: -i option to return uid instead of uname
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    dwmalone
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 13 19:10:01 PST 2001
>Closed-Date:    Tue Jul 17 03:45:21 PDT 2001
>Last-Modified:  Tue Jul 17 03:45:48 PDT 2001
>Originator:     Arne Dag Fidjestl
>Release:        FreeBSD 4.2-STABLE i386
>Organization:
>Environment:

	

>Description:

	The enclosed patch adds an -i option to the auth-service in inetd,
which  makes it possible to report an identity without revealing the username.

>How-To-Repeat:

	

>Fix:

Index: builtins.c
===================================================================
RCS file: /mnt/home/ncvs/src/usr.sbin/inetd/builtins.c,v
retrieving revision 1.19.2.4
diff -u -r1.19.2.4 builtins.c
--- builtins.c	2001/02/28 12:16:37	1.19.2.4
+++ builtins.c	2001/03/13 07:33:33
@@ -345,13 +345,14 @@
 	}, to;
 	struct passwd *pw = NULL;
 	fd_set fdset;
-	char buf[BUFSIZE], *cp = NULL, *p, **av, *osname = NULL, garbage[7], e;
-	char *fallback = NULL;
+	char buf[BUFSIZE], *p, **av, *osname = NULL, garbage[7], e;
+	char uidbuf[sizeof "1234567890"];   
+	char *fallback = NULL, *res;
 	socklen_t socklen;
 	ssize_t ssize;
 	size_t size, bufsiz;
-	int c, fflag = 0, nflag = 0, rflag = 0, argc = 0, usedfallback = 0;
-	int gflag = 0, Fflag = 0, getcredfail = 0, onreadlen;
+	int c, fflag = 0, nflag = 0, rflag = 0, argc = 0;
+	int gflag = 0, Fflag = 0, getcredfail = 0, onreadlen, useuid = 0;
 	u_short lport, fport;
 
 	inetd_setproctitle(sep->se_service, s);
@@ -373,7 +374,7 @@
 		size_t i;
 		u_int32_t random;
 
-		while ((c = getopt(argc, sep->se_argv, "d:fFgno:rt:")) != -1)
+		while ((c = getopt(argc, sep->se_argv, "d:fFgino:rt:")) != -1)
 			switch (c) {
 			case 'd':
 				fallback = optarg;
@@ -410,6 +411,9 @@
 				}
 				garbage[i] = '\0';
 				break;
+			case 'i':
+				useuid = 1;
+				break;
 			case 'n':
 				nflag = 1;
 				break;
@@ -494,7 +498,7 @@
 	if (sscanf(buf, "%hu , %hu%c", &lport, &fport, &e) != 3 || isdigit(e))
 		iderror(0, 0, s, 0);
 	if (gflag) {
-		cp = garbage;
+		res = garbage;
 		goto printit;
 	}
 		
@@ -506,10 +510,8 @@
 	if (!rflag) {
 		if (fallback == NULL)
 			iderror(lport, fport, s, -1);
-		else {
-			cp = fallback;
-			goto printit;
-		}
+		res = fallback;
+		goto printit;
 	}
 		
 	/*
@@ -552,19 +554,27 @@
 	if (getcredfail != 0) {
 		if (fallback == NULL)		/* Use a default, if asked to */
 			iderror(lport, fport, s, getcredfail);
-		usedfallback = 1;
-	} else {
-		/* Look up the pw to get the username */
-		errno = 0;
-		pw = getpwuid(uc.cr_uid);
-	}
-	if (pw == NULL && !usedfallback)		/* No such user... */
+		res = fallback;
+		goto printit;
+	} 
+
+	/* Look up the pw to get the username */
+	errno = 0;
+	pw = getpwuid(uc.cr_uid);
+	if (pw == NULL)				/* No such user... */
 		iderror(lport, fport, s, errno != 0 ? errno : ENOENT);
+
+	if (useuid){
+		snprintf(uidbuf, sizeof uidbuf, "%u", pw->pw_uid);
+		res = uidbuf;
+	} else
+		res = pw->pw_name;
+
 	/*
 	 * If enabled, we check for a file named ".noident" in the user's
 	 * home directory. If found, we return HIDDEN-USER.
 	 */
-	if (nflag && !usedfallback) {
+	if (nflag) {
 		if (asprintf(&p, "%s/.noident", pw->pw_dir) == -1)
 			iderror(lport, fport, s, errno);
 		if (lstat(p, &sb) == 0) {
@@ -578,7 +588,7 @@
 	 * home directory. It consists of a line containing the name
 	 * they want.
 	 */
-	if (fflag && !usedfallback) {
+	if (fflag) {
 		FILE *fakeid = NULL;
 		int fakeid_fd;
 
@@ -604,9 +614,9 @@
 		if (fakeid_fd != -1 && fstat(fakeid_fd, &sb) != -1 &&
 		    S_ISREG(sb.st_mode) &&
 		    (fakeid = fdopen(fakeid_fd, "r")) != NULL) {
+			char *cp;
 			buf[sizeof(buf) - 1] = '\0';
 			if (fgets(buf, sizeof(buf), fakeid) == NULL) {
-				cp = pw->pw_name;
 				fclose(fakeid);
 				goto printit;
 			}
@@ -626,33 +636,23 @@
 			if (strlen(cp) > 16)
 				cp[16] = '\0';
 			/*
-			 * If the name is a zero-length string or matches
-			 * the name of another user, it's invalid, so
-			 * we will return their real identity instead.
+			 * If the name is a non-empty string and it
+			 * doesn't match the the name of another
+			 * user ( unless so permitted by -F option ),
+			 * it is used for reply.  Otherwise the real
+			 * identity is used.
 			 */
-			
-			if (!*cp || (!Fflag && getpwnam(cp))) {
-				errno = 0;
-				pw = getpwuid(uc.cr_uid);
-				if (pw == NULL)
-					iderror(lport, fport, s,
-					    errno != 0 ? errno : ENOENT);
-				cp = pw->pw_name;
-			}
-		} else
-			cp = pw->pw_name;
-		if (fakeid != NULL)
+			if (*cp && (Fflag || !getpwnam(cp)))
+				res = cp;
 			fclose(fakeid);
-		else if (fakeid_fd != -1)
+		}
+		if (fakeid_fd != -1)
 			close(fakeid_fd);
-	} else if (!usedfallback)
-		cp = pw->pw_name;
-	else
-		cp = fallback;
+	}
 printit:
 	/* Finally, we make and send the reply. */
 	if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname,
-	    cp) == -1) {
+	    res) == -1) {
 		syslog(LOG_ERR, "asprintf: %m");
 		exit(EX_OSERR);
 	}
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25336 
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25346 
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25542 
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25793 
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25572 
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25754 
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: phk 
Responsible-Changed-When: Wed Apr 4 00:38:57 PDT 2001 
Responsible-Changed-Why:  
over to bugs 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25787 
Responsible-Changed-From-To: freebsd-bugs->dwmalone 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Wed Apr 4 02:23:54 PDT 2001 
Responsible-Changed-Why:  
Inetd is my problem. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25787 
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Tue Jul 17 03:45:21 PDT 2001 
State-Changed-Why:  
Now in -current and -stable. Thanks for the patches. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=25787 
>Unformatted:
