/src/sudo/plugins/sudoers/iolog_path_escapes.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2011-2015 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  |  | #include <config.h>  | 
20  |  |  | 
21  |  | #include <stdio.h>  | 
22  |  | #include <stdlib.h>  | 
23  |  | #include <string.h>  | 
24  |  | #include <pwd.h>  | 
25  |  | #include <grp.h>  | 
26  |  | #include <unistd.h>  | 
27  |  |  | 
28  |  | #include <sudoers.h>  | 
29  |  | #include <sudo_iolog.h>  | 
30  |  |  | 
31  |  | /*  | 
32  |  |  * Like strlcpy(3) but replaces '/' with '_'.  | 
33  |  |  */  | 
34  |  | static size_t  | 
35  |  | strlcpy_no_slash(char * restrict dst, const char * restrict src, size_t size)  | 
36  | 0  | { | 
37  | 0  |     size_t len = 0;  | 
38  | 0  |     char ch;  | 
39  | 0  |     debug_decl(strlcpy_no_slash, SUDOERS_DEBUG_UTIL);  | 
40  |  | 
  | 
41  | 0  |     while ((ch = *src++) != '\0') { | 
42  | 0  |   if (size > 1) { | 
43  |  |       /* Replace '/' with '_' */  | 
44  | 0  |       if (ch == '/')  | 
45  | 0  |     ch = '_';  | 
46  | 0  |       *dst++ = ch;  | 
47  | 0  |       size--;  | 
48  | 0  |   }  | 
49  | 0  |   len++;  | 
50  | 0  |     }  | 
51  | 0  |     if (size > 0)  | 
52  | 0  |   *dst = '\0';  | 
53  |  | 
  | 
54  | 0  |     debug_return_size_t(len);  | 
55  | 0  | }  | 
56  |  |  | 
57  |  | static size_t  | 
58  |  | fill_seq(char * restrict str, size_t strsize, void * restrict v)  | 
59  | 0  | { | 
60  |  | #ifdef SUDOERS_NO_SEQ  | 
61  |  |     debug_decl(fill_seq, SUDOERS_DEBUG_UTIL);  | 
62  |  |     debug_return_size_t(strlcpy(str, "%{seq}", strsize)); | 
63  |  | #else  | 
64  | 0  |     struct sudoers_context *ctx = v;  | 
65  | 0  |     static char sessid[7];  | 
66  | 0  |     int len;  | 
67  | 0  |     debug_decl(fill_seq, SUDOERS_DEBUG_UTIL);  | 
68  |  | 
  | 
69  | 0  |     if (sessid[0] == '\0') { | 
70  | 0  |   if (!iolog_nextid(ctx->iolog_dir, sessid))  | 
71  | 0  |       debug_return_size_t((size_t)-1);  | 
72  | 0  |     }  | 
73  |  |  | 
74  |  |     /* Path is of the form /var/log/sudo-io/00/00/01. */  | 
75  | 0  |     len = snprintf(str, strsize, "%c%c/%c%c/%c%c", sessid[0],  | 
76  | 0  |   sessid[1], sessid[2], sessid[3], sessid[4], sessid[5]);  | 
77  | 0  |     if (len < 0)  | 
78  | 0  |   debug_return_size_t(strsize); /* handle non-standard snprintf() */  | 
79  | 0  |     debug_return_size_t((size_t)len);  | 
80  | 0  | #endif /* SUDOERS_NO_SEQ */  | 
81  | 0  | }  | 
82  |  |  | 
83  |  | static size_t  | 
84  |  | fill_user(char * restrict str, size_t strsize, void * restrict v)  | 
85  | 0  | { | 
86  | 0  |     struct sudoers_context *ctx = v;  | 
87  | 0  |     debug_decl(fill_user, SUDOERS_DEBUG_UTIL);  | 
88  | 0  |     debug_return_size_t(strlcpy_no_slash(str, ctx->user.name, strsize));  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | static size_t  | 
92  |  | fill_group(char * restrict str, size_t strsize, void * restrict v)  | 
93  | 0  | { | 
94  | 0  |     struct sudoers_context *ctx = v;  | 
95  | 0  |     struct group *grp;  | 
96  | 0  |     size_t len;  | 
97  | 0  |     debug_decl(fill_group, SUDOERS_DEBUG_UTIL);  | 
98  |  | 
  | 
99  | 0  |     if ((grp = sudo_getgrgid(ctx->user.gid)) != NULL) { | 
100  | 0  |   len = strlcpy_no_slash(str, grp->gr_name, strsize);  | 
101  | 0  |   sudo_gr_delref(grp);  | 
102  | 0  |     } else { | 
103  | 0  |   len = (size_t)snprintf(str, strsize, "#%u", (unsigned int)ctx->user.gid);  | 
104  | 0  |     }  | 
105  | 0  |     debug_return_size_t(len);  | 
106  | 0  | }  | 
107  |  |  | 
108  |  | static size_t  | 
109  |  | fill_runas_user(char * restrict str, size_t strsize, void * restrict v)  | 
110  | 0  | { | 
111  | 0  |     struct sudoers_context *ctx = v;  | 
112  | 0  |     debug_decl(fill_runas_user, SUDOERS_DEBUG_UTIL);  | 
113  | 0  |     debug_return_size_t(strlcpy_no_slash(str, ctx->runas.pw->pw_name, strsize));  | 
114  | 0  | }  | 
115  |  |  | 
116  |  | static size_t  | 
117  |  | fill_runas_group(char * restrict str, size_t strsize, void * restrict v)  | 
118  | 0  | { | 
119  | 0  |     struct sudoers_context *ctx = v;  | 
120  | 0  |     struct group *grp;  | 
121  | 0  |     size_t len;  | 
122  | 0  |     debug_decl(fill_runas_group, SUDOERS_DEBUG_UTIL);  | 
123  |  | 
  | 
124  | 0  |     if (ctx->runas.gr != NULL) { | 
125  | 0  |   len = strlcpy_no_slash(str, ctx->runas.gr->gr_name, strsize);  | 
126  | 0  |     } else { | 
127  | 0  |   if ((grp = sudo_getgrgid(ctx->runas.pw->pw_gid)) != NULL) { | 
128  | 0  |       len = strlcpy_no_slash(str, grp->gr_name, strsize);  | 
129  | 0  |       sudo_gr_delref(grp);  | 
130  | 0  |   } else { | 
131  | 0  |       len = (size_t)snprintf(str, strsize, "#%u",  | 
132  | 0  |     (unsigned int)ctx->runas.pw->pw_gid);  | 
133  | 0  |   }  | 
134  | 0  |     }  | 
135  | 0  |     debug_return_size_t(len);  | 
136  | 0  | }  | 
137  |  |  | 
138  |  | static size_t  | 
139  |  | fill_hostname(char * restrict str, size_t strsize, void * restrict v)  | 
140  | 0  | { | 
141  | 0  |     struct sudoers_context *ctx = v;  | 
142  | 0  |     debug_decl(fill_hostname, SUDOERS_DEBUG_UTIL);  | 
143  | 0  |     debug_return_size_t(strlcpy_no_slash(str, ctx->user.shost, strsize));  | 
144  | 0  | }  | 
145  |  |  | 
146  |  | static size_t  | 
147  |  | fill_command(char * restrict str, size_t strsize, void * restrict v)  | 
148  | 0  | { | 
149  | 0  |     struct sudoers_context *ctx = v;  | 
150  | 0  |     debug_decl(fill_command, SUDOERS_DEBUG_UTIL);  | 
151  | 0  |     debug_return_size_t(strlcpy_no_slash(str, ctx->user.cmnd_base, strsize));  | 
152  | 0  | }  | 
153  |  |  | 
154  |  | /* Note: "seq" must be first in the list. */  | 
155  |  | static const struct iolog_path_escape path_escapes[] = { | 
156  |  |     { "seq", fill_seq }, | 
157  |  |     { "user", fill_user }, | 
158  |  |     { "group", fill_group }, | 
159  |  |     { "runas_user", fill_runas_user }, | 
160  |  |     { "runas_group", fill_runas_group }, | 
161  |  |     { "hostname", fill_hostname }, | 
162  |  |     { "command", fill_command }, | 
163  |  |     { NULL, NULL } | 
164  |  | };  | 
165  |  | const struct iolog_path_escape *sudoers_iolog_path_escapes = path_escapes;  |