priv_proc_setlogin.c revision 256281
1285242Sachim/*-
2285242Sachim * Copyright (c) 2006 nCircle Network Security, Inc.
3285242Sachim * Copyright (c) 2007 Robert N. M. Watson
4285242Sachim * All rights reserved.
5285242Sachim *
6285242Sachim * This software was developed by Robert N. M. Watson for the TrustedBSD
7285242Sachim * Project under contract to nCircle Network Security, Inc.
8285242Sachim *
9285242Sachim * Redistribution and use in source and binary forms, with or without
10285242Sachim * modification, are permitted provided that the following conditions
11285242Sachim * are met:
12285242Sachim * 1. Redistributions of source code must retain the above copyright
13285242Sachim *    notice, this list of conditions and the following disclaimer.
14285242Sachim * 2. Redistributions in binary form must reproduce the above copyright
15285242Sachim *    notice, this list of conditions and the following disclaimer in the
16285242Sachim *    documentation and/or other materials provided with the distribution.
17285242Sachim *
18285242Sachim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19285242Sachim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20285242Sachim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21285242Sachim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22285242Sachim * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23285242Sachim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24285242Sachim * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25285242Sachim * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26285242Sachim * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27285242Sachim * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28285242Sachim * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29285242Sachim *
30285242Sachim * $FreeBSD: stable/10/tools/regression/priv/priv_proc_setlogin.c 172106 2007-09-09 23:08:39Z rwatson $
31285242Sachim */
32285242Sachim
33285242Sachim/*
34285242Sachim * Test privileges for setlogin(); first query with getlogin() so that the
35285242Sachim * result is a no-op, since it affects the entire login session.
36285242Sachim */
37285242Sachim
38285242Sachim#include <sys/param.h>
39285242Sachim
40285242Sachim#include <err.h>
41285242Sachim#include <errno.h>
42285242Sachim#include <unistd.h>
43285242Sachim
44285242Sachim#include "main.h"
45285242Sachim
46285242Sachimstatic int initialized;
47285242Sachimstatic char *loginname;
48285242Sachim
49285242Sachimint
50285242Sachimpriv_proc_setlogin_setup(int asroot, int injail, struct test *test)
51285242Sachim{
52285242Sachim
53285242Sachim	if (initialized)
54285242Sachim		return (0);
55285242Sachim	loginname = getlogin();
56285242Sachim	if (loginname == NULL) {
57285242Sachim		warn("priv_proc_setlogin_setup: getlogin");
58285242Sachim		return (-1);
59285242Sachim	}
60285242Sachim	initialized = 1;
61285242Sachim	return (0);
62285242Sachim}
63285242Sachim
64285242Sachimvoid
65285242Sachimpriv_proc_setlogin(int asroot, int injail, struct test *test)
66285242Sachim{
67285242Sachim	int error;
68285242Sachim
69285242Sachim	error = setlogin(loginname);
70285242Sachim	if (asroot && injail)
71285242Sachim		expect("priv_proc_setlogin(asroot, injail)", error, 0, 0);
72285242Sachim	if (asroot && !injail)
73285242Sachim		expect("priv_proc_setlogin(asroot, !injail)", error, 0, 0);
74285242Sachim	if (!asroot && injail)
75285242Sachim		expect("priv_proc_setlogin(!sroot, injail)", error, -1,
76285242Sachim		    EPERM);
77285242Sachim	if (!asroot && !injail)
78285242Sachim		expect("priv_proc_setlogin(!asroot, !injail)", error, -1,
79285242Sachim		    EPERM);
80285242Sachim}
81285242Sachim
82285242Sachimvoid
83285242Sachimpriv_proc_setlogin_cleanup(int asroot, int injail, struct test *test)
84285242Sachim{
85285242Sachim
86285242Sachim	if (initialized)
87285242Sachim		(void)setlogin(loginname);
88285242Sachim}
89285242Sachim