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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: openpam_readword.c 588 2012-04-08 11:52:25Z des $
41 #include <security/pam_appl.h>
43 #include "openpam_impl.h"
44 #include "openpam_ctype.h"
46 #define MIN_WORD_SIZE 32
51 * Read a word from a file, respecting shell quoting rules.
55 openpam_readword(FILE *f, int *lineno, size_t *lenp)
59 int ch, comment, escape, quote;
64 /* skip initial whitespace */
66 while ((ch = getc(f)) != EOF && ch != '\n') {
69 if (!is_lws(ch) && !comment)
81 while ((ch = fgetc(f)) != EOF && (!is_ws(ch) || quote || escape)) {
82 if (ch == '\\' && !escape && quote != '\'') {
83 /* escape next character */
85 } else if ((ch == '\'' || ch == '"') && !quote && !escape) {
88 /* edge case: empty quoted string */
89 if (openpam_straddch(&word, &size, &len, 0) != 0)
91 } else if (ch == quote && !escape) {
94 } else if (ch == '\n' && escape && quote != '\'') {
95 /* line continuation */
98 if (escape && quote && ch != '\\' && ch != quote &&
99 openpam_straddch(&word, &size, &len, '\\') != 0) {
104 if (openpam_straddch(&word, &size, &len, ch) != 0) {
111 if (lineno != NULL && ch == '\n')
114 if (ch == EOF && ferror(f)) {
120 if (ch == EOF && (escape || quote)) {
121 /* Missing escaped character or closing quote. */
122 openpam_log(PAM_LOG_ERROR, "unexpected end of file");
134 * The =openpam_readword function reads the next word from a file, and
135 * returns it in a NUL-terminated buffer allocated with =!malloc.
137 * A word is a sequence of non-whitespace characters.
138 * However, whitespace characters can be included in a word if quoted or
139 * escaped according to the following rules:
141 * - An unescaped single or double quote introduces a quoted string,
142 * which ends when the same quote character is encountered a second
144 * The quotes themselves are stripped.
146 * - Within a single- or double-quoted string, all whitespace characters,
147 * including the newline character, are preserved as-is.
149 * - Outside a quoted string, a backslash escapes the next character,
150 * which is preserved as-is, unless that character is a newline, in
151 * which case it is discarded and reading continues at the beginning of
152 * the next line as if the backslash and newline had not been there.
153 * In all cases, the backslash itself is discarded.
155 * - Within a single-quoted string, double quotes and backslashes are
158 * - Within a double-quoted string, a single quote is preserved as-is,
159 * and a backslash is preserved as-is unless used to escape a double
162 * In addition, if the first non-whitespace character on the line is a
163 * hash character (#), the rest of the line is discarded.
164 * If a hash character occurs within a word, however, it is preserved
166 * A backslash at the end of a comment does cause line continuation.
168 * If =lineno is not =NULL, the integer variable it points to is
169 * incremented every time a quoted or escaped newline character is read.
171 * If =lenp is not =NULL, the length of the word (after quotes and
172 * backslashes have been removed) is stored in the variable it points to.
176 * If successful, the =openpam_readword function returns a pointer to a
177 * dynamically allocated NUL-terminated string containing the first word
178 * encountered on the line.
180 * The caller is responsible for releasing the returned buffer by passing
183 * If =openpam_readword reaches the end of the line or file before any
184 * characters are copied to the word, it returns =NULL. In the former
185 * case, the newline is pushed back to the file.
187 * If =openpam_readword reaches the end of the file while a quote or
188 * backslash escape is in effect, it sets :errno to =EINVAL and returns
191 * IMPLEMENTATION NOTES
193 * The parsing rules are intended to be equivalent to the normal POSIX
194 * shell quoting rules.
195 * Any discrepancy is a bug and should be reported to the author along
196 * with sample input that can be used to reproduce the error.