6 var SSHPubkeyDecoder = L.Class.singleton({
7 lengthDecode: function(s, off)
9 var l = (s.charCodeAt(off++) << 24) |
10 (s.charCodeAt(off++) << 16) |
11 (s.charCodeAt(off++) << 8) |
14 if (l < 0 || (off + l) > s.length)
22 var parts = s.split(/\s+/);
27 try { key = atob(parts[1]); } catch(e) {}
34 len = this.lengthDecode(key, off);
39 var type = key.substr(off + 4, len);
40 if (type !== parts[0])
45 var len1 = off < key.length ? this.lengthDecode(key, off) : 0;
50 if (type.indexOf('ecdsa-sha2-') === 0) {
51 curve = key.substr(off + 4, len1);
53 if (!len1 || type.substr(11) !== curve)
57 curve = curve.replace(/^nistp(\d+)$/, 'NIST P-$1');
62 var len2 = off < key.length ? this.lengthDecode(key, off) : 0;
72 var comment = parts.slice(2).join(' '),
73 fprint = parts[1].length > 68 ? parts[1].substr(0, 33) + '…' + parts[1].substr(-34) : parts[1];
78 return { type: 'RSA', bits: len2 * 8, comment: comment, fprint: fprint };
81 return { type: 'DSA', bits: len1 * 8, comment: comment, fprint: fprint };
84 return { type: 'ECDH', curve: 'Curve25519', comment: comment, fprint: fprint };
87 return { type: 'ECDSA', curve: curve, comment: comment, fprint: fprint };
95 function renderKeys(keys) {
96 var list = document.querySelector('.cbi-dynlist');
98 while (!matchesElem(list.firstElementChild, '.add-item'))
99 list.removeChild(list.firstElementChild);
101 keys.forEach(function(key) {
102 var pubkey = SSHPubkeyDecoder.decode(key);
104 list.insertBefore(E('div', {
109 E('strong', pubkey.comment || _('Unnamed key')), E('br'),
111 '%s, %s'.format(pubkey.type, pubkey.curve || _('%d Bit').format(pubkey.bits)),
112 E('br'), E('code', pubkey.fprint)
114 ]), list.lastElementChild);
117 if (list.firstElementChild === list.lastElementChild)
118 list.insertBefore(E('p', _('No public keys present yet.')), list.lastElementChild);
121 function saveKeys(keys) {
122 return fs.write('/etc/dropbear/authorized_keys', keys.join('\n') + '\n', 384 /* 0600 */)
123 .then(renderKeys.bind(this, keys))
124 .catch(function(e) { ui.addNotification(null, E('p', e.message)) })
125 .finally(ui.hideModal);
128 function addKey(ev) {
129 var list = findParent(ev.target, '.cbi-dynlist'),
130 input = list.querySelector('input[type="text"]'),
131 key = input.value.trim(),
132 pubkey = SSHPubkeyDecoder.decode(key),
138 list.querySelectorAll('.item').forEach(function(item) {
139 keys.push(item.getAttribute('data-key'));
142 if (keys.indexOf(key) !== -1) {
143 ui.showModal(_('Add key'), [
144 E('div', { class: 'alert-message warning' }, _('The given SSH public key has already been added.')),
145 E('div', { class: 'right' }, E('div', { class: 'btn', click: L.hideModal }, _('Close')))
149 ui.showModal(_('Add key'), [
150 E('div', { class: 'alert-message warning' }, _('The given SSH public key is invalid. Please supply proper public RSA or ECDSA keys.')),
151 E('div', { class: 'right' }, E('div', { class: 'btn', click: L.hideModal }, _('Close')))
158 return saveKeys(keys).then(function() {
159 var added = list.querySelector('[data-key="%s"]'.format(key));
161 added.classList.add('flash');
166 function removeKey(ev) {
167 var list = findParent(ev.target, '.cbi-dynlist'),
168 delkey = ev.target.getAttribute('data-key'),
171 list.querySelectorAll('.item').forEach(function(item) {
172 var key = item.getAttribute('data-key');
177 L.showModal(_('Delete key'), [
178 E('div', _('Do you really want to delete the following SSH key?')),
180 E('div', { class: 'right' }, [
181 E('div', { class: 'btn', click: L.hideModal }, _('Cancel')),
183 E('div', { class: 'btn danger', click: ui.createHandlerFn(this, saveKeys, keys) }, _('Delete key')),
188 function dragKey(ev) {
189 ev.stopPropagation();
191 ev.dataTransfer.dropEffect = 'copy';
194 function dropKey(ev) {
195 var file = ev.dataTransfer.files[0],
196 input = ev.currentTarget.querySelector('input[type="text"]'),
197 reader = new FileReader();
200 reader.onload = function(rev) {
201 input.value = rev.target.result.trim();
206 reader.readAsText(file);
209 ev.stopPropagation();
213 function handleWindowDragDropIgnore(ev) {
219 return fs.lines('/etc/dropbear/authorized_keys').then(function(lines) {
220 return lines.filter(function(line) {
221 return line.match(/^(ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2)\b/) != null;
226 render: function(keys) {
227 var list = E('div', { 'class': 'cbi-dynlist', 'dragover': dragKey, 'drop': dropKey }, [
228 E('div', { 'class': 'add-item' }, [
230 'class': 'cbi-input-text',
232 'placeholder': _('Paste or drag SSH key file…') ,
233 'keydown': function(ev) { if (ev.keyCode === 13) addKey(ev) }
236 'class': 'cbi-button',
237 'click': ui.createHandlerFn(this, addKey)
242 keys.forEach(L.bind(function(key) {
243 var pubkey = SSHPubkeyDecoder.decode(key);
245 list.insertBefore(E('div', {
247 click: ui.createHandlerFn(this, removeKey),
250 E('strong', pubkey.comment || _('Unnamed key')), E('br'),
252 '%s, %s'.format(pubkey.type, pubkey.curve || _('%d Bit').format(pubkey.bits)),
253 E('br'), E('code', pubkey.fprint)
255 ]), list.lastElementChild);
258 if (list.firstElementChild === list.lastElementChild)
259 list.insertBefore(E('p', _('No public keys present yet.')), list.lastElementChild);
261 window.addEventListener('dragover', handleWindowDragDropIgnore);
262 window.addEventListener('drop', handleWindowDragDropIgnore);
264 return E('div', {}, [
265 E('h2', _('SSH-Keys')),
266 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.')),
267 E('div', { 'class': 'cbi-section-node' }, list)
271 handleSaveApply: null,