xref: /freebsd/contrib/openpam/lib/libpam/openpam_readlinev.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * Copyright (c) 2012-2016 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 
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 #include <security/pam_appl.h>
39 
40 #include "openpam_impl.h"
41 
42 #define MIN_WORDV_SIZE	32
43 
44 /*
45  * OpenPAM extension
46  *
47  * Read a line from a file and split it into words.
48  */
49 
50 char **
51 openpam_readlinev(FILE *f, int *lineno, int *lenp)
52 {
53 	char *word, **wordv, **tmp;
54 	size_t wordlen, wordvsize;
55 	int ch, serrno, wordvlen;
56 
57 	wordvsize = MIN_WORDV_SIZE;
58 	wordvlen = 0;
59 	if ((wordv = malloc(wordvsize * sizeof *wordv)) == NULL) {
60 		errno = ENOMEM;
61 		return (NULL);
62 	}
63 	wordv[wordvlen] = NULL;
64 	while ((word = openpam_readword(f, lineno, &wordlen)) != NULL) {
65 		if ((unsigned int)wordvlen + 1 >= wordvsize) {
66 			/* need to expand the array */
67 			wordvsize *= 2;
68 			tmp = realloc(wordv, wordvsize * sizeof *wordv);
69 			if (tmp == NULL) {
70 				errno = ENOMEM;
71 				break;
72 			}
73 			wordv = tmp;
74 		}
75 		/* insert our word */
76 		wordv[wordvlen++] = word;
77 		wordv[wordvlen] = NULL;
78 		word = NULL;
79 	}
80 	if (errno != 0) {
81 		/* I/O error or out of memory */
82 		serrno = errno;
83 		while (wordvlen--)
84 			free(wordv[wordvlen]);
85 		free(wordv);
86 		free(word);
87 		errno = serrno;
88 		return (NULL);
89 	}
90 	/* assert(!ferror(f)) */
91 	ch = fgetc(f);
92 	/* assert(ch == EOF || ch == '\n') */
93 	if (ch == EOF && wordvlen == 0) {
94 		free(wordv);
95 		return (NULL);
96 	}
97 	if (ch == '\n' && lineno != NULL)
98 		++*lineno;
99 	if (lenp != NULL)
100 		*lenp = wordvlen;
101 	return (wordv);
102 }
103 
104 /**
105  * The =openpam_readlinev function reads a line from a file, splits it
106  * into words according to the rules described in the =openpam_readword
107  * manual page, and returns a list of those words.
108  *
109  * If =lineno is not =NULL, the integer variable it points to is
110  * incremented every time a newline character is read.
111  * This includes quoted or escaped newline characters and the newline
112  * character at the end of the line.
113  *
114  * If =lenp is not =NULL, the number of words on the line is stored in the
115  * variable to which it points.
116  *
117  * RETURN VALUES
118  *
119  * If successful, the =openpam_readlinev function returns a pointer to a
120  * dynamically allocated array of pointers to individual dynamically
121  * allocated NUL-terminated strings, each containing a single word, in the
122  * order in which they were encountered on the line.
123  * The array is terminated by a =NULL pointer.
124  *
125  * The caller is responsible for freeing both the array and the individual
126  * strings by passing each of them to =!free.
127  *
128  * If the end of the line was reached before any words were read,
129  * =openpam_readlinev returns a pointer to a dynamically allocated array
130  * containing a single =NULL pointer.
131  *
132  * The =openpam_readlinev function can fail and return =NULL for one of
133  * four reasons:
134  *
135  *  - The end of the file was reached before any words were read; :errno is
136  *    zero, =!ferror returns zero, and =!feof returns a non-zero value.
137  *
138  *  - The end of the file was reached while a quote or backslash escape
139  *    was in effect; :errno is set to =EINVAL, =!ferror returns zero, and
140  *    =!feof returns a non-zero value.
141  *
142  *  - An error occurred while reading from the file; :errno is non-zero,
143  *    =!ferror returns a non-zero value and =!feof returns zero.
144  *
145  *  - A =!malloc or =!realloc call failed; :errno is set to =ENOMEM,
146  *    =!ferror returns a non-zero value, and =!feof may or may not return
147  *    a non-zero value.
148  *
149  * >openpam_readline
150  * >openpam_readword
151  *
152  * AUTHOR DES
153  */
154