26 Commits

Author SHA1 Message Date
Morten Linderud
71024417a3 Rework sbctl backend
Signed-off-by: Morten Linderud <morten@linderud.pw>
2024-07-24 21:15:29 +02:00
guangwu
1a6b822968 fix: close file 2024-05-20 15:33:49 +08:00
Morten Linderud
e1ce225d88 Implement fs package and remove all direct filesystem calls 2023-02-17 19:19:26 +01:00
Morten Linderud
dcdc703aa9 verify: Implement file verification
Fixes https://github.com/Foxboron/sbctl/issues/163
2022-12-15 23:21:08 +01:00
Morten Linderud
9025537418 util: Implement CopyDirectory 2022-12-11 20:02:05 +01:00
Hugo Osvaldo Barrera
3f9d1d926c Check returned errors
Check a few errors that were not being handled properly.

Co-authored-by: Morten Linderud <morten@linderud.pw>
2022-07-07 19:59:15 +02:00
Morten Linderud
0f12ba744b import-keys: Implement a way to import existing keys into sbctl
Signed-off-by: Morten Linderud <morten@linderud.pw>
2022-01-09 21:39:52 +01:00
Morten Linderud
2b37ebd823 util: Also catch io.ErrUnexpectedEOF
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-12-20 00:09:36 +01:00
Morten Linderud
92aee6c430 enroll-keys: Refactor a bit
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-11-10 19:06:11 +01:00
Morten Linderud
d0e0ad6805 util: Switch from if to cases
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-11-10 19:05:37 +01:00
Silke Hofstra
d2f7f9707f Ignore EOF errors in CheckMSDos
CheckMSDos attempts to read the first two bytes of a file to determine
if it is an MS-DOS executable. If a file (for whatever reason) is shorter,
attempting to read this will result in an `io.EOF` error.

The error can be safely ignore because such an error indicates that the file
could be read successfully, it just doesn't contain enough bytes.
2021-10-24 18:17:02 +02:00
Morten Linderud
ac8723dacd util: Ensure we ignore when files do not exist in efivarfs
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-06-02 21:55:37 +02:00
Morten Linderud
0781f6bb98 sbctl/create-keys: Move up the GUID logic
We also make a helper for creating directories in a proper way

Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-06-02 21:55:37 +02:00
Morten Linderud
57a1c93eb9 Remove last of the log.* stuff
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-05-30 14:47:45 +02:00
Morten Linderud
8b4fc40724 Added internal functions for checked paths, and CheckMSDos
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-05-30 14:46:08 +02:00
Morten Linderud
97435cc48e More internal restructuring
Move more logic top-level, move prints to top-level

Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-05-30 14:46:07 +02:00
Morten Linderud
bb78cf9c01 Remove previous logging, improve error bubling
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-05-30 14:43:04 +02:00
igo95862
d3feae2791 Remove ioutil
Deprecated in GO 1.16
2021-05-16 21:14:42 +03:00
Morten Linderud
f53632ebd2 util: Expand array in print generator
Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-05-13 12:34:19 +02:00
Morten Linderud
59ec7a813f sbctl: IsImmutable should return false if the file does not exist
Weird efivarfs quirk is that sometimes empty vars have no file. This
means they are not immutable and we can write to them.

Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-04-18 03:29:29 +02:00
Morten Linderud
2572b38a8e sbctl: Check for immutable files before sbkeysync
This allows us to give a sensible error for `enroll-keys` if the files
are set as immutable.

    $ sbctl enroll-keys
    ==> ERROR: File is immutable: /sys/firmware/efi/efivars/PK-8be4df61-93ca-11d2-aa0d-00e098032b8c
    ==> ERROR: File is immutable: /sys/firmware/efi/efivars/KEK-8be4df61-93ca-11d2-aa0d-00e098032b8c
    ==> ERROR: File is immutable: /sys/firmware/efi/efivars/db-d719b2cb-3d3a-4596-a3bc-dad00e67656f
    ==> ERROR: You need to chattr -i files in efivarfs

Signed-off-by: Morten Linderud <morten@linderud.pw>
2021-04-03 12:27:44 +02:00
Érico Rolim
cc55d6e443 Add ReadOrCreateFile utility function.
This function will try to read a file into a byte buffer, and, if the
file doesn't exist, create its containing directory and the file itself.
If any of those actions fail due to permissions, the function will print
a warning about running the tool as root.

Reading from the file and bundle databases works like this, so the error
checking should be implemented in a single place.

Also, use the new function in ReadFileDatabase().

Signed-off-by: Érico Rolim <erico.erc@gmail.com>
2021-01-11 00:49:45 -03:00
Érico Rolim
0a7c6e8bc5 Fix potential seg fault in ChecksumFile.
If ReadFile errors out, the error would only be checked after the
function attempts to read the buffer into the hasher. This commit fixes
that, checking the error as soon as possible.

Signed-off-by: Érico Rolim <erico.erc@gmail.com>
2021-01-11 00:43:21 -03:00
Érico Rolim
2b09a8e645 Change PrintGenerateError signature.
This avoid boilerplate calls to fmt.Sprintf in the main code.
2020-07-25 18:12:42 +02:00
Érico Rolim
ed103661f0 Create PrintGenerateError and use it where possible.
It's a function for generating an error from a message and still
printing it. Helps in avoiding duplication.
2020-07-25 18:12:41 +02:00