1
Fork 0
turbonss/src/user.zig

293 lines
8.5 KiB
Zig
Raw Normal View History

2022-02-18 17:34:50 +02:00
const std = @import("std");
2022-02-19 18:18:14 +02:00
const pad = @import("padding.zig");
2022-02-18 20:29:45 +02:00
2022-02-20 12:44:21 +02:00
const assert = std.debug.assert;
2022-02-18 20:29:45 +02:00
const Allocator = std.mem.Allocator;
2022-02-19 16:56:30 +02:00
const ArrayList = std.ArrayList;
2022-02-19 11:35:29 +02:00
const cast = std.math.cast;
2022-02-18 17:34:50 +02:00
pub const PackedUser = packed struct {
2022-02-18 17:34:50 +02:00
uid: u32,
gid: u32,
2022-02-19 11:35:29 +02:00
shell_here: bool,
2022-02-18 20:36:32 +02:00
shell_len_or_idx: u6,
2022-02-19 11:35:29 +02:00
home_len: u6,
name_is_a_suffix: bool,
name_len: u5,
2022-02-18 17:34:50 +02:00
gecos_len: u8,
padding: u5,
2022-02-20 13:17:05 +02:00
// blobSize returns the length of the blob storing string values.
pub fn blobLength(self: *const PackedUser) usize {
2022-02-22 06:44:58 +02:00
var result: usize = self.realHomeLen();
2022-02-20 13:17:05 +02:00
if (!self.name_is_a_suffix) {
2022-02-22 06:44:58 +02:00
result += self.realNameLen();
2022-02-20 13:17:05 +02:00
}
2022-02-22 06:44:58 +02:00
result += self.realGecosLen();
2022-02-20 13:17:05 +02:00
if (self.shell_here) {
2022-02-22 06:44:58 +02:00
result += self.realShellLen();
2022-02-20 13:17:05 +02:00
}
return result;
}
2022-02-22 06:44:58 +02:00
pub fn realHomeLen(self: *const PackedUser) usize {
2022-02-22 15:04:59 +02:00
return @as(u32, self.home_len) + 1;
2022-02-22 06:44:58 +02:00
}
pub fn realNameLen(self: *const PackedUser) usize {
2022-02-22 15:04:59 +02:00
return @as(u32, self.name_len) + 1;
2022-02-22 06:44:58 +02:00
}
pub fn realShellLen(self: *const PackedUser) usize {
2022-02-22 15:04:59 +02:00
return @as(u32, self.shell_len_or_idx) + 1;
2022-02-22 06:44:58 +02:00
}
pub fn realGecosLen(self: *const PackedUser) usize {
return self.gecos_len;
}
2022-02-18 17:34:50 +02:00
};
2022-02-20 09:47:47 +02:00
const PackedUserAlignmentBits = 3;
2022-02-18 17:34:50 +02:00
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,
};
// 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.
2022-02-19 11:35:29 +02:00
const shellIndexFnType = fn ([]const u8) ?u6;
2022-02-20 09:47:47 +02:00
2022-02-19 22:10:55 +02:00
appendTo: *ArrayList(u8),
shellIndexFn: shellIndexFnType,
2022-02-18 20:29:45 +02:00
2022-02-19 22:10:55 +02:00
pub fn init(appendTo: *ArrayList(u8), shellIndexFn: shellIndexFnType) UserWriter {
2022-02-18 20:29:45 +02:00
return UserWriter{
2022-02-19 16:56:30 +02:00
.appendTo = appendTo,
.shellIndexFn = shellIndexFn,
2022-02-18 20:29:45 +02:00
};
}
2022-02-20 09:47:47 +02:00
pub fn downCast(comptime T: type, n: u64) error{InvalidRecord}!T {
2022-02-20 09:19:59 +02:00
return std.math.cast(T, n) catch |err| switch (err) {
2022-02-20 09:10:25 +02:00
error.Overflow => {
return error.InvalidRecord;
},
2022-02-20 09:19:59 +02:00
};
2022-02-20 09:10:25 +02:00
}
2022-02-19 16:56:30 +02:00
pub fn appendUser(self: *UserWriter, user: User) !void {
2022-02-20 09:10:25 +02:00
const home_len = try downCast(u6, user.home.len - 1);
const name_len = try downCast(u5, user.name.len - 1);
const shell_len = try downCast(u6, user.shell.len - 1);
const gecos_len = try downCast(u8, user.gecos.len);
2022-02-19 11:35:29 +02:00
var puser = PackedUser{
2022-02-19 16:56:30 +02:00
.uid = user.uid,
.gid = user.gid,
.shell_here = self.shellIndexFn(user.shell) == null,
.shell_len_or_idx = self.shellIndexFn(user.shell) orelse shell_len,
2022-02-19 11:35:29 +02:00
.home_len = home_len,
2022-02-19 16:56:30 +02:00
.name_is_a_suffix = std.mem.endsWith(u8, user.home, user.name),
2022-02-19 11:35:29 +02:00
.name_len = name_len,
.gecos_len = gecos_len,
.padding = 0,
2022-02-19 11:35:29 +02:00
};
2022-02-19 16:56:30 +02:00
try self.appendTo.appendSlice(std.mem.asBytes(&puser));
try self.appendTo.appendSlice(user.home);
2022-02-19 11:35:29 +02:00
if (!puser.name_is_a_suffix) {
2022-02-19 16:56:30 +02:00
try self.appendTo.appendSlice(user.name);
2022-02-19 11:35:29 +02:00
}
2022-02-19 16:56:30 +02:00
try self.appendTo.appendSlice(user.gecos);
2022-02-19 11:35:29 +02:00
if (puser.shell_here) {
2022-02-19 16:56:30 +02:00
try self.appendTo.appendSlice(user.shell);
2022-02-19 11:35:29 +02:00
}
2022-02-19 18:18:14 +02:00
2022-02-20 09:10:25 +02:00
try self.appendTo.appendNTimes(0, pad.roundUpPadding(
u64,
2022-02-20 09:47:47 +02:00
PackedUserAlignmentBits,
2022-02-20 09:10:25 +02:00
self.appendTo.items.len,
));
2022-02-18 20:29:45 +02:00
}
};
2022-02-20 12:44:21 +02:00
pub const UserReader = struct {
2022-02-20 13:17:05 +02:00
const shellIndexProto = fn (u6) []const u8;
2022-02-20 12:44:21 +02:00
2022-02-20 13:17:05 +02:00
blob: []u8,
2022-02-20 12:44:21 +02:00
2022-02-20 14:13:06 +02:00
pub const PackedEntry = struct {
2022-02-20 12:44:21 +02:00
packed_user: *PackedUser,
blob: []const u8,
};
2022-02-20 14:13:06 +02:00
pub fn init(blob: []u8) UserReader {
2022-02-20 12:44:21 +02:00
return UserReader{
.blob = blob,
};
}
2022-02-20 13:17:05 +02:00
pub const PackedIterator = struct {
ur: *UserReader,
index: usize = 0,
2022-02-20 12:44:21 +02:00
2022-02-20 13:17:05 +02:00
pub fn next(it: *PackedIterator) ?PackedEntry {
2022-02-20 12:44:21 +02:00
if (it.index == it.ur.blob.len) return null;
2022-02-20 13:17:05 +02:00
assert(it.index < it.ur.blob.len);
2022-02-21 13:48:11 +02:00
const endUser = it.index + @sizeOf(PackedUser);
2022-02-20 13:17:05 +02:00
var packedUser = std.mem.bytesAsValue(
PackedUser,
it.ur.blob[it.index..endUser][0..@sizeOf(PackedUser)],
2022-02-20 13:17:05 +02:00
);
2022-02-22 06:44:58 +02:00
const startBlob = endUser;
2022-02-20 13:17:05 +02:00
const endBlob = startBlob + packedUser.blobLength();
2022-02-22 06:44:58 +02:00
it.index = pad.roundUp(usize, PackedUserAlignmentBits, endBlob);
2022-02-20 13:17:05 +02:00
return PackedEntry{
.packed_user = packedUser,
.blob = it.ur.blob[startBlob..endBlob],
};
2022-02-20 12:44:21 +02:00
}
};
2022-02-20 13:17:05 +02:00
pub fn packedIterator(self: *UserReader) PackedIterator {
return .{ .ur = self };
}
2022-02-20 14:13:06 +02:00
pub const Iterator = struct {
2022-02-22 06:44:58 +02:00
pit: PackedIterator,
2022-02-20 14:13:06 +02:00
shellIndex: shellIndexProto,
pub fn next(it: *Iterator) ?User {
2022-02-22 06:44:58 +02:00
const entry = it.pit.next() orelse return null;
const u = entry.packed_user;
const home = entry.blob[0..u.realHomeLen()];
2022-02-20 14:13:06 +02:00
var name: []const u8 = undefined;
var pos: usize = undefined;
2022-02-22 06:44:58 +02:00
if (u.name_is_a_suffix) {
const name_start = u.realHomeLen() - u.realNameLen();
name = entry.blob[name_start..u.realHomeLen()];
2022-02-22 15:04:59 +02:00
pos = u.realHomeLen();
2022-02-20 14:13:06 +02:00
} else {
2022-02-22 06:44:58 +02:00
const name_start = u.realHomeLen();
name = entry.blob[name_start .. name_start + u.realNameLen()];
pos = name_start + u.realNameLen();
2022-02-20 14:13:06 +02:00
}
2022-02-22 06:44:58 +02:00
const gecos = entry.blob[pos .. pos + u.realGecosLen()];
pos += u.realGecosLen();
2022-02-20 14:13:06 +02:00
var shell: []const u8 = undefined;
2022-02-22 06:44:58 +02:00
if (u.shell_here) {
shell = entry.blob[pos .. pos + u.realShellLen()];
2022-02-20 14:13:06 +02:00
} else {
2022-02-22 06:44:58 +02:00
shell = it.shellIndex(u.shell_len_or_idx);
2022-02-20 14:13:06 +02:00
}
return User{
2022-02-22 06:44:58 +02:00
.uid = u.uid,
.gid = u.gid,
.name = name,
.gecos = gecos,
2022-02-20 14:13:06 +02:00
.home = home,
2022-02-22 06:44:58 +02:00
.shell = shell,
2022-02-20 14:13:06 +02:00
};
}
};
pub fn iterator(self: *UserReader, shellIndex: shellIndexProto) Iterator {
return .{
.pit = self.packedIterator(),
.shellIndex = shellIndex,
};
}
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" {
try testing.expectEqual(@bitSizeOf(PackedUser), @sizeOf(PackedUser) * 8);
2022-02-18 17:34:50 +02:00
}
2022-02-19 11:35:29 +02:00
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;
}
2022-02-20 13:17:05 +02:00
fn testShell(index: u6) []const u8 {
return switch (index) {
0 => "/bin/bash",
1 => "/bin/zsh",
else => unreachable,
};
}
2022-02-19 11:35:29 +02:00
test "construct PackedUser blob" {
2022-02-19 16:56:30 +02:00
var buf = ArrayList(u8).init(testing.allocator);
defer buf.deinit();
2022-02-19 22:10:55 +02:00
var writer = UserWriter.init(&buf, testShellIndex);
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,
.gid = 4294967295,
.name = "n" ** 32,
.gecos = "g" ** 255,
.home = "h" ** 64,
.shell = "s" ** 64,
2022-02-22 06:44:58 +02:00
} };
2022-02-20 13:17:05 +02:00
for (users) |user| {
try writer.appendUser(user);
}
2022-02-20 14:13:06 +02:00
var rd = UserReader.init(buf.items);
2022-02-22 06:44:58 +02:00
{
var it = rd.packedIterator();
var i: u32 = 0;
while (it.next()) |entry| : (i += 1) {
try testing.expectEqual(users[i].uid, entry.packed_user.uid);
try testing.expectEqual(users[i].gid, entry.packed_user.gid);
}
}
{
var it = rd.iterator(testShell);
var i: u32 = 0;
while (it.next()) |user| : (i += 1) {
try testing.expectEqual(users[i].uid, user.uid);
try testing.expectEqual(users[i].gid, user.gid);
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);
}
2022-02-20 13:17:05 +02:00
}
2022-02-19 11:35:29 +02:00
}