1f7e6344dSDag-Erling Smørgrav /*-
2f7e6344dSDag-Erling Smørgrav * Copyright (c) 2012 Dag-Erling Smørgrav
3f7e6344dSDag-Erling Smørgrav * All rights reserved.
4f7e6344dSDag-Erling Smørgrav *
5f7e6344dSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
6f7e6344dSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
7f7e6344dSDag-Erling Smørgrav * are met:
8f7e6344dSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
9*ce77a8d6SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
10f7e6344dSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
11f7e6344dSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
12f7e6344dSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
13f7e6344dSDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote
14f7e6344dSDag-Erling Smørgrav * products derived from this software without specific prior written
15f7e6344dSDag-Erling Smørgrav * permission.
16f7e6344dSDag-Erling Smørgrav *
17f7e6344dSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18f7e6344dSDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f7e6344dSDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f7e6344dSDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21f7e6344dSDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22f7e6344dSDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23f7e6344dSDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24f7e6344dSDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25f7e6344dSDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26f7e6344dSDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27f7e6344dSDag-Erling Smørgrav * SUCH DAMAGE.
28f7e6344dSDag-Erling Smørgrav */
29f7e6344dSDag-Erling Smørgrav
30f7e6344dSDag-Erling Smørgrav #ifdef HAVE_CONFIG_H
31f7e6344dSDag-Erling Smørgrav # include "config.h"
32f7e6344dSDag-Erling Smørgrav #endif
33f7e6344dSDag-Erling Smørgrav
34f7e6344dSDag-Erling Smørgrav #include <errno.h>
35f7e6344dSDag-Erling Smørgrav #include <stdlib.h>
36f7e6344dSDag-Erling Smørgrav
37f7e6344dSDag-Erling Smørgrav #include <security/pam_appl.h>
38f7e6344dSDag-Erling Smørgrav
39f7e6344dSDag-Erling Smørgrav #include "openpam_impl.h"
40f7e6344dSDag-Erling Smørgrav
41f7e6344dSDag-Erling Smørgrav #define MIN_STR_SIZE 32
42f7e6344dSDag-Erling Smørgrav
43f7e6344dSDag-Erling Smørgrav /*
44f7e6344dSDag-Erling Smørgrav * OpenPAM extension
45f7e6344dSDag-Erling Smørgrav *
46f7e6344dSDag-Erling Smørgrav * Add a character to a string, expanding the buffer if needed.
47f7e6344dSDag-Erling Smørgrav */
48f7e6344dSDag-Erling Smørgrav
49f7e6344dSDag-Erling Smørgrav int
openpam_straddch(char ** str,size_t * size,size_t * len,int ch)50f7e6344dSDag-Erling Smørgrav openpam_straddch(char **str, size_t *size, size_t *len, int ch)
51f7e6344dSDag-Erling Smørgrav {
52f7e6344dSDag-Erling Smørgrav size_t tmpsize;
53f7e6344dSDag-Erling Smørgrav char *tmpstr;
54f7e6344dSDag-Erling Smørgrav
55f7e6344dSDag-Erling Smørgrav if (*str == NULL) {
56f7e6344dSDag-Erling Smørgrav /* initial allocation */
57f7e6344dSDag-Erling Smørgrav tmpsize = MIN_STR_SIZE;
58f7e6344dSDag-Erling Smørgrav if ((tmpstr = malloc(tmpsize)) == NULL) {
59f7e6344dSDag-Erling Smørgrav errno = ENOMEM;
60f7e6344dSDag-Erling Smørgrav return (-1);
61f7e6344dSDag-Erling Smørgrav }
62f7e6344dSDag-Erling Smørgrav *str = tmpstr;
63f7e6344dSDag-Erling Smørgrav *size = tmpsize;
64f7e6344dSDag-Erling Smørgrav *len = 0;
65*ce77a8d6SDag-Erling Smørgrav } else if (ch != 0 && *len + 1 >= *size) {
66f7e6344dSDag-Erling Smørgrav /* additional space required */
67f7e6344dSDag-Erling Smørgrav tmpsize = *size * 2;
68f7e6344dSDag-Erling Smørgrav if ((tmpstr = realloc(*str, tmpsize)) == NULL) {
69f7e6344dSDag-Erling Smørgrav errno = ENOMEM;
70f7e6344dSDag-Erling Smørgrav return (-1);
71f7e6344dSDag-Erling Smørgrav }
72f7e6344dSDag-Erling Smørgrav *size = tmpsize;
73f7e6344dSDag-Erling Smørgrav *str = tmpstr;
74f7e6344dSDag-Erling Smørgrav }
75*ce77a8d6SDag-Erling Smørgrav if (ch != 0) {
76f7e6344dSDag-Erling Smørgrav (*str)[*len] = ch;
77f7e6344dSDag-Erling Smørgrav ++*len;
78*ce77a8d6SDag-Erling Smørgrav }
79f7e6344dSDag-Erling Smørgrav (*str)[*len] = '\0';
80f7e6344dSDag-Erling Smørgrav return (0);
81f7e6344dSDag-Erling Smørgrav }
82f7e6344dSDag-Erling Smørgrav
83f7e6344dSDag-Erling Smørgrav /**
84f7e6344dSDag-Erling Smørgrav * The =openpam_straddch function appends a character to a dynamically
85f7e6344dSDag-Erling Smørgrav * allocated NUL-terminated buffer, reallocating the buffer as needed.
86f7e6344dSDag-Erling Smørgrav *
87f7e6344dSDag-Erling Smørgrav * The =str argument points to a variable containing either a pointer to
88f7e6344dSDag-Erling Smørgrav * an existing buffer or =NULL.
89f7e6344dSDag-Erling Smørgrav * If the value of the variable pointed to by =str is =NULL, a new buffer
90f7e6344dSDag-Erling Smørgrav * is allocated.
91f7e6344dSDag-Erling Smørgrav *
92f7e6344dSDag-Erling Smørgrav * The =size and =len argument point to variables used to hold the size
93f7e6344dSDag-Erling Smørgrav * of the buffer and the length of the string it contains, respectively.
94f7e6344dSDag-Erling Smørgrav *
95*ce77a8d6SDag-Erling Smørgrav * The final argument, =ch, is the character that should be appended to
96*ce77a8d6SDag-Erling Smørgrav * the string. If =ch is 0, nothing is appended, but a new buffer is
97*ce77a8d6SDag-Erling Smørgrav * still allocated if =str is NULL. This can be used to "bootstrap" the
98*ce77a8d6SDag-Erling Smørgrav * string.
99*ce77a8d6SDag-Erling Smørgrav *
100f7e6344dSDag-Erling Smørgrav * If a new buffer is allocated or an existing buffer is reallocated to
101f7e6344dSDag-Erling Smørgrav * make room for the additional character, =str and =size are updated
102f7e6344dSDag-Erling Smørgrav * accordingly.
103f7e6344dSDag-Erling Smørgrav *
104f7e6344dSDag-Erling Smørgrav * The =openpam_straddch function ensures that the buffer is always
105f7e6344dSDag-Erling Smørgrav * NUL-terminated.
106f7e6344dSDag-Erling Smørgrav *
107f7e6344dSDag-Erling Smørgrav * If the =openpam_straddch function is successful, it increments the
108*ce77a8d6SDag-Erling Smørgrav * integer variable pointed to by =len (unless =ch was 0) and returns 0.
109f7e6344dSDag-Erling Smørgrav * Otherwise, it leaves the variables pointed to by =str, =size and =len
110f7e6344dSDag-Erling Smørgrav * unmodified, sets :errno to =ENOMEM and returns -1.
111f7e6344dSDag-Erling Smørgrav *
112f7e6344dSDag-Erling Smørgrav * AUTHOR DES
113f7e6344dSDag-Erling Smørgrav */
114