xref: /freebsd/contrib/openpam/lib/libpam/openpam_set_option.c (revision 3ba4c8c81a28de3e00ccf6d7f92c6f8e58bef456)
1f7e6344dSDag-Erling Smørgrav /*-
2f7e6344dSDag-Erling Smørgrav  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3*3ba4c8c8SDag-Erling Smørgrav  * Copyright (c) 2004-2023 Dag-Erling Smørgrav
4f7e6344dSDag-Erling Smørgrav  * All rights reserved.
5f7e6344dSDag-Erling Smørgrav  *
6f7e6344dSDag-Erling Smørgrav  * This software was developed for the FreeBSD Project by ThinkSec AS and
7f7e6344dSDag-Erling Smørgrav  * Network Associates Laboratories, the Security Research Division of
8f7e6344dSDag-Erling Smørgrav  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
9f7e6344dSDag-Erling Smørgrav  * ("CBOSS"), as part of the DARPA CHATS research program.
10f7e6344dSDag-Erling Smørgrav  *
11f7e6344dSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
12f7e6344dSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
13f7e6344dSDag-Erling Smørgrav  * are met:
14f7e6344dSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
15f7e6344dSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
16f7e6344dSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
17f7e6344dSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
18f7e6344dSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
19f7e6344dSDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote
20f7e6344dSDag-Erling Smørgrav  *    products derived from this software without specific prior written
21f7e6344dSDag-Erling Smørgrav  *    permission.
22f7e6344dSDag-Erling Smørgrav  *
23f7e6344dSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24f7e6344dSDag-Erling Smørgrav  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f7e6344dSDag-Erling Smørgrav  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f7e6344dSDag-Erling Smørgrav  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27f7e6344dSDag-Erling Smørgrav  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f7e6344dSDag-Erling Smørgrav  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f7e6344dSDag-Erling Smørgrav  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f7e6344dSDag-Erling Smørgrav  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f7e6344dSDag-Erling Smørgrav  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f7e6344dSDag-Erling Smørgrav  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f7e6344dSDag-Erling Smørgrav  * SUCH DAMAGE.
34f7e6344dSDag-Erling Smørgrav  */
35f7e6344dSDag-Erling Smørgrav 
36f7e6344dSDag-Erling Smørgrav #ifdef HAVE_CONFIG_H
37f7e6344dSDag-Erling Smørgrav # include "config.h"
38f7e6344dSDag-Erling Smørgrav #endif
39f7e6344dSDag-Erling Smørgrav 
40f7e6344dSDag-Erling Smørgrav #include <sys/param.h>
41f7e6344dSDag-Erling Smørgrav 
42f7e6344dSDag-Erling Smørgrav #include <stdio.h>
43f7e6344dSDag-Erling Smørgrav #include <stdlib.h>
44f7e6344dSDag-Erling Smørgrav #include <string.h>
45f7e6344dSDag-Erling Smørgrav 
46f7e6344dSDag-Erling Smørgrav #include <security/pam_appl.h>
47f7e6344dSDag-Erling Smørgrav 
48f7e6344dSDag-Erling Smørgrav #include "openpam_impl.h"
49ce77a8d6SDag-Erling Smørgrav #include "openpam_asprintf.h"
50f7e6344dSDag-Erling Smørgrav 
51f7e6344dSDag-Erling Smørgrav /*
52f7e6344dSDag-Erling Smørgrav  * OpenPAM extension
53f7e6344dSDag-Erling Smørgrav  *
54f7e6344dSDag-Erling Smørgrav  * Sets the value of a module option
55f7e6344dSDag-Erling Smørgrav  */
56f7e6344dSDag-Erling Smørgrav 
57f7e6344dSDag-Erling Smørgrav int
openpam_set_option(pam_handle_t * pamh,const char * option,const char * value)58f7e6344dSDag-Erling Smørgrav openpam_set_option(pam_handle_t *pamh,
59f7e6344dSDag-Erling Smørgrav 	const char *option,
60f7e6344dSDag-Erling Smørgrav 	const char *value)
61f7e6344dSDag-Erling Smørgrav {
62f7e6344dSDag-Erling Smørgrav 	pam_chain_t *cur;
63f7e6344dSDag-Erling Smørgrav 	char *opt, **optv;
64f7e6344dSDag-Erling Smørgrav 	size_t len;
65f7e6344dSDag-Erling Smørgrav 	int i;
66f7e6344dSDag-Erling Smørgrav 
67f7e6344dSDag-Erling Smørgrav 	ENTERS(option);
68f7e6344dSDag-Erling Smørgrav 	if (pamh == NULL || pamh->current == NULL || option == NULL)
69f7e6344dSDag-Erling Smørgrav 		RETURNC(PAM_SYSTEM_ERR);
70f7e6344dSDag-Erling Smørgrav 	cur = pamh->current;
71f7e6344dSDag-Erling Smørgrav 	for (len = 0; option[len] != '\0'; ++len)
72f7e6344dSDag-Erling Smørgrav 		if (option[len] == '=')
73f7e6344dSDag-Erling Smørgrav 			break;
74f7e6344dSDag-Erling Smørgrav 	for (i = 0; i < cur->optc; ++i) {
75f7e6344dSDag-Erling Smørgrav 		if (strncmp(cur->optv[i], option, len) == 0 &&
76f7e6344dSDag-Erling Smørgrav 		    (cur->optv[i][len] == '\0' || cur->optv[i][len] == '='))
77f7e6344dSDag-Erling Smørgrav 			break;
78f7e6344dSDag-Erling Smørgrav 	}
79f7e6344dSDag-Erling Smørgrav 	if (value == NULL) {
80f7e6344dSDag-Erling Smørgrav 		/* remove */
81f7e6344dSDag-Erling Smørgrav 		if (i == cur->optc)
82f7e6344dSDag-Erling Smørgrav 			RETURNC(PAM_SUCCESS);
83f7e6344dSDag-Erling Smørgrav 		for (free(cur->optv[i]); i < cur->optc; ++i)
84f7e6344dSDag-Erling Smørgrav 			cur->optv[i] = cur->optv[i + 1];
85f7e6344dSDag-Erling Smørgrav 		cur->optv[i] = NULL;
86*3ba4c8c8SDag-Erling Smørgrav 		--cur->optc;
87f7e6344dSDag-Erling Smørgrav 		RETURNC(PAM_SUCCESS);
88f7e6344dSDag-Erling Smørgrav 	}
89f7e6344dSDag-Erling Smørgrav 	if (asprintf(&opt, "%.*s=%s", (int)len, option, value) < 0)
90f7e6344dSDag-Erling Smørgrav 		RETURNC(PAM_BUF_ERR);
91f7e6344dSDag-Erling Smørgrav 	if (i == cur->optc) {
92f7e6344dSDag-Erling Smørgrav 		/* add */
93f7e6344dSDag-Erling Smørgrav 		optv = realloc(cur->optv, sizeof(char *) * (cur->optc + 2));
94f7e6344dSDag-Erling Smørgrav 		if (optv == NULL) {
95f7e6344dSDag-Erling Smørgrav 			FREE(opt);
96f7e6344dSDag-Erling Smørgrav 			RETURNC(PAM_BUF_ERR);
97f7e6344dSDag-Erling Smørgrav 		}
98f7e6344dSDag-Erling Smørgrav 		optv[i] = opt;
99f7e6344dSDag-Erling Smørgrav 		optv[i + 1] = NULL;
100f7e6344dSDag-Erling Smørgrav 		cur->optv = optv;
101f7e6344dSDag-Erling Smørgrav 		++cur->optc;
102f7e6344dSDag-Erling Smørgrav 	} else {
103f7e6344dSDag-Erling Smørgrav 		/* replace */
104f7e6344dSDag-Erling Smørgrav 		FREE(cur->optv[i]);
105f7e6344dSDag-Erling Smørgrav 		cur->optv[i] = opt;
106f7e6344dSDag-Erling Smørgrav 	}
107f7e6344dSDag-Erling Smørgrav 	RETURNC(PAM_SUCCESS);
108f7e6344dSDag-Erling Smørgrav }
109f7e6344dSDag-Erling Smørgrav 
110f7e6344dSDag-Erling Smørgrav /*
111f7e6344dSDag-Erling Smørgrav  * Error codes:
112f7e6344dSDag-Erling Smørgrav  *
113f7e6344dSDag-Erling Smørgrav  *	PAM_SYSTEM_ERR
114f7e6344dSDag-Erling Smørgrav  *	PAM_BUF_ERR
115f7e6344dSDag-Erling Smørgrav  */
116f7e6344dSDag-Erling Smørgrav 
117f7e6344dSDag-Erling Smørgrav /**
118f7e6344dSDag-Erling Smørgrav  * The =openpam_set_option function sets the specified option in the
119f7e6344dSDag-Erling Smørgrav  * context of the currently executing service module.
120f7e6344dSDag-Erling Smørgrav  *
121f7e6344dSDag-Erling Smørgrav  * >openpam_get_option
122f7e6344dSDag-Erling Smørgrav  */
123