Print this page
4078 groupadd execs getent unnecessarily
Reviewed by: Rich Lowe <richlowe@richlowe.net>
Reviewed by: Gary Mills <gary_mills@fastmail.fm>
Reviewed by: Milan Jurik <milan.jurik@xylab.cz>
Reviewed by: Gordon Ross <Gordon.W.Ross@gmail.com>


  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
  24  * All rights reserved.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 



  30 
  31 #pragma ident   "%Z%%M% %I%     %E% SMI"        /* SVr4.0 1.5 */
  32 
  33 #include <sys/types.h>
  34 #include <stdio.h>
  35 #include <userdefs.h>
  36 #include <pwd.h>

  37 
  38 #include <sys/param.h>
  39 #ifndef MAXUID
  40 #include <limits.h>
  41 #ifdef UID_MAX
  42 #define MAXUID  UID_MAX
  43 #else
  44 #define MAXUID  60000
  45 #endif
  46 #endif
  47 
  48 static uid_t getrangeboundid(uid_t start, uid_t stop);
  49 static int isreserveduid(uid_t uid);
  50 
  51 /*
  52  * Find the highest uid currently in use and return it. If the highest unused
  53  * uid is MAXUID, then attempt to find a hole in the range. If there are no
  54  * more unused uids, then return -1.
  55  */
  56 uid_t
  57 findnextuid(void)
  58 {
  59         uid_t uid = DEFRID + 1;
  60         struct passwd *pwd;
  61         uchar_t overflow = 0;
  62 
  63         setpwent();
  64         for (pwd = getpwent(); pwd != NULL; pwd = getpwent()) {
  65                 if (isreserveduid(pwd->pw_uid))              /* Skip reserved IDs */
  66                         continue;
  67                 if (pwd->pw_uid >= uid) {
  68                         if (pwd->pw_uid == MAXUID) { /* Overflow check */
  69                                 overflow = 1;
  70                                 break;
  71                         }
  72                         uid = pwd->pw_uid + 1;
  73                         while (isreserveduid(uid) &&
  74                             uid < MAXUID) {          /* Skip reserved IDs */
  75                                 uid++;
  76                         }
  77                 }



  78         }
  79         endpwent();
  80         if (overflow == 1)                              /* Find a hole */
  81                 return (getrangeboundid(DEFRID + 1, MAXUID));
  82         return (uid);



  83 }
  84 
  85 /*
  86  * Check to see that the uid is a reserved uid
  87  * -- nobody, noaccess or nobody4
  88  */
  89 static int
  90 isreserveduid(uid_t uid)
  91 {
  92         return (uid == 60001 || uid == 60002 || uid == 65534);
  93 }
  94 
  95 
  96 /*
  97  * getrangeboundid() attempts to return the next valid usable id between the
  98  * supplied upper and lower limits. If these limits exceed the system
  99  * boundaries of DEFRID +1 and MAXUID (lower and upper bound respectively),
 100  * then they are ignored and DEFRID + 1 and MAXUID are used.
 101  *
 102  * Returns a valid uid_t between DEFRID +1 and MAXUID, -1 is returned on fail
 103  */
 104 static uid_t
 105 getrangeboundid(uid_t start, uid_t stop)
 106 {
 107         uid_t low = (start <= DEFRID) ? DEFRID + 1 : start;
 108         uid_t high = (stop < MAXUID) ? stop : MAXUID;
 109         uid_t uid;
 110 
 111         for (uid = low; uid <= high; uid++) {
 112                 if (isreserveduid(uid))
 113                         continue;
 114                 if (getpwuid(uid) == NULL)


 115                         break;
 116         }
 117         return ((uid > high) ? -1 : uid);




 118 }


  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
  24  * All rights reserved.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 /*
  31  * Copyright (c) 2013 RackTop Systems.
  32  */
  33 
  34 #include <errno.h>

  35 #include <sys/types.h>
  36 #include <stdio.h>
  37 #include <userdefs.h>
  38 #include <pwd.h>
  39 #include <libcmdutils.h>
  40 
  41 static int findunuseduid(uid_t start, uid_t stop, uid_t *ret);
  42 static boolean_t isreserveduid(uid_t uid);










  43 
  44 /*
  45  * Find the highest unused uid. If the highest unused uid is "stop",
  46  * then attempt to find a hole in the range. Returns 0 on success.

  47  */
  48 int
  49 findnextuid(uid_t start, uid_t stop, uid_t *ret)
  50 {
  51         uid_t uid = start;
  52         struct passwd *pwd;
  53         boolean_t overflow = B_FALSE;
  54 
  55         setpwent();
  56         for (pwd = getpwent(); pwd != NULL; pwd = getpwent()) {
  57                 if (isreserveduid(pwd->pw_uid))              /* Skip reserved IDs */
  58                         continue;
  59                 if (pwd->pw_uid >= uid) {
  60                         if (pwd->pw_uid == stop) {   /* Overflow check */
  61                                 overflow = B_TRUE;
  62                                 break;
  63                         }
  64                         uid = pwd->pw_uid + 1;



  65                 }
  66         }
  67         if (pwd == NULL && errno != 0) {
  68                 endpwent();
  69                 return (-1);
  70         }
  71         endpwent();
  72         if (overflow == B_TRUE)                         /* Find a hole */
  73                 return (findunuseduid(start, stop, ret));
  74         while (isreserveduid(uid) && uid < stop)     /* Skip reserved IDs */
  75                 uid++;
  76         *ret = uid;
  77         return (0);
  78 }
  79 
  80 /*
  81  * Check to see whether the uid is a reserved uid
  82  * -- nobody, noaccess or nobody4
  83  */
  84 static boolean_t
  85 isreserveduid(uid_t uid)
  86 {
  87         return (uid == 60001 || uid == 60002 || uid == 65534);
  88 }
  89 

  90 /*
  91  * findunuseduid() attempts to return the next valid usable id between the
  92  * supplied upper and lower limits. Returns 0 on success.




  93  */
  94 static int
  95 findunuseduid(uid_t start, uid_t stop, uid_t *ret)
  96 {


  97         uid_t uid;
  98 
  99         for (uid = start; uid <= stop; uid++) {
 100                 if (isreserveduid(uid))
 101                         continue;
 102                 if (getpwuid(uid) == NULL) {
 103                         if (errno != 0)
 104                                 return (-1);
 105                         break;
 106                 }
 107         }
 108         if (uid > stop)
 109                 return (-1);
 110         *ret = uid;
 111         return (0);
 112 }