xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_straddch.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1*0d9d0fd8Schristos /*	$NetBSD: openpam_straddch.c,v 1.4 2023/06/30 21:46:20 christos Exp $	*/
2201780c4Schristos 
376e8c542Schristos /*-
476e8c542Schristos  * Copyright (c) 2012 Dag-Erling Smørgrav
576e8c542Schristos  * All rights reserved.
676e8c542Schristos  *
776e8c542Schristos  * Redistribution and use in source and binary forms, with or without
876e8c542Schristos  * modification, are permitted provided that the following conditions
976e8c542Schristos  * are met:
1076e8c542Schristos  * 1. Redistributions of source code must retain the above copyright
1176e8c542Schristos  *    notice, this list of conditions and the following disclaimer.
1276e8c542Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1376e8c542Schristos  *    notice, this list of conditions and the following disclaimer in the
1476e8c542Schristos  *    documentation and/or other materials provided with the distribution.
1576e8c542Schristos  * 3. The name of the author may not be used to endorse or promote
1676e8c542Schristos  *    products derived from this software without specific prior written
1776e8c542Schristos  *    permission.
1876e8c542Schristos  *
1976e8c542Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2076e8c542Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2176e8c542Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2276e8c542Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2376e8c542Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2476e8c542Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2576e8c542Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2676e8c542Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2776e8c542Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2876e8c542Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2976e8c542Schristos  * SUCH DAMAGE.
3076e8c542Schristos  */
3176e8c542Schristos 
3276e8c542Schristos #ifdef HAVE_CONFIG_H
3376e8c542Schristos # include "config.h"
3476e8c542Schristos #endif
3576e8c542Schristos 
36201780c4Schristos #include <sys/cdefs.h>
37*0d9d0fd8Schristos __RCSID("$NetBSD: openpam_straddch.c,v 1.4 2023/06/30 21:46:20 christos Exp $");
38201780c4Schristos 
3976e8c542Schristos #include <errno.h>
4076e8c542Schristos #include <stdlib.h>
4176e8c542Schristos 
4276e8c542Schristos #include <security/pam_appl.h>
4376e8c542Schristos 
4476e8c542Schristos #include "openpam_impl.h"
4576e8c542Schristos 
4676e8c542Schristos #define MIN_STR_SIZE	32
4776e8c542Schristos 
4876e8c542Schristos /*
4976e8c542Schristos  * OpenPAM extension
5076e8c542Schristos  *
5176e8c542Schristos  * Add a character to a string, expanding the buffer if needed.
5276e8c542Schristos  */
5376e8c542Schristos 
5476e8c542Schristos int
openpam_straddch(char ** str,size_t * size,size_t * len,int ch)5576e8c542Schristos openpam_straddch(char **str, size_t *size, size_t *len, int ch)
5676e8c542Schristos {
5776e8c542Schristos 	size_t tmpsize;
5876e8c542Schristos 	char *tmpstr;
5976e8c542Schristos 
6076e8c542Schristos 	if (*str == NULL) {
6176e8c542Schristos 		/* initial allocation */
6276e8c542Schristos 		tmpsize = MIN_STR_SIZE;
6376e8c542Schristos 		if ((tmpstr = malloc(tmpsize)) == NULL) {
6476e8c542Schristos 			errno = ENOMEM;
6576e8c542Schristos 			return (-1);
6676e8c542Schristos 		}
6776e8c542Schristos 		*str = tmpstr;
6876e8c542Schristos 		*size = tmpsize;
6976e8c542Schristos 		*len = 0;
7076e8c542Schristos 	} else if (ch != 0 && *len + 1 >= *size) {
7176e8c542Schristos 		/* additional space required */
7276e8c542Schristos 		tmpsize = *size * 2;
7376e8c542Schristos 		if ((tmpstr = realloc(*str, tmpsize)) == NULL) {
7476e8c542Schristos 			errno = ENOMEM;
7576e8c542Schristos 			return (-1);
7676e8c542Schristos 		}
7776e8c542Schristos 		*size = tmpsize;
7876e8c542Schristos 		*str = tmpstr;
7976e8c542Schristos 	}
8076e8c542Schristos 	if (ch != 0) {
81201780c4Schristos 		(*str)[*len] = (char)ch;
8276e8c542Schristos 		++*len;
8376e8c542Schristos 	}
8476e8c542Schristos 	(*str)[*len] = '\0';
8576e8c542Schristos 	return (0);
8676e8c542Schristos }
8776e8c542Schristos 
8876e8c542Schristos /**
8976e8c542Schristos  * The =openpam_straddch function appends a character to a dynamically
9076e8c542Schristos  * allocated NUL-terminated buffer, reallocating the buffer as needed.
9176e8c542Schristos  *
9276e8c542Schristos  * The =str argument points to a variable containing either a pointer to
9376e8c542Schristos  * an existing buffer or =NULL.
9476e8c542Schristos  * If the value of the variable pointed to by =str is =NULL, a new buffer
9576e8c542Schristos  * is allocated.
9676e8c542Schristos  *
9776e8c542Schristos  * The =size and =len argument point to variables used to hold the size
9876e8c542Schristos  * of the buffer and the length of the string it contains, respectively.
9976e8c542Schristos  *
10076e8c542Schristos  * The final argument, =ch, is the character that should be appended to
10176e8c542Schristos  * the string.  If =ch is 0, nothing is appended, but a new buffer is
10276e8c542Schristos  * still allocated if =str is NULL.  This can be used to "bootstrap" the
10376e8c542Schristos  * string.
10476e8c542Schristos  *
10576e8c542Schristos  * If a new buffer is allocated or an existing buffer is reallocated to
10676e8c542Schristos  * make room for the additional character, =str and =size are updated
10776e8c542Schristos  * accordingly.
10876e8c542Schristos  *
10976e8c542Schristos  * The =openpam_straddch function ensures that the buffer is always
11076e8c542Schristos  * NUL-terminated.
11176e8c542Schristos  *
11276e8c542Schristos  * If the =openpam_straddch function is successful, it increments the
11376e8c542Schristos  * integer variable pointed to by =len (unless =ch was 0) and returns 0.
11476e8c542Schristos  * Otherwise, it leaves the variables pointed to by =str, =size and =len
11576e8c542Schristos  * unmodified, sets :errno to =ENOMEM and returns -1.
11676e8c542Schristos  *
11776e8c542Schristos  * AUTHOR DES
11876e8c542Schristos  */
119