Path: blob/main/tests/sys/cddl/zfs/bin/chg_usr_exec.c
39537 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/252627#include <stdio.h>28#include <stdlib.h>29#include <unistd.h>30#include <string.h>31#include <errno.h>32#include <pwd.h>3334int35main(int argc, char *argv[])36{37char *plogin = NULL;38char cmds[BUFSIZ] = { 0 };39char sep[] = " ";40struct passwd *ppw = NULL;41int i, len;4243if (argc < 3 || strlen(argv[1]) == 0) {44(void) printf("\tUsage: %s <login> <commands> ...\n", argv[0]);45return (1);46}4748plogin = argv[1];49len = 0;50for (i = 2; i < argc; i++) {51(void) snprintf(cmds+len, sizeof (cmds)-len,52"%s%s", argv[i], sep);53len += strlen(argv[i]) + strlen(sep);54}5556if ((ppw = getpwnam(plogin)) == NULL) {57perror("getpwnam");58return (errno);59}60if (setgid(ppw->pw_gid) != 0) {61perror("setgid");62return (errno);63}64if (setuid(ppw->pw_uid) != 0) {65perror("setuid");66return (errno);67}6869if (execl("/bin/sh", "sh", "-c", cmds, (char *)0) != 0) {70perror("execl");71return (errno);72}7374return (0);75}767778