/src/sudo/lib/util/parseln.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2007, 2013-2016 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 <ctype.h>  | 
22  |  | #include <errno.h>  | 
23  |  | #include <stdio.h>  | 
24  |  | #include <stdlib.h>  | 
25  |  | #include <string.h>  | 
26  |  | #ifdef HAVE_STDBOOL_H  | 
27  |  | # include <stdbool.h>  | 
28  |  | #else  | 
29  |  | # include <compat/stdbool.h>  | 
30  |  | #endif  | 
31  |  |  | 
32  |  | #include <sudo_compat.h>  | 
33  |  | #include <sudo_util.h>  | 
34  |  | #include <sudo_debug.h>  | 
35  |  |  | 
36  |  | /*  | 
37  |  |  * Read a line of input, honoring line continuation chars.  | 
38  |  |  * Remove comments and strip off leading and trailing spaces.  | 
39  |  |  * Returns the line length and updates the buf and bufsize pointers.  | 
40  |  |  * XXX - just use a struct w/ state, including getdelim buffer?  | 
41  |  |  *       could also make comment char and line continuation configurable  | 
42  |  |  */  | 
43  |  | ssize_t  | 
44  |  | sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, int flags)  | 
45  | 26.4k  | { | 
46  | 26.4k  |     ssize_t len, total = 0;  | 
47  | 26.4k  |     size_t bufsize, linesize = 0;  | 
48  | 26.4k  |     char *cp, *line = NULL;  | 
49  | 26.4k  |     bool continued, comment;  | 
50  | 26.4k  |     debug_decl(sudo_parseln, SUDO_DEBUG_UTIL);  | 
51  |  |  | 
52  | 26.9k  |     do { | 
53  | 26.9k  |   comment = false;  | 
54  | 26.9k  |   continued = false;  | 
55  | 26.9k  |   len = getdelim(&line, &linesize, '\n', fp);  | 
56  | 26.9k  |   if (len == -1)  | 
57  | 916  |       break;  | 
58  | 26.0k  |   if (lineno != NULL)  | 
59  | 26.0k  |       (*lineno)++;  | 
60  |  |  | 
61  |  |   /* Remove trailing newline(s) if present. */  | 
62  | 49.1k  |   while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))  | 
63  | 23.1k  |       line[--len] = '\0';  | 
64  |  |  | 
65  |  |   /* Remove comments or check for line continuation (but not both) */  | 
66  | 26.0k  |   if ((cp = strchr(line, '#')) != NULL) { | 
67  | 474  |       if (cp == line || !ISSET(flags, PARSELN_COMM_BOL)) { | 
68  | 474  |     *cp = '\0';  | 
69  | 474  |     len = (ssize_t)(cp - line);  | 
70  | 474  |     comment = true;  | 
71  | 474  |       }  | 
72  | 474  |   }  | 
73  | 26.0k  |   if (!comment && !ISSET(flags, PARSELN_CONT_IGN)) { | 
74  | 25.5k  |       if (len > 0 && line[len - 1] == '\\' && (len == 1 || line[len - 2] != '\\')) { | 
75  | 524  |     line[--len] = '\0';  | 
76  | 524  |     continued = true;  | 
77  | 524  |       }  | 
78  | 25.5k  |   }  | 
79  |  |  | 
80  |  |   /* Trim leading and trailing whitespace */  | 
81  | 26.0k  |   if (!continued) { | 
82  | 25.8k  |       while (len > 0 && isblank((unsigned char)line[len - 1]))  | 
83  | 344  |     line[--len] = '\0';  | 
84  | 25.4k  |   }  | 
85  | 26.0k  |   for (cp = line; isblank((unsigned char)*cp); cp++)  | 
86  | 416  |       len--;  | 
87  |  |  | 
88  | 26.0k  |   bufsize = (size_t)(total + len + 1);  | 
89  | 26.0k  |   if (*bufp == NULL || bufsize > *bufsizep) { | 
90  | 3.94k  |       const size_t newsize = sudo_pow2_roundup(bufsize);  | 
91  | 3.94k  |       void *newbuf;  | 
92  |  |  | 
93  | 3.94k  |       if (newsize < bufsize) { | 
94  |  |     /* overflow */  | 
95  | 0  |     errno = ENOMEM;  | 
96  | 0  |     sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
97  | 0  |         "unable to allocate memory");  | 
98  | 0  |     len = -1;  | 
99  | 0  |     total = 0;  | 
100  | 0  |     break;  | 
101  | 0  |       }  | 
102  | 3.94k  |       if ((newbuf = realloc(*bufp, newsize)) == NULL) { | 
103  | 0  |     sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
104  | 0  |         "unable to allocate memory");  | 
105  | 0  |     len = -1;  | 
106  | 0  |     total = 0;  | 
107  | 0  |     break;  | 
108  | 0  |       }  | 
109  | 3.94k  |       *bufp = newbuf;  | 
110  | 3.94k  |       *bufsizep = newsize;  | 
111  | 3.94k  |   }  | 
112  | 26.0k  |   memcpy(*bufp + total, cp, (size_t)(len + 1));  | 
113  | 26.0k  |   total += len;  | 
114  | 26.0k  |     } while (continued);  | 
115  | 26.4k  |     free(line);  | 
116  | 26.4k  |     if (len == -1 && total == 0)  | 
117  | 855  |   debug_return_ssize_t(-1);  | 
118  | 25.5k  |     debug_return_ssize_t(total);  | 
119  | 25.5k  | }  | 
120  |  |  | 
121  |  | ssize_t  | 
122  |  | sudo_parseln_v1(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp)  | 
123  | 0  | { | 
124  | 0  |     return sudo_parseln_v2(bufp, bufsizep, lineno, fp, 0);  | 
125  | 0  | }  |