17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*a776e906Snakanon * Common Development and Distribution License (the "License"). 6*a776e906Snakanon * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*a776e906Snakanon 227c478bd9Sstevel@tonic-gate /* 23*a776e906Snakanon * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*a776e906Snakanon * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2 */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <sys/types.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <ctype.h> 39*a776e906Snakanon #include <errno.h> 40*a776e906Snakanon #include <limits.h> 41*a776e906Snakanon #include <sys/param.h> 427c478bd9Sstevel@tonic-gate #include <users.h> 437c478bd9Sstevel@tonic-gate #include <userdefs.h> 447c478bd9Sstevel@tonic-gate 45*a776e906Snakanon extern int valid_gid(gid_t, struct group **); 46*a776e906Snakanon 47*a776e906Snakanon static int isalldigit(char *); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate /* 507c478bd9Sstevel@tonic-gate * validate a group name or number and return the appropriate 517c478bd9Sstevel@tonic-gate * group structure for it. 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate int 547c478bd9Sstevel@tonic-gate valid_group(char *group, struct group **gptr, int *warning) 557c478bd9Sstevel@tonic-gate { 56*a776e906Snakanon int r, warn; 57*a776e906Snakanon long l; 587c478bd9Sstevel@tonic-gate char *ptr; 59*a776e906Snakanon struct group *grp; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate *warning = 0; 62*a776e906Snakanon 63*a776e906Snakanon if (!isalldigit(group)) 64*a776e906Snakanon return (valid_gname(group, gptr, warning)); 65*a776e906Snakanon 66*a776e906Snakanon /* 67*a776e906Snakanon * There are only digits in group name. 68*a776e906Snakanon * strtol() doesn't return negative number here. 69*a776e906Snakanon */ 70*a776e906Snakanon errno = 0; 71*a776e906Snakanon l = strtol(group, &ptr, 10); 72*a776e906Snakanon if ((l == LONG_MAX && errno == ERANGE) || l > MAXUID) { 73*a776e906Snakanon r = TOOBIG; 74*a776e906Snakanon } else { 75*a776e906Snakanon if ((r = valid_gid((gid_t)l, &grp)) == NOTUNIQUE) { 76*a776e906Snakanon /* It is a valid existing gid */ 77*a776e906Snakanon if (gptr != NULL) 78*a776e906Snakanon *gptr = grp; 79*a776e906Snakanon return (NOTUNIQUE); 807c478bd9Sstevel@tonic-gate } 81*a776e906Snakanon } 82*a776e906Snakanon /* 83*a776e906Snakanon * It's all digit, but not a valid gid nor an existing gid. 84*a776e906Snakanon * There might be an existing group name of all digits. 85*a776e906Snakanon */ 86*a776e906Snakanon if (valid_gname(group, &grp, &warn) == NOTUNIQUE) { 87*a776e906Snakanon /* It does exist */ 88*a776e906Snakanon *warning = warn; 89*a776e906Snakanon if (gptr != NULL) 90*a776e906Snakanon *gptr = grp; 91*a776e906Snakanon return (NOTUNIQUE); 92*a776e906Snakanon } 93*a776e906Snakanon /* 94*a776e906Snakanon * It isn't either existing gid or group name. We return the 95*a776e906Snakanon * error code from valid_gid() assuming that given string 96*a776e906Snakanon * represents an integer GID. 97*a776e906Snakanon */ 98*a776e906Snakanon return (r); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 101*a776e906Snakanon static int 102*a776e906Snakanon isalldigit(char *str) 103*a776e906Snakanon { 104*a776e906Snakanon while (*str != '\0') { 105*a776e906Snakanon if (!isdigit((unsigned char)*str)) 106*a776e906Snakanon return (0); 107*a776e906Snakanon str++; 108*a776e906Snakanon } 109*a776e906Snakanon return (1); 1107c478bd9Sstevel@tonic-gate } 111