1174199Srwatson/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4224859Srwatson * Copyright (c) 2007-2008 Robert N. M. Watson
5287486Sallanjude * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6174199Srwatson * All rights reserved.
7174199Srwatson *
8174199Srwatson * Redistribution and use in source and binary forms, with or without
9174199Srwatson * modification, are permitted provided that the following conditions
10174199Srwatson * are met:
11174199Srwatson * 1. Redistributions of source code must retain the above copyright
12174199Srwatson *    notice, this list of conditions and the following disclaimer.
13174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14174199Srwatson *    notice, this list of conditions and the following disclaimer in the
15174199Srwatson *    documentation and/or other materials provided with the distribution.
16174199Srwatson *
17174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20174199Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27174199Srwatson * SUCH DAMAGE.
28174199Srwatson *
29174199Srwatson * $FreeBSD: stable/11/usr.bin/procstat/procstat_cred.c 330449 2018-03-05 07:26:05Z eadler $
30174199Srwatson */
31174199Srwatson
32186567Srwatson#include <sys/param.h>
33174199Srwatson#include <sys/sysctl.h>
34174199Srwatson#include <sys/user.h>
35174199Srwatson
36174199Srwatson#include <err.h>
37221807Sstas#include <libprocstat.h>
38195853Sbrooks#include <stdlib.h>
39174199Srwatson#include <stdio.h>
40195853Sbrooks#include <unistd.h>
41174199Srwatson
42174199Srwatson#include "procstat.h"
43174199Srwatson
44249673Strocinystatic const char *get_umask(struct procstat *procstat,
45249673Strociny    struct kinfo_proc *kipp);
46232182Strociny
47174199Srwatsonvoid
48249671Strocinyprocstat_cred(struct procstat *procstat, struct kinfo_proc *kipp)
49174199Srwatson{
50249671Strociny	unsigned int i, ngroups;
51249671Strociny	gid_t *groups;
52174199Srwatson
53174199Srwatson	if (!hflag)
54287486Sallanjude		xo_emit("{T:/%5s %-16s %5s %5s %5s %5s %5s %5s %5s %5s %-15s}\n",
55232182Strociny		    "PID", "COMM", "EUID", "RUID", "SVUID", "EGID", "RGID",
56232182Strociny		    "SVGID", "UMASK", "FLAGS", "GROUPS");
57174199Srwatson
58287486Sallanjude	xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
59287486Sallanjude	xo_emit("{:command/%-16s/%s} ", kipp->ki_comm);
60287486Sallanjude	xo_emit("{:uid/%5d} ", kipp->ki_uid);
61287486Sallanjude	xo_emit("{:ruid/%5d} ", kipp->ki_ruid);
62287486Sallanjude	xo_emit("{:svuid/%5d} ", kipp->ki_svuid);
63287486Sallanjude	xo_emit("{:group/%5d} ", kipp->ki_groups[0]);
64287486Sallanjude	xo_emit("{:rgid/%5d} ", kipp->ki_rgid);
65287486Sallanjude	xo_emit("{:svgid/%5d} ", kipp->ki_svgid);
66287486Sallanjude	xo_emit("{:umask/%5s} ", get_umask(procstat, kipp));
67287486Sallanjude	xo_emit("{:cr_flags/%s}", kipp->ki_cr_flags & CRED_FLAG_CAPMODE ?
68287486Sallanjude	    "C" : "-");
69287486Sallanjude	xo_emit("{P:     }");
70195853Sbrooks
71249671Strociny	groups = NULL;
72195853Sbrooks	/*
73195853Sbrooks	 * We may have too many groups to fit in kinfo_proc's statically
74249671Strociny	 * sized storage.  If that occurs, attempt to retrieve them using
75249671Strociny	 * libprocstat.
76195853Sbrooks	 */
77249671Strociny	if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW)
78249671Strociny		groups = procstat_getgroups(procstat, kipp, &ngroups);
79195853Sbrooks	if (groups == NULL) {
80195853Sbrooks		ngroups = kipp->ki_ngroups;
81195853Sbrooks		groups = kipp->ki_groups;
82195853Sbrooks	}
83287486Sallanjude	xo_open_list("groups");
84195853Sbrooks	for (i = 0; i < ngroups; i++)
85287486Sallanjude		xo_emit("{D:/%s}{l:groups/%d}", (i > 0) ? "," : "", groups[i]);
86195853Sbrooks	if (groups != kipp->ki_groups)
87249671Strociny		procstat_freegroups(procstat, groups);
88195853Sbrooks
89287486Sallanjude	xo_close_list("groups");
90287486Sallanjude	xo_emit("\n");
91174199Srwatson}
92232182Strociny
93232182Strocinystatic const char *
94249673Strocinyget_umask(struct procstat *procstat, struct kinfo_proc *kipp)
95232182Strociny{
96232182Strociny	u_short fd_cmask;
97232182Strociny	static char umask[4];
98232182Strociny
99249673Strociny	if (procstat_getumask(procstat, kipp, &fd_cmask) == 0) {
100232182Strociny		snprintf(umask, 4, "%03o", fd_cmask);
101232182Strociny		return (umask);
102232182Strociny	} else {
103232182Strociny		return ("-");
104232182Strociny	}
105232182Strociny}
106