Below is the file 'account.c' from this revision. You can also download the file.

/*
 * account.c:
 * Accounting for sshd.
 *
 * Based on code (c) 2001 Chris Lightfoot.
 * Modified 2001 by Matt Johnston for use with dispense
 *
 */

#include <sys/types.h>
#include <stdlib.h>

#include <ucc.h>

#include "dbutil.h"

size_t total_read, total_written;
char *acct_user = 0;
int isctrl = 0;

/*
 * Validates the user, checking for sufficient coke credit etc.
 * Assuming that the username is the same as the coke account (pretty
 * sure this is valid, what would dispense client for drinks do???)
 * If it isn't valid, sent DROPBEAR_FAILURE, else DROPBEAR_SUCCESS
 */
int check_coke_user(char *name) {

	SetCokebankToSocks();
	if (!name) {
		cokebank_close();
		dropbear_exit("cokebank eek!");
	}

	isctrl = InCokeControl(name);
	if(!cokebank_open() || cokebank_get(name)-(isctrl?COKE_CONTROLLERS_MIN:0)<CONNECT_MIN) {
		cokebank_close();
		return DROPBEAR_FAILURE;
	}
	cokebank_close();

	/* name is valid */
	if (acct_user)
		m_free(acct_user);
	acct_user = strdup(name);
	if (!acct_user) {
		dropbear_exit("strdup failed for accounting");
	}

	return DROPBEAR_SUCCESS;
}

void acct_reset() {
	if (acct_user)
		m_free(acct_user);
    total_read = total_written = 0;
}

void acct_add(size_t r, size_t w) {

	total_read += r;
	total_written += w;

	SetCokebankToSocks();

	if (total_read > CUNIT)
	{
		if(!cokebank_open() || (cokebank_add_bytes(acct_user, total_read, "Incoming SSH traffic", "-server-", "sshd")))
		{
			cokebank_close();
			dropbear_exit("UCC Incoming Connection Server -- INFO: Error adjusting coke balance.");
		}
		else if(cokebank_get(acct_user)-(isctrl?COKE_CONTROLLERS_MIN:0)<CONNECT_MIN) {
			cokebank_close();
			dropbear_exit("UCC Incoming Connection Server -- INFO: Coke balance bad, low or missing.");
		}
		cokebank_close();
		total_read = total_written = 0;
	}
}

/* Called on connection close etc, or any other time */
void acct_report() {

	if (total_read)
	{
		if (acct_user)
		{
			SetCokebankToSocks();
			if(!cokebank_open() || cokebank_add_bytes(acct_user, total_read,"Incoming SSH traffic","-server-","sshd"))
			{
				cokebank_close();
				dropbear_exit("UCC Incoming Connection Server -- INFO: Error adjusting coke balance.");
			}
			else
			{
				cokebank_close();
				total_read = total_written = 0;
			}
		}
	}
}