accountmanager.sh 10.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
#!/bin/sh

AMNGRDB="${AMNGRDB:-${HOME}/.account.db}"
AMNGRID="${AMNGRID:-${USER}@${HOSTNAME}}"
AMNGRPWLEN="${AMNGRPWLEN:-10}"

alias random="cat /dev/urandom"

function rand_printable() {
	local OPT OPTARG OPTIND
	local USAGE="$(printf "Usage: %s [-h] [len]" "$0")"

	while getopts h OPT
	do
		case $OPT in
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	if ! [[ "${1}" =~ ^[0-9]*$ ]]
	then
		echo "${USAGE}"
		return 1
	fi

	echo -n "$(random | tr -dc ' !#-&(-~' | head -c${1:-512})"
}

function amngrdbinit() {
	local OPT OPTARG OPTIND DB
	local USAGE="$(printf "Usage: %s [-h] [dbfile]" "$0")"

	while getopts h OPT
	do
		case $OPT in
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	DB="${1:-${AMNGRDB}}"
	if [ ! \( -d "$(dirname "${DB}")" \) ]
	then
		echo "${USAGE}"
		return 1
	fi

	sqlite3 "${DB}" <<-EOD
		PRAGMA foreign_keys = ON;
		CREATE TABLE IF NOT EXISTS account (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		name VARCHAR(128) UNIQUE NOT NULL,
		desc TEXT DEFAULT NULL);
		CREATE TABLE IF NOT EXISTS cred (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		user VARCHAR(128) NOT NULL,
		pass TEXT NOT NULL);
		CREATE TABLE IF NOT EXISTS account_cred (
		account_id INTEGER,
		cred_id INTEGER,
		state SMALLINT(1) NOT NULL,
		FOREIGN KEY(account_id) REFERENCES account(id)
		ON UPDATE CASCADE ON DELETE CASCADE,
		FOREIGN KEY(cred_id) REFERENCES cred(id)
		ON UPDATE CASCADE ON DELETE CASCADE);
	EOD
}

function amngrdbdestroy() {
	local OPT OPTARG OPTIND DB CHECK
	local USAGE="$(printf "Usage: %s [-h] [dbfile]" "$0")"

	while getopts h OPT
	do
		case $OPT in
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	DB="${1:-${AMNGRDB}}"
	cat <<-EOT
		WARNING: You are about to remove your account data. There is no way to
		recover from this. Are you really sure you want to do this?
	EOT
	printf "Really remove %s (Yes|[No]): " "${DB}" && read CHECK
	CHECK="${CHECK:-No}"

	test "${CHECK}" == "Yes" && rm -f "${DB}"
}

function amngrid() {
	local OPT OPTARG OPTIND ACCOUNT_NAME DB
	local USAGE="$(printf "Usage: %s [-h] [-D dbfile] account" "$0")"

	while getopts hD: OPT
	do
		case $OPT in
			D)
				DB="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	ACCOUNT_NAME="${1}"
	DB="${DB:-${AMNGRDB}}"

	if [ -z "${ACCOUNT_NAME}" ]
	then
		echo "${USAGE}"
		return 1
	fi

	echo "$(sqlite3 "${DB}" <<-EOD
		SELECT id FROM account WHERE name='${ACCOUNT_NAME}';
	EOD
	)"
}

function amngrcrypt() {
	local OPT OPTARG OPTIND PLAIN RECIPIENT
	local USAGE="$(printf "Usage: %s [-h] [-r recipient] password" "$0")"

	while getopts hr: OPT
	do
		case $OPT in
			r)
				RECIPIENT="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	RECIPIENT="${RECIPIENT:-${AMNGRID}}"
	PLAIN="${1}"

	if [ -z "${PLAIN}" ]
	then
		echo "${USAGE}"
		return 1
	fi

	echo -n "${PLAIN}" | gpg -aeqr "${RECIPIENT}"
}

function amngrgen() {
	local OPT OPTARG OPTIND RECIPIENT
	local USAGE="$(printf "Usage: %s [-h] [-r recipient] [len]" "$0")"

	while getopts hr: OPT
	do
		case $OPT in
			r)
				RECIPIENT="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	local LEN="${1:-${AMNGRPWLEN}}"

	if [ "${RECIPIENT}" ]
	then
		amngrcrypt -r "${RECIPIENT}" "$(rand_printable "${LEN}")"
	else
		amngrcrypt "$(rand_printable "${LEN}")"
	fi
}

