155939Snsouch/*-
255939Snsouch * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
355939Snsouch * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
455939Snsouch * All rights reserved.
555939Snsouch *
655939Snsouch * This software was developed for the FreeBSD Project by ThinkSec AS and
755939Snsouch * Network Associates Laboratories, the Security Research Division of
855939Snsouch * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
955939Snsouch * ("CBOSS"), as part of the DARPA CHATS research program.
1055939Snsouch *
1155939Snsouch * Redistribution and use in source and binary forms, with or without
1255939Snsouch * modification, are permitted provided that the following conditions
1355939Snsouch * are met:
1455939Snsouch * 1. Redistributions of source code must retain the above copyright
1555939Snsouch *    notice, this list of conditions and the following disclaimer.
1655939Snsouch * 2. Redistributions in binary form must reproduce the above copyright
1755939Snsouch *    notice, this list of conditions and the following disclaimer in the
1855939Snsouch *    documentation and/or other materials provided with the distribution.
1955939Snsouch * 3. The name of the author may not be used to endorse or promote
2055939Snsouch *    products derived from this software without specific prior written
2155939Snsouch *    permission.
2255939Snsouch *
2355939Snsouch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2455939Snsouch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2555939Snsouch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2655939Snsouch * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2755939Snsouch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2855939Snsouch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2955939Snsouch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3055939Snsouch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3155939Snsouch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3255939Snsouch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3355939Snsouch * SUCH DAMAGE.
3455939Snsouch *
3555939Snsouch * $Id: pam_start.c 648 2013-03-05 17:54:27Z des $
3655939Snsouch */
3755939Snsouch
3855939Snsouch#ifdef HAVE_CONFIG_H
3955939Snsouch# include "config.h"
4055939Snsouch#endif
4155939Snsouch
4255939Snsouch#include <stdlib.h>
4355939Snsouch#include <string.h>
4455939Snsouch#include <unistd.h>
4555939Snsouch
4655939Snsouch#include <security/pam_appl.h>
4755939Snsouch
4855939Snsouch#include "openpam_impl.h"
4955939Snsouch#include "openpam_strlcpy.h"
5055939Snsouch
5155939Snsouch#ifdef _SC_HOST_NAME_MAX
5255939Snsouch#define HOST_NAME_MAX sysconf(_SC_HOST_NAME_MAX)
5355939Snsouch#else
5455939Snsouch#define HOST_NAME_MAX 1024
5555939Snsouch#endif
5655939Snsouch
5755939Snsouch/*
5855939Snsouch * XSSO 4.2.1
5955939Snsouch * XSSO 6 page 89
6055939Snsouch *
6155939Snsouch * Initiate a PAM transaction
6255939Snsouch */
6355939Snsouch
6455939Snsouchint
6555939Snsouchpam_start(const char *service,
6655939Snsouch	const char *user,
6755939Snsouch	const struct pam_conv *pam_conv,
6855939Snsouch	pam_handle_t **pamh)
6955939Snsouch{
7055939Snsouch	char hostname[HOST_NAME_MAX + 1];
7155939Snsouch	struct pam_handle *ph;
7255939Snsouch	int r;
7355939Snsouch
7455939Snsouch	ENTER();
7555939Snsouch	if ((ph = calloc(1, sizeof *ph)) == NULL)
7655939Snsouch		RETURNC(PAM_BUF_ERR);
7755939Snsouch	if ((r = pam_set_item(ph, PAM_SERVICE, service)) != PAM_SUCCESS)
7855939Snsouch		goto fail;
7955939Snsouch	if (gethostname(hostname, sizeof hostname) != 0)
8055939Snsouch		strlcpy(hostname, "localhost", sizeof hostname);
8155939Snsouch	if ((r = pam_set_item(ph, PAM_HOST, hostname)) != PAM_SUCCESS)
8255939Snsouch		goto fail;
8355939Snsouch	if ((r = pam_set_item(ph, PAM_USER, user)) != PAM_SUCCESS)
84		goto fail;
85	if ((r = pam_set_item(ph, PAM_CONV, pam_conv)) != PAM_SUCCESS)
86		goto fail;
87	if ((r = openpam_configure(ph, service)) != PAM_SUCCESS)
88		goto fail;
89	*pamh = ph;
90	openpam_log(PAM_LOG_DEBUG, "pam_start(\"%s\") succeeded", service);
91	RETURNC(PAM_SUCCESS);
92fail:
93	pam_end(ph, r);
94	RETURNC(r);
95}
96
97/*
98 * Error codes:
99 *
100 *	=openpam_configure
101 *	=pam_set_item
102 *	!PAM_SYMBOL_ERR
103 *	PAM_BUF_ERR
104 */
105
106/**
107 * The =pam_start function creates and initializes a PAM context.
108 *
109 * The =service argument specifies the name of the policy to apply, and is
110 * stored in the =PAM_SERVICE item in the created context.
111 *
112 * The =user argument specifies the name of the target user - the user the
113 * created context will serve to authenticate.
114 * It is stored in the =PAM_USER item in the created context.
115 *
116 * The =pam_conv argument points to a =struct pam_conv describing the
117 * conversation function to use; see =pam_conv for details.
118 *
119 * >pam_get_item
120 * >pam_set_item
121 * >pam_end
122 */
123