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