4 var SSHPubkeyDecoder
= L
.Class
.singleton({
5 lengthDecode: function(s
, off
)
7 var l
= (s
.charCodeAt(off
++) << 24) |
8 (s
.charCodeAt(off
++) << 16) |
9 (s
.charCodeAt(off
++) << 8) |
12 if (l
< 0 || (off
+ l
) > s
.length
)
20 var parts
= s
.split(/\s+/);
25 try { key
= atob(parts
[1]); } catch(e
) {}
32 len
= this.lengthDecode(key
, off
);
37 var type
= key
.substr(off
+ 4, len
);
38 if (type
!== parts
[0])
43 var len1
= off
< key
.length
? this.lengthDecode(key
, off
) : 0;
48 if (type
.indexOf('ecdsa-sha2-') === 0) {
49 curve
= key
.substr(off
+ 4, len1
);
51 if (!len1
|| type
.substr(11) !== curve
)
55 curve
= curve
.replace(/^nistp(\d+)$/, 'NIST P-$1');
60 var len2
= off
< key
.length
? this.lengthDecode(key
, off
) : 0;
70 var comment
= parts
.slice(2).join(' '),
71 fprint
= parts
[1].length
> 68 ? parts
[1].substr(0, 33) + '…' + parts
[1].substr(-34) : parts
[1];
76 return { type
: 'RSA', bits
: len2
* 8, comment
: comment
, fprint
: fprint
};
79 return { type
: 'DSA', bits
: len1
* 8, comment
: comment
, fprint
: fprint
};
82 return { type
: 'ECDH', curve
: 'Curve25519', comment
: comment
, fprint
: fprint
};
85 return { type
: 'ECDSA', curve
: curve
, comment
: comment
, fprint
: fprint
};
93 function renderKeys(keys
) {
94 var list
= document
.querySelector('.cbi-dynlist');
96 while (!matchesElem(list
.firstElementChild
, '.add-item'))
97 list
.removeChild(list
.firstElementChild
);
99 keys
.forEach(function(key
) {
100 var pubkey
= SSHPubkeyDecoder
.decode(key
);
102 list
.insertBefore(E('div', {
107 E('strong', pubkey
.comment
|| _('Unnamed key')), E('br'),
109 '%s, %s'.format(pubkey
.type
, pubkey
.curve
|| _('%d Bit').format(pubkey
.bits
)),
110 E('br'), E('code', pubkey
.fprint
)
112 ]), list
.lastElementChild
);
115 if (list
.firstElementChild
=== list
.lastElementChild
)
116 list
.insertBefore(E('p', _('No public keys present yet.')), list
.lastElementChild
);
119 function saveKeys(keys
) {
120 return fs
.write('/etc/dropbear/authorized_keys', keys
.join('\n') + '\n', 384 /* 0600 */)
121 .then(renderKeys
.bind(this, keys
))
122 .catch(function(e
) { L
.ui
.addNotification(null, E('p', e
.message
)) })
123 .finally(L
.ui
.hideModal
);
126 function addKey(ev
) {
127 var list
= findParent(ev
.target
, '.cbi-dynlist'),
128 input
= list
.querySelector('input[type="text"]'),
129 key
= input
.value
.trim(),
130 pubkey
= SSHPubkeyDecoder
.decode(key
),
136 list
.querySelectorAll('.item').forEach(function(item
) {
137 keys
.push(item
.getAttribute('data-key'));
140 if (keys
.indexOf(key
) !== -1) {
141 L
.ui
.showModal(_('Add key'), [
142 E('div', { class: 'alert-message warning' }, _('The given SSH public key has already been added.')),
143 E('div', { class: 'right' }, E('div', { class: 'btn', click
: L
.hideModal
}, _('Close')))
147 L
.ui
.showModal(_('Add key'), [
148 E('div', { class: 'alert-message warning' }, _('The given SSH public key is invalid. Please supply proper public RSA or ECDSA keys.')),
149 E('div', { class: 'right' }, E('div', { class: 'btn', click
: L
.hideModal
}, _('Close')))
156 return saveKeys(keys
).then(function() {
157 var added
= list
.querySelector('[data-key="%s"]'.format(key
));
159 added
.classList
.add('flash');
164 function removeKey(ev
) {
165 var list
= findParent(ev
.target
, '.cbi-dynlist'),
166 delkey
= ev
.target
.getAttribute('data-key'),
169 list
.querySelectorAll('.item').forEach(function(item
) {
170 var key
= item
.getAttribute('data-key');
175 L
.showModal(_('Delete key'), [
176 E('div', _('Do you really want to delete the following SSH key?')),
178 E('div', { class: 'right' }, [
179 E('div', { class: 'btn', click
: L
.hideModal
}, _('Cancel')),
181 E('div', { class: 'btn danger', click
: L
.ui
.createHandlerFn(this, saveKeys
, keys
) }, _('Delete key')),
186 function dragKey(ev
) {
187 ev
.stopPropagation();
189 ev
.dataTransfer
.dropEffect
= 'copy';
192 function dropKey(ev
) {
193 var file
= ev
.dataTransfer
.files
[0],
194 input
= ev
.currentTarget
.querySelector('input[type="text"]'),
195 reader
= new FileReader();
198 reader
.onload = function(rev
) {
199 input
.value
= rev
.target
.result
.trim();
204 reader
.readAsText(file
);
207 ev
.stopPropagation();
211 function handleWindowDragDropIgnore(ev
) {
215 return L
.view
.extend({
217 return fs
.lines('/etc/dropbear/authorized_keys').then(function(lines
) {
218 return lines
.filter(function(line
) {
219 return line
.match(/^ssh-/) != null;
224 render: function(keys
) {
225 var list
= E('div', { 'class': 'cbi-dynlist', 'dragover': dragKey
, 'drop': dropKey
}, [
226 E('div', { 'class': 'add-item' }, [
228 'class': 'cbi-input-text',
230 'placeholder': _('Paste or drag SSH key file…') ,
231 'keydown': function(ev
) { if (ev
.keyCode
=== 13) addKey(ev
) }
234 'class': 'cbi-button',
235 'click': L
.ui
.createHandlerFn(this, addKey
)
240 keys
.forEach(L
.bind(function(key
) {
241 var pubkey
= SSHPubkeyDecoder
.decode(key
);
243 list
.insertBefore(E('div', {
245 click
: L
.ui
.createHandlerFn(this, removeKey
),
248 E('strong', pubkey
.comment
|| _('Unnamed key')), E('br'),
250 '%s, %s'.format(pubkey
.type
, pubkey
.curve
|| _('%d Bit').format(pubkey
.bits
)),
251 E('br'), E('code', pubkey
.fprint
)
253 ]), list
.lastElementChild
);
256 if (list
.firstElementChild
=== list
.lastElementChild
)
257 list
.insertBefore(E('p', _('No public keys present yet.')), list
.lastElementChild
);
259 window
.addEventListener('dragover', handleWindowDragDropIgnore
);
260 window
.addEventListener('drop', handleWindowDragDropIgnore
);
262 return E('div', {}, [
263 E('h2', _('SSH-Keys')),
264 E('div', { 'class': 'cbi-section-descr' }, _('Public keys allow for the passwordless SSH logins with a higher security compared to the use of plain passwords. In order to upload a new key to the device, paste an OpenSSH compatible public key line or drag a <code>.pub</code> file into the input field.')),
265 E('div', { 'class': 'cbi-section-node' }, list
)
269 handleSaveApply
: null,