1
Fork 0
turbonss/src/user.zig

308 lines
9.0 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-22 15:22:30 +02:00
gecos_len: u10,
padding: u3,
2022-02-20 13:17:05 +02:00
// blobLength returns the length of the blob storing string values.
2022-02-20 13:17:05 +02:00
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
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
}
pub fn validateUtf8(s: []const u8) error{InvalidRecord}!void {
if (!std.unicode.utf8ValidateSlice(s)) {
return error.InvalidRecord;
}
}
// FIXME(motiejus) record valiation should return a separate type. For User
// case, it should be length-bound slices and utf8-codepoints instead of
// strings.
//
// zig does not have error contexts
// (https://github.com/ziglang/zig/issues/2647) and length-limited slices.
// (It does have bounded_array, but that preallocates the maximum length,
// which is not great for User records). So I am using those excuses to
// do the validation here. I may move it once I learn the language better.
const appendUserErr = error{InvalidRecord} || Allocator.Error;
pub fn appendUser(self: *UserWriter, user: User) appendUserErr!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);
2022-02-22 15:22:30 +02:00
const gecos_len = try downCast(u10, user.gecos.len);
2022-02-19 11:35:29 +02:00
try validateUtf8(user.home);
try validateUtf8(user.name);
try validateUtf8(user.shell);
try validateUtf8(user.gecos);
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
section: []const u8,
shellIndex: shellIndexProto,
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,
section: []const u8,
2022-02-20 12:44:21 +02:00
};
pub fn init(section: []u8, shellIndex: shellIndexProto) UserReader {
2022-02-20 12:44:21 +02:00
return UserReader{
.section = section,
.shellIndex = shellIndex,
2022-02-20 12:44:21 +02:00
};
}
pub const Entry = struct {
user: User,
nextOffset: usize,
2022-02-20 12:44:21 +02:00
};
2022-02-20 13:17:05 +02:00
// atOffset returns a ?User in a given offset of the User section. Also,
// the offset to the next user.
pub fn atOffset(self: *UserReader, index: usize) ?Entry {
if (index == self.section.len) return null;
assert(index < self.section.len);
const endUser = index + @sizeOf(PackedUser);
var u = std.mem.bytesAsValue(
PackedUser,
self.section[index..endUser][0..@sizeOf(PackedUser)],
);
const startBlob = endUser;
const endBlob = startBlob + u.blobLength();
const section = self.section[startBlob..endBlob];
const home = section[0..u.realHomeLen()];
var name: []const u8 = undefined;
var pos: usize = undefined;
if (u.name_is_a_suffix) {
const name_start = u.realHomeLen() - u.realNameLen();
name = section[name_start..u.realHomeLen()];
pos = u.realHomeLen();
} else {
const name_start = u.realHomeLen();
name = section[name_start .. name_start + u.realNameLen()];
pos = name_start + u.realNameLen();
}
const gecos = section[pos .. pos + u.realGecosLen()];
pos += u.realGecosLen();
var shell: []const u8 = undefined;
if (u.shell_here) {
shell = section[pos .. pos + u.realShellLen()];
} else {
shell = self.shellIndex(u.shell_len_or_idx);
}
2022-02-20 14:13:06 +02:00
return Entry{
.user = 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,
},
.nextOffset = pad.roundUp(usize, PackedUserAlignmentBits, endBlob),
};
}
pub const Iterator = struct {
ur: *UserReader,
offset: usize = 0,
pub fn next(it: *Iterator) ?User {
if (it.ur.atOffset(it.offset)) |result| {
it.offset = result.nextOffset;
return result.user;
}
return null;
2022-02-20 14:13:06 +02:00
}
};
pub fn iterator(self: *UserReader) Iterator {
return Iterator{
.ur = self,
.offset = 0,
2022-02-20 14:13:06 +02:00
};
}
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,
};
}
test "construct PackedUser section" {
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,
2022-02-22 15:22:30 +02:00
.gecos = "g" ** 1023,
2022-02-22 15:04:59 +02:00
.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);
}
var rd = UserReader.init(buf.items, testShell);
var it = rd.iterator();
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
}