xref: /freebsd/tests/sys/cddl/zfs/bin/chg_usr_exec.c (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
1*2fae26bdSAlan Somers /*
2*2fae26bdSAlan Somers  * CDDL HEADER START
3*2fae26bdSAlan Somers  *
4*2fae26bdSAlan Somers  * The contents of this file are subject to the terms of the
5*2fae26bdSAlan Somers  * Common Development and Distribution License (the "License").
6*2fae26bdSAlan Somers  * You may not use this file except in compliance with the License.
7*2fae26bdSAlan Somers  *
8*2fae26bdSAlan Somers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2fae26bdSAlan Somers  * or http://www.opensolaris.org/os/licensing.
10*2fae26bdSAlan Somers  * See the License for the specific language governing permissions
11*2fae26bdSAlan Somers  * and limitations under the License.
12*2fae26bdSAlan Somers  *
13*2fae26bdSAlan Somers  * When distributing Covered Code, include this CDDL HEADER in each
14*2fae26bdSAlan Somers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2fae26bdSAlan Somers  * If applicable, add the following below this CDDL HEADER, with the
16*2fae26bdSAlan Somers  * fields enclosed by brackets "[]" replaced with your own identifying
17*2fae26bdSAlan Somers  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2fae26bdSAlan Somers  *
19*2fae26bdSAlan Somers  * CDDL HEADER END
20*2fae26bdSAlan Somers  */
21*2fae26bdSAlan Somers 
22*2fae26bdSAlan Somers /*
23*2fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*2fae26bdSAlan Somers  * Use is subject to license terms.
25*2fae26bdSAlan Somers  */
26*2fae26bdSAlan Somers 
27*2fae26bdSAlan Somers 
28*2fae26bdSAlan Somers #include <stdio.h>
29*2fae26bdSAlan Somers #include <stdlib.h>
30*2fae26bdSAlan Somers #include <unistd.h>
31*2fae26bdSAlan Somers #include <string.h>
32*2fae26bdSAlan Somers #include <errno.h>
33*2fae26bdSAlan Somers #include <pwd.h>
34*2fae26bdSAlan Somers 
35*2fae26bdSAlan Somers int
main(int argc,char * argv[])36*2fae26bdSAlan Somers main(int argc, char *argv[])
37*2fae26bdSAlan Somers {
38*2fae26bdSAlan Somers 	char *plogin = NULL;
39*2fae26bdSAlan Somers 	char cmds[BUFSIZ] = { 0 };
40*2fae26bdSAlan Somers 	char sep[] = " ";
41*2fae26bdSAlan Somers 	struct passwd *ppw = NULL;
42*2fae26bdSAlan Somers 	int i, len;
43*2fae26bdSAlan Somers 
44*2fae26bdSAlan Somers 	if (argc < 3 || strlen(argv[1]) == 0) {
45*2fae26bdSAlan Somers 		(void) printf("\tUsage: %s <login> <commands> ...\n", argv[0]);
46*2fae26bdSAlan Somers 		return (1);
47*2fae26bdSAlan Somers 	}
48*2fae26bdSAlan Somers 
49*2fae26bdSAlan Somers 	plogin = argv[1];
50*2fae26bdSAlan Somers 	len = 0;
51*2fae26bdSAlan Somers 	for (i = 2; i < argc; i++) {
52*2fae26bdSAlan Somers 		(void) snprintf(cmds+len, sizeof (cmds)-len,
53*2fae26bdSAlan Somers 		    "%s%s", argv[i], sep);
54*2fae26bdSAlan Somers 		len += strlen(argv[i]) + strlen(sep);
55*2fae26bdSAlan Somers 	}
56*2fae26bdSAlan Somers 
57*2fae26bdSAlan Somers 	if ((ppw = getpwnam(plogin)) == NULL) {
58*2fae26bdSAlan Somers 		perror("getpwnam");
59*2fae26bdSAlan Somers 		return (errno);
60*2fae26bdSAlan Somers 	}
61*2fae26bdSAlan Somers 	if (setgid(ppw->pw_gid) != 0) {
62*2fae26bdSAlan Somers 		perror("setgid");
63*2fae26bdSAlan Somers 		return (errno);
64*2fae26bdSAlan Somers 	}
65*2fae26bdSAlan Somers 	if (setuid(ppw->pw_uid) != 0) {
66*2fae26bdSAlan Somers 		perror("setuid");
67*2fae26bdSAlan Somers 		return (errno);
68*2fae26bdSAlan Somers 	}
69*2fae26bdSAlan Somers 
70*2fae26bdSAlan Somers 	if (execl("/bin/sh", "sh",  "-c", cmds, (char *)0) != 0) {
71*2fae26bdSAlan Somers 		perror("execl");
72*2fae26bdSAlan Somers 		return (errno);
73*2fae26bdSAlan Somers 	}
74*2fae26bdSAlan Somers 
75*2fae26bdSAlan Somers 	return (0);
76*2fae26bdSAlan Somers }
77