4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2020, Whamcloud.
26 * This file is part of Lustre, http://www.lustre.org/
36 void print_groups(int num_groups, gid_t *groups)
40 fprintf(stdout, "\tGroups ");
41 for (i = 0; i < num_groups; i++) {
42 struct group *gr = getgrgid(groups[i]);
46 fprintf(stdout, "%6d - %s, ", gr->gr_gid, gr->gr_name);
48 fprintf(stdout, "\n");
52 "Usage: %s <user to switch euid to> <path to file to access>\n";
54 int main(int argc, char **argv)
58 struct passwd *user_pwd;
62 struct passwd *switch_pwd;
63 uid_t switch_uid, switch_gid;
64 int switch_num_groups = 0;
66 int final_num_groups = 0;
73 fprintf(stderr, usage, argv[0]);
77 user_to_euid = argv[1];
80 user_pwd = getpwuid(getuid());
87 fprintf(stderr, "Cannot find user in passwd db\n");
91 user_name = user_pwd->pw_name;
93 fprintf(stdout, "Initially %s ruid:rgid %d:%d, euid:egid %d:%d\n",
94 user_name, getuid(), getgid(), geteuid(), getegid());
96 num_groups = getgroups(0, NULL);
98 groups = malloc(sizeof(gid_t) * num_groups);
104 rc = getgrouplist(user_name, getgid(), groups, &num_groups);
108 perror("groups list");
111 print_groups(num_groups, groups);
115 /* lookup information about switch_user provided */
116 switch_pwd = getpwnam(user_to_euid);
123 fprintf(stderr, "Cannot find user %s in passwd db\n",
128 switch_uid = switch_pwd->pw_uid;
129 switch_gid = switch_pwd->pw_gid;
131 fprintf(stdout, "To switch to effective %s uid:gid %d:%d\n",
132 user_to_euid, switch_uid, switch_gid);
134 rc = setegid(switch_gid);
141 getgrouplist(user_to_euid, switch_gid, NULL, &switch_num_groups);
142 if (switch_num_groups) {
143 switch_groups = malloc(sizeof(gid_t) * switch_num_groups);
144 if (!switch_groups) {
146 perror("switch_groups");
149 rc = getgrouplist(user_to_euid, switch_gid,
150 switch_groups, &switch_num_groups);
154 perror("switch_groups list");
157 print_groups(switch_num_groups, switch_groups);
158 rc = setgroups(switch_num_groups, switch_groups);
167 rc = seteuid(switch_uid);
174 fprintf(stdout, "Now %s ruid:rgid %d:%d, euid:egid %d:%d\n",
175 user_name, getuid(), getgid(), geteuid(), getegid());
177 final_num_groups = getgroups(0, NULL);
178 final_groups = malloc(sizeof(gid_t) * final_num_groups);
181 perror("final_groups");
184 rc = getgroups(final_num_groups, final_groups);
188 perror("final_groups list");
191 print_groups(final_num_groups, final_groups);
194 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666);
200 rc = write(fd, "test", 5);
207 fprintf(stdout, "File %s successfully written\n", filename);