2 * Copyright (c) 2011 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: openpam_subst.c 543 2012-03-31 22:11:34Z des $
37 #include <security/pam_appl.h>
39 #include "openpam_impl.h"
41 #define subst_char(ch) do { \
43 if (buf && len < *bufsize) \
48 #define subst_string(s) do { \
49 const char *s_ = (s); \
54 #define subst_item(i) do { \
57 ret = pam_get_item(pamh, i_, &p_); \
58 if (ret == PAM_SUCCESS && p_ != NULL) \
65 * Substitute PAM item values in a string
69 openpam_subst(const pam_handle_t
*pamh
,
70 char *buf
, size_t *bufsize
, const char *template)
79 len
= 1; /* initialize to 1 for terminating NUL */
81 while (*template && ret
== PAM_SUCCESS
) {
82 if (template[0] == '%') {
86 subst_item(PAM_SERVICE
);
98 subst_item(PAM_RHOST
);
101 subst_item(PAM_RUSER
);
108 subst_char(*template);
112 subst_char(*template++);
117 if (ret
== PAM_SUCCESS
) {
134 * The =openpam_subst function expands a string, substituting PAM item
135 * values for all occurrences of specific substitution codes.
136 * The =template argument points to the initial string.
137 * The result is stored in the buffer pointed to by the =buf argument; the
138 * =bufsize argument specifies the size of that buffer.
139 * The actual size of the resulting string, including the terminating NUL
140 * character, is stored in the location pointed to by the =bufsize
143 * If =buf is NULL, or if the buffer is too small to hold the expanded
144 * string, =bufsize is updated to reflect the amount of space required to
145 * hold the entire string, and =openpam_subst returns =PAM_TRY_AGAIN.
147 * If =openpam_subst fails for any other reason, the =bufsize argument is
148 * untouched, but part of the buffer may still have been overwritten.
150 * Substitution codes are introduced by a percent character and correspond
154 * Replaced by the current value of the =PAM_RHOST item.
156 * Replaced by the current value of the =PAM_HOST item.
158 * Replaced by the current value of the =PAM_SERVICE item.
160 * Replaced by the current value of the =PAM_TTY item.
162 * Replaced by the current value of the =PAM_RUSER item.
164 * Replaced by the current value of the =PAM_USER item.