aboutsummaryrefslogtreecommitdiff
path: root/keyringer
blob: 890468532d308d132f3f9212cdbd25d4b73666cd (plain)
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
#!/bin/bash
#
# Keyringer key management system.
#
# Copyright (C) 2010 Silvio Rhatto - rhatto at riseup.net
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

function keyringer_init {
  BASEDIR="$3"
  URL="$4"
  RECIPIENTS="$BASEDIR/config/recipients"
  OPTIONS="$BASEDIR/config/options"

  # Parse
  if [ -z "$BASEDIR" ]; then
    echo "Usage: $BASENAME <keyring> init <path> [url]"
    exit 1
  elif grep -q -e "^$KEYRING=" $CONFIG; then
    echo "Keyring $KEYRING already defined"
    exit 1
  fi

  # Setup
  if [ ! -z "$URL" ]; then
    git clone "$URL" "$BASEDIR"
    if [ "$?" != "0" ]; then
      echo "Error cloning remote $URL"
      exit 1
    fi
  else
    if [ -e "$BASEDIR" ]; then
      if [ ! -d "$BASEDIR/keys" ] || [ ! -e "$RECIPIENTS" ]; then
        echo "Invalid keyring $BASEDIR: incomplete installation"

        # A common mistake
        if [ -d "$BASEDIR/../keys" ]; then
          echo "You might try `cd $BASEDIR/.. && pwd` instead"
        fi

        exit 1
      fi
    else
      # Setup folders
      mkdir -p "$BASEDIR/"{config,keys}

      # Setup recipients
      keyringer_create_new_recipients "$RECIPIENTS/default"

      # Setup options
      touch "$OPTIONS"

      # Setup README
      echo "Keyring repository powered by http://git.sarava.org/?p=keyringer.git;a=summary" > "$BASEDIR/README"
      echo "" >> "$BASEDIR/README"
    fi

    # Secure
    chmod 600 "$RECIPIENTS"
  fi

  # Reparse basedir to force absolute folder
  BASEDIR="`cd $BASEDIR && pwd`"

  # Add entry
  chmod 700 "$BASEDIR"
  echo "$KEYRING=\"$BASEDIR\"" >> "$CONFIG"

  # Init
  if ! keyringer_is_git "$BASEDIR"; then
    keyringer_exec git "$BASEDIR" init
    keyringer_exec git "$BASEDIR" add .
    keyringer_exec git "$BASEDIR" commit -m Importing
  fi
}

function keyringer_dispatch {
  BASEDIR="`keyringer_config $KEYRING`"

  # Dispatch
  if [ ! -z "$BASEDIR" ]; then
    shift 2
    keyringer_exec "$ACTION" "$BASEDIR" $*
    exit $?
  else
    echo "No keydir configured for $KEYRING"
    exit 1
  fi
}

function keyringer_preferences {
  COMMAND="$3"

  if [ -z "$COMMAND" ]; then
    echo "Usage: keyringer <keyring> `basename $0` <command> [arguments]"
    exit 1
  fi
  
  # Create options file if old repository
  if [ ! -e "$PREFERENCES" ]; then
    echo "Creating preferences file..."
    touch "$PREFERENCES"
  fi
  
  if [ "$COMMAND" == "ls" ]; then
    cat "$PREFERENCES"
  elif [ "$COMMAND" == "edit" ]; then
    "$EDITOR" "$PREFERENCES"
  elif [ "$COMMAND" == "add" ]; then
    shift 3
    echo $* >> "$PREFERENCES"
  else
    printf "%s: No such command %s\n" "$BASENAME" "$COMMAND"
    exit 1
  fi
}

# Config
NAME="keyringer"
CONFIG="$HOME/.$NAME/config"
BASENAME="`basename $0`"
KEYRING="$1"
ACTION="$2"
ACTIONS="`dirname $(readlink -f $0)`/share/$NAME"

# Export preferences for other scripts
export PREFERENCES="`dirname $CONFIG`/$KEYRING"

# Load functions
LIB="`dirname $(readlink -f $0)`/lib/$NAME/functions"
source "$LIB" || exit 1

# Setup main configuration and load preferences
keyringer_config_load

if [ -z "$ACTION" ]; then
  printf "Usage: %s <keyring> <action> [arguments]\n" "$BASENAME"
  printf "Available commands: \n"
  ls $ACTIONS | sed -e 's/^/\t/'
  exit 1
fi

if [ "$ACTION" == "init" ]; then
  keyringer_init $*
elif [ "$ACTION" == "preferences" ]; then
  keyringer_preferences $*
elif keyringer_has_action "$ACTION"; then
  keyringer_dispatch $*
else
  echo "No such action $ACTION"
  exit 1
fi