Skip to content

Latest commit

 

History

History
776 lines (648 loc) · 23.4 KB

1pass.lua

File metadata and controls

776 lines (648 loc) · 23.4 KB
 
Dec 18, 2013
Dec 18, 2013
1
JSON = (loadfile "JSON.lua")()
Dec 23, 2013
Dec 23, 2013
2
3
dofile("dumptable.lua")
Dec 24, 2013
Dec 24, 2013
4
5
6
local basedir = "1Password/1Password.agilekeychain/data/default" -- !!! FIXME
local password = argv[2]
local items = nil
Apr 26, 2014
Apr 26, 2014
7
local faveitems = nil
Dec 24, 2013
Dec 24, 2013
8
local keyhookRunning = false
Oct 18, 2015
Oct 18, 2015
9
10
11
12
13
14
15
16
17
18
local keyhookGuiMenus = nil
local function runGarbageCollector()
--local memused = math.floor(collectgarbage("count") * 1024.0)
--print("Collecting garbage (currently using " .. memused .. " bytes).")
collectgarbage()
--local newmemused = math.floor(collectgarbage("count") * 1024.0)
--print("Now using " .. newmemused .. " bytes (" .. memused - newmemused .. " bytes savings).")
end
Dec 24, 2013
Dec 24, 2013
19
Dec 23, 2013
Dec 23, 2013
20
21
22
23
24
25
26
27
local passwordTypeNameMap = {
["webforms.WebForm"] = "Logins",
["wallet.financial.CreditCard"] = "Credit cards",
["passwords.Password"] = "Passwords",
["wallet.financial.BankAccountUS"] = "Bank accounts",
["wallet.membership.Membership"] = "Memberships",
["wallet.government.DriversLicense"] = "Drivers licenses",
["system.Tombstone"] = "Dead items",
Feb 11, 2014
Feb 11, 2014
28
["securenotes.SecureNote"] = "Secure notes",
Dec 17, 2016
Dec 17, 2016
29
30
["wallet.government.SsnUS"] = "Social Security Numbers",
["wallet.computer.Router"] = "Router passwords",
Dec 23, 2013
Dec 23, 2013
31
32
33
34
35
36
37
38
39
40
-- !!! FIXME: more!
}
local passwordTypeOrdering = {
"webforms.WebForm",
"wallet.financial.CreditCard",
"passwords.Password",
"wallet.financial.BankAccountUS",
"wallet.membership.Membership",
"wallet.government.DriversLicense",
Dec 17, 2016
Dec 17, 2016
41
"wallet.government.SsnUS",
Feb 11, 2014
Feb 11, 2014
42
"securenotes.SecureNote",
Dec 17, 2016
Dec 17, 2016
43
"wallet.computer.Router",
Dec 23, 2013
Dec 23, 2013
44
45
46
-- never show "system.Tombstone",
-- !!! FIXME: more!
}
Dec 18, 2013
Dec 18, 2013
47
Dec 18, 2013
Dec 18, 2013
48
49
50
51
52
local function load_json_str(str, desc)
local retval = JSON:decode(str)
return retval
end
Dec 18, 2013
Dec 18, 2013
53
54
55
56
57
58
local function load_json(fname)
local f = io.open(fname, "rb")
if (f == nil) then
return nil
end
Nov 9, 2015
Nov 9, 2015
59
local str = f:read("*a")
Dec 18, 2013
Dec 18, 2013
60
61
f:close()
Dec 18, 2013
Dec 18, 2013
62
return load_json_str(str, fname)
Dec 18, 2013
Dec 18, 2013
63
64
65
end
Dec 18, 2013
Dec 18, 2013
66
local keys = {}
Dec 24, 2013
Dec 24, 2013
67
local function loadKey(level, password)
Dec 18, 2013
Dec 18, 2013
68
69
70
71
if keys[level] ~= nil then
return keys[level]
end
Dec 23, 2013
Dec 23, 2013
72
local keysjson = load_json(basedir .. "/encryptionKeys.js")
Dec 18, 2013
Dec 18, 2013
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
if (keysjson == nil) or (keysjson[level] == nil) then
return nil
end
local identifier = keysjson[level]
for i,v in ipairs(keysjson.list) do
if v.identifier == identifier then
local iterations = v.iterations
if (iterations == nil) or (iterations < 1000) then
iterations = 1000
end
local decrypted = decryptUsingPBKDF2(v.data, password, iterations)
if decrypted == nil then
return nil
end
local validate = decryptBase64UsingKey(v.validation, decrypted)
if validate ~= decrypted then
return nil
end
Dec 18, 2013
Dec 18, 2013
95
keys[level] = decrypted
Dec 18, 2013
Dec 18, 2013
96
97
98
99
100
101
102
return decrypted
end
end
return nil
end
Dec 24, 2013
Dec 24, 2013
103
local function getHint()
Dec 18, 2013
Dec 18, 2013
104
105
106
107
108
local f = io.open(basedir .. "/.password.hint", "r")
if (f == nil) then
return
end
Nov 9, 2015
Nov 9, 2015
109
local str = "(hint is '" .. f:read("*a") .. "')."
Dec 18, 2013
Dec 18, 2013
110
f:close()
Dec 23, 2013
Dec 23, 2013
111
112
--print(str)
return str
Dec 18, 2013
Dec 18, 2013
113
114
end
Dec 18, 2013
Dec 18, 2013
115
Dec 24, 2013
Dec 24, 2013
116
local function loadContents()
Dec 23, 2013
Dec 23, 2013
117
118
119
return load_json(basedir .. "/contents.js")
end
Oct 18, 2015
Oct 18, 2015
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
local function makeMenu()
return {}
end
local function appendMenuItem(menu, text, callback)
local item = {}
item["text"] = text
if callback ~= nil then
item["callback"] = callback
end
menu[#menu+1] = item
return item
end
local function setMenuItemSubmenu(menuitem, submenu)
menuitem["submenu"] = submenu
end
Nov 9, 2015
Nov 9, 2015
138
139
140
local function setMenuItemChecked(menuitem, ischecked)
menuitem["checked"] = ischecked
end
Oct 18, 2015
Oct 18, 2015
141
142
Jun 18, 2017
Jun 18, 2017
143
local function build_secret_menuitem(menu, type, str, hidden, transform)
Dec 23, 2013
Dec 23, 2013
144
145
146
147
148
149
150
151
152
153
154
if str == nil then
return nil
end
local valuestr = str
if hidden == true then
valuestr = "*****"
end
local text = type .. " " .. valuestr
local callback = function()
Jun 18, 2017
Jun 18, 2017
155
156
157
if transform ~= nil then
str = transform(str)
end
Dec 23, 2013
Dec 23, 2013
158
159
copyToClipboard(str)
--print("Copied data [" .. str .. "] to clipboard.")
Oct 18, 2015
Oct 18, 2015
160
guiDestroyMenu(keyhookGuiMenus[1])
Dec 23, 2013
Dec 23, 2013
161
end
Oct 18, 2015
Oct 18, 2015
162
return appendMenuItem(menu, text, callback)
Dec 18, 2013
Dec 18, 2013
163
164
end
Jun 18, 2017
Jun 18, 2017
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
local function transform_otp(str)
local algorithm, name, argstr = string.match(str, "^otpauth://(.-)/(.-)?(.+)$")
if algorithm == nil then algorithm = "" end
if algorithm ~= "totp" then
print("FIXME: don't know how to handle One Time Passwords using the '" .. algorithm .. "' algorithm!")
return "000000"
end
local args = {}
while argstr ~= nil and argstr ~= "" do
local arg
local idx = string.find(argstr, "&")
if idx == nil then
arg = argstr
argstr = nil
else
Apr 9, 2020
Apr 9, 2020
181
arg = string.sub(argstr, 0, idx - 1);
Jun 18, 2017
Jun 18, 2017
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
argstr = string.sub(argstr, idx + 1);
end
local key, val = string.match(arg, "^(.-)=(.*)$")
if (key ~= nil) and (val ~= nil) then
args[key] = val
end
end
--dumptable("otpauth://" .. algorithm .. "/" .. name, args);
if args["secret"] == nil then
print("FIXME: this One Time Password doesn't seem to have a secret key!")
return "000000"
end
local retval = decryptTopt(args["secret"]);
if retval == nil then
print("FIXME: failed to generate One Time Password; is the secret key bogus?")
retval = "000000"
end
return retval
end
Dec 23, 2013
Dec 23, 2013
206
207
208
209
210
211
212
local secret_menuitem_builders = {}
local function build_secret_menuitem_webform(menu, info, secure)
local addthis = false
local username = nil
local password = nil
Jun 18, 2017
Jun 18, 2017
213
local otp = nil
Apr 19, 2014
Apr 19, 2014
214
215
local designated_password = nil
local designated_username = nil
Dec 23, 2013
Dec 23, 2013
216
local email = nil
Jan 19, 2014
Jan 19, 2014
217
Feb 11, 2014
Feb 11, 2014
218
219
220
221
222
if secure.fields == nil then
print("no secure fields, don't know how to handle this item")
return
end
Dec 23, 2013
Dec 23, 2013
223
224
225
for i,v in ipairs(secure.fields) do
--print(info.name .. ": " .. v.type .. ", " .. v.value)
local ignored = false
Apr 19, 2014
Apr 19, 2014
226
227
228
229
230
231
232
if (v.value == nil) or (v.value == "") then
ignored = true
elseif (v.designation ~= nil) and (v.designation == "password") then
designated_password = v.value
elseif (v.designation ~= nil) and (v.designation == "username") then
designated_username = v.value
elseif (v.type == "P") then
Dec 23, 2013
Dec 23, 2013
233
password = v.value
Apr 19, 2014
Apr 19, 2014
234
elseif (v.type == "T") then
Dec 23, 2013
Dec 23, 2013
235
username = v.value
Apr 19, 2014
Apr 19, 2014
236
elseif (v.type == "E") then
Dec 23, 2013
Dec 23, 2013
237
238
239
240
241
242
243
244
245
246
email = v.value
else
ignored = true
end
if not ignored then
addthis = true
end
end
Jun 18, 2017
Jun 18, 2017
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- this is probably all wrong.
if secure.sections ~= nil then
for i,v in ipairs(secure.sections) do
if v.fields ~= nil then
for i2,v2 in ipairs(v.fields) do
if (type(v2.v) == "string") and (string.sub(v2.v, 0, 10) == "otpauth://") then
otp = v2.v
addthis = true
end
end
end
end
end
Dec 23, 2013
Dec 23, 2013
261
if addthis then
Apr 19, 2014
Apr 19, 2014
262
263
264
265
266
267
268
269
270
-- designated fields always win out.
if (designated_username ~= nil) then
username = designated_username
end
if (designated_password ~= nil) then
password = designated_password
end
Dec 23, 2013
Dec 23, 2013
271
272
273
274
275
276
277
if (username ~= nil) and (email ~= nil) and (email == username) then
email = nil
end
build_secret_menuitem(menu, "username", username)
build_secret_menuitem(menu, "email", email)
build_secret_menuitem(menu, "password", password, true)
Jun 18, 2017
Jun 18, 2017
278
build_secret_menuitem(menu, "otp", otp, true, transform_otp)
Dec 19, 2013
Dec 19, 2013
279
end
Dec 23, 2013
Dec 23, 2013
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
end
secret_menuitem_builders["webforms.WebForm"] = build_secret_menuitem_webform
local function build_secret_menuitem_password(menu, info, secure)
build_secret_menuitem(menu, "password", secure.password, true)
end
secret_menuitem_builders["passwords.Password"] = build_secret_menuitem_password
local function build_secret_menuitem_bankacctus(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
build_secret_menuitem(menu, "Account type", secure.accountType)
build_secret_menuitem(menu, "Routing number", secure.routingNo)
build_secret_menuitem(menu, "Account number", secure.accountNo)
build_secret_menuitem(menu, "Bank name", secure.bankName)
build_secret_menuitem(menu, "Owner", secure.owner)
Dec 4, 2014
Dec 4, 2014
297
298
build_secret_menuitem(menu, "SWIFT code", secure.swift)
build_secret_menuitem(menu, "PIN", secure.telephonePin)
Dec 23, 2013
Dec 23, 2013
299
300
301
302
303
end
secret_menuitem_builders["wallet.financial.BankAccountUS"] = build_secret_menuitem_bankacctus
local function build_secret_menuitem_driverslic(menu, info, secure)
Feb 23, 2015
Feb 23, 2015
304
305
306
307
308
309
310
311
312
313
314
315
316
-- !!! FIXME: there's more data for this menuitem than this, in a generic dictionary.
local birthdate = nil
if secure.birthdate_yy ~= nil then
birthdate = secure.birthdate_yy
if secure.birthdate_mm ~= nil then
birthdate = birthdate .. "/" .. string.sub("00" .. secure.birthdate_mm, -2)
if secure.birthdate_dd ~= nil then
birthdate = birthdate .. "/" .. string.sub("00" .. secure.birthdate_dd, -2)
end
end
end
Mar 29, 2015
Mar 29, 2015
317
local expiredate = nil
Feb 23, 2015
Feb 23, 2015
318
319
320
321
322
323
324
325
326
327
if secure.expiry_date_yy ~= nil then
expiredate = secure.expiry_date_yy
if secure.expiry_date_mm ~= nil then
expiredate = expiredate .. "/" .. string.sub("00" .. secure.expiry_date_mm, -2)
if secure.expiry_date_dd ~= nil then
expiredate = expiredate .. "/" .. string.sub("00" .. secure.expiry_date_dd, -2)
end
end
end
Dec 23, 2013
Dec 23, 2013
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
build_secret_menuitem(menu, "License number", secure.number)
build_secret_menuitem(menu, "Class", secure.class)
build_secret_menuitem(menu, "Expires", expiredate)
build_secret_menuitem(menu, "State", secure.state)
build_secret_menuitem(menu, "Country", secure.country)
build_secret_menuitem(menu, "Conditions", secure.conditions)
build_secret_menuitem(menu, "Full name", secure.fullname)
build_secret_menuitem(menu, "Address", secure.address)
build_secret_menuitem(menu, "Gender", secure.sex)
build_secret_menuitem(menu, "Birthdate", birthdate)
build_secret_menuitem(menu, "Height", secure.height)
end
secret_menuitem_builders["wallet.government.DriversLicense"] = build_secret_menuitem_driverslic
local function build_secret_menuitem_membership(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
build_secret_menuitem(menu, "Membership number", secure.membership_no)
end
secret_menuitem_builders["wallet.membership.Membership"] = build_secret_menuitem_membership
local function build_secret_menuitem_creditcard(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
local expiredate = secure.expiry_yy .. "/" .. string.sub("00" .. secure.expiry_mm, -2)
build_secret_menuitem(menu, "Type", secure.type)
build_secret_menuitem(menu, "CC number", secure.ccnum, true)
build_secret_menuitem(menu, "CVV", secure.cvv, true)
May 1, 2014
May 1, 2014
356
build_secret_menuitem(menu, "Expires", expiredate)
Dec 23, 2013
Dec 23, 2013
357
358
359
360
361
build_secret_menuitem(menu, "Card holder", secure.cardholder)
build_secret_menuitem(menu, "Bank", secure.bank)
end
secret_menuitem_builders["wallet.financial.CreditCard"] = build_secret_menuitem_creditcard
Dec 17, 2016
Dec 17, 2016
362
Feb 11, 2014
Feb 11, 2014
363
364
365
366
local function build_secret_menuitem_securenote(menu, info, secure)
build_secret_menuitem(menu, "Notes", secure.notesPlain, true)
end
secret_menuitem_builders["securenotes.SecureNote"] = build_secret_menuitem_securenote
Dec 23, 2013
Dec 23, 2013
367
Dec 17, 2016
Dec 17, 2016
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
local function build_secret_menuitem_ssnus(menu, info, secure)
build_secret_menuitem(menu, "Name", secure.name, false)
build_secret_menuitem(menu, "SSN", secure.number, true)
end
secret_menuitem_builders["wallet.government.SsnUS"] = build_secret_menuitem_ssnus
local function build_secret_menuitem_router(menu, info, secure)
build_secret_menuitem(menu, "Name", secure.name, false)
build_secret_menuitem(menu, "Password", secure.password, true)
end
secret_menuitem_builders["wallet.computer.Router"] = build_secret_menuitem_router
Dec 24, 2013
Dec 24, 2013
383
local function build_secret_menuitems(info, menu)
Dec 23, 2013
Dec 23, 2013
384
local metadata = load_json(basedir .. "/" .. info.uuid .. ".1password")
Jul 9, 2014
Jul 9, 2014
385
if (metadata == nil) or (next(metadata) == nil) then -- the "next" trick tests if table is empty.
Dec 23, 2013
Dec 23, 2013
386
387
388
return
end
Jan 19, 2014
Jan 19, 2014
389
390
local securityLevel = metadata.securityLevel
if securityLevel == nil then
Apr 26, 2014
Apr 26, 2014
391
securityLevel = metadata.openContents.securityLevel
Jan 19, 2014
Jan 19, 2014
392
end
Apr 15, 2014
Apr 15, 2014
393
--print("title: " .. metadata.title)
Jan 19, 2014
Jan 19, 2014
394
395
396
397
398
399
if securityLevel == nil then
--print("can't find security level, assuming SL5" .. metadata.title)
securityLevel = "SL5"
end
local plaintext = decryptBase64UsingKey(metadata.encrypted, loadKey(securityLevel, password))
Dec 23, 2013
Dec 23, 2013
400
401
402
403
404
405
406
407
408
409
if plaintext == nil then
return
end
local secure = load_json_str(plaintext, info.uuid)
if secure == nil then
return
end
--dumptable("secure " .. info.name, secure)
Oct 18, 2015
Oct 18, 2015
410
local menuitem = appendMenuItem(menu, info.name)
Dec 23, 2013
Dec 23, 2013
411
412
413
414
415
416
417
if secret_menuitem_builders[info.type] == nil then
print("WARNING: don't know how to handle items of type " .. info.type)
dumptable("secure " .. info.type .. " (" .. info.name .. ")", secure)
return
end
Apr 26, 2014
Apr 26, 2014
418
419
420
421
422
if metadata.faveIndex ~= nil then
--dumptable("fave metadata " .. info.name, metadata)
faveitems[metadata.faveIndex] = { info=info, secure=secure }
end
Oct 18, 2015
Oct 18, 2015
423
local submenu = makeMenu()
Dec 23, 2013
Dec 23, 2013
424
secret_menuitem_builders[info.type](submenu, info, secure)
Oct 18, 2015
Oct 18, 2015
425
setMenuItemSubmenu(menuitem, submenu)
Dec 19, 2013
Dec 19, 2013
426
427
end
Dec 24, 2013
Dec 24, 2013
428
429
430
local function prepItems()
items = {}
local contents = loadContents()
Apr 1, 2015
Apr 1, 2015
431
432
433
if contents == nil then
return false
end
Dec 24, 2013
Dec 24, 2013
434
435
436
437
for i,v in ipairs(contents) do
local t = v[2]
if items[t] == nil then
items[t] = {}
Dec 23, 2013
Dec 23, 2013
438
end
Dec 24, 2013
Dec 24, 2013
439
440
local bucket = items[t]
bucket[#bucket+1] = { uuid=v[1], type=t, name=v[3], url=v[4] } -- !!! FIXME: there are more fields, don't know what they mean yet.
Dec 23, 2013
Dec 23, 2013
441
end
Apr 1, 2015
Apr 1, 2015
442
return true
Dec 23, 2013
Dec 23, 2013
443
end
Dec 22, 2013
Dec 22, 2013
444
Dec 24, 2013
Dec 24, 2013
445
446
local passwordUnlockTime = nil
Apr 15, 2014
Apr 15, 2014
447
448
449
450
451
452
local function lockKeychain()
-- lose the existing password and key, prompt user again.
password = argv[2] -- might be nil, don't reset if on command line.
keys["SL5"] = nil
passwordUnlockTime = nil
setPowermateLED(false)
Oct 18, 2015
Oct 18, 2015
453
454
455
456
457
-- kill the popup if it exists.
if (keyhookGuiMenus ~= nil) and (keyhookGuiMenus[1] ~= nil) then
guiDestroyMenu(keyhookGuiMenus[1])
end
Apr 15, 2014
Apr 15, 2014
458
end
Dec 24, 2013
Dec 24, 2013
459
Apr 15, 2014
Apr 15, 2014
460
function pumpLua() -- not local! Called from C!
Apr 15, 2014
Apr 15, 2014
461
462
463
-- !!! FIXME: this should lose the key in RAM and turn off the Powermate
-- !!! FIXME: LED when the time expires instead of if the time has
-- !!! FIXME: expired when the user is trying to get at the keychain.
Dec 24, 2013
Dec 24, 2013
464
465
466
467
if passwordUnlockTime ~= nil then
local now = os.time()
local maxTime = (15 * 60) -- !!! FIXME: don't hardcode.
if os.difftime(now, passwordUnlockTime) > maxTime then
Apr 15, 2014
Apr 15, 2014
468
lockKeychain()
Dec 24, 2013
Dec 24, 2013
469
470
end
end
Apr 15, 2014
Apr 15, 2014
471
472
end
Oct 18, 2015
Oct 18, 2015
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
function escapePressed() -- not local! Called from C!
if keyhookGuiMenus[1] then
guiDestroyMenu(keyhookGuiMenus[1])
end
end
local buildGuiMenuList
local function spawnSubMenu(button, submenu, depth)
local guimenu = guiCreateSubMenu(button)
for i = #keyhookGuiMenus, depth, -1 do
if keyhookGuiMenus[i] then
--print("Destroying conflicting submenu at depth " .. i)
guiDestroyMenu(keyhookGuiMenus[i])
keyhookGuiMenus[i] = nil
end
end
--print("New submenu at depth " .. depth)
keyhookGuiMenus[depth] = guimenu
buildGuiMenuList(guimenu, submenu)
guiShowWindow(guimenu)
end
local function buildGuiMenuItem(guimenu, item)
local cb = item["callback"]
if cb == nil then
local submenu = item["submenu"]
local depth = #keyhookGuiMenus+1
cb = function (button)
return spawnSubMenu(button, submenu, depth)
end
end
Nov 9, 2015
Nov 9, 2015
509
guiAddMenuItem(guimenu, item["text"], item["checked"], cb)
Oct 18, 2015
Oct 18, 2015
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
end
buildGuiMenuList = function(guimenu, list)
for i,v in ipairs(list) do
buildGuiMenuItem(guimenu, v)
end
end
local function buildSearchResultsMenuCategory(guimenu, menu, str)
local submenu = menu["submenu"]
if not submenu then return end
local name = menu["text"]
-- !!! FIXME: hacky. We should really list favorites first anyhow.
if name == "Favorites" then return end
for i,v in ipairs(submenu) do
if string.find(string.lower(v["text"]), str, 1, true) ~= nil then
buildGuiMenuItem(guimenu, v)
end
end
end
local function buildSearchResultsMenuList(guimenu, topmenu, str)
for i,v in ipairs(topmenu) do
buildSearchResultsMenuCategory(guimenu, v, str)
end
end
local function searchEntryChanged(guimenu, str, topmenu)
--print("search changed to '" .. str .. "' ...")
guiRemoveAllMenuItems(guimenu)
if str == "" then
buildGuiMenuList(guimenu, topmenu)
else
buildSearchResultsMenuList(guimenu, topmenu, string.lower(str))
end
guiShowWindow(guimenu)
end
local function handleMenuDestroyed()
--print("Destroying main menu...")
for i,v in ipairs(keyhookGuiMenus) do
if i > 1 then
guiDestroyMenu(v)
end
end
keyhookGuiMenus = nil
keyhookRunning = false
runGarbageCollector()
end
local function launchGuiMenu(topmenu)
local guimenu = guiCreateTopLevelMenu("1pass",
function(guimenu, str) -- search text changed callback
return searchEntryChanged(guimenu, str, topmenu)
end,
function() -- window destroyed callback
handleMenuDestroyed()
end
)
keyhookGuiMenus = {}
keyhookGuiMenus[#keyhookGuiMenus+1] = guimenu
buildGuiMenuList(guimenu, topmenu)
guiShowWindow(guimenu)
end
Apr 15, 2014
Apr 15, 2014
579
Nov 9, 2015
Nov 9, 2015
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
local trustedDisks = {}
local function getTrustedDiskChecksumPath(mntpoint)
return mntpoint .. "/1pass.dat"
end
local function getTrustedDiskChecksum(mntpoint)
local f = io.open(getTrustedDiskChecksumPath(mntpoint), "rb")
if f == nil then
return nil
end
local str = f:read("*a")
f:close()
return calcSha256(str)
end
local function choseTrustedDisk(mntpoint)
if trustedDisks[mntpoint] ~= nil then
trustedDisks[mntpoint] = nil -- no longer check existing trusted disk.
else
-- !!! FIXME: probably needs a message box if this fails.
local checksum = getTrustedDiskChecksum(mntpoint)
-- No checksum file yet? Generate and write out a random string.
if checksum == nil then
local f = io.open("/dev/urandom", "rb")
if f ~= nil then
local str = f:read(4096)
f:close()
if (str ~= nil) and (#str == 4096) then
f = io.open(getTrustedDiskChecksumPath(mntpoint), "wb")
if f ~= nil then
if f:write(str) and f:flush() then
checksum = calcSha256(str)
end
f:close()
end
end
end
end
trustedDisks[mntpoint] = checksum
end
-- kill the popup if it exists.
-- !!! FIXME: put this in its own function, this is a copy/paste from elsewhere.
if (keyhookGuiMenus ~= nil) and (keyhookGuiMenus[1] ~= nil) then
guiDestroyMenu(keyhookGuiMenus[1])
end
end
local function buildTrustedDeviceMenu()
local menu = makeMenu()
local disks = getMountedDisks() -- this is a C function.
table.sort(disks, function(a, b) return a < b end)
for i,v in ipairs(disks) do
local item = appendMenuItem(menu, v, function() choseTrustedDisk(v) end)
if trustedDisks[v] ~= nil then
setMenuItemChecked(item, true)
end
end
return menu
end
Apr 15, 2014
Apr 15, 2014
645
function keyhookPressed() -- not local! Called from C!
Oct 18, 2015
Oct 18, 2015
646
647
648
649
--print("keyhookPressed: running==" .. tostring(keyhookRunning))
if keyhookRunning then
return
end
Apr 15, 2014
Apr 15, 2014
650
651
keyhookRunning = true
Dec 24, 2013
Dec 24, 2013
652
Nov 9, 2015
Nov 9, 2015
653
654
655
656
657
658
659
660
661
662
663
664
665
666
local allowaccess = true;
for mntpoint,checksum in pairs(trustedDisks) do
if getTrustedDiskChecksum(mntpoint) ~= checksum then
allowaccess = false
break
end
end
if not allowaccess then
-- !!! FIXME: probably needs a message box if this happens.
keyhookRunning = false
return
end
Dec 24, 2013
Dec 24, 2013
667
668
669
670
671
672
673
674
675
676
677
678
679
while password == nil do
password = runGuiPasswordPrompt(getHint())
if password == nil then
keyhookRunning = false
return
end
if loadKey("SL5", password) == nil then
password = nil -- wrong password
local start = os.time() -- cook the CPU for three seconds.
local now = start
while os.difftime(now, start) < 3 do
now = os.time()
end
Dec 24, 2013
Dec 24, 2013
680
681
else
passwordUnlockTime = os.time()
Apr 15, 2014
Apr 15, 2014
682
setPowermateLED(true)
Dec 24, 2013
Dec 24, 2013
683
end
Dec 22, 2013
Dec 22, 2013
684
end
Dec 24, 2013
Dec 24, 2013
685
Apr 1, 2015
Apr 1, 2015
686
687
688
689
if not prepItems() then
keyhookRunning = false
return
end
Dec 24, 2013
Dec 24, 2013
690
Oct 18, 2015
Oct 18, 2015
691
692
local topmenu = makeMenu()
local favesmenu = makeMenu()
Nov 9, 2015
Nov 9, 2015
693
local securitymenu = makeMenu()
Apr 26, 2014
Apr 26, 2014
694
695
faveitems = {}
Oct 18, 2015
Oct 18, 2015
696
setMenuItemSubmenu(appendMenuItem(topmenu, "Favorites"), favesmenu)
Nov 9, 2015
Nov 9, 2015
697
setMenuItemSubmenu(appendMenuItem(topmenu, "Security"), securitymenu)
Dec 24, 2013
Dec 24, 2013
698
Nov 9, 2015
Nov 9, 2015
699
700
appendMenuItem(securitymenu, "Lock keychain now", function() lockKeychain() end)
setMenuItemSubmenu(appendMenuItem(securitymenu, "Require trusted device"), buildTrustedDeviceMenu())
Dec 24, 2013
Dec 24, 2013
701
Dec 24, 2013
Dec 24, 2013
702
703
for orderi,type in ipairs(passwordTypeOrdering) do
local bucket = items[type]
Jan 19, 2014
Jan 19, 2014
704
705
706
707
708
if bucket ~= nil then
local realname = passwordTypeNameMap[type]
if realname == nil then
realname = type
end
Oct 18, 2015
Oct 18, 2015
709
710
local menuitem = appendMenuItem(topmenu, realname)
local submenu = makeMenu()
Jan 19, 2014
Jan 19, 2014
711
712
713
714
table.sort(bucket, function(a, b) return a.name < b.name end)
for i,v in pairs(bucket) do
build_secret_menuitems(v, submenu)
end
Oct 18, 2015
Oct 18, 2015
715
setMenuItemSubmenu(menuitem, submenu)
Jan 19, 2014
Jan 19, 2014
716
else
Apr 26, 2014
Apr 26, 2014
717
--print("no bucket found for item type '" .. type .. "'")
Dec 24, 2013
Dec 24, 2013
718
end
Dec 22, 2013
Dec 22, 2013
719
end
Apr 26, 2014
Apr 26, 2014
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
-- This favepairs stuff is obnoxious.
local function favepairs(t)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a)
local i = 0
local iter = function()
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
return iter
end
for i,v in favepairs(faveitems) do
--dumptable("fave " .. i, v)
Oct 18, 2015
Oct 18, 2015
740
741
local menuitem = appendMenuItem(favesmenu, v.info.name)
local submenu = makeMenu()
Apr 26, 2014
Apr 26, 2014
742
secret_menuitem_builders[v.info.type](submenu, v.info, v.secure)
Oct 18, 2015
Oct 18, 2015
743
setMenuItemSubmenu(menuitem, submenu)
Apr 26, 2014
Apr 26, 2014
744
745
746
747
end
favepairs = nil
faveitems = nil
Dec 24, 2013
Dec 24, 2013
748
Oct 18, 2015
Oct 18, 2015
749
launchGuiMenu(topmenu)
Dec 22, 2013
Dec 22, 2013
750
751
end
Dec 24, 2013
Dec 24, 2013
752
753
754
755
756
757
758
759
-- Mainline!
--for i,v in ipairs(argv) do
-- print("argv[" .. i .. "] = " .. v)
--end
-- !!! FIXME: message box, exit if basedir is wack.
Apr 1, 2015
Apr 1, 2015
760
761
762
763
764
765
766
767
768
769
local f = io.open(basedir .. "/contents.js", "rb")
if f == nil then
print("ERROR: Couldn't read your 1Password keychain in '" .. basedir .. "'.")
print("ERROR: Please make sure it exists and you have permission to access it.")
print("ERROR: (maybe you need to run 'ln -s ~/Dropbox/1Password' here?")
print("ERROR: Giving up for now.")
os.exit(1)
end
f:close()
Dec 24, 2013
Dec 24, 2013
770
-- !!! FIXME: this can probably happen in C now (the Lua mainline is basically gone now).
Apr 15, 2014
Apr 15, 2014
771
setPowermateLED(false) -- off by default
Feb 11, 2014
Feb 11, 2014
772
print("Now waiting for the magic key combo (probably Alt-Meta-\\) ...")
Dec 22, 2013
Dec 22, 2013
773
774
giveControlToGui()
Dec 18, 2013
Dec 18, 2013
775
-- end of 1pass.lua ...