changeset 3995:63fc894a6109

6490974 getent fails when groupname or username begins with a number
author vp157776
date Mon, 09 Apr 2007 05:58:12 -0700
parents 4fafdadace7c
children 82246a4b06ef
files usr/src/cmd/getent/dogetgr.c usr/src/cmd/getent/dogetpw.c
diffstat 2 files changed, 43 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/getent/dogetgr.c	Sun Apr 08 20:25:56 2007 -0700
+++ b/usr/src/cmd/getent/dogetgr.c	Mon Apr 09 05:58:12 2007 -0700
@@ -2,9 +2,8 @@
  * CDDL HEADER START
  *
  * The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License").  You may not use this file except in compliance
- * with the License.
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
  *
  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  * or http://www.opensolaris.org/os/licensing.
@@ -19,15 +18,18 @@
  *
  * CDDL HEADER END
  */
-#ident	"%Z%%M%	%I%	%E% SMI"
 
 /*
- * Copyright (c) 1994, by Sun Microsystems, Inc.
+ * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
  */
 
+#pragma ident	"%Z%%M%	%I%	%E% SMI"
+
 #include <stdio.h>
 #include <grp.h>
 #include <stdlib.h>
+#include <errno.h>
 #include "getent.h"
 
 
@@ -76,11 +78,22 @@
 			(void) putgrent(grp, stdout);
 	} else {
 		for (; *list != NULL; list++) {
+			errno = 0;
+
+			/*
+			 * Here we assume that the argument passed is
+			 * a gid, if it can be completely transformed
+			 * to a long integer. So we check for gid in
+			 * the database and if we fail then we check
+			 * for the group name.
+			 * If the argument passed is not numeric, then
+			 * we take it as the group name and proceed.
+			 */
 			gid = strtol(*list, &ptr, 10);
-			if (ptr == *list)
+			if (!(*ptr == '\0' && errno == 0) ||
+			    ((grp = getgrgid(gid)) == NULL)) {
 				grp = getgrnam(*list);
-			else
-				grp = getgrgid(gid);
+			}
 			if (grp == NULL)
 				rc = EXC_NAME_NOT_FOUND;
 			else
--- a/usr/src/cmd/getent/dogetpw.c	Sun Apr 08 20:25:56 2007 -0700
+++ b/usr/src/cmd/getent/dogetpw.c	Mon Apr 09 05:58:12 2007 -0700
@@ -2,9 +2,8 @@
  * CDDL HEADER START
  *
  * The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License").  You may not use this file except in compliance
- * with the License.
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
  *
  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  * or http://www.opensolaris.org/os/licensing.
@@ -19,15 +18,18 @@
  *
  * CDDL HEADER END
  */
-#ident	"%Z%%M%	%I%	%E% SMI"
+
+#pragma ident	"%Z%%M%	%I%	%E% SMI"
 
 /*
- * Copyright (c) 1994, by Sun Microsystems, Inc.
+ * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
  */
 
 #include <stdio.h>
 #include <pwd.h>
 #include <stdlib.h>
+#include <errno.h>
 #include "getent.h"
 
 /*
@@ -47,11 +49,23 @@
 			(void) putpwent(pwp, stdout);
 	} else {
 		for (; *list != NULL; list++) {
+			errno = 0;
+
+			/*
+			 * Here we assume that the argument passed is
+			 * a uid, if it can be completely transformed
+			 * to a long integer. So we check for uid in
+			 * the database and if we fail then we check
+			 * for the user name.
+			 * If the argument passed is not numeric, then
+			 * we take it as the user name and proceed.
+			 */
 			uid = strtol(*list, &ptr, 10);
-			if (ptr == *list)
+			if (!(*ptr == '\0' && errno == 0) ||
+			    ((pwp = getpwuid(uid)) == NULL)) {
 				pwp = getpwnam(*list);
-			else
-				pwp = getpwuid(uid);
+			}
+
 			if (pwp == NULL)
 				rc = EXC_NAME_NOT_FOUND;
 			else