if (gr = mygetgroup(group)) {
if ((gr = mygetgroup(group))) {
static int fchog(fd, file, owner, group, mode)	int fd;	char *file, *owner, *group;	int mode;{	struct group *gr = NULL;	static char last_group[128];	static char last_owner[128];	static GID_T last_gid = (GID_T)-2;	static UID_T last_uid = (UID_T)-2;	static GID_T last_primegid;	extern char *locuser;	register int i;	UID_T uid;	GID_T gid;	GID_T primegid = (GID_T)-2;	uid = userid;	if (userid == 0) {	/* running as root; take anything */		if (*owner == ':') {			uid = (UID_T) atoi(owner + 1);		} else if (last_uid == (UID_T)-2 ||			   strcmp(owner, last_owner) != 0) {			if ((pw = getpwnam(owner)) == NULL) {				if (mode != -1 && IS_ON(mode, S_ISUID)) {					message(MT_NOTICE,			      "%s: unknown login name \"%s\", clearing setuid",						target, owner);					mode &= ~S_ISUID;					uid = 0;				} else					message(MT_NOTICE,					"%s: unknown login name \"%s\"",						target, owner);			} else {				uid	 = last_uid	 = pw->pw_uid;				primegid = last_primegid = pw->pw_gid;				strcpy(last_owner, owner);			}		} else {			uid = last_uid;			primegid = last_primegid;		}		if (*group == ':') {			gid = (GID_T) atoi(group + 1);			goto ok;		}	} else {	/* not root, setuid only if user==owner */		struct passwd *lupw;		if (mode != -1) {			if (IS_ON(mode, S_ISUID) && 			    strcmp(locuser, owner) != 0)				mode &= ~S_ISUID;			if (mode)				mode &= ~S_ISVTX; /* and strip sticky too */		}		if ((lupw = getpwnam(locuser)) != NULL)			primegid = lupw->pw_gid;	}	gid = (GID_T) -1;	if (last_gid < (GID_T)0 || strcmp(group, last_group) != 0) {	        /*		 * Invalid cached values so we need to do a new lookup.		 */		if (gr = mygetgroup(group)) {			last_gid = gid = gr->gr_gid;			strcpy(last_group, gr->gr_name);		} else {			if (mode != -1 && IS_ON(mode, S_ISGID)) {				message(MT_NOTICE, 				"%s: unknown group \"%s\", clearing setgid",					target, group);				mode &= ~S_ISGID;			} else				message(MT_NOTICE, 					"%s: unknown group \"%s\"",					target, group);		}	} else {	    	/*		 * Use the cached values.		 */		gid = last_gid;	}	/*	 * We need to check non-root users to make sure they're a member	 * of the group.  If they are not, we don't set that gid ownership.	 */	if (userid && gid >= 0 && gid != primegid) {		if (!gr)		    	gr = mygetgroup(group);		if (gr)			for (i = 0; gr->gr_mem[i] != NULL; i++)			    	if (strcmp(locuser, gr->gr_mem[i]) == 0)					goto ok;		if (mode != -1 && IS_ON(mode, S_ISGID)) {			message(MT_NOTICE, 				"%s: user %s not in group %s, clearing setgid",				target, locuser, group);			mode &= ~S_ISGID;		}		gid = (GID_T) -1;	}ok:	/*	 * Set uid and gid ownership.  If that fails, strip setuid and	 * setgid bits from mode.  Once ownership is set, successful	 * or otherwise, set the new file mode.	 */	if (setownership(file, fd, uid, gid) < 0) {		if (mode != -1 && IS_ON(mode, S_ISUID)) {			message(MT_NOTICE, 				"%s: chown failed, clearing setuid", target);			mode &= ~S_ISUID;		}		if (mode != -1 && IS_ON(mode, S_ISGID)) {			message(MT_NOTICE, 				"%s: chown failed, clearing setgid", target);			mode &= ~S_ISGID;		}	}	(void) setfilemode(file, fd, mode);	return(0);}