OpenVMS Source-Code Demos

MOD_AUTH_TEMPLATE

//==============================================================================================================================
// title  : mod_auth_template_102.c
// author : Neil Rieck (Waterloo, Ontario, Canada)
// created: 2015-05-04
// target : CSWS-2.2 (a.k.a. Apache httpd-2.0.63 on OpenVMS)
// notes  : 1) this program is based upon "mod_auth_template_102.c"
//        : 2) It has almost all the authentication ripped-out so I can just play with the plugin hooks
//		a. I want to see how much load is placed upon Apache by loading a module but never using
//		b. I want to see if disabled per-directory authentication hurt the remainder of Apache
// history:
// ver who when   what
// --- --- ------ --------------------------------------------------------------------------------------------------------------
// 102 NSR 150504 1. dervived from "mod_auth_template_102.C"
//     NSR 151210 2. mtce tweak to the VMSIFY macro
//==============================================================================================================================
// Docs:
// 1) build this program then copy the execuatable to sys$common:[modules]
//	be sure to check file ownership and protection bits
//	consider using "$SET SECURITY/ACL" to modify/delete access control list params
//    add the next line to file: apache$common:[conf]httpd.conf
//	LoadModule auth_vms_ext_module     modules/mod_auth_vms_ext.exe
//    restart CSWS to load the new module
// 3) directives:
//	AuthTemplateUserEnable	on/off			off means disabled
//	AuthTemplateGroupEnable	on/off			off means disabled
//==============================================================================================================================
#define __NEW_STARLET	1					// enable new (strict) starlet (OpenVMS Alpha 7.0 and above)
//
//  Include files
//
#include <ctype.h>
#include <types.h>
#include <limits.h>
#include <string.h>
//
#include <ssdef.h>
#include <kgbdef.h>
#include <lgidef.h>
#include <stsdef.h>
#include <descrip.h>
#include <starlet.h>
#include <builtins.h>
#include <lib$routines.h>					// need this for lib$spawn
#include <stdlib.h>						// need this for getenv
//
#ifdef SHADOW
#undef SHADOW
#endif
#ifdef MULTITHREADING
#undef MULTITHREADING
#endif
//
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "apr_strings.h"
#include "protshr.h"
//
//  Definitions
//
#ifndef INTERNAL
#define INTERNAL static
#endif
#ifndef NULL
#define NULL (void *) 0
#endif
#ifndef alloca
#define alloca __ALLOCA
#endif
//
//	VMSIFY
//      a macro for use in the VMS world (VMS strings employ this structure)
//	notes:	this macro can be used to pass VMS strings down to lower modules
//		the $DESCRIPTOR macro does something similar employing sizeof-1
//		this macro combines two operations
//
#define VMSIFY(a,b) {					\
    a.dsc$b_dtype = DSC$K_DTYPE_T;			\
    a.dsc$b_class = DSC$K_CLASS_S;			\
    a.dsc$w_length = strlen(b);				\
    a.dsc$a_pointer = (char *) malloc(strlen(b));	\
    strncpy(a.dsc$a_pointer,b,a.dsc$w_length);		\
}
//
#define DEBUG 1
#if DEBUG
//dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd Neil's debug code
//
//	this block is for development purposes only
//
char spy_stamp[50];								// buffer for time-stamp
void build_stamp(){
#include <time.h>
	//----------------------------------------------------------------------
	struct	timeb	timebuffer;						// for ftime()
	struct	tm	*time_fields;						// for localtime()
	char		millisecs[5];						//
	char		my_date_time[30];					//
	//----------------------------------------------------------------------
	ftime( &timebuffer );							// record current system time
	sprintf(millisecs, "%03hu", timebuffer.millitm);			// extract milliseconds as three chars
	time_fields = localtime( &timebuffer.time );				// breakout other time fields
	strftime(	my_date_time,						// ccyymmdd.hhmmss
			sizeof(my_date_time),					//
			"%Y%m%d.%H%M%S",					//
			time_fields );						//
	sprintf(	spy_stamp,						// ccyymmdd.hhmmss.xxx
			"%s%s%s",						//
			my_date_time,						//
			".",							//
			millisecs);						// xxx
}

char trc_buf[MAX_STRING_LEN];							//
FILE *trc_file = NULL;								//
void TRC1(char *msg) {								// trace (one param)
    build_stamp();								//
    trc_file = fopen("APACHE$COMMON:[000000]aaa_mod_template.trc", "a");	// open the trace file
    if (trc_file != NULL) {							//
	fprintf(trc_file, "%s %s\n",spy_stamp,msg);				//
	fclose (trc_file);							//
    }										//
}										//
//	trace (two params)
#define TRC2(a,b) {		\
	sprintf(trc_buf,a,b);	\
	TRC1 (trc_buf);		\
} 										//
//dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd end of debug
#else
#define TRC2(a,b)
#define TRC1(a)
#endif
//
//  Data structures
//
typedef unsigned int VMS_STATUS;
//
typedef struct dsc$descriptor_s DSC_S;		// all VMS strings must be passed by descriptor
//
typedef struct                          	// context block
{
    int     fUserEnable;			// 0= false (this module abstains)
    int     fGroupEnable;			// 0= false (this module abstains)
}
CTXBLK;

