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