Started on the database
authorDan Fuhry <dan@enanocms.org>
Sat, 23 Feb 2013 14:26:05 -0500
changeset 8 f68fdcc18df9
parent 5 cdd708efa505
child 9 f4bf6556fb9f
Started on the database
make-sso
packages/ssoinabox-webui/root/usr/local/share/ssoinabox/db/install.sql
packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/db.php
packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/functions.php
packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/ldap.php
packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/starthere.php
--- a/make-sso	Fri Jan 18 19:59:50 2013 -0500
+++ b/make-sso	Sat Feb 23 14:26:05 2013 -0500
@@ -1,6 +1,8 @@
 #!/bin/bash
 
 set -e
+cd "`dirname $0`"
+
 . resources/functions
 
 cat <<EOF
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/db/install.sql	Sat Feb 23 14:26:05 2013 -0500
@@ -0,0 +1,130 @@
+DROP TABLE IF EXISTS addresses;
+DROP TABLE IF EXISTS interfaces;
+DROP TABLE IF EXISTS hosts;
+DROP TABLE IF EXISTS pools;
+DROP TABLE IF EXISTS dns_records;
+DROP TABLE IF EXISTS networks;
+DROP TABLE IF EXISTS sites;
+
+CREATE TABLE sites(
+	id int(12) unsigned NOT NULL auto_increment,
+	name varchar(64) NOT NULL DEFAULT 'Default site',
+	domain varchar(64) NOT NULL DEFAULT 'example.com',
+	PRIMARY KEY ( id )
+);
+
+CREATE TABLE networks(
+	id int(12) unsigned NOT NULL auto_increment,
+	site_id int(12) unsigned NOT NULL,
+	name varchar(64) NOT NULL DEFAULT 'Main network',
+	subdomain_name varchar(16) NOT NULL DEFAULT 'hq',
+	ipv4_subnet int(8) unsigned DEFAULT NULL,
+	ipv4_subnet_mask int(8) unsigned DEFAULT NULL,
+	ipv6_prefix varbinary(32) NOT NULL DEFAULT 0,
+	PRIMARY KEY ( id ),
+	CONSTRAINT FOREIGN KEY ( site_id ) REFERENCES sites(id)
+	  ON DELETE CASCADE
+);
+
+CREATE TABLE pools(
+	id int(12) unsigned NOT NULL auto_increment,
+	network_id int(12) unsigned NOT NULL,
+	address_class ENUM('inet4', 'inet6', 'eui48') NOT NULL DEFAULT 'inet4',
+	address_start varbinary(32),
+	address_end varbinary(32),
+	name varchar(64) NOT NULL DEFAULT 'A pool',
+	PRIMARY KEY ( id ),
+	CONSTRAINT FOREIGN KEY ( network_id ) REFERENCES networks(id)
+	  ON DELETE CASCADE
+);
+
+CREATE TABLE hosts(
+	id int(12) unsigned NOT NULL auto_increment,
+	site_id int(12) unsigned NOT NULL,
+	hostname varchar(32) NOT NULL DEFAULT 'myhost',
+	owner varchar(32) NOT NULL DEFAULT 'root',
+	created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+	PRIMARY KEY ( id ),
+	CONSTRAINT FOREIGN KEY ( site_id ) REFERENCES sites(id)
+	  ON DELETE CASCADE
+);
+
+CREATE TABLE interfaces(
+	id int(12) unsigned NOT NULL auto_increment,
+	host_id int(12) unsigned NOT NULL,
+	network_id int(12) unsigned NOT NULL,
+	name varchar(16) NOT NULL DEFAULT 'eth0',
+	description varchar(255) NOT NULL DEFAULT 'eth0',
+	PRIMARY KEY ( id ),
+	CONSTRAINT FOREIGN KEY ( host_id ) REFERENCES hosts(id)
+	  ON DELETE CASCADE,
+	CONSTRAINT FOREIGN KEY ( network_id ) REFERENCES networks(id)
+	  ON DELETE CASCADE
+);
+
+CREATE TABLE addresses(
+	id int(12) unsigned NOT NULL auto_increment,
+	interface_id int(12) unsigned NOT NULL,
+	pool_id int(12) unsigned DEFAULT NULL,
+	address_class ENUM('inet4', 'inet6', 'eui48') NOT NULL DEFAULT 'inet4',
+	address_type ENUM('static', 'dynamic', 'automatic') NOT NULL DEFAULT 'dynamic',
+	address_value varbinary(32) DEFAULT NULL,
+	PRIMARY KEY ( id ),
+	CONSTRAINT FOREIGN KEY ( interface_id ) REFERENCES interfaces(id)
+	  ON DELETE CASCADE,
+	FOREIGN KEY ( pool_id ) REFERENCES pools(id)
+	  ON DELETE CASCADE
+);
+
+CREATE TABLE dns_records (
+	id int(12) unsigned NOT NULL auto_increment,
+	network_id int(12) unsigned NOT NULL,
+	owner varchar(64) NOT NULL DEFAULT 'root',
+	rname varchar(128) NOT NULL,
+	type ENUM('A','AAAA','MX','CNAME','NS','SRV','TXT','SSHFP') DEFAULT NULL,
+	ttl int(6) unsigned DEFAULT NULL,
+	rdata mediumtext,
+	PRIMARY KEY (id),
+	CONSTRAINT FOREIGN KEY ( network_id ) REFERENCES networks(id)
+		ON DELETE CASCADE
+);
+
+INSERT INTO sites(name, domain) VALUES
+	('Rochester', 'tits123.com'),
+	('Cleveland', 'tits123.com');
+
+INSERT INTO networks(site_id, subdomain_name, name, ipv4_subnet, ipv4_subnet_mask, ipv6_prefix) VALUES
+	(1, 'roc', 'Main VLAN', 0x0a010000, 0xffff0000, 0x20010470e18f0000),
+	(1, 'oe', 'Guest VLAN', 0x0a028000, 0xffff8000, 0x20010470e18f0001),
+	(2, 'cle', 'Main VLAN', 0x0a000000, 0xffff0000, 0x20010470e0d80000);
+
+INSERT INTO pools(network_id, address_class, address_start, address_end, name) VALUES
+	(1, 'inet4', 0x0a010001, 0x0a0100ff, 'Servers'),
+	(1, 'inet4', 0x0a010100, 0x0a01017f, 'User machines'),
+	(1, 'inet4', 0x0a010180, 0x0a0101ff, 'VMs'),
+	(2, 'inet4', 0x0a028003, 0x0a02fffe, 'OpenEars clients'),
+	(3, 'inet4', 0x0a000001, 0x0a0000ff, 'Servers'),
+	(3, 'inet4', 0x0a000100, 0x0a0003ff, 'User machines'),
+	(1, 'inet6', 0x20010470e18f0000020000fffe000000, 0x20010470e18f0000fffffffffeffffff, 'IPv6 autoconfig hosts'),
+	(3, 'inet6', 0x20010470e0d80000020000fffe000000, 0x20010470e0d80000fffffffffeffffff, 'IPv6 autoconfig hosts');
+
+INSERT INTO hosts(site_id, hostname, owner) VALUES
+	(1, 'xombie', 'root'),
+	(1, 'nighthawk', 'root'),
+	(1, 'ratsalad', 'dan');
+
+INSERT INTO interfaces(host_id, network_id, name, description) VALUES
+	(1, 1, 'vlan1', 'Internal interface'),
+	(1, 2, 'vlan5', 'OpenEars interface'),
+	(2, 1, 'vl-xx0r', 'Main interface'),
+	(3, 1, 'eth0', 'Main interface');
+
+INSERT INTO addresses(interface_id, pool_id, address_class, address_type, address_value) VALUES
+	(1, NULL, 'eui48', 'static', 0x001b21c2092c),
+	(1, 1	, 'inet4', 'static', 0x0a010001),
+	(1, 7	, 'inet6', 'static', 0x20010470e18f00000000000000000001),
+	(2, NULL, 'eui48', 'static', 0x001b21c2092c),
+	(2, 1	, 'inet4', 'static', 0x0a027f01),
+	(3, NULL, 'eui48', 'static', 0x001b21c4f583),
+	(3, 1	, 'inet4', 'static', 0x0a010003),
+	(4, 2	, 'inet4', 'dynamic', NULL);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/db.php	Sat Feb 23 14:26:05 2013 -0500
@@ -0,0 +1,20 @@
+<?php
+
+global $db;
+
+function db_setup()
+{
+	global $db, $mysql;
+	
+	$db = new PDO("mysql:host={$mysql['host']};user={$mysql['user']};{$mysql['password']};dbname={$mysql['dbname']}");
+}
+
+function db_prepare($sql)
+{
+	global $db;
+	
+	$st = $db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
+	return $st;
+}
+
+db_setup();
--- a/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/functions.php	Fri Jan 18 19:59:50 2013 -0500
+++ b/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/functions.php	Sat Feb 23 14:26:05 2013 -0500
@@ -34,7 +34,7 @@
 function load_credentials()
 {
 	$config = yaml_parse_file("/usr/local/etc/ssoinabox/webcreds.yml");
-	$keys = array('LDAP_BASEDN', 'UID_MIN', 'GID_MIN', 'ldap_server', 'ldap_manager', 'ldap_user_basedn', 'ldap_group_basedn', 'kerberos_admin', 'PHONE_EXT_MIN', 'hmac_secret');
+	$keys = array('LDAP_BASEDN', 'UID_MIN', 'GID_MIN', 'ldap_server', 'ldap_manager', 'ldap_user_basedn', 'ldap_group_basedn', 'kerberos_admin', 'PHONE_EXT_MIN', 'hmac_secret', 'mysql');
 	
 	foreach ( $keys as $key )
 	{
--- a/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/ldap.php	Fri Jan 18 19:59:50 2013 -0500
+++ b/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/ldap.php	Sat Feb 23 14:26:05 2013 -0500
@@ -62,13 +62,17 @@
 {
 	global $_ldapconn, $ldap_user_basedn;
 	
+	static $cache = array();
+	if ( isset($cache[$username]) )
+		return $cache[$username];
+	
 	$search_filter = sprintf("(&(uid=%s)(objectClass=posixAccount))", ldap_escape($username));
 	
 	$search_result = ldap_search($_ldapconn, $ldap_user_basedn, $search_filter);
 	if ( ldap_count_entries($_ldapconn, $search_result) !== 1 )
 		return false;
 	
-	return ldap_array_cleanup(ldap_get_attributes($_ldapconn, ldap_first_entry($_ldapconn, $search_result)));
+	return $cache[$username] = ldap_array_cleanup(ldap_get_attributes($_ldapconn, ldap_first_entry($_ldapconn, $search_result)));
 }
 
 function ldap_get_group($group)
--- a/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/starthere.php	Fri Jan 18 19:59:50 2013 -0500
+++ b/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/starthere.php	Sat Feb 23 14:26:05 2013 -0500
@@ -7,9 +7,14 @@
 // define root directory
 define('ACCOUNTS', dirname(dirname(__FILE__)) . '/');
 
+// setup error handling policies
+ini_set('display_errors', 'on');
+error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
+
 // include required files
 require_once(ACCOUNTS . 'includes/functions.php');
 load_credentials();
+require_once(ACCOUNTS . 'includes/db.php');
 require_once(ACCOUNTS . 'includes/smarty/Smarty.class.php');
 require_once(ACCOUNTS . 'includes/template-wrapper.php');
 require_once(ACCOUNTS . 'includes/ldap.php');