//
//  Function prototypes
//
extern module AP_MODULE_DECLARE_DATA auth_template_module;
//
void *create_auth_vms_ext_cntxt (apr_pool_t *p, char *d)
{
    CTXBLK *sec = (CTXBLK *) apr_pcalloc (p, sizeof(CTXBLK));
    sec->fUserEnable	= 0;
    sec->fGroupEnable	= 0;
    return (void *) sec;
}
const char* my_first_cmd_func(cmd_parms* cmd, void* cfg, const char* arg);
//
//  Directives handled by this module
//
command_rec auth_vms_ext_cmds[] =
{
    { "AuthTemplateUserEnable",
	ap_set_flag_slot,
	(void *) APR_XtOffsetOf(CTXBLK,fUserEnable),
	OR_AUTHCFG,
	FLAG,
	"User authentication/authorization on/off" },
    { "AuthTemplateGroupEnable",
	ap_set_flag_slot,
	(void *) APR_XtOffsetOf(CTXBLK,fGroupEnable),
	OR_AUTHCFG,
	FLAG,
	"Group authentication/authorization on/off" },
    { NULL }
};

//=====================================================================================================================
//  a u t h e n t i c a t e _ u s e r
//======================================================================================================================
INTERNAL int authenticate_user (request_rec *r)
{
    unsigned int st;								//
    unsigned int rc;								//
    CTXBLK *sec			= (CTXBLK *) ap_get_module_config (r->per_dir_config, &auth_template_module);
/*---
    if (!sec->fUserEnable) {							// not enabled
        return DECLINED;							// then exit
    }
---*/
    TRC1("new transaction (authenticate_user) ==========");
    conn_rec *c			= r->connection;				//
    const char *remoteHost	= ap_get_remote_host ( r->connection, r->per_dir_config, REMOTE_NOLOOKUP, 0 );
    const char *cookie_data	= apr_table_get( r->headers_in, "Cookie");	//
    //--------------------------------------------------------------------------
    TRC2(" LocalHost : %s",r->server->server_hostname);
    TRC2(" RemoteHost: %s",remoteHost);
    TRC2(" CookieData: %s",cookie_data);
    TRC2(" USER      : %s",r->user);
    TRC2(" URI       : %s",r->uri);
    //--------------------------------------------------------------------------
    return DECLINED;
}

//=====================================================================================================================
//  a u t h e n t i c a t e _ g r o u p
//=====================================================================================================================
INTERNAL int authenticate_group (request_rec *r)
{
    unsigned int st;								//
    unsigned int rc;								//
    CTXBLK *sec			= (CTXBLK *) ap_get_module_config (r->per_dir_config, &auth_template_module);
/*---
    if (!sec->fGroupEnable) {							// not enabled
        return DECLINED;							// then exit
    }
---*/
    TRC1("new transaction (authenticate_group) ==========");
    conn_rec *c			= r->connection;				//
    const char *remoteHost	= ap_get_remote_host ( r->connection, r->per_dir_config, REMOTE_NOLOOKUP, 0 );
    const char *cookie_data	= apr_table_get( r->headers_in, "Cookie");	//
    //--------------------------------------------------------------------------
    TRC2(" LocalHost : %s",r->server->server_hostname);
    TRC2(" RemoteHost: %s",remoteHost);
    TRC2(" CookieData: %s",cookie_data);
    TRC2(" USER      : %s",r->user);
    TRC2(" URI       : %s",r->uri);
    //--------------------------------------------------------------------------
    return DECLINED;
}

//=====================================================================================================================
//	common apache module stuff
//=====================================================================================================================
static void register_hooks(apr_pool_t *p)
{
    ap_hook_check_user_id(authenticate_user,NULL,NULL,APR_HOOK_MIDDLE);	// username+pass
    ap_hook_auth_checker(authenticate_group,NULL,NULL,APR_HOOK_MIDDLE);	// group access
}

module AP_MODULE_DECLARE_DATA auth_template_module =
{
    STANDARD20_MODULE_STUFF,
    create_auth_vms_ext_cntxt,		// per dir config creater
    NULL,				// per dir merger --- default is to override
    NULL,				// server config
    NULL,				// merge server config
    auth_vms_ext_cmds,			// command apr_table_t
    register_hooks			// register hooks
};