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, comment, escape, quote;
63 /* skip initial whitespace */
65 while ((ch = getc(f)) != EOF && ch != '\n') {
68 if (!is_lws(ch) && !comment)
80 while ((ch = fgetc(f)) != EOF && (!is_ws(ch) || quote || escape)) {
81 if (ch == '\\' && !escape && quote != '\'') {
82 /* escape next character */
84 } else if ((ch == '\'' || ch == '"') && !quote && !escape) {
87 /* edge case: empty quoted string */
88 if (openpam_straddch(&word, &size, &len, 0) != 0)
90 } else if (ch == quote && !escape) {
93 } else if (ch == '\n' && escape && quote != '\'') {
94 /* line continuation */
97 if (escape && quote && ch != '\\' && ch != quote &&
98 openpam_straddch(&word, &size, &len, '\\') != 0) {
103 if (openpam_straddch(&word, &size, &len, ch) != 0) {
110 if (lineno != NULL && ch == '\n')
113 if (ch == EOF && ferror(f)) {
119 if (ch == EOF && (escape || quote)) {
120 /* Missing escaped character or closing quote. */
121 openpam_log(PAM_LOG_ERROR, "unexpected end of file");
133 * The =openpam_readword function reads the next word from a file, and
134 * returns it in a NUL-terminated buffer allocated with =!malloc.
136 * A word is a sequence of non-whitespace characters.
137 * However, whitespace characters can be included in a word if quoted or
138 * escaped according to the following rules:
140 * - An unescaped single or double quote introduces a quoted string,
141 * which ends when the same quote character is encountered a second
143 * The quotes themselves are stripped.
145 * - Within a single- or double-quoted string, all whitespace characters,
146 * including the newline character, are preserved as-is.
148 * - Outside a quoted string, a backslash escapes the next character,
149 * which is preserved as-is, unless that character is a newline, in
150 * which case it is discarded and reading continues at the beginning of
151 * the next line as if the backslash and newline had not been there.
152 * In all cases, the backslash itself is discarded.
154 * - Within a single-quoted string, double quotes and backslashes are
157 * - Within a double-quoted string, a single quote is preserved as-is,
158 * and a backslash is preserved as-is unless used to escape a double
161 * In addition, if the first non-whitespace character on the line is a
162 * hash character (#), the rest of the line is discarded.
163 * If a hash character occurs within a word, however, it is preserved
165 * A backslash at the end of a comment does cause line continuation.
167 * If =lineno is not =NULL, the integer variable it points to is
168 * incremented every time a quoted or escaped newline character is read.
170 * If =lenp is not =NULL, the length of the word (after quotes and
171 * backslashes have been removed) is stored in the variable it points to.
175 * If successful, the =openpam_readword function returns a pointer to a
176 * dynamically allocated NUL-terminated string containing the first word
177 * encountered on the line.
179 * The caller is responsible for releasing the returned buffer by passing
182 * If =openpam_readword reaches the end of the line or file before any
183 * characters are copied to the word, it returns =NULL. In the former
184 * case, the newline is pushed back to the file.
186 * If =openpam_readword reaches the end of the file while a quote or
187 * backslash escape is in effect, it sets :errno to =EINVAL and returns
190 * IMPLEMENTATION NOTES
192 * The parsing rules are intended to be equivalent to the normal POSIX
193 * shell quoting rules.
194 * Any discrepancy is a bug and should be reported to the author along
195 * with sample input that can be used to reproduce the error.