1
Fork 0
turbonss/src/user.zig

121 lines
3.7 KiB
Zig

const std = @import("std");
const pad = @import("padding.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const cast = std.math.cast;
pub const PackedUserSize = @divExact(@bitSizeOf(PackedUser), 8);
pub const PackedUser = packed struct {
uid: u32,
gid: u32,
additional_gids_offset: u29,
shell_here: bool,
shell_len_or_idx: u6,
home_len: u6,
name_is_a_suffix: bool,
name_len: u5,
gecos_len: u8,
};
pub const User = struct {
uid: u32,
gid: u32,
name: []const u8,
gecos: []const u8,
home: []const u8,
shell: []const u8,
};
// UserWriter accepts a naive User struct and returns a PackedUser
pub const UserWriter = struct {
// shellIndexFnType is a signature for a function that accepts a shell
// string and returns it's index in the global shell section. Passing a
// function makes tests easier, and removes the Shell dependency of this
// module.
const shellIndexFnType = fn ([]const u8) ?u6;
appendTo: ArrayList(u8),
shellIndexFn: shellIndexFnType,
pub fn init(appendTo: ArrayList(u8), shellIndexFn: shellIndexFnType) UserWriter {
return UserWriter{
.appendTo = appendTo,
.shellIndexFn = shellIndexFn,
};
}
const fromUserErr = error{InvalidRecord};
pub fn appendUser(self: *UserWriter, user: User) !void {
const home_len = cast(u6, user.home.len - 1) catch return error.InvalidRecord;
const name_len = cast(u5, user.name.len - 1) catch return error.InvalidRecord;
const shell_len = cast(u6, user.shell.len - 1) catch return error.InvalidRecord;
const gecos_len = cast(u8, user.gecos.len) catch return error.InvalidRecord;
var puser = PackedUser{
.uid = user.uid,
.gid = user.gid,
.additional_gids_offset = std.math.maxInt(u29), // needs second pass
.shell_here = self.shellIndexFn(user.shell) == null,
.shell_len_or_idx = self.shellIndexFn(user.shell) orelse shell_len,
.home_len = home_len,
.name_is_a_suffix = std.mem.endsWith(u8, user.home, user.name),
.name_len = name_len,
.gecos_len = gecos_len,
};
try self.appendTo.appendSlice(std.mem.asBytes(&puser));
try self.appendTo.appendSlice(user.home);
if (!puser.name_is_a_suffix) {
try self.appendTo.appendSlice(user.name);
}
try self.appendTo.appendSlice(user.gecos);
if (puser.shell_here) {
try self.appendTo.appendSlice(user.shell);
}
const padding = pad.roundUpPadding(u64, 2, self.appendTo.items.len);
try self.appendTo.appendNTimes(0, padding);
}
};
const testing = std.testing;
test "PackedUser is byte-aligned" {
try testing.expectEqual(0, @rem(@bitSizeOf(PackedUser), 8));
}
fn testShellIndex(shell: []const u8) ?u6 {
if (std.mem.eql(u8, shell, "/bin/bash")) {
return 0;
} else if (std.mem.eql(u8, shell, "/bin/zsh")) {
return 1;
}
return null;
}
test "construct PackedUser blob" {
var buf = ArrayList(u8).init(testing.allocator);
defer buf.deinit();
var writer = UserWriter.init(buf, testShellIndex);
const user1 = User{
.uid = 1000,
.gid = 1000,
.name = "vidmantas",
.gecos = "Vidmantas Kaminskas",
.home = "/home/vidmantas",
.shell = "/bin/bash",
};
const user2 = User{
.uid = 1001,
.gid = 1001,
.name = "svc-foo",
.gecos = "Service Account",
.home = "/home/service1",
.shell = "/usr/bin/nologin",
};
try writer.appendUser(user1);
try writer.appendUser(user2);
}