apt-get update errors on debian server – signature couldn’t be verified because the public key is not available

I was running into Warnings and Errors while executing an apt-get update on a debian server.

W: GPG error: http://ftp.us.debian.org squeeze Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY AED4B06F473041FA NO_PUBKEY 64481591B98321F9
W: GPG error: http://security.debian.org squeeze/updates Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY AED4B06F473041FA
W: GPG error: http://ftp.us.debian.org squeeze-updates Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 8B48AD6246925553

In computers based on a Debian operating system that uses Linux kernel, error messages may come up like ‘NO_PUBKEY’.This happens while using the Apt-Get command line tool and this error is associated with the tool’s update feature. The new feature in the Apt-Get package management tool guarantees the authenticity of the server before updating the Debian OS. That’s why the error ‘NO_PUBKEY’ pops up. This problem can be solved by keying in the appropriate commands. The fix for this is to re-download the keys using the hexidecimal numbers given in the error.

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 64481591B98321F9 AED4B06F473041FA 8B48AD6246925553

You should see a similar looking out put

Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver keyserver.ubuntu.com --recv-keys 64481591B98321F9 AED4B06F473041FA 8B48AD6246925553

Postgres login without password

In order to login to postgres without a password, you need to create a .pgpass file in your home directory. The contents of the file should look similar to:

localhost:5432:primarydb:root:RootPassword
localhost:5432:other_db:otheruser:Other_Password

After creating the file, make sure that the file permissions are 600 or less

chmod 600 ~/.pgpass

After the file is setup, you can login to the database just by executing the following command (or alternatively setup an alias in your bash file to that would execute the command. If you’re applying shortcuts, why not get maximum results):

psql -hlocalhost -Uotheruser other_db

jQuery – bind checkbox and text input such that input allowed only if box is checked

Using jQuery to bind together a checkbox and input text field. The text input is only editable if the check box is selected. Otherwise, reset the textbox value and make it readonly.

$(document).ready(function(){
	var $objFSM = $( "#id_file_size_max" ); // Checkbox
	var $objMAFS = $( "#id_max_allowed_file_size" ); // Input TEXT
	// Check checked property of FSM and set readonly property of MAFS accordingly on form load
	if($objFSM.attr('checked')) { // Make MAFS editable
		$objMAFS.attr('readonly', false);
	}
	else { // Make MAFS readonly
		$objMAFS.attr('readonly', true);
	}
	
	$objFSM.click(function() {
		if($objFSM.attr('checked')) { // Make MAFS editable
			$objMAFS.attr('readonly', false);
		}
		else { // Make MAFS readonly
			$objMAFS.val('');
			$objMAFS.attr('readonly', true);
		}
		
	});
});

PHP – Validate JSON

Here’s what I did to validate if the JSON is valid. I found tutorials on using json_last_error(), but I found this approach to be much simpler.

$arr_license = json_decode($_POST['json_license'], true);
if(empty($arr_license))
  exit('{"ReturnType":"Error", "ReturnCode":"101", "ReturnMessage":"json_license must be a valid JSON string."}');
//-- Proceed from here with valid $arr_license

PHP API Framework supporting multiple functions and arguments

Here’s a scalable framework to make API’s that support multiple functions. Each function in turn has mandatory and/or optional parameters. The parameters are combined to create a hash for security and it’s dynamically generated based on the function definition.

<?
define('SECRET', 'p24gvszaox6a1b2c392uewkyhezyqwk9');

class ApiFuncs {
	const FileDownload			=	'file-download';
	const FileAdd				=	'file-add';
	const MailSend				=	'mail-send';
	const FoldersList			=	'folders-list';
	const LastModFileFromFolder		=	'getLastModFileFromFolder';
	const AllFilesFromFolder		=	'getAllFilesFromFolder';
}

$INPUT_PARAMS = array (
	ApiFuncs::FileDownload	=> array('mandatory' => array('fileId')),
	ApiFuncs::FileAdd	=> array('mandatory' => array('fileContent','fileName','folderId')),
	ApiFuncs::MailSend	=> array('mandatory' => array('csv_fileIds', 'to', 'subject'),
							'optional' => array('message', 'cc', 'bcc')),
	ApiFuncs::FoldersList	=> array('mandatory' => array('folderId')),
	ApiFuncs::LastModFileFromFolder	=> array('mandatory' => array('folderId'),
						'optional' => array('fileExtension')),
	ApiFuncs::AllFilesFromFolder	=> array('mandatory' => array('folderId'),
						'optional' => array('fileExtension')),
);

validate();

//**************************************
//-- API Functions
//**************************************

function validate() {
	global $_POST, $INPUT_PARAMS;
	try {
		if(!array_key_exists('function', $_POST) || !array_key_exists($_POST['function'], $INPUT_PARAMS)) {
			exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidFunction)));
		}
		$token_params = $INPUT_PARAMS[$_POST['function']]['mandatory'];
		foreach($token_params as $key => $value) {
			if(!array_key_exists($value, $_POST)) {
				exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::MissingApiParam, $value)));
			}
		}
	}
	catch(Exception $e){
		exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidAPICall, $e)));
	}
	
	validate_values();
	verify_token();
}

function validate_values() {
	global $_POST, $INPUT_PARAMS;
	switch($_POST['function']) {
		case ApiFuncs::FileDownload:
			if(!preg_match('/^\d+$/',$_POST['fileId'])) {
				exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidFileId, $_POST['fileId'])));
			}
			break;
		default:
			break;
	}
}

function verify_token() {
	global $_POST, $INPUT_PARAMS, $SECRET;
	$token_params = $INPUT_PARAMS[$_POST['function']]['mandatory'];
	$hash = "function=".$_POST['function'];
	foreach($token_params as $key => $value) {
		$hash .= $value.'='.$_POST[$value];
	}
	$hash .= "secret=".SECRET;
	if(getSHA1Token($hash) != $_POST['token']) {
		exit(json_encode(getSimpleDictResponse(ReturnType::Error, ReturnCode::InvalidToken)));
	}
}
?>