blob: 35069080806f120c428f6f015dac4757e85b7b16 (
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
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
|
#!/usr/bin/env bash
###
### arching-kaos-tools
### Tools to interact and build an Arching Kaos Infochain
### Copyright (C) 2021 - 2025 kaotisk
###
### This program is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### (at your option) 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 General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program. If not, see <http://www.gnu.org/licenses/>.
###
source $AK_LIBDIR/_ak_lib_load
_ak_lib_load _ak_log
_ak_lib_load _ak_script
_ak_lib_load _ak_gpg
_ak_lib_load _ak_fs
_ak_lib_load _ak_zchain
AK_NS_DIR="${AK_WORKDIR}/akns"
_ak_check_and_create_dir ${AK_NS_DIR}
function _ak_ns_create(){
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key name was given"
exit 1
fi
if [ "$(echo -n $1| tr -d '[:alnum:]')" != "" ]
then
_ak_log_error "Name $1 is not allowed. Use only letters and numbers"
exit 1
fi
keyname="$1@keynames.kaos.kaos"
_ak_log_info "Checking for ${keyname}..."
_ak_gpg_list_secret_keys_long | grep ${keyname} > /dev/null 2>&1
if [ $? -ne 0 ]
then
_ak_log_info "Creating ${keyname}"
_ak_gpg_create_key ${keyname}
else
_ak_log_error "Name ${keyname} already exists"
exit 1
fi
}
function _ak_ns_list(){
_ak_gpg_list_secret_keys_long | grep '@keynames.kaos.kaos' | cut -d ' ' -f 2
}
function _ak_ns_list_long(){
_ak_gpg_list_secret_keys_long \
| grep '@keynames.kaos.kaos' \
| while read key name
do
printf '%s %s %s\n' "${key}" "$(_ak_ns_encode_key ${key})" "${name}"
done
}
function _ak_ns_resolve_from_key(){
# $1; exit;;
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key was given"
exit 1
fi
key="$1"
_ak_log_info "${key} was given"
if [ ! -f ${AK_NS_DIR}/${key} ]
then
_ak_log_error "${key} was not found"
exit 1
fi
_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${key}
}
function _ak_ns_resolve_from_key_with_proof(){
# $1; exit;;
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key was given"
exit 1
fi
key="$1"
_ak_log_info "${key} was given"
if [ ! -f ${AK_NS_DIR}/${key}.map ]
then
_ak_log_error "${key} was not found"
exit 1
fi
cat ${AK_NS_DIR}/${key}.map
}
function _ak_ns_resolve_from_key_with_proof_json(){
# $1; exit;;
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key was given"
exit 1
fi
key="$1"
json=$(_ak_ns_resolve_from_key_with_proof $key | while read proof resolved; do printf '{"fingerprint":"%s","proof":"%s","resolved":"%s"}' "$1" "$proof" "$resolved"; done)
if [ $? -ne 0 ]
then
_ak_log_error "Something happened"
exit 1
fi
echo $json
}
function _ak_ns_encode_key(){
# $1; exit;;
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key was given"
exit 1
fi
key="$1"
_ak_log_info "${key} was given"
printf '%s' "$(echo -n ${key}|xxd -r -p|base64)"
}
function _ak_ns_decode_key(){
# $1; exit;;
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key was given"
exit 1
fi
key="$1"
_ak_log_info "${key} was given"
printf '%s' "$(echo -n ${key}|base64 -d|xxd -p|tr '[:lower:]' '[:upper:]')"
}
function _ak_ns_resolve_from_name(){
# $1; exit;;
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No name was given"
exit 1
fi
key_name="$1"
_ak_log_info "${key_name} was given"
if [ "${key_name}" == "zchain" ] || [ "${key_name}" == "zconfig" ]
then
_ak_log_info "${key_name} search on local secret keychain"
key="$(_ak_gpg_list_secret_keys_long | grep ${key_name}'@keynames.kaos.kaos' | cut -d ' ' -f 1)"
if [ ! -n "${key}" ]
then
_ak_log_error "${key_name} was not found locally"
exit 1
fi
_ak_ns_resolve_from_key ${key}
fi
}
function _ak_ns_publish(){
# $1 $2; exit;;
_ak_not_implemented "${FUNCNAME}"
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key was given"
exit 1
fi
if [ ! -z $2 ] || [ ! -n "$2" ]
then
_ak_log_error "No value was given"
exit 1
fi
if [ "$(echo -n $1| sed -e 's/[A-F0-9]\{40\}//')" != "" ]
then
_ak_log_error "$1 is not a valid key"
exit 1
fi
if [ "$(echo -n $2| sed -e 's/[a-f0-9]\{128\}//')" != "" ]
then
_ak_log_error "$2 is not a valid hash"
exit 1
fi
key="$(_ak_gpg_list_secret_keys | grep $1)"
value="$2"
if [ ! -n "${key}" ]
then
_ak_log_error "Key $1 was not found"
exit 1
fi
_ak_log_info "Key $1 was found"
if [ ! -z $2 ] || [ -n "$2" ]
then
_ak_gpg_sign_clear_with_key $key
fi
}
function _ak_ns_publish2name(){
# $1 $2; exit;;
_ak_not_implemented "${FUNCNAME}"
if [ -z $1 ] || [ ! -n "$1" ]
then
_ak_log_error "No key name was given"
exit 1
fi
if [ "$(echo -n $1| tr -d '[:alnum:]')" != "" ]
then
_ak_log_error "Name $1 is not allowed. Use only letters and numbers"
exit 1
fi
key="$(_ak_gpg_list_secret_keys_long | grep $1'@keynames.kaos.kaos' | cut -d ' ' -f 1)"
if [ ! -n "${key}" ]
then
_ak_log_error "No key found with name $1"
exit 1
fi
_ak_ns_publish ${key} $2
}
function _ak_ns_publish_zchain(){
zlatest="$(_ak_zchain_get_latest)"
zchain_key="$(_ak_gpg_list_secret_keys_long | grep 'zchain@keynames.kaos.kaos' | cut -d ' ' -f 1)"
if [ ! -n "${zchain_key}" ]
then
_ak_log_warning "zchain_key not there"
_ak_log_info "Creating zchain_key"
_ak_ns_create zchain
fi
zchain_key="$(_ak_gpg_list_secret_keys_long | grep 'zchain@keynames.kaos.kaos' | cut -d ' ' -f 1)"
zlatest_file="$(_ak_make_temp_file)"
echo -n ${zlatest} > ${zlatest_file}
zlatest_csigned_file="$(_ak_make_temp_file)"
_ak_gpg_sign_clear_with_key ${zlatest_csigned_file} ${zlatest_file} ${zchain_key}
if [ -f ${AK_NS_DIR}/${zchain_key} ]
then
_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${zchain_key} >> ${AK_NS_DIR}/${zchain_key}.history
fi
signed_akfs_map=$(_ak_fs_import ${zlatest_csigned_file})
if [ -f ${AK_NS_DIR}/${zchain_key}.map ]
then
printf '%s %s\n' "$(echo -n $signed_akfs_map)" \
"$(_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${zchain_key})" \
>> ${AK_NS_DIR}/${zchain_key}.history_map
fi
mv ${zlatest_csigned_file} ${AK_NS_DIR}/${zchain_key}
printf '%s %s\n' "$(echo -n $signed_akfs_map)" \
"$(_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${zchain_key})" \
> ${AK_NS_DIR}/${zchain_key}.map
}
function _ak_ns_publish_config(){
zconfig="$(_ak_node_info_ipfs_hash)"
zconfig_key="$(_ak_gpg_list_secret_keys_long | grep 'zconfig@keynames.kaos.kaos' | cut -d ' ' -f 1)"
if [ ! -n "${zconfig_key}" ]
then
_ak_log_warning "zconfig_key not there"
_ak_log_info "Creating zconfig_key"
_ak_ns_create zconfig
fi
zconfig_key="$(_ak_gpg_list_secret_keys_long | grep 'zconfig@keynames.kaos.kaos' | cut -d ' ' -f 1)"
zconfig_file="$(_ak_make_temp_file)"
echo -n ${zconfig} > ${zconfig_file}
zconfig_csigned_file="$(_ak_make_temp_file)"
_ak_gpg_sign_clear_with_key ${zconfig_csigned_file} ${zconfig_file} ${zconfig_key}
if [ -f ${AK_NS_DIR}/${zconfig_key} ]
then
_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${zconfig_key} >> ${AK_NS_DIR}/${zconfig_key}.history
fi
signed_akfs_map=$(_ak_fs_import ${zconfig_csigned_file})
if [ -f ${AK_NS_DIR}/${zconfig_key}.map ]
then
printf '%s %s\n' "$(echo -n $signed_akfs_map)" \
"$(_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${zconfig_key})" \
>> ${AK_NS_DIR}/${zconfig_key}.history_map
fi
mv ${zconfig_csigned_file} ${AK_NS_DIR}/${zconfig_key}
printf '%s %s\n' "$(echo -n $signed_akfs_map)" \
"$(_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${zconfig_key})" \
> ${AK_NS_DIR}/${zconfig_key}.map
}
function _ak_ns_resolve_all_keys(){
tmpdir="$(_ak_make_temp_directory)"
cd $tmpdir
ip a | grep inet6 | sed 's/ *inet6 //g' | cut -d '/' -f 1 > ip_list
# cat /dev/null > ip_list
ak network -p 2>/dev/null| jq -r '.[].incoming.ip' | while read ip ; do if [ $ip != null ]; then echo $ip ;fi;done > peer_list
cat ip_list | while read ip ; do sed -i 's/^'$ip'$//g' peer_list; done
# cat peer_list | while read ip ; do ak network -p | jq --arg ip "$ip" '.[] | select(.incoming.ip == $ip )' | jq -r '.node_info.keys.[]'> key_list.$ip; done
cat peer_list | while read ip ; do ak network -p | jq --arg ip "$ip" '.[] | select(.incoming.ip == $ip )' | jq '.node_info.keymaps.[]'> keymaps_list.$ip; done
# cat peer_list | while read ip
# do
# cat key_list.$ip | while read key
# do
# ak fs --net-cat-from-map $key > $key && ak fs --import $key
# done
# done
# cat peer_list | while read ip
# do
# proof="$(cat keymaps_list.$ip | jq -r '.')"
# while read key
# do
# ak fs --net-cat-from-map $key > $key && ak fs --import $key
# done
# done
cat peer_list | while read ip
do
cat keymaps_list.$ip | jq -r '.fingerprint' | while read key
do
map="$(cat keymaps_list.$ip | jq -r 'select(.fingerprint == "'$key'")|.map')"
ak fs --net-cat-from-map $map > $key
ak fs --import $key
curl -s http://[$ip]:8610/v0/ns_get/$key > $key.reply
proof="$(cat $key.reply | jq -r '.proof')"
resolved="$(cat $key.reply | jq -r '.resolved')"
key="$(cat $key.reply | jq -r '.key')"
ak fs --net-cat-from-map $proof > $key.proof
_ak_gpg_verify_clear_signature $key.proof
if [ $? -ne 0 ]
then
_ak_log_error "Couldn't verify"
exit 1
fi
if [ -f ${AK_NS_DIR}/${key} ]
then
_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${key} >> ${AK_NS_DIR}/${key}.history
fi
signed_akfs_map=$(_ak_fs_import ${key}.proof)
if [ -f ${AK_NS_DIR}/${key}.map ]
then
printf '%s %s\n' "$(echo -n $signed_akfs_map)" \
"$(_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${key})" \
>> ${AK_NS_DIR}/${key}.history_map
fi
mv ${key}.proof ${AK_NS_DIR}/${key}
printf '%s %s\n' "$(echo -n $proof)" \
"$(_ak_gpg_verify_clear_signature ${AK_NS_DIR}/${key})" \
> ${AK_NS_DIR}/${key}.map
done
done
}
_ak_log_debug "_ak_ns loaded $(caller)"
|