1
Fork 0
turbonss/src/user.zig

379 lines
12 KiB
Zig
Raw Normal View History

2022-02-18 17:34:50 +02:00
const std = @import("std");
2022-02-27 15:49:00 +02:00
2022-02-19 18:18:14 +02:00
const pad = @import("padding.zig");
2022-02-27 15:49:00 +02:00
const validate = @import("validate.zig");
2022-03-08 20:44:32 +02:00
const compress = @import("compress.zig");
const shellImport = @import("shell.zig");
2022-02-27 15:49:00 +02:00
const InvalidRecord = validate.InvalidRecord;
2022-02-18 20:29:45 +02:00
2022-02-20 12:44:21 +02:00
const assert = std.debug.assert;
2022-02-24 10:13:31 +02:00
const mem = std.mem;
2022-03-06 13:11:06 +02:00
const math = std.math;
2022-02-25 15:20:35 +02:00
const Allocator = mem.Allocator;
2022-02-27 06:09:46 +02:00
const ArrayList = std.ArrayList;
const StringHashMap = std.StringHashMap;
2022-03-02 06:07:07 +02:00
2022-02-27 06:09:46 +02:00
// Idx2ShellProto is a function prototype that, given a shell's index (in
// global shell section), will return a shell string. Matches ShellReader.get.
const Idx2ShellProto = fn (u6) []const u8;
2022-02-18 17:34:50 +02:00
2022-02-25 13:58:06 +02:00
// User is a convenient public struct for record construction and
2022-03-02 06:07:07 +02:00
// serialization.
2022-02-18 20:29:45 +02:00
pub const User = struct {
uid: u32,
gid: u32,
name: []const u8,
gecos: []const u8,
home: []const u8,
shell: []const u8,
2022-03-01 11:01:45 +02:00
// deep-clones a User record with a given Allocator.
2022-03-02 06:07:07 +02:00
pub fn clone(
self: *const User,
allocator: Allocator,
) Allocator.Error!User {
2022-03-01 15:25:38 +02:00
const stringdata = try allocator.alloc(u8, self.strlen());
2022-03-01 11:01:45 +02:00
const gecos_start = self.name.len;
const home_start = gecos_start + self.gecos.len;
2022-03-04 10:37:07 +02:00
const shell_start = home_start + self.home.len;
2022-03-01 11:01:45 +02:00
mem.copy(u8, stringdata[0..self.name.len], self.name);
mem.copy(u8, stringdata[gecos_start..], self.gecos);
mem.copy(u8, stringdata[home_start..], self.home);
mem.copy(u8, stringdata[shell_start..], self.shell);
2022-03-01 15:25:38 +02:00
return User{
2022-03-01 11:01:45 +02:00
.uid = self.uid,
.gid = self.gid,
.name = stringdata[0..self.name.len],
.gecos = stringdata[gecos_start .. gecos_start + self.gecos.len],
.home = stringdata[home_start .. home_start + self.home.len],
.shell = stringdata[shell_start .. shell_start + self.shell.len],
};
}
2022-03-01 15:25:38 +02:00
fn strlen(self: *const User) usize {
2022-03-01 11:01:45 +02:00
return self.name.len +
self.gecos.len +
self.home.len +
self.shell.len;
}
2022-03-01 15:25:38 +02:00
pub fn deinit(self: *User, allocator: Allocator) void {
const slice = self.home.ptr[0..self.strlen()];
2022-03-01 11:01:45 +02:00
allocator.free(slice);
2022-03-01 15:25:38 +02:00
self.* = undefined;
2022-03-01 11:01:45 +02:00
}
2022-02-18 20:29:45 +02:00
};
pub fn Shell2Index(T: type) type {
return struct {
const Self = @This();
data: T,
pub fn init(data: T) Self {
return Self{ .data = data };
}
2022-02-26 11:20:44 +02:00
pub fn get(self: *const Self, str: []const u8) ?u6 {
return self.data.get(str);
}
};
}
pub const PackedUser = struct {
const Self = @This();
pub const alignment_bits = 3;
2022-02-26 11:20:44 +02:00
const Inner = packed struct {
uid: u32,
gid: u32,
padding: u2 = 0,
shell_len_or_idx: u6,
shell_here: bool,
name_is_a_suffix: bool,
home_len: u6,
name_len: u5,
gecos_len: u11,
2022-02-25 10:48:19 +02:00
fn homeLen(self: *const Inner) usize {
return @as(u32, self.home_len) + 1;
}
2022-02-24 10:13:31 +02:00
fn nameStart(self: *const Inner) usize {
const name_len = self.nameLen();
if (self.name_is_a_suffix) {
return self.homeLen() - name_len;
} else return self.homeLen();
}
2022-02-25 10:48:19 +02:00
fn nameLen(self: *const Inner) usize {
return @as(u32, self.name_len) + 1;
}
2022-02-25 13:59:35 +02:00
fn gecosStart(self: *const Inner) usize {
if (self.name_is_a_suffix) {
return self.homeLen();
} else return self.homeLen() + self.nameLen();
}
2022-02-25 10:48:19 +02:00
fn gecosLen(self: *const Inner) usize {
return self.gecos_len;
}
2022-02-24 10:13:31 +02:00
fn maybeShellStart(self: *const Inner) usize {
assert(self.shell_here);
return self.gecosStart() + self.gecosLen();
}
2022-02-26 11:20:44 +02:00
fn shellLen(self: *const Inner) usize {
return @as(u32, self.shell_len_or_idx) + 1;
}
2022-02-24 10:13:31 +02:00
// stringLength returns the length of the blob storing string values.
fn stringLength(self: *const Inner) usize {
var result: usize = self.homeLen() + self.gecosLen();
if (!self.name_is_a_suffix)
result += self.nameLen();
if (self.shell_here)
result += self.shellLen();
return result;
}
};
// PackedUser does not allocate; it re-interprets the "bytes" blob
// field. Both of those fields are pointers to "our representation" of
// that field.
inner: *const Inner,
bytes: []const u8,
additional_gids_offset: u64,
2022-02-26 11:20:44 +02:00
pub const Entry = struct {
user: Self,
next: ?[]const u8,
};
// TODO(motiejus) provide a way to return an entry without decoding the
// additional_gids_offset:
// - will not return the 'next' slice.
// - cannot throw an Overflow error.
pub fn fromBytes(bytes: []const u8) error{Overflow}!Entry {
const inner = mem.bytesAsValue(Inner, bytes[0..@sizeOf(Inner)]);
const start_blob = @sizeOf(Inner);
const end_strings = start_blob + inner.stringLength();
const gids_offset = try compress.uvarint(bytes[end_strings..]);
const end_blob = end_strings + gids_offset.bytes_read;
const nextStart = pad.roundUp(usize, alignment_bits, end_blob);
var next: ?[]const u8 = null;
if (nextStart < bytes.len)
next = bytes[nextStart..];
return Entry{
.user = Self{
.inner = inner,
.bytes = bytes[start_blob..end_blob],
.additional_gids_offset = gids_offset.value,
},
.next = next,
2022-02-26 11:20:44 +02:00
};
}
2022-02-26 11:20:44 +02:00
pub const Iterator = struct {
section: ?[]const u8,
shell_reader: shellImport.ShellReader,
2022-02-25 10:48:19 +02:00
pub fn next(it: *Iterator) error{Overflow}!?Self {
if (it.section) |section| {
const entry = try Self.fromBytes(section);
it.section = entry.next;
return entry.user;
2022-02-26 11:20:44 +02:00
}
return null;
}
};
pub fn iterator(section: []const u8, shell_reader: shellImport.ShellReader) Iterator {
return Iterator{ .section = section, .shell_reader = shell_reader };
}
// packTo packs the User record and copies it to the given byte slice.
// The slice must have at least maxRecordSize() bytes available. The
// slice is passed as a pointer, so it can be mutated.
pub fn packTo(
arr: *ArrayList(u8),
user: User,
additional_gids_offset: u64,
idxFn: StringHashMap(u6),
) error{ InvalidRecord, OutOfMemory }!void {
std.debug.assert(arr.items.len & 7 == 0);
// function arguments are consts. We need to mutate the underlying
// slice, so passing it via pointer instead.
const home_len = try validate.downCast(u6, user.home.len - 1);
const name_len = try validate.downCast(u5, user.name.len - 1);
const shell_len = try validate.downCast(u6, user.shell.len - 1);
const gecos_len = try validate.downCast(u8, user.gecos.len);
try validate.utf8(user.home);
try validate.utf8(user.name);
try validate.utf8(user.shell);
try validate.utf8(user.gecos);
const inner = Inner{
.uid = user.uid,
.gid = user.gid,
.shell_here = idxFn.get(user.shell) == null,
.shell_len_or_idx = idxFn.get(user.shell) orelse shell_len,
.home_len = home_len,
.name_is_a_suffix = mem.endsWith(u8, user.home, user.name),
.name_len = name_len,
.gecos_len = gecos_len,
2022-02-27 15:49:00 +02:00
};
const innerBytes = mem.asBytes(&inner);
2022-02-27 15:49:00 +02:00
try arr.*.appendSlice(innerBytes[0..@sizeOf(Inner)]);
try arr.*.appendSlice(user.home);
if (!inner.name_is_a_suffix)
try arr.*.appendSlice(user.name);
try arr.*.appendSlice(user.gecos);
if (inner.shell_here)
try arr.*.appendSlice(user.shell);
try compress.appendUvarint(arr, additional_gids_offset);
}
2022-02-19 18:18:14 +02:00
pub fn uid(self: Self) u32 {
return self.inner.uid;
}
2022-02-24 10:13:31 +02:00
pub fn gid(self: Self) u32 {
return self.inner.gid;
}
2022-02-25 10:48:19 +02:00
pub fn additionalGidsOffset(self: Self) u64 {
return self.additional_gids_offset;
}
2022-02-27 06:09:46 +02:00
pub fn home(self: Self) []const u8 {
return self.bytes[0..self.inner.homeLen()];
}
2022-02-25 10:48:19 +02:00
pub fn name(self: Self) []const u8 {
const name_pos = self.inner.nameStart();
const name_len = self.inner.nameLen();
return self.bytes[name_pos .. name_pos + name_len];
}
2022-02-24 10:13:31 +02:00
pub fn gecos(self: Self) []const u8 {
const gecos_pos = self.inner.gecosStart();
const gecos_len = self.inner.gecosLen();
return self.bytes[gecos_pos .. gecos_pos + gecos_len];
}
2022-02-24 10:13:31 +02:00
pub fn shell(self: Self, shell_reader: shellImport.ShellReader) []const u8 {
if (self.inner.shell_here) {
const shell_pos = self.inner.maybeShellStart();
const shell_len = self.inner.shellLen();
return self.bytes[shell_pos .. shell_pos + shell_len];
2022-02-26 11:20:44 +02:00
}
return shell_reader.get(self.inner.shell_len_or_idx);
}
};
2022-02-20 12:44:21 +02:00
2022-02-18 17:34:50 +02:00
const testing = std.testing;
test "PackedUser internal and external alignment" {
2022-03-02 06:07:07 +02:00
try testing.expectEqual(
@sizeOf(PackedUser.Inner) * 8,
@bitSizeOf(PackedUser.Inner),
2022-03-02 06:07:07 +02:00
);
2022-02-18 17:34:50 +02:00
}
2022-02-19 11:35:29 +02:00
fn testShellIndex(allocator: Allocator) StringHashMap(u6) {
var result = StringHashMap(u6).init(allocator);
result.put("/bin/bash", 0) catch unreachable;
result.put("/bin/zsh", 1) catch unreachable;
return result;
}
2022-02-19 11:35:29 +02:00
const test_shell_reader = shellImport.ShellReader{
.section_blob = "/bin/bash000/bin/zsh",
.section_index = &[_]shellImport.ShellIndex{
shellImport.ShellIndex{ .offset = 0, .len = 9 - 1 },
shellImport.ShellIndex{ .offset = 12 >> 2, .len = 8 - 1 },
},
};
2022-02-20 13:17:05 +02:00
test "construct PackedUser section" {
2022-02-19 16:56:30 +02:00
var buf = ArrayList(u8).init(testing.allocator);
defer buf.deinit();
2022-02-22 06:44:58 +02:00
const users = [_]User{ User{
.uid = 1000,
.gid = 1000,
.name = "vidmantas",
.gecos = "Vidmantas Kaminskas",
.home = "/home/vidmantas",
.shell = "/bin/bash",
}, User{
.uid = 1001,
.gid = 1001,
.name = "svc-foo",
.gecos = "Service Account",
.home = "/home/service1",
.shell = "/usr/bin/nologin",
}, User{
2022-02-22 15:04:59 +02:00
.uid = 0,
2022-03-06 13:11:06 +02:00
.gid = math.maxInt(u32),
2022-02-25 10:48:19 +02:00
.name = "Name" ** 8,
.gecos = "Gecos" ** 51,
.home = "Home" ** 16,
2022-02-25 15:17:24 +02:00
.shell = "She.LllL" ** 8,
2022-02-25 10:48:19 +02:00
}, User{
.uid = 1002,
.gid = 1002,
.name = "svc-bar",
.gecos = "",
.home = "/",
.shell = "/",
2022-02-22 06:44:58 +02:00
} };
var shellIndex = testShellIndex(testing.allocator);
defer shellIndex.deinit();
for (users) |user| {
try PackedUser.packTo(&buf, user, math.maxInt(u64), shellIndex);
try pad.arrayList(&buf, PackedUser.alignment_bits);
}
2022-02-20 13:17:05 +02:00
2022-02-27 06:09:46 +02:00
var i: u29 = 0;
var it1 = PackedUser.iterator(buf.items, test_shell_reader);
2022-03-08 20:44:32 +02:00
while (try it1.next()) |user| : (i += 1) {
2022-03-02 06:07:07 +02:00
try testing.expectEqual(users[i].uid, user.uid());
try testing.expectEqual(users[i].gid, user.gid());
try testing.expectEqual(
2022-03-08 20:44:32 +02:00
@as(u64, math.maxInt(u64)),
2022-03-02 06:07:07 +02:00
user.additionalGidsOffset(),
);
try testing.expectEqualStrings(users[i].name, user.name());
try testing.expectEqualStrings(users[i].gecos, user.gecos());
try testing.expectEqualStrings(users[i].home, user.home());
try testing.expectEqualStrings(users[i].shell, user.shell(test_shell_reader));
2022-02-27 06:09:46 +02:00
}
2022-03-02 06:07:07 +02:00
try testing.expectEqual(users.len, i);
2022-02-19 11:35:29 +02:00
}
2022-02-25 15:17:24 +02:00
2022-03-01 11:01:45 +02:00
test "User.clone" {
var allocator = testing.allocator;
const user = User{
.uid = 1000,
.gid = 1000,
.name = "vidmantas",
.gecos = "Vidmantas Kaminskas",
.home = "/home/vidmantas",
.shell = "/bin/bash",
};
var user2 = try user.clone(allocator);
defer user2.deinit(allocator);
try testing.expectEqualStrings(user.shell, "/bin/bash");
}