function amngradd() {
	local OPT OPTARG OPTIND DESCRIPTION RECIPIENT DB
	local USAGE="$(cat <<-EOT
		Usage: $0 [-h] [-d description] [-D dbfile] [-r recipient]
		       account [username [password]]
	EOT
	)"

	while getopts d:D:hr: OPT
	do
		case $OPT in
			d)
				DESCRIPTION="${OPTARG}";;
			D)
				DB="${OPTARG}";;
			r)
				RECIPIENT="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	RECIPIENT="${RECIPIENT:-${AMNGRID}}"
	DB="${DB:-${AMNGRDB}}"

	local ACCOUNT_NAME="${1}"
	DESCRIPTION="${DESCRIPTION:-NO DESCRIPTION}"

	if [ -z "${ACCOUNT_NAME}" ]
	then
		echo "No account name given."
		echo "${USAGE}"
		return 1
	fi

	if [ "${2}" ]
	then
		USER="${2}"
	else
		USER="$(amngrgetuser -D "${DB}" -c "${ACCOUNT_NAME}")"
	fi

	if [ -z "${USER}" ]
	then
		printf "Can't find current user for account %s\n" "${ACCOUNT_NAME}"
		echo "${USAGE}"
		return 2
	fi

	if [ "${3}" ]
	then
		PASSWORD="$(amngrcrypt -r "${RECIPIENT}" "${3}")"
	else
		PASSWORD="$(amngrgen -r "${RECIPIENT}")"
		echo -n "${PASSWORD}" | gpg -dq | xclip -i
	fi

	if [ -z "${PASSWORD}" ]
	then
		printf "Failed to create encrypted password for account %s\n" \
			"${ACCOUNT_NAME}"
		echo "${USAGE}"
		return 3
	fi

	local ACCOUNT_ID="$(amngrid -D "${DB}" "${ACCOUNT_NAME}")"
	local QUERY="$(cat <<-EOD
		PRAGMA foreign_keys = ON;
		BEGIN TRANSACTION;
		INSERT INTO cred (user, pass)
		VALUES ('${USER}', '${PASSWORD}');
	EOD
	)"

	if [ -z "${ACCOUNT_ID}" ]
	then
		QUERY="$(cat <<-EOD
			${QUERY}
			INSERT INTO account (name, desc)
			VALUES ('${ACCOUNT_NAME}', '${DESCRIPTION}');
		EOD
		)"
	else
		QUERY="$(cat <<-EOD
			${QUERY}
			UPDATE account_cred SET state=0
			WHERE account_id=(SELECT id FROM account
			WHERE name='${ACCOUNT_NAME}' AND state=2);
			UPDATE account_cred SET state=2
			WHERE account_id=(SELECT id FROM account
			WHERE name='${ACCOUNT_NAME}' AND state=1);
		EOD
		)"
	fi

	QUERY="$(cat <<-EOD
		${QUERY}
		INSERT INTO account_cred (account_id, cred_id, state)
		VALUES ((SELECT id FROM account WHERE name='${ACCOUNT_NAME}'),
		(SELECT id FROM cred WHERE user='${USER}' AND pass='${PASSWORD}'),
		1);
		COMMIT TRANSACTION;
	EOD
	)"

	sqlite3 "${DB}" "${QUERY}"
}

function amngrgetuser() {
	local OPT OPTARG OPTIND DB
	local STDOUT=0
	local USAGE="$(cat <<-EOT
		Usage: $0 [-h] [-c] [-D dbfile] account [state]
	EOT
	)"

	while getopts hcD: OPT
	do
		case $OPT in
			c)
				STDOUT=1;;
			D)
				DB="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	local ACCOUNT_NAME="${1}"
	local STATE="${2:-"1"}"
	DB="${DB:-${AMNGRDB}}"

	if [ -z "${ACCOUNT_NAME}" ]
	then
		echo "${USAGE}"
		return 1
	fi
	test "${STATE}" != "1" -a "${STATE}" != "2" && STATE="1"

	if [ $STDOUT -eq 0 ]
	then
		sqlite3 "${DB}" <<-EOD |\
			awk 'NR>1{print p}{p=$0}END{ORS="";print}' | xclip -i
			SELECT user FROM account
			JOIN account_cred ON account.id=account_cred.account_id
			JOIN cred ON cred.id=account_cred.cred_id
			WHERE name='${ACCOUNT_NAME}' AND state=${STATE};
		EOD
	else
		sqlite3 "${DB}" <<-EOD |\
			awk 'NR>1{print p}{p=$0}END{ORS="";print}'
			SELECT user FROM account
			JOIN account_cred ON account.id=account_cred.account_id
			JOIN cred ON cred.id=account_cred.cred_id
			WHERE name='${ACCOUNT_NAME}' AND state=${STATE};
		EOD
	fi
}

function amngrgetolduser() {
	amngrgetuser "$@" "2"
}

