/* Generated from "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod" by precompile.pike running Pike v8.0 release 1737
 *
 * Do NOT edit this file.
 */

#undef PRECOMPILE_API_VERSION
#define PRECOMPILE_API_VERSION 6

#undef PRECOMPILE_SUB_PIKEVARS
#define PRECOMPILE_SUB_PIKEVARS 1

#undef PRECOMPILE_DYNAMIC_TYPES
#define PRECOMPILE_DYNAMIC_TYPES 1



#undef cmod___CMOD__
#define cmod___CMOD__ 1
#line 1 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
#line 3003 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"







#line 3010 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"
#line 1 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
/* -*- c -*-
 * Kerberos support for Pike.
 *
 * 2004-05-12 Henrik Grubbström
 *
 * FIXME: Currently only minimal support, and only for Kerberos 5 (not 4).
 */

#include "global.h"
#include "config.h"

#include "svalue.h"
#include "module.h"
#include "interpret.h"



#ifndef PIKE_UNUSED_ATTRIBUTE
#define PIKE_UNUSED_ATTRIBUTE
#endif
#define CMOD_MAP_PROGRAM_IDS_DEFINED 1
static int ___cmod_map_program_ids(int id) PIKE_UNUSED_ATTRIBUTE;
#ifndef TYPEOF
/* Compat with older Pikes. */
#define TYPEOF(SVAL)	((SVAL).type)
#define SUBTYPEOF(SVAL)	((SVAL).subtype)
#define SET_SVAL_TYPE(SVAL, TYPE)	(TYPEOF(SVAL) = TYPE)
#define SET_SVAL_SUBTYPE(SVAL, TYPE)	(SUBTYPEOF(SVAL) = TYPE)
#define SET_SVAL(SVAL, TYPE, SUBTYPE, FIELD, EXPR) do {	\
    /* Set the type afterwards to avoid a clobbered	\
     * svalue in case EXPR throws. */			\
    (SVAL).u.FIELD = (EXPR);				\
    SET_SVAL_TYPE((SVAL), (TYPE));			\
    SET_SVAL_SUBTYPE((SVAL), (SUBTYPE));		\
  } while(0)
#endif /* !TYPEOF */
#ifndef PIKE_UNUSED
/* Compat with Pike 7.8 and earlier. */
/* NB: Not strictly correct; PIKE_UNUSED was added the
 *     day after set_program_id_to_id(), but good enough.
 */
#ifndef UNUSED
#define UNUSED(X)	X
#endif
#ifndef set_program_id_to_id
/* NB: Recent Pike 7.8 has a #define that conflicts with
 *     the following declaration.
 */
static void set_program_id_to_id(void*UNUSED(id)){}
#endif
#else /* */
PMOD_EXPORT void set_program_id_to_id( int (*to)(int) );
#endif /* !PIKE_UNUSED */


#ifndef DEFAULT_CMOD_STORAGE
#define CMOD_COND_USED
#define DEFAULT_CMOD_STORAGE static
#endif
#ifndef CMOD_COND_USED
# define f_Kerberos_Context_authenticate_fun_num_used 1
#else
#undef f_Kerberos_Context_authenticate_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define Kerberos_Context_program_fun_num_used 1
#else
#undef Kerberos_Context_program_fun_num_used
#endif
#line 18 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
/* #define KRB_DEBUG */

#ifdef KRB_DEBUG
#define KRB_FPRINTF(X)	fprintf X
#else /* !KRB_DEBUG */
#define KRB_FPRINTF(X)
#endif /* KRB_DEBUG */

#ifdef HAVE_KRB5

#ifdef HAVE_KRB5_H
#include <krb5.h>
#else
#ifdef HAVE_KERBEROSV5_KRB5_H
#include <kerberosv5/krb5.h>
#endif
#endif


#undef class_Kerberos_Context_defined
#define class_Kerberos_Context_defined
DEFAULT_CMOD_STORAGE struct program *Kerberos_Context_program=NULL;

#ifdef Kerberos_Context_program_fun_num_used
DEFAULT_CMOD_STORAGE int Kerberos_Context_program_fun_num=-1;
#endif /* Kerberos_Context_program_fun_num_used */

