<commit_before>/*
 * Copyright 2003,2004,2005,2006,2007 Red Hat, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, and the entire permission notice in its entirety,
 *    including the disclaimer of warranties.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * ALTERNATIVELY, this product may be distributed under the terms of the
 * GNU Lesser General Public License, in which case the provisions of the
 * LGPL are required INSTEAD OF the above restrictions.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "../config.h"

#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef HAVE_SECURITY_PAM_APPL_H
#include <security/pam_appl.h>
#endif

#ifdef HAVE_SECURITY_PAM_MODULES_H
#include <security/pam_modules.h>
#endif

#include KRB5_H
#ifdef USE_KRB4
#include KRB4_DES_H
#include KRB4_KRB_H
#ifdef KRB4_KRB_ERR_H
#include KRB4_KRB_ERR_H
#endif
#endif

#include "init.h"
#include "log.h"
#include "options.h"
#include "sly.h"
#include "stash.h"
#include "tokens.h"
#include "userinfo.h"
#include "v5.h"
#include "v4.h"

#ident "$Id$"

#ifdef USE_KRB4
/* Store the v4 TGT in $KRBTKFILE. */
static void
sly_v4(krb5_context ctx, const char *v4tktfile,
       struct _pam_krb5_user_info *userinfo, struct _pam_krb5_stash *stash)
{
	int i;
	char name[ANAME_SZ + 1], instance[INST_SZ + 1], realm[REALM_SZ + 1];

	i = krb5_524_conv_principal(ctx, userinfo->principal_name,
				    name, instance, realm);
	if (i != 0) {
		return;
	}

	tf_init(v4tktfile, W_TKT_FIL);
	v4_in_tkt(name, instance, realm);
	v4_save_credentials(KRB5_TGS_NAME, realm, realm,
			    stash->v4creds.session,
			    stash->v4creds.lifetime,
			    stash->v4creds.kvno,
			    &stash->v4creds.ticket_st,
			    stash->v4creds.issue_date);
	tf_close();
}
#else
static void
sly_v4(krb5_context ctx, const char *v4tktfile,
       struct _pam_krb5_user_info *userinfo, struct _pam_krb5_stash *stash)
{
}
#endif

/* Store the v5 TGT in $KRB5CCNAME. */
static int
sly_v5(krb5_context ctx, const char *v5ccname,
       struct _pam_krb5_user_info *userinfo, struct _pam_krb5_stash *stash)
{
	krb5_ccache ccache;
	krb5_principal princ;
	int i;

	ccache = NULL;
	i = krb5_cc_resolve(ctx, v5ccname, &ccache);
	if (i == 0) {
		princ = NULL;
		if (krb5_cc_get_principal(ctx, ccache, &princ) == 0) {
			if (krb5_principal_compare(ctx, princ,
						   userinfo->principal_name) == FALSE) {
				krb5_cc_close(ctx, ccache);
				return PAM_SERVICE_ERR;
			}
		}
		i = krb5_cc_initialize(ctx, ccache, userinfo->principal_name);
		if (i == 0) {
			i = krb5_cc_store_cred(ctx, ccache, &stash->v5creds);
		}
		krb5_cc_close(ctx, ccache);
	}

	return PAM_SUCCESS;
}

/* Inexpensive checks. */
int
_pam_krb5_sly_looks_unsafe(void)
{
	if (getenv("SUDO_COMMAND") != NULL) {
		return 1;
	}
	if (getuid() != geteuid()) {
		return 2;
	}
	if (getgid() != getegid()) {
		return 3;<commit_msg>- don't leak that principal name on refresh<commit_after>/*
 * Copyright 2003,2004,2005,2006,2007 Red Hat, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, and the entire permission notice in its entirety,
 *    including the disclaimer of warranties.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * ALTERNATIVELY, this product may be distributed under the terms of the
 * GNU Lesser General Public License, in which case the provisions of the
 * LGPL are required INSTEAD OF the above restrictions.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "../config.h"

#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef HAVE_SECURITY_PAM_APPL_H
#include <security/pam_appl.h>
#endif

#ifdef HAVE_SECURITY_PAM_MODULES_H
#include <security/pam_modules.h>
#endif

#include KRB5_H
#ifdef USE_KRB4
#include KRB4_DES_H
#include KRB4_KRB_H
#ifdef KRB4_KRB_ERR_H
#include KRB4_KRB_ERR_H
#endif
#endif

#include "init.h"
#include "log.h"
#include "options.h"
#include "sly.h"
#include "stash.h"
#include "tokens.h"
#include "userinfo.h"
#include "v5.h"
#include "v4.h"

#ident "$Id$"

#ifdef USE_KRB4
/* Store the v4 TGT in $KRBTKFILE. */
static void
sly_v4(krb5_context ctx, const char *v4tktfile,
       struct _pam_krb5_user_info *userinfo, struct _pam_krb5_stash *stash)
{
	int i;
	char name[ANAME_SZ + 1], instance[INST_SZ + 1], realm[REALM_SZ + 1];

	i = krb5_524_conv_principal(ctx, userinfo->principal_name,
				    name, instance, realm);
	if (i != 0) {
		return;
	}

	tf_init(v4tktfile, W_TKT_FIL);
	v4_in_tkt(name, instance, realm);
	v4_save_credentials(KRB5_TGS_NAME, realm, realm,
			    stash->v4creds.session,
			    stash->v4creds.lifetime,
			    stash->v4creds.kvno,
			    &stash->v4creds.ticket_st,
			    stash->v4creds.issue_date);
	tf_close();
}
#else
static void
sly_v4(krb5_context ctx, const char *v4tktfile,
       struct _pam_krb5_user_info *userinfo, struct _pam_krb5_stash *stash)
{
}
#endif

/* Store the v5 TGT in $KRB5CCNAME. */
static int
sly_v5(krb5_context ctx, const char *v5ccname,
       struct _pam_krb5_user_info *userinfo, struct _pam_krb5_stash *stash)
{
	krb5_ccache ccache;
	krb5_principal princ;
	int i;

	ccache = NULL;
	i = krb5_cc_resolve(ctx, v5ccname, &ccache);
	if (i == 0) {
		princ = NULL;
		if (krb5_cc_get_principal(ctx, ccache, &princ) == 0) {
			if (krb5_principal_compare(ctx, princ,
						   userinfo->principal_name) == FALSE) {
				krb5_free_principal(ctx, princ);
				krb5_cc_close(ctx, ccache);
				return PAM_SERVICE_ERR;
			}
		}
		i = krb5_cc_initialize(ctx, ccache, userinfo->principal_name);
		if (i == 0) {
			i = krb5_cc_store_cred(ctx, ccache, &stash->v5creds);
		}
		krb5_free_principal(ctx, princ);
		krb5_cc_close(ctx, ccache);
	}

	return PAM_SUCCESS;
}

/* Inexpensive checks. */
int
_pam_krb5_sly_looks_unsafe(void)
{
	if (getenv("SUDO_COMMAND") != NULL) {
		return 1;
	}
	if (getuid() != geteuid()) {
		return 2;
	}
	if (getgid() != getegid()) {
		return 3;
{'fraction_non_alphanumeric': 0.09906001446131597, 'fraction_numerical': 0.020727886237647625, 'mean_word_length': 4.24920936116382, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1935541', 'n_tokens_mistral': 3266, 'n_tokens_neox': 2972, 'n_words': 912}