Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/chg_usr_exec.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2007 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627#include <stdio.h>28#include <stdlib.h>29#include <unistd.h>30#include <string.h>31#include <errno.h>32#include <pwd.h>3334#define EXECSHELL "/bin/sh"3536int37main(int argc, char *argv[])38{39char *plogin = NULL;40char cmds[BUFSIZ] = { 0 };41char sep[] = " ";42struct passwd *ppw = NULL;43int i, len;4445if (argc < 3 || strlen(argv[1]) == 0) {46(void) printf("\tUsage: %s <login> <commands> ...\n", argv[0]);47return (1);48}4950plogin = argv[1];51len = 0;52for (i = 2; i < argc; i++) {53(void) snprintf(cmds+len, sizeof (cmds)-len,54"%s%s", argv[i], sep);55len += strlen(argv[i]) + strlen(sep);56}5758if ((ppw = getpwnam(plogin)) == NULL) {59perror("getpwnam");60return (errno);61}62if (setgid(ppw->pw_gid) != 0) {63perror("setgid");64return (errno);65}66if (setuid(ppw->pw_uid) != 0) {67perror("setuid");68return (errno);69}7071if (execl(EXECSHELL, "sh", "-c", cmds, (char *)NULL) != 0) {72perror("execl: " EXECSHELL);73return (errno);74}7576return (0);77}787980