openpam_readlinev.c revision 255369
1260684Skaiw/*-
2260684Skaiw * Copyright (c) 2012 Dag-Erling Sm��rgrav
3260684Skaiw * All rights reserved.
4260684Skaiw *
5260684Skaiw * Redistribution and use in source and binary forms, with or without
6260684Skaiw * modification, are permitted provided that the following conditions
7260684Skaiw * are met:
8260684Skaiw * 1. Redistributions of source code must retain the above copyright
9260684Skaiw *    notice, this list of conditions and the following disclaimer
10260684Skaiw *    in this position and unchanged.
11260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright
12260684Skaiw *    notice, this list of conditions and the following disclaimer in the
13260684Skaiw *    documentation and/or other materials provided with the distribution.
14260684Skaiw * 3. The name of the author may not be used to endorse or promote
15260684Skaiw *    products derived from this software without specific prior written
16260684Skaiw *    permission.
17260684Skaiw *
18260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21260684Skaiw * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25367466Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28367466Sdim * SUCH DAMAGE.
29260684Skaiw *
30260684Skaiw * $Id: openpam_readlinev.c 588 2012-04-08 11:52:25Z des $
31260684Skaiw */
32260684Skaiw
33260684Skaiw#ifdef HAVE_CONFIG_H
34260684Skaiw# include "config.h"
35260684Skaiw#endif
36260684Skaiw
37260684Skaiw#include <errno.h>
38260684Skaiw#include <stdio.h>
39260684Skaiw#include <stdlib.h>
40260684Skaiw
41260684Skaiw#include <security/pam_appl.h>
42260684Skaiw
43260684Skaiw#include "openpam_impl.h"
44260684Skaiw
45260684Skaiw#define MIN_WORDV_SIZE	32
46260684Skaiw
47260684Skaiw/*
48260684Skaiw * OpenPAM extension
49260684Skaiw *
50260684Skaiw * Read a line from a file and split it into words.
51260684Skaiw */
52260684Skaiw
53260684Skaiwchar **
54260684Skaiwopenpam_readlinev(FILE *f, int *lineno, int *lenp)
55260684Skaiw{
56260684Skaiw	char *word, **wordv, **tmp;
57260684Skaiw	size_t wordlen, wordvsize;
58260684Skaiw	int ch, serrno, wordvlen;
59260684Skaiw
60260684Skaiw	wordvsize = MIN_WORDV_SIZE;
61260684Skaiw	wordvlen = 0;
62260684Skaiw	if ((wordv = malloc(wordvsize * sizeof *wordv)) == NULL) {
63260684Skaiw		openpam_log(PAM_LOG_ERROR, "malloc(): %m");
64260684Skaiw		errno = ENOMEM;
65260684Skaiw		return (NULL);
66260684Skaiw	}
67260684Skaiw	wordv[wordvlen] = NULL;
68260684Skaiw	while ((word = openpam_readword(f, lineno, &wordlen)) != NULL) {
69260684Skaiw		if ((unsigned int)wordvlen + 1 >= wordvsize) {
70260684Skaiw			/* need to expand the array */
71260684Skaiw			wordvsize *= 2;
72260684Skaiw			tmp = realloc(wordv, wordvsize * sizeof *wordv);
73260684Skaiw			if (tmp == NULL) {
74260684Skaiw				openpam_log(PAM_LOG_ERROR, "malloc(): %m");
75260684Skaiw				errno = ENOMEM;
76260684Skaiw				break;
77260684Skaiw			}
78260684Skaiw			wordv = tmp;
79260684Skaiw		}
80260684Skaiw		/* insert our word */
81260684Skaiw		wordv[wordvlen++] = word;
82260684Skaiw		wordv[wordvlen] = NULL;
83260684Skaiw	}
84260684Skaiw	if (errno != 0) {
85260684Skaiw		/* I/O error or out of memory */
86260684Skaiw		serrno = errno;
87260684Skaiw		while (wordvlen--)
88260684Skaiw			free(wordv[wordvlen]);
89260684Skaiw		free(wordv);
90260684Skaiw		errno = serrno;
91260684Skaiw		return (NULL);
92260684Skaiw	}
93260684Skaiw	/* assert(!ferror(f)) */
94260684Skaiw	ch = fgetc(f);
95260684Skaiw	/* assert(ch == EOF || ch == '\n') */
96260684Skaiw	if (ch == EOF && wordvlen == 0) {
97260684Skaiw		free(wordv);
98260684Skaiw		return (NULL);
99260684Skaiw	}
100260684Skaiw	if (ch == '\n' && lineno != NULL)
101260684Skaiw		++*lineno;
102260684Skaiw	if (lenp != NULL)
103260684Skaiw		*lenp = wordvlen;
104260684Skaiw	return (wordv);
105260684Skaiw}
106260684Skaiw
107260684Skaiw/**
108260684Skaiw * The =openpam_readlinev function reads a line from a file, splits it
109260684Skaiw * into words according to the rules described in the =openpam_readword
110260684Skaiw * manual page, and returns a list of those words.
111260684Skaiw *
112260684Skaiw * If =lineno is not =NULL, the integer variable it points to is
113260684Skaiw * incremented every time a newline character is read.
114260684Skaiw * This includes quoted or escaped newline characters and the newline
115260684Skaiw * character at the end of the line.
116260684Skaiw *
117260684Skaiw * If =lenp is not =NULL, the number of words on the line is stored in the
118260684Skaiw * variable to which it points.
119260684Skaiw *
120260684Skaiw * RETURN VALUES
121260684Skaiw *
122260684Skaiw * If successful, the =openpam_readlinev function returns a pointer to a
123260684Skaiw * dynamically allocated array of pointers to individual dynamically
124260684Skaiw * allocated NUL-terminated strings, each containing a single word, in the
125 * order in which they were encountered on the line.
126 * The array is terminated by a =NULL pointer.
127 *
128 * The caller is responsible for freeing both the array and the individual
129 * strings by passing each of them to =!free.
130 *
131 * If the end of the line was reached before any words were read,
132 * =openpam_readlinev returns a pointer to a dynamically allocated array
133 * containing a single =NULL pointer.
134 *
135 * The =openpam_readlinev function can fail and return =NULL for one of
136 * four reasons:
137 *
138 *  - The end of the file was reached before any words were read; :errno is
139 *    zero, =!ferror returns zero, and =!feof returns a non-zero value.
140 *
141 *  - The end of the file was reached while a quote or backslash escape
142 *    was in effect; :errno is set to =EINVAL, =!ferror returns zero, and
143 *    =!feof returns a non-zero value.
144 *
145 *  - An error occurred while reading from the file; :errno is non-zero,
146 *    =!ferror returns a non-zero value and =!feof returns zero.
147 *
148 *  - A =!malloc or =!realloc call failed; :errno is set to =ENOMEM,
149 *    =!ferror returns a non-zero value, and =!feof may or may not return
150 *    a non-zero value.
151 *
152 * >openpam_readline
153 * >openpam_readword
154 *
155 * AUTHOR DES
156 */
157