dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ptools / pcred / pcred.c
blob464d3ed204767573d65eb3a833a81c67d3ce1f2b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdio_ext.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/types.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <libproc.h>
41 extern int _getgroupsbymember(const char *, gid_t[], int, int);
43 static int look(char *);
44 static int perr(char *);
46 static void usage(void);
47 static void initcred(void);
49 static char *command;
50 static char *procname;
52 static char *user;
53 static char *group;
54 static char *grplst;
55 static char *login;
57 static boolean_t all = B_FALSE;
58 static boolean_t doset = B_FALSE;
59 static int ngrp = -1;
60 static gid_t *groups;
61 static long ngroups_max;
63 static uid_t uid = (uid_t)-1;
64 static gid_t gid = (gid_t)-1;
66 int
67 main(int argc, char **argv)
69 int rc = 0;
70 int c;
71 struct rlimit rlim;
73 if ((command = strrchr(argv[0], '/')) != NULL)
74 command++;
75 else
76 command = argv[0];
78 if ((ngroups_max = sysconf(_SC_NGROUPS_MAX)) < 0)
79 return (perr("sysconf(_SC_NGROUPS_MAX)"));
81 opterr = 0;
83 while ((c = getopt(argc, argv, "au:g:l:G:")) != EOF) {
84 switch (c) {
85 case 'a':
86 all = B_TRUE;
87 break;
88 case 'u':
89 user = optarg;
90 doset = B_TRUE;
91 break;
92 case 'g':
93 group = optarg;
94 doset = B_TRUE;
95 break;
96 case 'G':
97 grplst = optarg;
98 doset = B_TRUE;
99 break;
100 case 'l':
101 login = optarg;
102 doset = B_TRUE;
103 break;
104 default:
105 usage();
106 /*NOTREACHED*/
109 if (login != NULL && (user != NULL || group != NULL || grplst != NULL))
110 usage();
112 if (all && doset)
113 usage();
115 argc -= optind;
116 argv += optind;
118 if (argc == 0)
119 usage();
121 if (doset)
122 initcred();
125 * Make sure we'll have enough file descriptors to handle a target
126 * that has many many mappings.
128 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
129 rlim.rlim_cur = rlim.rlim_max;
130 (void) setrlimit(RLIMIT_NOFILE, &rlim);
131 (void) enable_extended_FILE_stdio(-1, -1);
134 while (argc-- > 0)
135 rc += look(*argv++);
137 return (rc > 255 ? 255 : rc);
140 static void
141 credupdate(prcred_t *pcr)
143 if (uid != (uid_t)-1)
144 pcr->pr_euid = pcr->pr_ruid = pcr->pr_suid = uid;
145 if (gid != (gid_t)-1)
146 pcr->pr_egid = pcr->pr_rgid = pcr->pr_sgid = gid;
147 if (ngrp >= 0) {
149 pcr->pr_ngroups = ngrp;
151 (void) memcpy(pcr->pr_groups, groups, ngrp * sizeof (gid_t));
155 static int
156 look(char *arg)
158 struct ps_prochandle *Pr;
159 static prcred_t *prcred = NULL;
160 int gcode;
162 procname = arg; /* for perr() */
164 if (prcred == NULL) {
165 prcred = malloc(sizeof (prcred_t) +
166 (ngroups_max - 1) * sizeof (gid_t));
167 if (prcred == NULL) {
168 (void) perr("malloc");
169 exit(1);
173 if ((Pr = proc_arg_grab(arg, doset ? PR_ARG_PIDS : PR_ARG_ANY,
174 PGRAB_RETAIN | PGRAB_FORCE | (doset ? 0 : PGRAB_RDONLY) |
175 PGRAB_NOSTOP, &gcode)) == NULL) {
176 (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
177 command, arg, Pgrab_error(gcode));
178 return (1);
181 if (Pcred(Pr, prcred, ngroups_max) == -1) {
182 (void) perr("getcred");
183 Prelease(Pr, 0);
184 return (1);
187 if (doset) {
188 credupdate(prcred);
189 if (Psetcred(Pr, prcred) != 0) {
190 (void) perr("setcred");
191 Prelease(Pr, 0);
192 return (1);
194 Prelease(Pr, 0);
195 return (0);
198 if (Pstate(Pr) == PS_DEAD)
199 (void) printf("core of %d:\t", (int)Pstatus(Pr)->pr_pid);
200 else
201 (void) printf("%d:\t", (int)Pstatus(Pr)->pr_pid);
203 if (!all &&
204 prcred->pr_euid == prcred->pr_ruid &&
205 prcred->pr_ruid == prcred->pr_suid)
206 (void) printf("e/r/suid=%u ", prcred->pr_euid);
207 else
208 (void) printf("euid=%u ruid=%u suid=%u ",
209 prcred->pr_euid, prcred->pr_ruid, prcred->pr_suid);
211 if (!all &&
212 prcred->pr_egid == prcred->pr_rgid &&
213 prcred->pr_rgid == prcred->pr_sgid)
214 (void) printf("e/r/sgid=%u\n", prcred->pr_egid);
215 else
216 (void) printf("egid=%u rgid=%u sgid=%u\n",
217 prcred->pr_egid, prcred->pr_rgid, prcred->pr_sgid);
219 if (prcred->pr_ngroups != 0 &&
220 (all || prcred->pr_ngroups != 1 ||
221 prcred->pr_groups[0] != prcred->pr_rgid)) {
222 int i;
224 (void) printf("\tgroups:");
225 for (i = 0; i < prcred->pr_ngroups; i++)
226 (void) printf(" %u", prcred->pr_groups[i]);
227 (void) printf("\n");
230 Prelease(Pr, 0);
231 return (0);
234 static int
235 perr(char *s)
237 if (s)
238 (void) fprintf(stderr, "%s: ", procname);
239 else
240 s = procname;
241 perror(s);
242 return (1);
245 static void
246 usage(void)
248 (void) fprintf(stderr, "usage:\t%s [-a] { pid | core } ...\n"
249 "\t%s [-u user] [-g group] [-G groups] pid ...\n"
250 "\t%s -l login pid ...\n"
251 " (report or modify process credentials)\n",
252 command, command, command);
253 exit(2);
257 static uint32_t
258 str2id(const char *str)
260 unsigned long res;
261 char *p;
263 errno = 0;
264 res = strtoul(str, &p, 0);
265 if (p == str || *p != '\0' || errno != 0)
266 return ((uint32_t)-1);
267 else
268 return ((uint32_t)res);
271 static gid_t
272 str2gid(const char *grnam)
274 struct group *grp = getgrnam(grnam);
275 gid_t res;
277 if (grp == NULL) {
278 res = (gid_t)str2id(grnam);
279 if (res == (gid_t)-1) {
280 (void) fprintf(stderr, "%s: %s: unknown group"
281 " or bad gid\n",
282 command, grnam);
283 exit(1);
285 } else {
286 res = grp->gr_gid;
288 return (res);
291 static void
292 initcred(void)
294 struct passwd *pwd;
296 if ((groups = malloc(ngroups_max * sizeof (gid_t))) == NULL) {
297 (void) perr("malloc");
298 exit(1);
301 if (login != NULL) {
302 pwd = getpwnam(login);
304 if (pwd == NULL) {
305 (void) fprintf(stderr, "%s: %s: unknown user\n",
306 command, login);
307 exit(1);
309 uid = pwd->pw_uid;
310 gid = pwd->pw_gid;
312 groups[0] = gid;
314 ngrp = _getgroupsbymember(login, groups, (int)ngroups_max, 1);
317 if (user != NULL) {
318 pwd = getpwnam(user);
319 if (pwd == NULL) {
320 uid = (uid_t)str2id(user);
321 if (uid == (uid_t)-1) {
322 (void) fprintf(stderr, "%s: %s: unknown user"
323 " or bad uid\n",
324 command, user);
325 exit(1);
327 } else {
328 uid = pwd->pw_uid;
332 if (group != NULL)
333 gid = str2gid(group);
335 if (grplst != NULL) {
336 char *cgrp;
338 ngrp = 0;
340 while ((cgrp = strtok(grplst, ",")) != NULL) {
342 if (ngrp >= ngroups_max) {
343 (void) fprintf(stderr, "%s: Too many groups\n",
344 command);
345 exit(1);
347 groups[ngrp++] = str2gid(cgrp);
349 /* For iterations of strtok */
350 grplst = NULL;