xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_readlinev.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1 /*	$NetBSD: openpam_readlinev.c,v 1.4 2023/06/30 21:46:20 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2012-2016 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: openpam_readlinev.c,v 1.4 2023/06/30 21:46:20 christos Exp $");
38 
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #include <security/pam_appl.h>
44 
45 #include "openpam_impl.h"
46 
47 #define MIN_WORDV_SIZE	32
48 
49 /*
50  * OpenPAM extension
51  *
52  * Read a line from a file and split it into words.
53  */
54 
55 char **
openpam_readlinev(FILE * f,int * lineno,int * lenp)56 openpam_readlinev(FILE *f, int *lineno, int *lenp)
57 {
58 	char *word, **wordv, **tmp;
59 	size_t wordlen, wordvsize;
60 	int ch, serrno, wordvlen;
61 
62 	wordvsize = MIN_WORDV_SIZE;
63 	wordvlen = 0;
64 	if ((wordv = malloc(wordvsize * sizeof *wordv)) == NULL) {
65 		errno = ENOMEM;
66 		return (NULL);
67 	}
68 	wordv[wordvlen] = NULL;
69 	while ((word = openpam_readword(f, lineno, &wordlen)) != NULL) {
70 		if ((unsigned int)wordvlen + 1 >= wordvsize) {
71 			/* need to expand the array */
72 			wordvsize *= 2;
73 			tmp = realloc(wordv, wordvsize * sizeof *wordv);
74 			if (tmp == NULL) {
75 				errno = ENOMEM;
76 				break;
77 			}
78 			wordv = tmp;
79 		}
80 		/* insert our word */
81 		wordv[wordvlen++] = word;
82 		wordv[wordvlen] = NULL;
83 		word = NULL;
84 	}
85 	if (errno != 0) {
86 		/* I/O error or out of memory */
87 		serrno = errno;
88 		while (wordvlen--)
89 			free(wordv[wordvlen]);
90 		free(wordv);
91 		free(word);
92 		errno = serrno;
93 		return (NULL);
94 	}
95 	/* assert(!ferror(f)) */
96 	ch = fgetc(f);
97 	/* assert(ch == EOF || ch == '\n') */
98 	if (ch == EOF && wordvlen == 0) {
99 		free(wordv);
100 		return (NULL);
101 	}
102 	if (ch == '\n' && lineno != NULL)
103 		++*lineno;
104 	if (lenp != NULL)
105 		*lenp = wordvlen;
106 	return (wordv);
107 }
108 
109 /**
110  * The =openpam_readlinev function reads a line from a file, splits it
111  * into words according to the rules described in the =openpam_readword
112  * manual page, and returns a list of those words.
113  *
114  * If =lineno is not =NULL, the integer variable it points to is
115  * incremented every time a newline character is read.
116  * This includes quoted or escaped newline characters and the newline
117  * character at the end of the line.
118  *
119  * If =lenp is not =NULL, the number of words on the line is stored in the
120  * variable to which it points.
121  *
122  * RETURN VALUES
123  *
124  * If successful, the =openpam_readlinev function returns a pointer to a
125  * dynamically allocated array of pointers to individual dynamically
126  * allocated NUL-terminated strings, each containing a single word, in the
127  * order in which they were encountered on the line.
128  * The array is terminated by a =NULL pointer.
129  *
130  * The caller is responsible for freeing both the array and the individual
131  * strings by passing each of them to =!free.
132  *
133  * If the end of the line was reached before any words were read,
134  * =openpam_readlinev returns a pointer to a dynamically allocated array
135  * containing a single =NULL pointer.
136  *
137  * The =openpam_readlinev function can fail and return =NULL for one of
138  * four reasons:
139  *
140  *  - The end of the file was reached before any words were read; :errno is
141  *    zero, =!ferror returns zero, and =!feof returns a non-zero value.
142  *
143  *  - The end of the file was reached while a quote or backslash escape
144  *    was in effect; :errno is set to =EINVAL, =!ferror returns zero, and
145  *    =!feof returns a non-zero value.
146  *
147  *  - An error occurred while reading from the file; :errno is non-zero,
148  *    =!ferror returns a non-zero value and =!feof returns zero.
149  *
150  *  - A =!malloc or =!realloc call failed; :errno is set to =ENOMEM,
151  *    =!ferror returns a non-zero value, and =!feof may or may not return
152  *    a non-zero value.
153  *
154  * >openpam_readline
155  * >openpam_readword
156  *
157  * AUTHOR DES
158  */
159