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