2 * Copyright (c) 2012 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Id: openpam_readword.c 648 2013-03-05 17:54:27Z des $
40 #include <security/pam_appl.h>
42 #include "openpam_impl.h"
43 #include "openpam_ctype.h"
45 #define MIN_WORD_SIZE 32
50 * Read a word from a file, respecting shell quoting rules.
54 openpam_readword(FILE *f, int *lineno, size_t *lenp)
58 int ch, escape, quote;
63 /* skip initial whitespace */
65 while ((ch = getc(f)) != EOF) {
67 /* either EOL or line continuation */
74 /* escaped something else */
76 } else if (ch == '#') {
77 /* comment: until EOL, no continuation */
78 while ((ch = getc(f)) != EOF)
82 } else if (ch == '\\') {
84 } else if (!is_ws(ch)) {
96 while ((ch = fgetc(f)) != EOF && (!is_ws(ch) || quote || escape)) {
97 if (ch == '\\' && !escape && quote != '\'') {
98 /* escape next character */
100 } else if ((ch == '\'' || ch == '"') && !quote && !escape) {
103 /* edge case: empty quoted string */
104 if (openpam_straddch(&word, &size, &len, 0) != 0)
106 } else if (ch == quote && !escape) {
109 } else if (ch == '\n' && escape) {
110 /* line continuation */
113 if (escape && quote && ch != '\\' && ch != quote &&
114 openpam_straddch(&word, &size, &len, '\\') != 0) {
119 if (openpam_straddch(&word, &size, &len, ch) != 0) {
126 if (lineno != NULL && ch == '\n')
129 if (ch == EOF && ferror(f)) {
135 if (ch == EOF && (escape || quote)) {
136 /* Missing escaped character or closing quote. */
137 openpam_log(PAM_LOG_ERROR, "unexpected end of file");
149 * The =openpam_readword function reads the next word from a file, and
150 * returns it in a NUL-terminated buffer allocated with =!malloc.
152 * A word is a sequence of non-whitespace characters.
153 * However, whitespace characters can be included in a word if quoted or
154 * escaped according to the following rules:
156 * - An unescaped single or double quote introduces a quoted string,
157 * which ends when the same quote character is encountered a second
159 * The quotes themselves are stripped.
161 * - Within a single- or double-quoted string, all whitespace characters,
162 * including the newline character, are preserved as-is.
164 * - Outside a quoted string, a backslash escapes the next character,
165 * which is preserved as-is, unless that character is a newline, in
166 * which case it is discarded and reading continues at the beginning of
167 * the next line as if the backslash and newline had not been there.
168 * In all cases, the backslash itself is discarded.
170 * - Within a single-quoted string, double quotes and backslashes are
173 * - Within a double-quoted string, a single quote is preserved as-is,
174 * and a backslash is preserved as-is unless used to escape a double
177 * In addition, if the first non-whitespace character on the line is a
178 * hash character (#), the rest of the line is discarded.
179 * If a hash character occurs within a word, however, it is preserved
181 * A backslash at the end of a comment does cause line continuation.
183 * If =lineno is not =NULL, the integer variable it points to is
184 * incremented every time a quoted or escaped newline character is read.
186 * If =lenp is not =NULL, the length of the word (after quotes and
187 * backslashes have been removed) is stored in the variable it points to.
191 * If successful, the =openpam_readword function returns a pointer to a
192 * dynamically allocated NUL-terminated string containing the first word
193 * encountered on the line.
195 * The caller is responsible for releasing the returned buffer by passing
198 * If =openpam_readword reaches the end of the line or file before any
199 * characters are copied to the word, it returns =NULL. In the former
200 * case, the newline is pushed back to the file.
202 * If =openpam_readword reaches the end of the file while a quote or
203 * backslash escape is in effect, it sets :errno to =EINVAL and returns
206 * IMPLEMENTATION NOTES
208 * The parsing rules are intended to be equivalent to the normal POSIX
209 * shell quoting rules.
210 * Any discrepancy is a bug and should be reported to the author along
211 * with sample input that can be used to reproduce the error.