1219304Strasz/*-
2219304Strasz * Copyright (c) 2011 The FreeBSD Foundation
3219304Strasz * All rights reserved.
4219304Strasz *
5219304Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6219304Strasz * from the FreeBSD Foundation.
7219304Strasz *
8219304Strasz * Redistribution and use in source and binary forms, with or without
9219304Strasz * modification, are permitted provided that the following conditions
10219304Strasz * are met:
11219304Strasz * 1. Redistributions of source code must retain the above copyright
12219304Strasz *    notice, this list of conditions and the following disclaimer.
13219304Strasz * 2. Redistributions in binary form must reproduce the above copyright
14219304Strasz *    notice, this list of conditions and the following disclaimer in the
15219304Strasz *    documentation and/or other materials provided with the distribution.
16219304Strasz *
17219304Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219304Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219304Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219304Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219304Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219304Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219304Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219304Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219304Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219304Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219304Strasz * SUCH DAMAGE.
28219304Strasz *
29219304Strasz * $FreeBSD$
30219304Strasz */
31219304Strasz
32219304Strasz/*
33219304Strasz * Processes may set login class name using setloginclass(2).  This
34219304Strasz * is usually done through call to setusercontext(3), by programs
35219304Strasz * such as login(1), based on information from master.passwd(5).  Kernel
36219304Strasz * uses this information to enforce per-class resource limits.  Current
37219304Strasz * login class can be determined using id(1).  Login class is inherited
38219304Strasz * from the parent process during fork(2).  If not set, it defaults
39219304Strasz * to "default".
40219304Strasz *
41219304Strasz * Code in this file implements setloginclass(2) and getloginclass(2)
42219304Strasz * system calls, and maintains class name storage and retrieval.
43219304Strasz */
44219304Strasz
45219304Strasz#include <sys/cdefs.h>
46219304Strasz__FBSDID("$FreeBSD$");
47219304Strasz
48219304Strasz#include <sys/param.h>
49219304Strasz#include <sys/eventhandler.h>
50219304Strasz#include <sys/kernel.h>
51219304Strasz#include <sys/lock.h>
52219304Strasz#include <sys/loginclass.h>
53219304Strasz#include <sys/malloc.h>
54219304Strasz#include <sys/mutex.h>
55219304Strasz#include <sys/types.h>
56219304Strasz#include <sys/priv.h>
57219304Strasz#include <sys/proc.h>
58219304Strasz#include <sys/queue.h>
59220137Strasz#include <sys/racct.h>
60219304Strasz#include <sys/refcount.h>
61219304Strasz#include <sys/sysproto.h>
62219304Strasz#include <sys/systm.h>
63219304Strasz
64219304Straszstatic MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
65219304Strasz
66219304StraszLIST_HEAD(, loginclass)	loginclasses;
67219304Strasz
68219304Strasz/*
69219304Strasz * Lock protecting loginclasses list.
70219304Strasz */
71219304Straszstatic struct mtx loginclasses_lock;
72267579StraszMTX_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock", MTX_DEF);
73219304Strasz
74219304Straszvoid
75219304Straszloginclass_hold(struct loginclass *lc)
76219304Strasz{
77219304Strasz
78219304Strasz	refcount_acquire(&lc->lc_refcount);
79219304Strasz}
80219304Strasz
81219304Straszvoid
82219304Straszloginclass_free(struct loginclass *lc)
83219304Strasz{
84219304Strasz	int old;
85219304Strasz
86219304Strasz	old = lc->lc_refcount;
87219304Strasz	if (old > 1 && atomic_cmpset_int(&lc->lc_refcount, old, old - 1))
88219304Strasz		return;
89219304Strasz
90219304Strasz	mtx_lock(&loginclasses_lock);
91219304Strasz	if (refcount_release(&lc->lc_refcount)) {
92220137Strasz		racct_destroy(&lc->lc_racct);
93219304Strasz		LIST_REMOVE(lc, lc_next);
94219304Strasz		mtx_unlock(&loginclasses_lock);
95219304Strasz		free(lc, M_LOGINCLASS);
96219304Strasz
97219304Strasz		return;
98219304Strasz	}
99219304Strasz	mtx_unlock(&loginclasses_lock);
100219304Strasz}
101219304Strasz
102219304Strasz/*
103219304Strasz * Return loginclass structure with a corresponding name.  Not
104219304Strasz * performance critical, as it's used mainly by setloginclass(2),
105219304Strasz * which happens once per login session.  Caller has to use
106219304Strasz * loginclass_free() on the returned value when it's no longer
107219304Strasz * needed.
108219304Strasz */
109219304Straszstruct loginclass *
110219304Straszloginclass_find(const char *name)
111219304Strasz{
112219304Strasz	struct loginclass *lc, *newlc;
113219304Strasz
114219304Strasz	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
115219304Strasz		return (NULL);
116219304Strasz
117219304Strasz	newlc = malloc(sizeof(*newlc), M_LOGINCLASS, M_ZERO | M_WAITOK);
118220137Strasz	racct_create(&newlc->lc_racct);
119219304Strasz
120219304Strasz	mtx_lock(&loginclasses_lock);
121219304Strasz	LIST_FOREACH(lc, &loginclasses, lc_next) {
122219304Strasz		if (strcmp(name, lc->lc_name) != 0)
123219304Strasz			continue;
124219304Strasz
125219304Strasz		/* Found loginclass with a matching name? */
126219304Strasz		loginclass_hold(lc);
127219304Strasz		mtx_unlock(&loginclasses_lock);
128220137Strasz		racct_destroy(&newlc->lc_racct);
129219304Strasz		free(newlc, M_LOGINCLASS);
130219304Strasz		return (lc);
131219304Strasz	}
132219304Strasz
133219304Strasz	/* Add new loginclass. */
134219304Strasz	strcpy(newlc->lc_name, name);
135219304Strasz	refcount_init(&newlc->lc_refcount, 1);
136219304Strasz	LIST_INSERT_HEAD(&loginclasses, newlc, lc_next);
137219304Strasz	mtx_unlock(&loginclasses_lock);
138219304Strasz
139219304Strasz	return (newlc);
140219304Strasz}
141219304Strasz
142219304Strasz/*
143219304Strasz * Get login class name.
144219304Strasz */
145219304Strasz#ifndef _SYS_SYSPROTO_H_
146219304Straszstruct getloginclass_args {
147219304Strasz	char	*namebuf;
148219304Strasz	size_t	namelen;
149219304Strasz};
150219304Strasz#endif
151219304Strasz/* ARGSUSED */
152219304Straszint
153225617Skmacysys_getloginclass(struct thread *td, struct getloginclass_args *uap)
154219304Strasz{
155219304Strasz	int error = 0;
156219304Strasz	size_t lcnamelen;
157219304Strasz	struct proc *p;
158219304Strasz	struct loginclass *lc;
159219304Strasz
160219304Strasz	p = td->td_proc;
161219304Strasz	PROC_LOCK(p);
162219304Strasz	lc = p->p_ucred->cr_loginclass;
163219304Strasz	loginclass_hold(lc);
164219304Strasz	PROC_UNLOCK(p);
165219304Strasz
166219304Strasz	lcnamelen = strlen(lc->lc_name) + 1;
167219304Strasz	if (lcnamelen > uap->namelen)
168219304Strasz		error = ERANGE;
169219304Strasz	if (error == 0)
170219304Strasz		error = copyout(lc->lc_name, uap->namebuf, lcnamelen);
171219304Strasz	loginclass_free(lc);
172219304Strasz	return (error);
173219304Strasz}
174219304Strasz
175219304Strasz/*
176219304Strasz * Set login class name.
177219304Strasz */
178219304Strasz#ifndef _SYS_SYSPROTO_H_
179219304Straszstruct setloginclass_args {
180219304Strasz	const char	*namebuf;
181219304Strasz};
182219304Strasz#endif
183219304Strasz/* ARGSUSED */
184219304Straszint
185225617Skmacysys_setloginclass(struct thread *td, struct setloginclass_args *uap)
186219304Strasz{
187219304Strasz	struct proc *p = td->td_proc;
188219304Strasz	int error;
189219304Strasz	char lcname[MAXLOGNAME];
190219304Strasz	struct loginclass *newlc;
191219304Strasz	struct ucred *newcred, *oldcred;
192219304Strasz
193219304Strasz	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
194219304Strasz	if (error != 0)
195219304Strasz		return (error);
196219304Strasz	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
197219304Strasz	if (error != 0)
198219304Strasz		return (error);
199219304Strasz
200219304Strasz	newlc = loginclass_find(lcname);
201219304Strasz	if (newlc == NULL)
202219304Strasz		return (EINVAL);
203219304Strasz	newcred = crget();
204219304Strasz
205219304Strasz	PROC_LOCK(p);
206219304Strasz	oldcred = crcopysafe(p, newcred);
207219304Strasz	newcred->cr_loginclass = newlc;
208219304Strasz	p->p_ucred = newcred;
209219304Strasz	PROC_UNLOCK(p);
210220137Strasz#ifdef RACCT
211220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
212220137Strasz#endif
213219304Strasz	loginclass_free(oldcred->cr_loginclass);
214219304Strasz	crfree(oldcred);
215219304Strasz
216219304Strasz	return (0);
217219304Strasz}
218219304Strasz
219220137Straszvoid
220220137Straszloginclass_racct_foreach(void (*callback)(struct racct *racct,
221220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
222220137Strasz{
223220137Strasz	struct loginclass *lc;
224220137Strasz
225220137Strasz	mtx_lock(&loginclasses_lock);
226220137Strasz	LIST_FOREACH(lc, &loginclasses, lc_next)
227220137Strasz		(callback)(lc->lc_racct, arg2, arg3);
228220137Strasz	mtx_unlock(&loginclasses_lock);
229220137Strasz}
230