/src/sudo/lib/iolog/iolog_conf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2009-2021 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <sys/types.h> |
27 | | #include <sys/stat.h> |
28 | | #include <stdio.h> |
29 | | #include <stdlib.h> |
30 | | #ifdef HAVE_STDBOOL_H |
31 | | # include <stdbool.h> |
32 | | #else |
33 | | # include "compat/stdbool.h" |
34 | | #endif |
35 | | |
36 | | #include "pathnames.h" |
37 | | #include "sudo_compat.h" |
38 | | #include "sudo_debug.h" |
39 | | #include "sudo_util.h" |
40 | | #include "sudo_iolog.h" |
41 | | |
42 | | static unsigned int sessid_max = SESSID_MAX; |
43 | | static mode_t iolog_filemode = S_IRUSR|S_IWUSR; |
44 | | static mode_t iolog_dirmode = S_IRWXU; |
45 | | static uid_t iolog_uid = ROOT_UID; |
46 | | static gid_t iolog_gid = ROOT_GID; |
47 | | static bool iolog_gid_set; |
48 | | static bool iolog_docompress; |
49 | | static bool iolog_doflush; |
50 | | |
51 | | /* |
52 | | * Reset I/O log settings to default values. |
53 | | */ |
54 | | void |
55 | | iolog_set_defaults(void) |
56 | 0 | { |
57 | 0 | sessid_max = SESSID_MAX; |
58 | 0 | iolog_filemode = S_IRUSR|S_IWUSR; |
59 | 0 | iolog_dirmode = S_IRWXU; |
60 | 0 | iolog_uid = ROOT_UID; |
61 | 0 | iolog_gid = ROOT_GID; |
62 | 0 | iolog_gid_set = false; |
63 | 0 | iolog_docompress = false; |
64 | 0 | iolog_doflush = false; |
65 | 0 | } |
66 | | |
67 | | /* |
68 | | * Set max sequence number (aka session ID) |
69 | | */ |
70 | | void |
71 | | iolog_set_maxseq(unsigned int newval) |
72 | 0 | { |
73 | 0 | debug_decl(iolog_set_maxseq, SUDO_DEBUG_UTIL); |
74 | | |
75 | | /* Clamp to SESSID_MAX as documented. */ |
76 | 0 | if (newval > SESSID_MAX) |
77 | 0 | newval = SESSID_MAX; |
78 | 0 | sessid_max = newval; |
79 | |
|
80 | 0 | debug_return; |
81 | 0 | } |
82 | | |
83 | | /* |
84 | | * Set iolog_uid (and iolog_gid if gid not explicitly set). |
85 | | */ |
86 | | void |
87 | | iolog_set_owner(uid_t uid, gid_t gid) |
88 | 0 | { |
89 | 0 | debug_decl(iolog_set_owner, SUDO_DEBUG_UTIL); |
90 | |
|
91 | 0 | iolog_uid = uid; |
92 | 0 | if (!iolog_gid_set) |
93 | 0 | iolog_gid = gid; |
94 | |
|
95 | 0 | debug_return; |
96 | 0 | } |
97 | | |
98 | | /* |
99 | | * Set iolog_gid. |
100 | | */ |
101 | | void |
102 | | iolog_set_gid(gid_t gid) |
103 | 0 | { |
104 | 0 | debug_decl(iolog_set_gid, SUDO_DEBUG_UTIL); |
105 | |
|
106 | 0 | iolog_gid = gid; |
107 | 0 | iolog_gid_set = true; |
108 | |
|
109 | 0 | debug_return; |
110 | 0 | } |
111 | | |
112 | | /* |
113 | | * Set iolog_filemode and iolog_dirmode. |
114 | | */ |
115 | | void |
116 | | iolog_set_mode(mode_t mode) |
117 | 0 | { |
118 | 0 | debug_decl(iolog_set_mode, SUDO_DEBUG_UTIL); |
119 | | |
120 | | /* I/O log files must be readable and writable by owner. */ |
121 | 0 | iolog_filemode = S_IRUSR|S_IWUSR; |
122 | | |
123 | | /* Add in group and other read/write if specified. */ |
124 | 0 | iolog_filemode |= mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); |
125 | | |
126 | | /* For directory mode, add execute bits as needed. */ |
127 | 0 | iolog_dirmode = iolog_filemode | S_IXUSR; |
128 | 0 | if (iolog_dirmode & (S_IRGRP|S_IWGRP)) |
129 | 0 | iolog_dirmode |= S_IXGRP; |
130 | 0 | if (iolog_dirmode & (S_IROTH|S_IWOTH)) |
131 | 0 | iolog_dirmode |= S_IXOTH; |
132 | |
|
133 | 0 | debug_return; |
134 | 0 | } |
135 | | |
136 | | /* |
137 | | * Set iolog_docompress |
138 | | */ |
139 | | void |
140 | | iolog_set_compress(bool newval) |
141 | 0 | { |
142 | 0 | debug_decl(iolog_set_compress, SUDO_DEBUG_UTIL); |
143 | 0 | iolog_docompress = newval; |
144 | 0 | debug_return; |
145 | 0 | } |
146 | | |
147 | | /* |
148 | | * Set iolog_doflush |
149 | | */ |
150 | | void |
151 | | iolog_set_flush(bool newval) |
152 | 0 | { |
153 | 0 | debug_decl(iolog_set_flush, SUDO_DEBUG_UTIL); |
154 | 0 | iolog_doflush = newval; |
155 | 0 | debug_return; |
156 | 0 | } |
157 | | |
158 | | /* |
159 | | * Getters. |
160 | | */ |
161 | | |
162 | | unsigned int |
163 | | iolog_get_maxseq(void) |
164 | 0 | { |
165 | 0 | return sessid_max; |
166 | 0 | } |
167 | | |
168 | | uid_t |
169 | | iolog_get_uid(void) |
170 | 843 | { |
171 | 843 | return iolog_uid; |
172 | 843 | } |
173 | | |
174 | | gid_t |
175 | | iolog_get_gid(void) |
176 | 843 | { |
177 | 843 | return iolog_gid; |
178 | 843 | } |
179 | | |
180 | | mode_t |
181 | | iolog_get_file_mode(void) |
182 | 843 | { |
183 | 843 | return iolog_filemode; |
184 | 843 | } |
185 | | |
186 | | mode_t |
187 | | iolog_get_dir_mode(void) |
188 | 843 | { |
189 | 843 | return iolog_dirmode; |
190 | 843 | } |
191 | | |
192 | | bool |
193 | | iolog_get_compress(void) |
194 | 0 | { |
195 | 0 | return iolog_docompress; |
196 | 0 | } |
197 | | |
198 | | bool |
199 | | iolog_get_flush(void) |
200 | 0 | { |
201 | 0 | return iolog_doflush; |
202 | 0 | } |