1/*-
2 * Copyright (c) 2012 Dag-Erling Smørgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
15 *    permission.
16 *
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
27 * SUCH DAMAGE.
28 *
29 * $Id: openpam_readword.c 648 2013-03-05 17:54:27Z des $
30 */
31
32#ifdef HAVE_CONFIG_H
33# include "config.h"
34#endif
35
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39
40#include <security/pam_appl.h>
41
42#include "openpam_impl.h"
43#include "openpam_ctype.h"
44
45#define MIN_WORD_SIZE	32
46
47/*
48 * OpenPAM extension
49 *
50 * Read a word from a file, respecting shell quoting rules.
51 */
52
53char *
54openpam_readword(FILE *f, int *lineno, size_t *lenp)
55{
56	char *word;
57	size_t size, len;
58	int ch, comment, escape, quote;
59	int serrno;
60
61	errno = 0;
62
63	/* skip initial whitespace */
64	comment = 0;
65	while ((ch = getc(f)) != EOF && ch != '\n') {
66		if (ch == '#')
67			comment = 1;
68		if (!is_lws(ch) && !comment)
69			break;
70	}
71	if (ch == EOF)
72		return (NULL);
73	ungetc(ch, f);
74	if (ch == '\n')
75		return (NULL);
76
77	word = NULL;
78	size = len = 0;
79	escape = quote = 0;
80	while ((ch = fgetc(f)) != EOF && (!is_ws(ch) || quote || escape)) {
81		if (ch == '\\' && !escape && quote != '\'') {
82			/* escape next character */
83			escape = ch;
84		} else if ((ch == '\'' || ch == '"') && !quote && !escape) {
85			/* begin quote */
86			quote = ch;
87			/* edge case: empty quoted string */
88			if (openpam_straddch(&word, &size, &len, 0) != 0)
89				return (NULL);
90		} else if (ch == quote && !escape) {
91			/* end quote */
92			quote = 0;
93		} else if (ch == '\n' && escape && quote != '\'') {
94			/* line continuation */
95			escape = 0;
96		} else {
97			if (escape && quote && ch != '\\' && ch != quote &&
98			    openpam_straddch(&word, &size, &len, '\\') != 0) {
99				free(word);
100				errno = ENOMEM;
101				return (NULL);
102			}
103			if (openpam_straddch(&word, &size, &len, ch) != 0) {
104				free(word);
105				errno = ENOMEM;
106				return (NULL);
107			}
108			escape = 0;
109		}
110		if (lineno != NULL && ch == '\n')
111			++*lineno;
112	}
113	if (ch == EOF && ferror(f)) {
114		serrno = errno;
115		free(word);
116		errno = serrno;
117		return (NULL);
118	}
119	if (ch == EOF && (escape || quote)) {
120		/* Missing escaped character or closing quote. */
121		openpam_log(PAM_LOG_ERROR, "unexpected end of file");
122		free(word);
123		errno = EINVAL;
124		return (NULL);
125	}
126	ungetc(ch, f);
127	if (lenp != NULL)
128		*lenp = len;
129	return (word);
130}
131
132/**
133 * The =openpam_readword function reads the next word from a file, and
134 * returns it in a NUL-terminated buffer allocated with =!malloc.
135 *
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:
139 *
140 *  - An unescaped single or double quote introduces a quoted string,
141 *    which ends when the same quote character is encountered a second
142 *    time.
143 *    The quotes themselves are stripped.
144 *
145 *  - Within a single- or double-quoted string, all whitespace characters,
146 *    including the newline character, are preserved as-is.
147 *
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.
153 *
154 *  - Within a single-quoted string, double quotes and backslashes are
155 *    preserved as-is.
156 *
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
159 *    quote.
160 *
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
164 * as-is.
165 * A backslash at the end of a comment does cause line continuation.
166 *
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.
169 *
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.
172 *
173 * RETURN VALUES
174 *
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.
178 *
179 * The caller is responsible for releasing the returned buffer by passing
180 * it to =!free.
181 *
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.
185 *
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
188 * =NULL.
189 *
190 * IMPLEMENTATION NOTES
191 *
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.
196 *
197 * >openpam_readline
198 * >openpam_readlinev
199 *
200 * AUTHOR DES
201 */
202