xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/pam_start.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1*0d9d0fd8Schristos /*	$NetBSD: pam_start.c,v 1.4 2023/06/30 21:46:21 christos Exp $	*/
2201780c4Schristos 
376e8c542Schristos /*-
476e8c542Schristos  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
576e8c542Schristos  * Copyright (c) 2004-2011 Dag-Erling Smørgrav
676e8c542Schristos  * All rights reserved.
776e8c542Schristos  *
876e8c542Schristos  * This software was developed for the FreeBSD Project by ThinkSec AS and
976e8c542Schristos  * Network Associates Laboratories, the Security Research Division of
1076e8c542Schristos  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1176e8c542Schristos  * ("CBOSS"), as part of the DARPA CHATS research program.
1276e8c542Schristos  *
1376e8c542Schristos  * Redistribution and use in source and binary forms, with or without
1476e8c542Schristos  * modification, are permitted provided that the following conditions
1576e8c542Schristos  * are met:
1676e8c542Schristos  * 1. Redistributions of source code must retain the above copyright
1776e8c542Schristos  *    notice, this list of conditions and the following disclaimer.
1876e8c542Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1976e8c542Schristos  *    notice, this list of conditions and the following disclaimer in the
2076e8c542Schristos  *    documentation and/or other materials provided with the distribution.
2176e8c542Schristos  * 3. The name of the author may not be used to endorse or promote
2276e8c542Schristos  *    products derived from this software without specific prior written
2376e8c542Schristos  *    permission.
2476e8c542Schristos  *
2576e8c542Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2676e8c542Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2776e8c542Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2876e8c542Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2976e8c542Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3076e8c542Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3176e8c542Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3276e8c542Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3376e8c542Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3476e8c542Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3576e8c542Schristos  * SUCH DAMAGE.
3676e8c542Schristos  */
3776e8c542Schristos 
3876e8c542Schristos #ifdef HAVE_CONFIG_H
3976e8c542Schristos # include "config.h"
4076e8c542Schristos #endif
4176e8c542Schristos 
42201780c4Schristos #include <sys/cdefs.h>
43*0d9d0fd8Schristos __RCSID("$NetBSD: pam_start.c,v 1.4 2023/06/30 21:46:21 christos Exp $");
44201780c4Schristos 
4576e8c542Schristos #include <stdlib.h>
4676e8c542Schristos #include <string.h>
4776e8c542Schristos #include <unistd.h>
4876e8c542Schristos 
4976e8c542Schristos #include <security/pam_appl.h>
5076e8c542Schristos 
5176e8c542Schristos #include "openpam_impl.h"
5276e8c542Schristos #include "openpam_strlcpy.h"
5376e8c542Schristos 
5476e8c542Schristos 
5576e8c542Schristos /*
5676e8c542Schristos  * XSSO 4.2.1
5776e8c542Schristos  * XSSO 6 page 89
5876e8c542Schristos  *
5976e8c542Schristos  * Initiate a PAM transaction
6076e8c542Schristos  */
6176e8c542Schristos 
6276e8c542Schristos int
pam_start(const char * service,const char * user,const struct pam_conv * pam_conv,pam_handle_t ** pamh)6376e8c542Schristos pam_start(const char *service,
6476e8c542Schristos 	const char *user,
6576e8c542Schristos 	const struct pam_conv *pam_conv,
6676e8c542Schristos 	pam_handle_t **pamh)
6776e8c542Schristos {
68201780c4Schristos 	char *hostname = NULL;
6976e8c542Schristos 	struct pam_handle *ph;
7076e8c542Schristos 	int r;
71201780c4Schristos 	size_t hostname_size;
72201780c4Schristos 	long h;
73201780c4Schristos 
74201780c4Schristos #ifdef _SC_HOST_NAME_MAX
75201780c4Schristos 	h = sysconf(_SC_HOST_NAME_MAX);
76201780c4Schristos #else
77201780c4Schristos 	h = -1;
78201780c4Schristos #endif
79201780c4Schristos 	hostname_size = (size_t)(h < 10 ? 1024 : h) + 1;
8076e8c542Schristos 
8176e8c542Schristos 	ENTER();
82201780c4Schristos 	if ((ph = calloc((size_t)1, sizeof *ph)) == NULL)
8376e8c542Schristos 		RETURNC(PAM_BUF_ERR);
8476e8c542Schristos 	if ((r = pam_set_item(ph, PAM_SERVICE, service)) != PAM_SUCCESS)
8576e8c542Schristos 		goto fail;
86201780c4Schristos 	if ((hostname = malloc(hostname_size)) == NULL)
87201780c4Schristos 		goto fail;
88201780c4Schristos 	if (gethostname(hostname, hostname_size) != 0)
89201780c4Schristos 		strlcpy(hostname, "localhost", hostname_size);
9076e8c542Schristos 	if ((r = pam_set_item(ph, PAM_HOST, hostname)) != PAM_SUCCESS)
9176e8c542Schristos 		goto fail;
9276e8c542Schristos 	if ((r = pam_set_item(ph, PAM_USER, user)) != PAM_SUCCESS)
9376e8c542Schristos 		goto fail;
9476e8c542Schristos 	if ((r = pam_set_item(ph, PAM_CONV, pam_conv)) != PAM_SUCCESS)
9576e8c542Schristos 		goto fail;
9676e8c542Schristos 	if ((r = openpam_configure(ph, service)) != PAM_SUCCESS)
9776e8c542Schristos 		goto fail;
98201780c4Schristos 	free(hostname);
9976e8c542Schristos 	*pamh = ph;
10076e8c542Schristos 	openpam_log(PAM_LOG_DEBUG, "pam_start(\"%s\") succeeded", service);
10176e8c542Schristos 	RETURNC(PAM_SUCCESS);
10276e8c542Schristos fail:
103201780c4Schristos 	free(hostname);
10476e8c542Schristos 	pam_end(ph, r);
10576e8c542Schristos 	RETURNC(r);
10676e8c542Schristos }
10776e8c542Schristos 
10876e8c542Schristos /*
10976e8c542Schristos  * Error codes:
11076e8c542Schristos  *
11176e8c542Schristos  *	=openpam_configure
11276e8c542Schristos  *	=pam_set_item
11376e8c542Schristos  *	!PAM_SYMBOL_ERR
11476e8c542Schristos  *	PAM_BUF_ERR
11576e8c542Schristos  */
11676e8c542Schristos 
11776e8c542Schristos /**
11876e8c542Schristos  * The =pam_start function creates and initializes a PAM context.
11976e8c542Schristos  *
12076e8c542Schristos  * The =service argument specifies the name of the policy to apply, and is
12176e8c542Schristos  * stored in the =PAM_SERVICE item in the created context.
12276e8c542Schristos  *
12376e8c542Schristos  * The =user argument specifies the name of the target user - the user the
12476e8c542Schristos  * created context will serve to authenticate.
12576e8c542Schristos  * It is stored in the =PAM_USER item in the created context.
12676e8c542Schristos  *
12776e8c542Schristos  * The =pam_conv argument points to a =struct pam_conv describing the
12876e8c542Schristos  * conversation function to use; see =pam_conv for details.
12976e8c542Schristos  *
13076e8c542Schristos  * >pam_get_item
13176e8c542Schristos  * >pam_set_item
13276e8c542Schristos  * >pam_end
13376e8c542Schristos  */
134