/*

gcc -shared dnewsauth2.c -o dnewsauth.so
cp dnewsauth.so /usr/local/dnews

In dnews.conf
auth_extern true
auth_lib /usr/local/dnews/dnewsauth.so

optionally
auth_user true


This is called for each 'article' read by the user
If FALSE is returned, the user is disconnected.


__declspec( dllexport ) int auth_readart(
		int chan,	Unique number for this user for this session 
		int type,	Type of request, only 1 is defined, other types should be ignored currently
		 char *ipname,	IP name of user 
                 char *ipnumber, IP number of user
                 char *username, username of user
                 char *password, password of user
                 char *group,    Group they are trying to read
                 int article,    article number they are reading
                 int size,  	Size of article they are reading
                 char *reason) 	Reason buffer for failures, fill this in when returning 'false' 




*/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
/*	After debugging remove the smsg logging lines or your system
	performance will suffer 
*/
int smsg(const char * arg_list, ...);

__declspec( dllexport ) int dnewsauth(char *user, char *pass, char *usergrps, int bfsz, char *bmsg)
{
	smsg("dnewsauth (%s) (%s) \n",user,pass);
	if (strcmp(user,"chrisp")!=0) {
		strcpy(bmsg,"No such user"); 
		smsg("dnewsauth - %s\n",bmsg);
		return FALSE;
	}
	if (strcmp(pass,"fred")!=0) {
		strcpy(bmsg,"Wrong password"); 
		smsg("dnewsauth - %s\n",bmsg);
		return FALSE;
	}
	strncpy(usergrps,"adults,gold",bfsz); usergrps[bfsz] = 0;
	strcpy(bmsg,"User logged in ok");  /* Optional */	
	smsg("dnewsauth - %s\n",bmsg);
	return TRUE;
}

__declspec( dllexport ) int auth_readart(int chan, int type,
		 char *ipname,
                 char *ipnumber, 
                 char *username,
                 char *password,
                 char *group,
                 int article,
                 int size,
                 char *reason) 
{

	smsg("readart [%d] t=%d u=%.80s p=%.80s group=%.80s:%d size=%d\n",chan,type,username,password,group,article,size);
	if (type!=1) return TRUE; /* Some type we don't understand - this allows expansion */
	if (article%4==3) {
		sprintf(reason,"Sorry, you are reading the fourth article in group %s",group);
		return FALSE;
	}
	return TRUE;
}
int smsg(const char * arg_list, ...)
{
        va_list arg_ptr;
        char *format;
        char output[4000];
	FILE *f;
		
        va_start(arg_ptr, arg_list);
        format = (char *) arg_list;
        vsnprintf(output, BFSZ-1, format, arg_ptr);
	output[BFSZ-1] = 0;
	f = fopen("readart.log","a");
	if (f==NULL) return 0;
	fprintf(f,"%s",output);
	fclose(f);
	return 0;
}