function amngrgetpass() {
	local OPT OPTARG OPTIND DB
	local USAGE="$(printf "Usage: %s [-h] [-D dbfile] account [state]" "$0")"

	while getopts hD: OPT
	do
		case $OPT in
			D)
				DB="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	local ACCOUNT_NAME="${1}"
	local STATE="${2:-1}"
	DB="${DB:-${AMNGRDB}}"

	if [ -z "${ACCOUNT_NAME}" ]
	then
		echo "${USAGE}"
		return 1
	fi
	test "${STATE}" != "1" -a "${STATE}" != "2" && STATE="1"

	sqlite3 "${DB}" <<-EOD |\
		awk 'NR>1{print p}{p=$0}END{ORS="";print}' | gpg -dq | xclip -i
		SELECT pass FROM account
		JOIN account_cred ON account.id=account_cred.account_id
		JOIN cred ON cred.id=account_cred.cred_id
		WHERE name='${ACCOUNT_NAME}' AND state=${STATE};
	EOD
}

function amngrgetoldpass() {
	amngrgetpass "$@" "2"
}

function amngrrename() {
	local OPT OPTARG OPTIND DB
	local USAGE="$(cat <<-EOT
		Usage: $0 [-h] [-D dbfile] old_account new_account
	EOT
	)"

	while getopts hD: OPT
	do
		case $OPT in
			D)
				DB="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	local OLD_NAME="${1}"
	local NEW_NAME="${2}"
	DB="${DB:-${AMNGRDB}}"

	if [ -z "${OLD_NAME}" -o -z "${NEW_NAME}" ]
	then
		echo "${USAGE}"
		return 1
	fi

	sqlite3 "${DB}" <<-EOD
		UPDATE account SET name='${NEW_NAME}'
		WHERE name='${OLD_NAME}';
	EOD
}

function amngrdelete() {
	local OPT OPTARG OPTIND DB
	local USAGE="$(printf "Usage: %s [-h] [-D dbfile] account" "$0")"

	while getopts hD: OPT
	do
		case $OPT in
			D)
				DB="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	local ACCOUNT_NAME="${1}"
	DB="${DB:-${AMNGRDB}}"

	if [ -z "${ACCOUNT_NAME}" ]
	then
		echo "${USAGE}"
		return 1
	fi

	sqlite3 "${DB}" <<-EOD
		PRAGMA foreign_keys = ON;
		BEGIN TRANSACTION;
		DELETE FROM cred WHERE id IN (
		SELECT cred_id FROM account
		JOIN account_cred ON account.id=account_cred.account_id
		WHERE name='${ACCOUNT_NAME}');
		DELETE FROM account WHERE name='${ACCOUNT_NAME}';
		COMMIT TRANSACTION;
	EOD
}

function amngrsearch() {
	local OPT OPTARG OPTIND SEPARATOR PATTERN
	local USAGE="$(cat <<-EOT
		Usage: $0 [-h] [-D dbfile] [-s separator] [pattern]
	EOT
	)"

	while getopts hD:s: OPT
	do
		case $OPT in
			D)
				DB="${OPTARG}";;
			s)
				SEPARATOR="${OPTARG}";;
			h)
				echo "${USAGE}"
				return 0;;
			?)
				echo "${USAGE}"
				return 1;;
		esac
	done
	shift $(($OPTIND-1))

	PATTERN="${1}"
	SEPARATOR="${SEPARATOR:-" => "}"
	DB="${DB:-${AMNGRDB}}"

	sqlite3 -separator "${SEPARATOR}" "${DB}" <<-EOD
		SELECT name, user, desc FROM account
		JOIN account_cred ON account.id=account_cred.account_id
		JOIN cred ON cred.id=account_cred.cred_id
		WHERE state=1 AND (
		name LIKE '%${PATTERN}%' OR user LIKE '%${PATTERN}%' OR
		desc LIKE '%${PATTERN}%');
	EOD
}

case "$(basename -- "$0")" in
	random) random;;
	rand_printable) rand_printable "$@";;

	amngrdbinit) amngrdbinit "$@";;
	amngrdbdestroy) amngrdbdestroy "$@";;

	amngradd) amngradd "$@";;
	amngrcreate) amngrcreate "$@";;
	amngrcrypt) amngrcrypt "$@";;
	amngrdelete) amngrdelete "$@";;
	amngrgen) amngrgen "$@";;
	amngrgetoldpass) amngrgetoldpass "$@";;
	amngrgetolduser) amngrgetolduser "$@";;
	amngrgetpass) amngrgetpass "$@";;
	amngrgetuser) amngrgetuser "$@";;
	amngrid) amngrid "$@";;
	amngrlist) amngrlist "$@";;
	amngrrename) amngrrename "$@";;
	amngrsearch) amngrsearch "$@";;

	*) ;;
esac

# vim: set ft=sh ts=4 sw=4: