xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_readline.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1*0d9d0fd8Schristos /*	$NetBSD: openpam_readline.c,v 1.4 2023/06/30 21:46:20 christos Exp $	*/
2201780c4Schristos 
376e8c542Schristos /*-
476e8c542Schristos  * Copyright (c) 2003 Networks Associates Technology, Inc.
576e8c542Schristos  * Copyright (c) 2004-2011 Dag-Erling Smørgrav
676e8c542Schristos  * All rights reserved.
776e8c542Schristos  *
876e8c542Schristos  * This software was developed for the FreeBSD Project by ThinkSec AS and
976e8c542Schristos  * Network Associates Laboratories, the Security Research Division of
1076e8c542Schristos  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1176e8c542Schristos  * ("CBOSS"), as part of the DARPA CHATS research program.
1276e8c542Schristos  *
1376e8c542Schristos  * Redistribution and use in source and binary forms, with or without
1476e8c542Schristos  * modification, are permitted provided that the following conditions
1576e8c542Schristos  * are met:
1676e8c542Schristos  * 1. Redistributions of source code must retain the above copyright
1776e8c542Schristos  *    notice, this list of conditions and the following disclaimer.
1876e8c542Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1976e8c542Schristos  *    notice, this list of conditions and the following disclaimer in the
2076e8c542Schristos  *    documentation and/or other materials provided with the distribution.
2176e8c542Schristos  * 3. The name of the author may not be used to endorse or promote
2276e8c542Schristos  *    products derived from this software without specific prior written
2376e8c542Schristos  *    permission.
2476e8c542Schristos  *
2576e8c542Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2676e8c542Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2776e8c542Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2876e8c542Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2976e8c542Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3076e8c542Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3176e8c542Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3276e8c542Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3376e8c542Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3476e8c542Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3576e8c542Schristos  * SUCH DAMAGE.
3676e8c542Schristos  */
3776e8c542Schristos 
3876e8c542Schristos #ifdef HAVE_CONFIG_H
3976e8c542Schristos # include "config.h"
4076e8c542Schristos #endif
4176e8c542Schristos 
42201780c4Schristos #include <sys/cdefs.h>
43*0d9d0fd8Schristos __RCSID("$NetBSD: openpam_readline.c,v 1.4 2023/06/30 21:46:20 christos Exp $");
44201780c4Schristos 
4576e8c542Schristos #include <stdio.h>
4676e8c542Schristos #include <stdlib.h>
4776e8c542Schristos 
4876e8c542Schristos #include <security/pam_appl.h>
4976e8c542Schristos 
5076e8c542Schristos #include "openpam_impl.h"
5176e8c542Schristos 
5276e8c542Schristos #define MIN_LINE_LENGTH 128
5376e8c542Schristos 
5476e8c542Schristos /*
5576e8c542Schristos  * OpenPAM extension
5676e8c542Schristos  *
5776e8c542Schristos  * Read a line from a file.
5876e8c542Schristos  */
5976e8c542Schristos 
6076e8c542Schristos char *
openpam_readline(FILE * f,int * lineno,size_t * lenp)6176e8c542Schristos openpam_readline(FILE *f, int *lineno, size_t *lenp)
6276e8c542Schristos {
6376e8c542Schristos 	char *line;
6476e8c542Schristos 	size_t len, size;
6576e8c542Schristos 	int ch;
6676e8c542Schristos 
6776e8c542Schristos 	line = NULL;
6876e8c542Schristos 	if (openpam_straddch(&line, &size, &len, 0) != 0)
6976e8c542Schristos 		return (NULL);
7076e8c542Schristos 	for (;;) {
7176e8c542Schristos 		ch = fgetc(f);
7276e8c542Schristos 		/* strip comment */
7376e8c542Schristos 		if (ch == '#') {
7476e8c542Schristos 			do {
7576e8c542Schristos 				ch = fgetc(f);
7676e8c542Schristos 			} while (ch != EOF && ch != '\n');
7776e8c542Schristos 		}
7876e8c542Schristos 		/* eof */
7976e8c542Schristos 		if (ch == EOF) {
8076e8c542Schristos 			/* done */
8176e8c542Schristos 			break;
8276e8c542Schristos 		}
8376e8c542Schristos 		/* eol */
8476e8c542Schristos 		if (ch == '\n') {
8576e8c542Schristos 			if (lineno != NULL)
8676e8c542Schristos 				++*lineno;
8776e8c542Schristos 			/* skip blank lines */
8876e8c542Schristos 			if (len == 0)
8976e8c542Schristos 				continue;
9076e8c542Schristos 			/* continuation */
9176e8c542Schristos 			if (line[len - 1] == '\\') {
9276e8c542Schristos 				line[--len] = '\0';
9376e8c542Schristos 				continue;
9476e8c542Schristos 			}
9576e8c542Schristos 			/* done */
9676e8c542Schristos 			break;
9776e8c542Schristos 		}
9876e8c542Schristos 		/* anything else */
9976e8c542Schristos 		if (openpam_straddch(&line, &size, &len, ch) != 0)
10076e8c542Schristos 			goto fail;
10176e8c542Schristos 	}
10276e8c542Schristos 	if (len == 0)
10376e8c542Schristos 		goto fail;
10476e8c542Schristos 	if (lenp != NULL)
10576e8c542Schristos 		*lenp = len;
10676e8c542Schristos 	return (line);
10776e8c542Schristos fail:
10876e8c542Schristos 	FREE(line);
10976e8c542Schristos 	return (NULL);
11076e8c542Schristos }
11176e8c542Schristos 
11276e8c542Schristos /**
11376e8c542Schristos  * DEPRECATED openpam_readlinev
11476e8c542Schristos  *
11576e8c542Schristos  * The =openpam_readline function reads a line from a file, and returns it
11676e8c542Schristos  * in a NUL-terminated buffer allocated with =!malloc.
11776e8c542Schristos  *
11876e8c542Schristos  * The =openpam_readline function performs a certain amount of processing
11976e8c542Schristos  * on the data it reads:
12076e8c542Schristos  *
12176e8c542Schristos  *  - Comments (introduced by a hash sign) are stripped.
12276e8c542Schristos  *
12376e8c542Schristos  *  - Blank lines are ignored.
12476e8c542Schristos  *
12576e8c542Schristos  *  - If a line ends in a backslash, the backslash is stripped and the
12676e8c542Schristos  *    next line is appended.
12776e8c542Schristos  *
12876e8c542Schristos  * If =lineno is not =NULL, the integer variable it points to is
12976e8c542Schristos  * incremented every time a newline character is read.
13076e8c542Schristos  *
13176e8c542Schristos  * If =lenp is not =NULL, the length of the line (not including the
13276e8c542Schristos  * terminating NUL character) is stored in the variable it points to.
13376e8c542Schristos  *
13476e8c542Schristos  * The caller is responsible for releasing the returned buffer by passing
13576e8c542Schristos  * it to =!free.
13676e8c542Schristos  *
13776e8c542Schristos  * >openpam_readlinev
13876e8c542Schristos  * >openpam_readword
13976e8c542Schristos  */
140