/src/sudo/lib/iolog/iolog_open.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 <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #ifdef HAVE_STDBOOL_H |
29 | | # include <stdbool.h> |
30 | | #else |
31 | | # include "compat/stdbool.h" |
32 | | #endif |
33 | | #include <errno.h> |
34 | | #include <fcntl.h> |
35 | | #include <time.h> |
36 | | #include <unistd.h> |
37 | | |
38 | | #include "sudo_compat.h" |
39 | | #include "sudo_debug.h" |
40 | | #include "sudo_iolog.h" |
41 | | #include "sudo_util.h" |
42 | | |
43 | | static unsigned char const gzip_magic[2] = {0x1f, 0x8b}; |
44 | | |
45 | | /* |
46 | | * Open the specified I/O log file and store in iol. |
47 | | * Stores the open file handle which has the close-on-exec flag set. |
48 | | */ |
49 | | bool |
50 | | iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode) |
51 | 843 | { |
52 | 843 | int flags; |
53 | 843 | const char *file; |
54 | 843 | unsigned char magic[2]; |
55 | 843 | const uid_t iolog_uid = iolog_get_uid(); |
56 | 843 | const gid_t iolog_gid = iolog_get_gid(); |
57 | 843 | debug_decl(iolog_open, SUDO_DEBUG_UTIL); |
58 | | |
59 | 843 | if (mode[0] == 'r') { |
60 | 843 | flags = mode[1] == '+' ? O_RDWR : O_RDONLY; |
61 | 843 | } else if (mode[0] == 'w') { |
62 | 0 | flags = O_CREAT|O_TRUNC; |
63 | 0 | flags |= mode[1] == '+' ? O_RDWR : O_WRONLY; |
64 | 0 | } else { |
65 | 0 | sudo_debug_printf(SUDO_DEBUG_ERROR, |
66 | 0 | "%s: invalid I/O mode %s", __func__, mode); |
67 | 0 | debug_return_bool(false); |
68 | 0 | } |
69 | 843 | if ((file = iolog_fd_to_name(iofd)) == NULL) { |
70 | 0 | sudo_debug_printf(SUDO_DEBUG_ERROR, |
71 | 0 | "%s: invalid iofd %d", __func__, iofd); |
72 | 0 | debug_return_bool(false); |
73 | 0 | } |
74 | | |
75 | 843 | iol->writable = false; |
76 | 843 | iol->compressed = false; |
77 | 843 | if (iol->enabled) { |
78 | 843 | int fd = iolog_openat(dfd, file, flags); |
79 | 843 | if (fd != -1) { |
80 | 843 | if (*mode == 'w') { |
81 | 0 | if (fchown(fd, iolog_uid, iolog_gid) != 0) { |
82 | 0 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, |
83 | 0 | "%s: unable to fchown %d:%d %s", __func__, |
84 | 0 | (int)iolog_uid, (int)iolog_gid, file); |
85 | 0 | } |
86 | 0 | iol->compressed = iolog_get_compress(); |
87 | 843 | } else { |
88 | | /* check for gzip magic number */ |
89 | 843 | if (pread(fd, magic, sizeof(magic), 0) == ssizeof(magic)) { |
90 | 828 | if (magic[0] == gzip_magic[0] && magic[1] == gzip_magic[1]) |
91 | 1 | iol->compressed = true; |
92 | 828 | } |
93 | 843 | } |
94 | 843 | if (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1) { |
95 | | #ifdef HAVE_ZLIB_H |
96 | | if (iol->compressed) |
97 | | iol->fd.g = gzdopen(fd, mode); |
98 | | else |
99 | | #endif |
100 | 843 | iol->fd.f = fdopen(fd, mode); |
101 | 843 | } |
102 | 843 | if (iol->fd.v != NULL) { |
103 | 843 | switch ((flags & O_ACCMODE)) { |
104 | 0 | case O_WRONLY: |
105 | 0 | case O_RDWR: |
106 | 0 | iol->writable = true; |
107 | 0 | break; |
108 | 843 | } |
109 | 843 | } else { |
110 | 0 | int save_errno = errno; |
111 | 0 | close(fd); |
112 | 0 | errno = save_errno; |
113 | 0 | fd = -1; |
114 | 0 | } |
115 | 843 | } |
116 | 843 | if (fd == -1) { |
117 | 0 | iol->enabled = false; |
118 | 0 | debug_return_bool(false); |
119 | 0 | } |
120 | 843 | } else { |
121 | 0 | if (*mode == 'w') { |
122 | | /* Remove old log file in case we recycled sequence numbers. */ |
123 | 0 | (void)unlinkat(dfd, file, 0); |
124 | 0 | } |
125 | 0 | } |
126 | 843 | debug_return_bool(true); |
127 | 843 | } |