#undef var_ctx_Kerberos_Context_defined
#define var_ctx_Kerberos_Context_defined

#undef THIS
#define THIS ((struct Kerberos_Context_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_KERBEROS_CONTEXT
#define THIS_KERBEROS_CONTEXT ((struct Kerberos_Context_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_KERBEROS_CONTEXT
#define OBJ2_KERBEROS_CONTEXT(o) ((struct Kerberos_Context_struct *)(o->storage+Kerberos_Context_storage_offset))

#undef GET_KERBEROS_CONTEXT_STORAGE
#define GET_KERBEROS_CONTEXT_STORAGE(o) ((struct Kerberos_Context_struct *)(o->storage+Kerberos_Context_storage_offset)
static ptrdiff_t Kerberos_Context_storage_offset;
struct Kerberos_Context_struct {

#ifdef var_ctx_Kerberos_Context_defined
#line 38 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
krb5_context ctx;
#endif /* var_ctx_Kerberos_Context_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct Kerberos_Context_struct *Kerberos_Context_gdb_dummy_ptr;
#endif

#undef internal_init_Kerberos_Context_defined
#define internal_init_Kerberos_Context_defined

#undef Kerberos_Context_event_handler_defined
#define Kerberos_Context_event_handler_defined
static void init_Kerberos_Context_struct(void)
#line 41 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
{
krb5_error_code err_code;
if ((err_code = krb5_init_context(&THIS->ctx))) {
Pike_error("Failed to initialize context: %d\n", err_code);
}
}


#undef internal_exit_Kerberos_Context_defined
#define internal_exit_Kerberos_Context_defined

#undef Kerberos_Context_event_handler_defined
#define Kerberos_Context_event_handler_defined
static void exit_Kerberos_Context_struct(void)
#line 50 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
{
if (THIS->ctx) {
krb5_free_context(THIS->ctx);
}
}

#define f_Kerberos_Context_authenticate_defined

#ifdef f_Kerberos_Context_authenticate_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Kerberos_Context_authenticate_fun_num = 0;

#endif /* f_Kerberos_Context_authenticate_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Kerberos_Context_authenticate(INT32 args) {
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
struct pike_string * user;
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
struct pike_string * password;
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
if(args != 2) wrong_number_of_args_error("authenticate",args,2);
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
if(TYPEOF(Pike_sp[0-2]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("authenticate",1,"string");
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
debug_malloc_pass(user=Pike_sp[0-2].u.string);
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
if(TYPEOF(Pike_sp[1-2]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("authenticate",2,"string");
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
debug_malloc_pass(password=Pike_sp[1-2].u.string);
#line 58 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
{
krb5_error_code err_code;
krb5_principal principal;
krb5_get_init_creds_opt get_creds_opt;
krb5_verify_init_creds_opt verify_creds_opt;
krb5_creds creds;
ONERROR err;

/* Hide the password from backtraces. */
Pike_sp[0] = Pike_sp[1-args];
SET_SVAL(Pike_sp[1-args], PIKE_T_INT, NUMBER_UNDEFINED, integer, 0);
Pike_sp++;
args++;

if (user->size_shift || password->size_shift) {
KRB_FPRINTF((stderr, "Wide password or user name.\n"));
pop_n_elems(args);
push_int(0);
return;
}

if ((err_code = krb5_parse_name(THIS->ctx, (const char *) STR0(user),
&principal))) {
KRB_FPRINTF((stderr, "Failed to parse user name: %d\n", err_code));
pop_n_elems(args);
push_int(0);
return;
}

krb5_get_init_creds_opt_init(&get_creds_opt);
krb5_verify_init_creds_opt_init(&verify_creds_opt);

if ((err_code = krb5_get_init_creds_password(THIS->ctx, &creds,
principal,
(char *) STR0(password),
krb5_prompter_posix,
NULL, 0, NULL,
&get_creds_opt))) {
KRB_FPRINTF((stderr, "Failed to get password credentials: %d.\n",
err_code));
pop_n_elems(args);
push_int(0);
return;
}

if ((err_code = krb5_verify_init_creds(THIS->ctx, &creds,
NULL, NULL, NULL,
&verify_creds_opt))) {
krb5_free_cred_contents(THIS->ctx, &creds);
KRB_FPRINTF((stderr, "Failed to verify credentials: %d.\n", err_code));
pop_n_elems(args);
push_int(0);
return;
}

krb5_free_cred_contents(THIS->ctx, &creds);
KRB_FPRINTF((stderr, "Credentials ok."));

pop_n_elems(args);
push_int(1);
return;
}
}

#ifdef Kerberos_Context_event_handler_defined
static void Kerberos_Context_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_Kerberos_Context_defined
  case PROG_EVENT_INIT: init_Kerberos_Context_struct(); break;

#endif /* internal_init_Kerberos_Context_defined */

#ifdef internal_exit_Kerberos_Context_defined
  case PROG_EVENT_EXIT: exit_Kerberos_Context_struct(); break;

#endif /* internal_exit_Kerberos_Context_defined */
  default: break; 
  }
}

#endif /* Kerberos_Context_event_handler_defined */
#line 123 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
#endif /* HAVE_KRB5 */

PIKE_MODULE_INIT
{

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( ___cmod_map_program_ids );

#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */

#ifdef class_Kerberos_Context_defined

#ifdef PROG_KERBEROS_CONTEXT_ID
#line 36 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
  START_NEW_PROGRAM_ID(KERBEROS_CONTEXT);
#else
#line 36 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
  start_new_program();

#endif /* PROG_KERBEROS_CONTEXT_ID */
#line 36 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
  Kerberos_Context_program = Pike_compiler->new_program;

#ifndef tObjImpl_KERBEROS_CONTEXT

#undef tObjIs_KERBEROS_CONTEXT
#define tObjIs_KERBEROS_CONTEXT "\3\1\177\0\0\2"

#undef tObjImpl_KERBEROS_CONTEXT
#define tObjImpl_KERBEROS_CONTEXT "\3\0\177\0\0\2"

#endif /* tObjImpl_KERBEROS_CONTEXT */

#ifdef THIS_KERBEROS_CONTEXT
  Kerberos_Context_storage_offset = ADD_STORAGE(struct Kerberos_Context_struct);
#endif /* THIS_KERBEROS_CONTEXT */

#ifdef Kerberos_Context_event_handler_defined
  pike_set_prog_event_callback(Kerberos_Context_event_handler);

#ifndef Kerberos_Context_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* Kerberos_Context_gc_live_obj */

#endif /* Kerberos_Context_event_handler_defined */

#ifdef f_Kerberos_Context_authenticate_defined

#ifdef f_Kerberos_Context_authenticate_fun_num_used
  f_Kerberos_Context_authenticate_fun_num =
#endif /* f_Kerberos_Context_authenticate_fun_num_used */
#line 56 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
    ADD_FUNCTION2("authenticate", f_Kerberos_Context_authenticate, tFunc(tStr tStr,"\10\0\0\0\0\0\0\0\1"), 0, OPT_EXTERNAL_DEPEND);

#endif /* f_Kerberos_Context_authenticate_defined */
#line 36 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
  Kerberos_Context_program=end_program();
#line 36 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
#ifdef Kerberos_Context_program_fun_num_used
Kerberos_Context_program_fun_num=
#endif
 add_program_constant("Context",Kerberos_Context_program,0);

#endif /* class_Kerberos_Context_defined */

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( 0 );
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
#line 127 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
;
}

PIKE_MODULE_EXIT
{

#ifdef class_Kerberos_Context_defined
  if(Kerberos_Context_program) {
#line 36 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
    free_program(Kerberos_Context_program);
    Kerberos_Context_program=0;
  }

#endif /* class_Kerberos_Context_defined */
#line 132 "/home/hww3/pike-git/src/post_modules/Kerberos/kerberos.cmod"
;
}


#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
static int ___cmod_map_program_ids(int id)
{
  int i = 0;
  if( (id&0x7f000000) != 0x7f000000 ) return id;
  id = id&0x00ffffff;
#ifdef class_Kerberos_Context_defined
if(id==2) return Kerberos_Context_program->id;
#endif
  return 0;
}
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
