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 (word == NULL && (word = malloc(1)) == NULL) {
90 openpam_log(PAM_LOG_ERROR, "malloc(): %m");
96 } else if (ch == quote && !escape) {
99 } else if (ch == '\n' && escape && quote != '\'') {
100 /* line continuation */
103 if (escape && quote && ch != '\\' && ch != quote &&
104 openpam_straddch(&word, &size, &len, '\\') != 0) {
109 if (openpam_straddch(&word, &size, &len, ch) != 0) {
116 if (lineno != NULL && ch == '\n')
119 if (ch == EOF && ferror(f)) {
125 if (ch == EOF && (escape || quote)) {
126 /* Missing escaped character or closing quote. */
127 openpam_log(PAM_LOG_ERROR, "unexpected end of file");
139 * The =openpam_readword function reads the next word from a file, and
140 * returns it in a NUL-terminated buffer allocated with =!malloc.
142 * A word is a sequence of non-whitespace characters.
143 * However, whitespace characters can be included in a word if quoted or
144 * escaped according to the following rules:
146 * - An unescaped single or double quote introduces a quoted string,
147 * which ends when the same quote character is encountered a second
149 * The quotes themselves are stripped.
151 * - Within a single- or double-quoted string, all whitespace characters,
152 * including the newline character, are preserved as-is.
154 * - Outside a quoted string, a backslash escapes the next character,
155 * which is preserved as-is, unless that character is a newline, in
156 * which case it is discarded and reading continues at the beginning of
157 * the next line as if the backslash and newline had not been there.
158 * In all cases, the backslash itself is discarded.
160 * - Within a single-quoted string, double quotes and backslashes are
163 * - Within a double-quoted string, a single quote is preserved as-is,
164 * and a backslash is preserved as-is unless used to escape a double
167 * In addition, if the first non-whitespace character on the line is a
168 * hash character (#), the rest of the line is discarded.
169 * If a hash character occurs within a word, however, it is preserved
171 * A backslash at the end of a comment does cause line continuation.
173 * If =lineno is not =NULL, the integer variable it points to is
174 * incremented every time a quoted or escaped newline character is read.
176 * If =lenp is not =NULL, the length of the word (after quotes and
177 * backslashes have been removed) is stored in the variable it points to.
181 * If successful, the =openpam_readword function returns a pointer to a
182 * dynamically allocated NUL-terminated string containing the first word
183 * encountered on the line.
185 * The caller is responsible for releasing the returned buffer by passing
188 * If =openpam_readword reaches the end of the line or file before any
189 * characters are copied to the word, it returns =NULL. In the former
190 * case, the newline is pushed back to the file.
192 * If =openpam_readword reaches the end of the file while a quote or
193 * backslash escape is in effect, it sets :errno to =EINVAL and returns
196 * IMPLEMENTATION NOTES
198 * The parsing rules are intended to be equivalent to the normal POSIX
199 * shell quoting rules.
200 * Any discrepancy is a bug and should be reported to the author along
201 * with sample input that can be used to reproduce the error.