Last active
October 20, 2018 10:06
-
-
Save masatake/57a62c19b92a2ffdc1a9362e2e8c317f to your computer and use it in GitHub Desktop.
pem-op, splitting pem file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Copyright (c) 2018, Red Hat, Inc. | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
print_help() | |
{ | |
local prog=$1 | |
local status=$2 | |
echo "Usage:" | |
echo " $prog help|--help|-h" | |
echo " $prog count PEMFILE" | |
echo " $prog extract N PEMFILE" | |
echo " $prog list PEMFILE" | |
echo "Note:" | |
echo " N starts from 0." | |
echo " Use \"-\" for reading pemfile from the standard input." | |
exit $status | |
} | |
do_count_or_list () | |
{ | |
local file=$1 | |
shift | |
if [ $# -gt 0 ]; then | |
echo "too many arguments" 1>&2 | |
return 1 | |
fi | |
if [ -z "$file" ]; then | |
file=- | |
fi | |
if [ "$file" = '-' ]; then | |
file=/dev/stdin | |
fi | |
if [[ ! -e "$file" ]]; then | |
echo "no such file: $file" 1>&2 | |
return 1 | |
fi | |
if [ ! -r "$file" ]; then | |
echo "$file is not readable" 1>&2 | |
return 1 | |
fi | |
if [ -d "$file" ]; then | |
echo "$file must be a file" 1>&2 | |
return 1 | |
fi | |
grep -e '^-----BEGIN' < $file | |
return 0 | |
} | |
do_count () | |
{ | |
do_count_or_list "$@" | wc -l | |
} | |
do_list () | |
{ | |
do_count_or_list "$@" | sed -n -e 's/^-----BEGIN \+\(.\+\)-----$/\1/p' | cat -n | |
} | |
print_pem () | |
{ | |
local old_ifs=$IFS | |
local in_range=0 | |
local not_found=2 | |
while read LINE; do | |
case "$LINE" in | |
-----BEGIN*) | |
echo "$LINE" | |
in_range=1 | |
;; | |
-----END*) | |
echo "$LINE" | |
not_found=0 | |
break | |
;; | |
*) | |
if [[ $in_range == 1 ]]; then | |
echo "$LINE" | |
fi | |
;; | |
esac | |
done | |
return $not_found | |
} | |
do_extract () | |
{ | |
local n=$1 | |
local file=$2 | |
if [ -z "$n" ]; then | |
echo "no ordinal number is given" 1>&2 | |
return 1 | |
fi | |
if [[ ! "$n" =~ [0-9]+$ ]]; then | |
echo "wrong format as ordinal number: \"$n\"" 1>&2 | |
return 1 | |
fi | |
if [[ "$n" =~ ^0.* ]]; then | |
echo "The first argument START should not start from 0" 1>&2 | |
return 1 | |
fi | |
if [ -z "$file" ]; then | |
file=- | |
return 1 | |
fi | |
if [ "$file" = '-' ]; then | |
file=/dev/stdin | |
fi | |
if [[ ! -e "$file" ]]; then | |
echo "no such file: $file" 1>&2 | |
return 1 | |
fi | |
if [ ! -r "$file" ]; then | |
echo "$file is not readable" 1>&2 | |
return 1 | |
fi | |
if [ -d "$file" ]; then | |
echo "$file must be a file" 1>&2 | |
return 1 | |
fi | |
local status=2 | |
local out=/dev/null | |
n=$((n - 1)) | |
for ((i = 0; i <= n; i++)); do | |
if (( i == n )); then | |
out=/dev/stdout | |
fi | |
print_pem > $out | |
status=$? | |
done < $file | |
return $status | |
} | |
case $1 in | |
--help|-h|help) | |
print_help $0 0 | |
;; | |
count) | |
shift | |
do_count "$@" | |
exit $? | |
;; | |
list) | |
shift | |
do_list "$@" | |
exit $? | |
;; | |
extract) | |
shift | |
do_extract "$@" | |
exit $? | |
;; | |
*) | |
echo "unexpected argument $1" 1>&2 | |
print_help $0 1 1>&2 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment