/src/sudo/lib/iolog/iolog_legacy.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2009-2020 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  |  | #ifdef HAVE_STDBOOL_H  | 
24  |  | # include <stdbool.h>  | 
25  |  | #else  | 
26  |  | # include <compat/stdbool.h>  | 
27  |  | #endif /* HAVE_STDBOOL_H */  | 
28  |  | #include <string.h>  | 
29  |  | #include <signal.h>  | 
30  |  | #include <limits.h>  | 
31  |  | #include <time.h>  | 
32  |  |  | 
33  |  | #include <sudo_compat.h>  | 
34  |  | #include <sudo_debug.h>  | 
35  |  | #include <sudo_eventlog.h>  | 
36  |  | #include <sudo_fatal.h>  | 
37  |  | #include <sudo_gettext.h>  | 
38  |  | #include <sudo_iolog.h>  | 
39  |  | #include <sudo_util.h>  | 
40  |  |  | 
41  |  | bool  | 
42  |  | iolog_parse_loginfo_legacy(FILE *fp, const char *iolog_dir,  | 
43  |  |     struct eventlog *evlog)  | 
44  | 387  | { | 
45  | 387  |     char *buf = NULL, *cp, *ep;  | 
46  | 387  |     const char *errstr;  | 
47  | 387  |     size_t bufsize = 0, cwdsize = 0, cmdsize = 0;  | 
48  | 387  |     bool ret = false;  | 
49  | 387  |     debug_decl(iolog_parse_loginfo_legacy, SUDO_DEBUG_UTIL);  | 
50  |  |  | 
51  |  |     /*  | 
52  |  |      * Info file has three lines:  | 
53  |  |      *  1) a log info line  | 
54  |  |      *  2) cwd  | 
55  |  |      *  3) command with args  | 
56  |  |      */  | 
57  | 387  |     if (getdelim(&buf, &bufsize, '\n', fp) == -1 ||  | 
58  | 387  |   getdelim(&evlog->cwd, &cwdsize, '\n', fp) == -1 ||  | 
59  | 365  |   getdelim(&evlog->command, &cmdsize, '\n', fp) == -1) { | 
60  | 42  |   sudo_warn(U_("%s: invalid log file"), iolog_dir); | 
61  | 42  |   goto done;  | 
62  | 42  |     }  | 
63  |  |  | 
64  |  |     /* Strip the newline from the cwd and command. */  | 
65  | 345  |     evlog->cwd[strcspn(evlog->cwd, "\n")] = '\0';  | 
66  | 345  |     evlog->command[strcspn(evlog->command, "\n")] = '\0';  | 
67  |  |  | 
68  |  |     /*  | 
69  |  |      * Crack the log line (lines and cols not present in old versions).  | 
70  |  |      *  timestamp:user:runas_user:runas_group:tty:lines:cols  | 
71  |  |      * XXX - probably better to use strtok and switch on the state.  | 
72  |  |      */  | 
73  | 345  |     buf[strcspn(buf, "\n")] = '\0';  | 
74  | 345  |     cp = buf;  | 
75  |  |  | 
76  |  |     /* timestamp */  | 
77  | 345  |     if ((ep = strchr(cp, ':')) == NULL) { | 
78  | 19  |   sudo_warn(U_("%s: time stamp field is missing"), iolog_dir); | 
79  | 19  |   goto done;  | 
80  | 19  |     }  | 
81  | 326  |     *ep = '\0';  | 
82  | 326  |     evlog->event_time.tv_sec =  | 
83  | 326  |   (time_t)sudo_strtonum(cp, 0, TIME_T_MAX, &errstr);  | 
84  | 326  |     if (errstr != NULL) { | 
85  | 110  |   sudo_warn(U_("%s: time stamp %s: %s"), iolog_dir, cp, errstr); | 
86  | 110  |   goto done;  | 
87  | 110  |     }  | 
88  |  |  | 
89  |  |     /* submit user */  | 
90  | 216  |     cp = ep + 1;  | 
91  | 216  |     if ((ep = strchr(cp, ':')) == NULL) { | 
92  | 132  |   sudo_warn(U_("%s: user field is missing"), iolog_dir); | 
93  | 132  |   goto done;  | 
94  | 132  |     }  | 
95  | 84  |     if ((evlog->submituser = strndup(cp, (size_t)(ep - cp))) == NULL) { | 
96  | 0  |   sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
97  | 0  |   goto done;  | 
98  | 0  |     }  | 
99  |  |  | 
100  |  |     /* runas user */  | 
101  | 84  |     cp = ep + 1;  | 
102  | 84  |     if ((ep = strchr(cp, ':')) == NULL) { | 
103  | 1  |   sudo_warn(U_("%s: runas user field is missing"), iolog_dir); | 
104  | 1  |   goto done;  | 
105  | 1  |     }  | 
106  | 83  |     if ((evlog->runuser = strndup(cp, (size_t)(ep - cp))) == NULL) { | 
107  | 0  |   sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
108  | 0  |   goto done;  | 
109  | 0  |     }  | 
110  |  |  | 
111  |  |     /* runas group */  | 
112  | 83  |     cp = ep + 1;  | 
113  | 83  |     if ((ep = strchr(cp, ':')) == NULL) { | 
114  | 1  |   sudo_warn(U_("%s: runas group field is missing"), iolog_dir); | 
115  | 1  |   goto done;  | 
116  | 1  |     }  | 
117  | 82  |     if (cp != ep) { | 
118  | 1  |   if ((evlog->rungroup = strndup(cp, (size_t)(ep - cp))) == NULL) { | 
119  | 0  |       sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
120  | 0  |       goto done;  | 
121  | 0  |   }  | 
122  | 1  |     }  | 
123  |  |  | 
124  |  |     /* tty, followed by optional lines + cols */  | 
125  | 82  |     cp = ep + 1;  | 
126  | 82  |     if ((ep = strchr(cp, ':')) == NULL) { | 
127  |  |   /* just the tty */  | 
128  | 2  |   if ((evlog->ttyname = strdup(cp)) == NULL) { | 
129  | 0  |       sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
130  | 0  |       goto done;  | 
131  | 0  |   }  | 
132  | 80  |     } else { | 
133  |  |   /* tty followed by lines + cols */  | 
134  | 80  |   if ((evlog->ttyname = strndup(cp, (size_t)(ep - cp))) == NULL) { | 
135  | 0  |       sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
136  | 0  |       goto done;  | 
137  | 0  |   }  | 
138  | 80  |   cp = ep + 1;  | 
139  |  |   /* need to NULL out separator to use sudo_strtonum() */  | 
140  |  |   /* XXX - use sudo_strtonumx */  | 
141  | 80  |   if ((ep = strchr(cp, ':')) != NULL) { | 
142  | 12  |       *ep = '\0';  | 
143  | 12  |   }  | 
144  | 80  |   evlog->lines = (int)sudo_strtonum(cp, 1, INT_MAX, &errstr);  | 
145  | 80  |   if (errstr != NULL) { | 
146  | 54  |       sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
147  | 54  |     "%s: tty lines %s: %s", iolog_dir, cp, errstr);  | 
148  | 54  |   }  | 
149  | 80  |   if (ep != NULL) { | 
150  | 12  |       cp = ep + 1;  | 
151  | 12  |       evlog->columns = (int)sudo_strtonum(cp, 1, INT_MAX, &errstr);  | 
152  | 12  |       if (errstr != NULL) { | 
153  | 8  |     sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
154  | 8  |         "%s: tty cols %s: %s", iolog_dir, cp, errstr);  | 
155  | 8  |       }  | 
156  | 12  |   }  | 
157  | 80  |     }  | 
158  |  |  | 
159  | 82  |     ret = true;  | 
160  |  |  | 
161  | 387  | done:  | 
162  | 387  |     free(buf);  | 
163  |  |     debug_return_bool(ret);  | 
164  | 387  | }  |