5 var SSHPubkeyDecoder
= L
.Class
.singleton({
6 lengthDecode: function(s
, off
)
8 var l
= (s
.charCodeAt(off
++) << 24) |
9 (s
.charCodeAt(off
++) << 16) |
10 (s
.charCodeAt(off
++) << 8) |
13 if (l
< 0 || (off
+ l
) > s
.length
)
21 var parts
= s
.split(/\s+/);
26 try { key
= atob(parts
[1]); } catch(e
) {}
33 len
= this.lengthDecode(key
, off
);
38 var type
= key
.substr(off
+ 4, len
);
39 if (type
!== parts
[0])
44 var len1
= off
< key
.length
? this.lengthDecode(key
, off
) : 0;
49 if (type
.indexOf('ecdsa-sha2-') === 0) {
50 curve
= key
.substr(off
+ 4, len1
);
52 if (!len1
|| type
.substr(11) !== curve
)
56 curve
= curve
.replace(/^nistp(\d+)$/, 'NIST P-$1');
61 var len2
= off
< key
.length
? this.lengthDecode(key
, off
) : 0;
71 var comment
= parts
.slice(2).join(' '),
72 fprint
= parts
[1].length
> 68 ? parts
[1].substr(0, 33) + '…' + parts
[1].substr(-34) : parts
[1];
77 return { type
: 'RSA', bits
: len2
* 8, comment
: comment
, fprint
: fprint
};
80 return { type
: 'DSA', bits
: len1
* 8, comment
: comment
, fprint
: fprint
};
83 return { type
: 'ECDH', curve
: 'Curve25519', comment
: comment
, fprint
: fprint
};
86 return { type
: 'ECDSA', curve
: curve
, comment
: comment
, fprint
: fprint
};
94 function renderKeys(keys
) {
95 var list
= document
.querySelector('.cbi-dynlist');
97 while (!matchesElem(list
.firstElementChild
, '.add-item'))
98 list
.removeChild(list
.firstElementChild
);
100 keys
.forEach(function(key
) {
101 var pubkey
= SSHPubkeyDecoder
.decode(key
);
103 list
.insertBefore(E('div', {
108 E('strong', pubkey
.comment
|| _('Unnamed key')), E('br'),
110 '%s, %s'.format(pubkey
.type
, pubkey
.curve
|| _('%d Bit').format(pubkey
.bits
)),
111 E('br'), E('code', pubkey
.fprint
)
113 ]), list
.lastElementChild
);
116 if (list
.firstElementChild
=== list
.lastElementChild
)
117 list
.insertBefore(E('p', _('No public keys present yet.')), list
.lastElementChild
);
120 function saveKeys(keys
) {
121 return fs
.write('/etc/dropbear/authorized_keys', keys
.join('\n') + '\n', 384 /* 0600 */)
122 .then(renderKeys
.bind(this, keys
))
123 .catch(function(e
) { ui
.addNotification(null, E('p', e
.message
)) })
124 .finally(ui
.hideModal
);
127 function addKey(ev
) {
128 var list
= findParent(ev
.target
, '.cbi-dynlist'),
129 input
= list
.querySelector('input[type="text"]'),
130 key
= input
.value
.trim(),
131 pubkey
= SSHPubkeyDecoder
.decode(key
),
137 list
.querySelectorAll('.item').forEach(function(item
) {
138 keys
.push(item
.getAttribute('data-key'));
141 if (keys
.indexOf(key
) !== -1) {
142 ui
.showModal(_('Add key'), [
143 E('div', { class: 'alert-message warning' }, _('The given SSH public key has already been added.')),
144 E('div', { class: 'right' }, E('div', { class: 'btn', click
: L
.hideModal
}, _('Close')))
148 ui
.showModal(_('Add key'), [
149 E('div', { class: 'alert-message warning' }, _('The given SSH public key is invalid. Please supply proper public RSA or ECDSA keys.')),
150 E('div', { class: 'right' }, E('div', { class: 'btn', click
: L
.hideModal
}, _('Close')))
157 return saveKeys(keys
).then(function() {
158 var added
= list
.querySelector('[data-key="%s"]'.format(key
));
160 added
.classList
.add('flash');
165 function removeKey(ev
) {
166 var list
= findParent(ev
.target
, '.cbi-dynlist'),
167 delkey
= ev
.target
.getAttribute('data-key'),
170 list
.querySelectorAll('.item').forEach(function(item
) {
171 var key
= item
.getAttribute('data-key');
176 L
.showModal(_('Delete key'), [
177 E('div', _('Do you really want to delete the following SSH key?')),
179 E('div', { class: 'right' }, [
180 E('div', { class: 'btn', click
: L
.hideModal
}, _('Cancel')),
182 E('div', { class: 'btn danger', click
: ui
.createHandlerFn(this, saveKeys
, keys
) }, _('Delete key')),
187 function dragKey(ev
) {
188 ev
.stopPropagation();
190 ev
.dataTransfer
.dropEffect
= 'copy';
193 function dropKey(ev
) {
194 var file
= ev
.dataTransfer
.files
[0],
195 input
= ev
.currentTarget
.querySelector('input[type="text"]'),
196 reader
= new FileReader();
199 reader
.onload = function(rev
) {
200 input
.value
= rev
.target
.result
.trim();
205 reader
.readAsText(file
);
208 ev
.stopPropagation();
212 function handleWindowDragDropIgnore(ev
) {
216 return L
.view
.extend({
218 return fs
.lines('/etc/dropbear/authorized_keys').then(function(lines
) {
219 return lines
.filter(function(line
) {
220 return line
.match(/^ssh-/) != null;
225 render: function(keys
) {
226 var list
= E('div', { 'class': 'cbi-dynlist', 'dragover': dragKey
, 'drop': dropKey
}, [
227 E('div', { 'class': 'add-item' }, [
229 'class': 'cbi-input-text',
231 'placeholder': _('Paste or drag SSH key file…') ,
232 'keydown': function(ev
) { if (ev
.keyCode
=== 13) addKey(ev
) }
235 'class': 'cbi-button',
236 'click': ui
.createHandlerFn(this, addKey
)
241 keys
.forEach(L
.bind(function(key
) {
242 var pubkey
= SSHPubkeyDecoder
.decode(key
);
244 list
.insertBefore(E('div', {
246 click
: ui
.createHandlerFn(this, removeKey
),
249 E('strong', pubkey
.comment
|| _('Unnamed key')), E('br'),
251 '%s, %s'.format(pubkey
.type
, pubkey
.curve
|| _('%d Bit').format(pubkey
.bits
)),
252 E('br'), E('code', pubkey
.fprint
)
254 ]), list
.lastElementChild
);
257 if (list
.firstElementChild
=== list
.lastElementChild
)
258 list
.insertBefore(E('p', _('No public keys present yet.')), list
.lastElementChild
);
260 window
.addEventListener('dragover', handleWindowDragDropIgnore
);
261 window
.addEventListener('drop', handleWindowDragDropIgnore
);
263 return E('div', {}, [
264 E('h2', _('SSH-Keys')),
265 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.')),
266 E('div', { 'class': 'cbi-section-node' }, list
)
270 handleSaveApply
: null,