xref: /freebsd/contrib/openpam/lib/libpam/openpam_subst.c (revision 3ba4c8c81a28de3e00ccf6d7f92c6f8e58bef456)
1f7e6344dSDag-Erling Smørgrav /*-
2*3ba4c8c8SDag-Erling Smørgrav  * Copyright (c) 2011-2023 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
9ce77a8d6SDag-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 <security/pam_appl.h>
35f7e6344dSDag-Erling Smørgrav 
36f7e6344dSDag-Erling Smørgrav #include "openpam_impl.h"
37f7e6344dSDag-Erling Smørgrav 
38f7e6344dSDag-Erling Smørgrav #define subst_char(ch) do {			\
39f7e6344dSDag-Erling Smørgrav 	int ch_ = (ch);				\
40f7e6344dSDag-Erling Smørgrav 	if (buf && len < *bufsize)		\
41f7e6344dSDag-Erling Smørgrav 		*buf++ = ch_;			\
42f7e6344dSDag-Erling Smørgrav 	++len;					\
43f7e6344dSDag-Erling Smørgrav } while (0)
44f7e6344dSDag-Erling Smørgrav 
45f7e6344dSDag-Erling Smørgrav #define subst_string(s) do {			\
46f7e6344dSDag-Erling Smørgrav 	const char *s_ = (s);			\
47f7e6344dSDag-Erling Smørgrav 	while (*s_)				\
48f7e6344dSDag-Erling Smørgrav 		subst_char(*s_++);		\
49f7e6344dSDag-Erling Smørgrav } while (0)
50f7e6344dSDag-Erling Smørgrav 
51f7e6344dSDag-Erling Smørgrav #define subst_item(i) do {			\
52f7e6344dSDag-Erling Smørgrav 	int i_ = (i);				\
53f7e6344dSDag-Erling Smørgrav 	const void *p_;				\
54f7e6344dSDag-Erling Smørgrav 	ret = pam_get_item(pamh, i_, &p_);	\
55f7e6344dSDag-Erling Smørgrav 	if (ret == PAM_SUCCESS && p_ != NULL)	\
56f7e6344dSDag-Erling Smørgrav 		subst_string(p_);		\
57f7e6344dSDag-Erling Smørgrav } while (0)
58f7e6344dSDag-Erling Smørgrav 
59f7e6344dSDag-Erling Smørgrav /*
60f7e6344dSDag-Erling Smørgrav  * OpenPAM internal
61f7e6344dSDag-Erling Smørgrav  *
62f7e6344dSDag-Erling Smørgrav  * Substitute PAM item values in a string
63f7e6344dSDag-Erling Smørgrav  */
64f7e6344dSDag-Erling Smørgrav 
65f7e6344dSDag-Erling Smørgrav int
openpam_subst(const pam_handle_t * pamh,char * buf,size_t * bufsize,const char * template)66f7e6344dSDag-Erling Smørgrav openpam_subst(const pam_handle_t *pamh,
67f7e6344dSDag-Erling Smørgrav     char *buf, size_t *bufsize, const char *template)
68f7e6344dSDag-Erling Smørgrav {
69f7e6344dSDag-Erling Smørgrav 	size_t len;
70f7e6344dSDag-Erling Smørgrav 	int ret;
71f7e6344dSDag-Erling Smørgrav 
72f7e6344dSDag-Erling Smørgrav 	ENTERS(template);
73f7e6344dSDag-Erling Smørgrav 	if (template == NULL)
74f7e6344dSDag-Erling Smørgrav 		template = "(null)";
75f7e6344dSDag-Erling Smørgrav 
76f7e6344dSDag-Erling Smørgrav 	len = 1; /* initialize to 1 for terminating NUL */
77f7e6344dSDag-Erling Smørgrav 	ret = PAM_SUCCESS;
78f7e6344dSDag-Erling Smørgrav 	while (*template && ret == PAM_SUCCESS) {
79f7e6344dSDag-Erling Smørgrav 		if (template[0] == '%') {
80f7e6344dSDag-Erling Smørgrav 			++template;
81f7e6344dSDag-Erling Smørgrav 			switch (*template) {
82f7e6344dSDag-Erling Smørgrav 			case 's':
83f7e6344dSDag-Erling Smørgrav 				subst_item(PAM_SERVICE);
84f7e6344dSDag-Erling Smørgrav 				break;
85f7e6344dSDag-Erling Smørgrav 			case 't':
86f7e6344dSDag-Erling Smørgrav 				subst_item(PAM_TTY);
87f7e6344dSDag-Erling Smørgrav 				break;
88f7e6344dSDag-Erling Smørgrav 			case 'h':
89f7e6344dSDag-Erling Smørgrav 				subst_item(PAM_HOST);
90f7e6344dSDag-Erling Smørgrav 				break;
91f7e6344dSDag-Erling Smørgrav 			case 'u':
92f7e6344dSDag-Erling Smørgrav 				subst_item(PAM_USER);
93f7e6344dSDag-Erling Smørgrav 				break;
94f7e6344dSDag-Erling Smørgrav 			case 'H':
95f7e6344dSDag-Erling Smørgrav 				subst_item(PAM_RHOST);
96f7e6344dSDag-Erling Smørgrav 				break;
97f7e6344dSDag-Erling Smørgrav 			case 'U':
98f7e6344dSDag-Erling Smørgrav 				subst_item(PAM_RUSER);
99f7e6344dSDag-Erling Smørgrav 				break;
100f7e6344dSDag-Erling Smørgrav 			case '\0':
101f7e6344dSDag-Erling Smørgrav 				subst_char('%');
102f7e6344dSDag-Erling Smørgrav 				break;
103f7e6344dSDag-Erling Smørgrav 			default:
104f7e6344dSDag-Erling Smørgrav 				subst_char('%');
105f7e6344dSDag-Erling Smørgrav 				subst_char(*template);
106f7e6344dSDag-Erling Smørgrav 			}
107*3ba4c8c8SDag-Erling Smørgrav 			if (*template)
108f7e6344dSDag-Erling Smørgrav 				++template;
109f7e6344dSDag-Erling Smørgrav 		} else {
110f7e6344dSDag-Erling Smørgrav 			subst_char(*template++);
111f7e6344dSDag-Erling Smørgrav 		}
112f7e6344dSDag-Erling Smørgrav 	}
113f7e6344dSDag-Erling Smørgrav 	if (buf)
114f7e6344dSDag-Erling Smørgrav 		*buf = '\0';
115f7e6344dSDag-Erling Smørgrav 	if (ret == PAM_SUCCESS) {
116f7e6344dSDag-Erling Smørgrav 		if (len > *bufsize)
117f7e6344dSDag-Erling Smørgrav 			ret = PAM_TRY_AGAIN;
118f7e6344dSDag-Erling Smørgrav 		*bufsize = len;
119f7e6344dSDag-Erling Smørgrav 	}
120f7e6344dSDag-Erling Smørgrav 	RETURNC(ret);
121f7e6344dSDag-Erling Smørgrav }
122f7e6344dSDag-Erling Smørgrav 
123f7e6344dSDag-Erling Smørgrav /*
124f7e6344dSDag-Erling Smørgrav  * Error codes:
125f7e6344dSDag-Erling Smørgrav  *
126f7e6344dSDag-Erling Smørgrav  *	=pam_get_item
127f7e6344dSDag-Erling Smørgrav  *	!PAM_SYMBOL_ERR
128f7e6344dSDag-Erling Smørgrav  *	PAM_TRY_AGAIN
129f7e6344dSDag-Erling Smørgrav  */
130f7e6344dSDag-Erling Smørgrav 
131f7e6344dSDag-Erling Smørgrav /**
132f7e6344dSDag-Erling Smørgrav  * The =openpam_subst function expands a string, substituting PAM item
133f7e6344dSDag-Erling Smørgrav  * values for all occurrences of specific substitution codes.
134f7e6344dSDag-Erling Smørgrav  * The =template argument points to the initial string.
135f7e6344dSDag-Erling Smørgrav  * The result is stored in the buffer pointed to by the =buf argument; the
136f7e6344dSDag-Erling Smørgrav  * =bufsize argument specifies the size of that buffer.
137f7e6344dSDag-Erling Smørgrav  * The actual size of the resulting string, including the terminating NUL
138f7e6344dSDag-Erling Smørgrav  * character, is stored in the location pointed to by the =bufsize
139f7e6344dSDag-Erling Smørgrav  * argument.
140f7e6344dSDag-Erling Smørgrav  *
141f7e6344dSDag-Erling Smørgrav  * If =buf is NULL, or if the buffer is too small to hold the expanded
142f7e6344dSDag-Erling Smørgrav  * string, =bufsize is updated to reflect the amount of space required to
143f7e6344dSDag-Erling Smørgrav  * hold the entire string, and =openpam_subst returns =PAM_TRY_AGAIN.
144f7e6344dSDag-Erling Smørgrav  *
145f7e6344dSDag-Erling Smørgrav  * If =openpam_subst fails for any other reason, the =bufsize argument is
146f7e6344dSDag-Erling Smørgrav  * untouched, but part of the buffer may still have been overwritten.
147f7e6344dSDag-Erling Smørgrav  *
148f7e6344dSDag-Erling Smørgrav  * Substitution codes are introduced by a percent character and correspond
149f7e6344dSDag-Erling Smørgrav  * to PAM items:
150f7e6344dSDag-Erling Smørgrav  *
151f7e6344dSDag-Erling Smørgrav  *	%H:
152f7e6344dSDag-Erling Smørgrav  *		Replaced by the current value of the =PAM_RHOST item.
153f7e6344dSDag-Erling Smørgrav  *	%h:
154f7e6344dSDag-Erling Smørgrav  *		Replaced by the current value of the =PAM_HOST item.
155f7e6344dSDag-Erling Smørgrav  *	%s:
156f7e6344dSDag-Erling Smørgrav  *		Replaced by the current value of the =PAM_SERVICE item.
157f7e6344dSDag-Erling Smørgrav  *	%t:
158f7e6344dSDag-Erling Smørgrav  *		Replaced by the current value of the =PAM_TTY item.
159f7e6344dSDag-Erling Smørgrav  *	%U:
160f7e6344dSDag-Erling Smørgrav  *		Replaced by the current value of the =PAM_RUSER item.
161f7e6344dSDag-Erling Smørgrav  *	%u:
162f7e6344dSDag-Erling Smørgrav  *		Replaced by the current value of the =PAM_USER item.
163f7e6344dSDag-Erling Smørgrav  *
164f7e6344dSDag-Erling Smørgrav  * >pam_get_authtok
165f7e6344dSDag-Erling Smørgrav  * >pam_get_item
166f7e6344dSDag-Erling Smørgrav  * >pam_get_user
167f7e6344dSDag-Erling Smørgrav  *
168f7e6344dSDag-Erling Smørgrav  * AUTHOR DES
169f7e6344dSDag-Erling Smørgrav  */
170