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
30#ifdef HAVE_CONFIG_H
31# include "config.h"
32#endif
33
34#include <errno.h>
35#include <stdlib.h>
36
37#include <security/pam_appl.h>
38
39#include "openpam_impl.h"
40
41#define MIN_STR_SIZE	32
42
43/*
44 * OpenPAM extension
45 *
46 * Add a character to a string, expanding the buffer if needed.
47 */
48
49int
50openpam_straddch(char **str, size_t *size, size_t *len, int ch)
51{
52	size_t tmpsize;
53	char *tmpstr;
54
55	if (*str == NULL) {
56		/* initial allocation */
57		tmpsize = MIN_STR_SIZE;
58		if ((tmpstr = malloc(tmpsize)) == NULL) {
59			errno = ENOMEM;
60			return (-1);
61		}
62		*str = tmpstr;
63		*size = tmpsize;
64		*len = 0;
65	} else if (ch != 0 && *len + 1 >= *size) {
66		/* additional space required */
67		tmpsize = *size * 2;
68		if ((tmpstr = realloc(*str, tmpsize)) == NULL) {
69			errno = ENOMEM;
70			return (-1);
71		}
72		*size = tmpsize;
73		*str = tmpstr;
74	}
75	if (ch != 0) {
76		(*str)[*len] = ch;
77		++*len;
78	}
79	(*str)[*len] = '\0';
80	return (0);
81}
82
83/**
84 * The =openpam_straddch function appends a character to a dynamically
85 * allocated NUL-terminated buffer, reallocating the buffer as needed.
86 *
87 * The =str argument points to a variable containing either a pointer to
88 * an existing buffer or =NULL.
89 * If the value of the variable pointed to by =str is =NULL, a new buffer
90 * is allocated.
91 *
92 * The =size and =len argument point to variables used to hold the size
93 * of the buffer and the length of the string it contains, respectively.
94 *
95 * The final argument, =ch, is the character that should be appended to
96 * the string.  If =ch is 0, nothing is appended, but a new buffer is
97 * still allocated if =str is NULL.  This can be used to "bootstrap" the
98 * string.
99 *
100 * If a new buffer is allocated or an existing buffer is reallocated to
101 * make room for the additional character, =str and =size are updated
102 * accordingly.
103 *
104 * The =openpam_straddch function ensures that the buffer is always
105 * NUL-terminated.
106 *
107 * If the =openpam_straddch function is successful, it increments the
108 * integer variable pointed to by =len (unless =ch was 0) and returns 0.
109 * Otherwise, it leaves the variables pointed to by =str, =size and =len
110 * unmodified, sets :errno to =ENOMEM and returns -1.
111 *
112 * AUTHOR DES
113 */
114