1 /* $NetBSD: openpam_readlinev.c,v 1.2 2014/10/24 18:17:56 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 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 * Id: openpam_readlinev.c 648 2013-03-05 17:54:27Z 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.2 2014/10/24 18:17:56 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 } 88 if (errno != 0) { 89 /* I/O error or out of memory */ 90 serrno = errno; 91 while (wordvlen--) 92 free(wordv[wordvlen]); 93 free(wordv); 94 errno = serrno; 95 return (NULL); 96 } 97 /* assert(!ferror(f)) */ 98 ch = fgetc(f); 99 /* assert(ch == EOF || ch == '\n') */ 100 if (ch == EOF && wordvlen == 0) { 101 free(wordv); 102 return (NULL); 103 } 104 if (ch == '\n' && lineno != NULL) 105 ++*lineno; 106 if (lenp != NULL) 107 *lenp = wordvlen; 108 return (wordv); 109 } 110 111 /** 112 * The =openpam_readlinev function reads a line from a file, splits it 113 * into words according to the rules described in the =openpam_readword 114 * manual page, and returns a list of those words. 115 * 116 * If =lineno is not =NULL, the integer variable it points to is 117 * incremented every time a newline character is read. 118 * This includes quoted or escaped newline characters and the newline 119 * character at the end of the line. 120 * 121 * If =lenp is not =NULL, the number of words on the line is stored in the 122 * variable to which it points. 123 * 124 * RETURN VALUES 125 * 126 * If successful, the =openpam_readlinev function returns a pointer to a 127 * dynamically allocated array of pointers to individual dynamically 128 * allocated NUL-terminated strings, each containing a single word, in the 129 * order in which they were encountered on the line. 130 * The array is terminated by a =NULL pointer. 131 * 132 * The caller is responsible for freeing both the array and the individual 133 * strings by passing each of them to =!free. 134 * 135 * If the end of the line was reached before any words were read, 136 * =openpam_readlinev returns a pointer to a dynamically allocated array 137 * containing a single =NULL pointer. 138 * 139 * The =openpam_readlinev function can fail and return =NULL for one of 140 * four reasons: 141 * 142 * - The end of the file was reached before any words were read; :errno is 143 * zero, =!ferror returns zero, and =!feof returns a non-zero value. 144 * 145 * - The end of the file was reached while a quote or backslash escape 146 * was in effect; :errno is set to =EINVAL, =!ferror returns zero, and 147 * =!feof returns a non-zero value. 148 * 149 * - An error occurred while reading from the file; :errno is non-zero, 150 * =!ferror returns a non-zero value and =!feof returns zero. 151 * 152 * - A =!malloc or =!realloc call failed; :errno is set to =ENOMEM, 153 * =!ferror returns a non-zero value, and =!feof may or may not return 154 * a non-zero value. 155 * 156 * >openpam_readline 157 * >openpam_readword 158 * 159 * AUTHOR DES 160 */ 161