7 var isReadonlyView
= !L
.hasViewPermission() || null;
9 var SSHPubkeyDecoder
= baseclass
.singleton({
10 lengthDecode: function(s
, off
)
12 var l
= (s
.charCodeAt(off
++) << 24) |
13 (s
.charCodeAt(off
++) << 16) |
14 (s
.charCodeAt(off
++) << 8) |
17 if (l
< 0 || (off
+ l
) > s
.length
)
25 var parts
= s
.trim().match(/^((?:(?:^|,)[^ =,]+(?:=(?:[^ ",]+|"(?:[^"\\]|\\.)*"))?)+ +)?(ssh-dss|ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp[0-9]+) +([^ ]+)( +.*)?$/);
31 try { key
= atob(parts
[3]); } catch(e
) {}
38 len
= this.lengthDecode(key
, off
);
43 var type
= key
.substr(off
+ 4, len
);
44 if (type
!== parts
[2])
49 var len1
= off
< key
.length
? this.lengthDecode(key
, off
) : 0;
54 if (type
.indexOf('ecdsa-sha2-') === 0) {
55 curve
= key
.substr(off
+ 4, len1
);
57 if (!len1
|| type
.substr(11) !== curve
)
61 curve
= curve
.replace(/^nistp(\d+)$/, 'NIST P-$1');
66 var len2
= off
< key
.length
? this.lengthDecode(key
, off
) : 0;
76 var comment
= (parts
[4] || '').trim(),
77 fprint
= parts
[3].length
> 68 ? parts
[3].substr(0, 33) + '…' + parts
[3].substr(-34) : parts
[3];
80 (parts
[1] || '').trim().replace(/(?:^|,)([^ =,]+)(?:=(?:([^ ",]+)|"((?:[^"\\]|\\.)*)"))?/g, function(m
, k
, p
, q
) {
81 options
= options
|| {};
83 if (options
.hasOwnProperty(k
))
84 options
[k
] += ',' + (q
|| p
|| true);
86 options
[k
] = (q
|| p
|| true);
92 return { type
: 'RSA', bits
: len2
* 8, comment
: comment
, options
: options
, fprint
: fprint
, src
: s
};
95 return { type
: 'DSA', bits
: len1
* 8, comment
: comment
, options
: options
, fprint
: fprint
, src
: s
};
98 return { type
: 'EdDSA', curve
: 'Curve25519', comment
: comment
, options
: options
, fprint
: fprint
, src
: s
};
101 return { type
: 'ECDSA', curve
: curve
, comment
: comment
, options
: options
, fprint
: fprint
, src
: s
};
109 function renderKeyItem(pubkey
) {
112 click
: isReadonlyView
? null : removeKey
,
113 'data-key': pubkey
.src
115 E('strong', pubkey
.comment
|| _('Unnamed key')), E('br'),
117 '%s, %s'.format(pubkey
.type
, pubkey
.curve
|| _('%d Bit').format(pubkey
.bits
)),
118 pubkey
.options
? E([], [
119 ' / ', _('Options:'), ' ',
120 E('code', Object
.keys(pubkey
.options
).sort().join(', '))
122 E('br'), E('code', pubkey
.fprint
)
127 function renderKeys(keys
) {
128 var list
= document
.querySelector('.cbi-dynlist');
130 while (!matchesElem(list
.firstElementChild
, '.add-item'))
131 list
.removeChild(list
.firstElementChild
);
133 keys
.forEach(function(key
) {
134 var pubkey
= SSHPubkeyDecoder
.decode(key
);
136 list
.insertBefore(renderKeyItem(pubkey
), list
.lastElementChild
);
139 if (list
.firstElementChild
=== list
.lastElementChild
)
140 list
.insertBefore(E('p', _('No public keys present yet.')), list
.lastElementChild
);
143 function saveKeys(keys
) {
144 return fs
.write('/etc/dropbear/authorized_keys', keys
.join('\n') + '\n', 384 /* 0600 */)
145 .then(renderKeys
.bind(this, keys
))
146 .catch(function(e
) { ui
.addNotification(null, E('p', e
.message
)) })
147 .finally(ui
.hideModal
);
150 function addKey(ev
) {
151 var list
= findParent(ev
.target
, '.cbi-dynlist'),
152 input
= list
.querySelector('input[type="text"]'),
153 key
= input
.value
.trim(),
154 pubkey
= SSHPubkeyDecoder
.decode(key
),
160 list
.querySelectorAll('.item').forEach(function(item
) {
161 keys
.push(item
.getAttribute('data-key'));
164 if (keys
.indexOf(key
) !== -1) {
165 ui
.showModal(_('Add key'), [
166 E('div', { class: 'alert-message warning' }, _('The given SSH public key has already been added.')),
167 E('div', { class: 'right' }, E('div', { class: 'btn', click
: L
.hideModal
}, _('Close')))
171 ui
.showModal(_('Add key'), [
172 E('div', { class: 'alert-message warning' }, _('The given SSH public key is invalid. Please supply proper public RSA, ED25519 or ECDSA keys.')),
173 E('div', { class: 'right' }, E('div', { class: 'btn', click
: L
.hideModal
}, _('Close')))
180 return saveKeys(keys
).then(function() {
181 var added
= list
.querySelector('[data-key="%s"]'.format(key
.replace(/["\\]/g, '\\$&')));
183 added
.classList
.add('flash');
188 function removeKey(ev
) {
189 var list
= findParent(ev
.target
, '.cbi-dynlist'),
190 delkey
= ev
.target
.getAttribute('data-key'),
193 list
.querySelectorAll('.item').forEach(function(item
) {
194 var key
= item
.getAttribute('data-key');
199 L
.showModal(_('Delete key'), [
200 E('div', _('Do you really want to delete the following SSH key?')),
202 E('div', { class: 'right' }, [
203 E('div', { class: 'btn', click
: L
.hideModal
}, _('Cancel')),
205 E('div', { class: 'btn danger', click
: ui
.createHandlerFn(this, saveKeys
, keys
) }, _('Delete key')),
210 function dragKey(ev
) {
211 ev
.stopPropagation();
213 ev
.dataTransfer
.dropEffect
= 'copy';
216 function dropKey(ev
) {
217 var file
= ev
.dataTransfer
.files
[0],
218 input
= ev
.currentTarget
.querySelector('input[type="text"]'),
219 reader
= new FileReader();
222 reader
.onload = function(rev
) {
223 input
.value
= rev
.target
.result
.trim();
228 reader
.readAsText(file
);
231 ev
.stopPropagation();
235 function handleWindowDragDropIgnore(ev
) {
241 return fs
.lines('/etc/dropbear/authorized_keys').then(function(lines
) {
242 return lines
.map(function(line
) {
243 return SSHPubkeyDecoder
.decode(line
);
244 }).filter(function(line
) {
250 render: function(keys
) {
251 var list
= E('div', {
252 'class': 'cbi-dynlist',
253 'dragover': isReadonlyView
? null : dragKey
,
254 'drop': isReadonlyView
? null : dropKey
256 E('div', { 'class': 'add-item' }, [
258 'class': 'cbi-input-text',
260 'placeholder': _('Paste or drag SSH key file…') ,
261 'keydown': function(ev
) { if (ev
.keyCode
=== 13) addKey(ev
) },
262 'disabled': isReadonlyView
265 'class': 'cbi-button',
266 'click': ui
.createHandlerFn(this, addKey
),
267 'disabled': isReadonlyView
272 keys
.forEach(L
.bind(function(pubkey
) {
273 list
.insertBefore(renderKeyItem(pubkey
), list
.lastElementChild
);
276 if (list
.firstElementChild
=== list
.lastElementChild
)
277 list
.insertBefore(E('p', _('No public keys present yet.')), list
.lastElementChild
);
279 window
.addEventListener('dragover', handleWindowDragDropIgnore
);
280 window
.addEventListener('drop', handleWindowDragDropIgnore
);
282 return E('div', {}, [
283 E('h2', _('SSH-Keys')),
284 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.')),
285 E('div', { 'class': 'cbi-section-node' }, list
)
289 handleSaveApply
: null,