1162271Srwatson/*-
2162271Srwatson * Copyright (c) 2006 nCircle Network Security, Inc.
3172106Srwatson * Copyright (c) 2007 Robert N. M. Watson
4162271Srwatson * All rights reserved.
5162271Srwatson *
6162271Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD
7162271Srwatson * Project under contract to nCircle Network Security, Inc.
8162271Srwatson *
9162271Srwatson * Redistribution and use in source and binary forms, with or without
10162271Srwatson * modification, are permitted provided that the following conditions
11162271Srwatson * are met:
12162271Srwatson * 1. Redistributions of source code must retain the above copyright
13162271Srwatson *    notice, this list of conditions and the following disclaimer.
14162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15162271Srwatson *    notice, this list of conditions and the following disclaimer in the
16162271Srwatson *    documentation and/or other materials provided with the distribution.
17162271Srwatson *
18162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19162271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20162271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21162271Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22162271Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23162271Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24162271Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25162271Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26162271Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27162271Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28162271Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29162271Srwatson *
30162271Srwatson * $FreeBSD$
31162271Srwatson */
32162271Srwatson
33162271Srwatson/*
34162271Srwatson * Confirm that privilege is required to invoke adjtime(); first query, then
35172106Srwatson * try setting with and without privilege.  Hopefully this will not disturb
36172106Srwatson * system time too much.
37162271Srwatson */
38162271Srwatson
39162271Srwatson#include <sys/time.h>
40162271Srwatson
41162271Srwatson#include <err.h>
42162271Srwatson#include <errno.h>
43162271Srwatson#include <unistd.h>
44162271Srwatson
45162271Srwatson#include "main.h"
46162271Srwatson
47172106Srwatsonstatic int		initialized;
48172106Srwatsonstatic struct timeval	query_tv;
49172106Srwatson
50172106Srwatsonint
51172106Srwatsonpriv_adjtime_setup(int asroot, int injail, struct test *test)
52172106Srwatson{
53172106Srwatson
54172106Srwatson	if (initialized)
55172106Srwatson		return (0);
56172106Srwatson	if (adjtime(NULL, &query_tv) < 0) {
57172106Srwatson		warn("priv_adjtime_setup: adjtime(NULL)");
58172106Srwatson		return (-1);
59172106Srwatson	}
60172106Srwatson	initialized = 1;
61172106Srwatson	return (0);
62172106Srwatson}
63172106Srwatson
64162271Srwatsonvoid
65172106Srwatsonpriv_adjtime_set(int asroot, int injail, struct test *test)
66162271Srwatson{
67162271Srwatson	int error;
68162271Srwatson
69172106Srwatson	error = adjtime(&query_tv, NULL);
70172106Srwatson	if (asroot && injail)
71172106Srwatson		expect("priv_adjtime(asroot, injail)", error, -1, EPERM);
72172106Srwatson	if (asroot && !injail)
73172106Srwatson		expect("priv_adjtime(asroot, !injail)", error, 0, 0);
74172106Srwatson	if (!asroot && injail)
75172106Srwatson		expect("priv_adjtime(!asroot, injail)", error, -1, EPERM);
76172106Srwatson	if (!asroot && !injail)
77172106Srwatson		expect("priv_adjtime(!asroot, !injail)", error, -1, EPERM);
78172106Srwatson}
79162271Srwatson
80172106Srwatsonvoid
81172106Srwatsonpriv_adjtime_cleanup(int asroot, int injail, struct test *test)
82172106Srwatson{
83162271Srwatson
84162271Srwatson}
85