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");
|
|
|
|
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-02-25 15:20:35 +02:00
|
|
|
const Allocator = mem.Allocator;
|
2022-02-27 06:09:46 +02:00
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
// 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
|
|
|
|
// serialization. Iterator can help retrieve these records.
|
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-02-26 11:20:44 +02:00
|
|
|
pub const PackedUserMut = packedUser(false);
|
|
|
|
pub const PackedUserConst = packedUser(true);
|
|
|
|
|
|
|
|
fn packedUser(immutable: bool) type {
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
const alignmentBits = 3;
|
2022-02-27 06:09:46 +02:00
|
|
|
const shell2idxProto = fn ([]const u8) ?u6;
|
2022-02-26 11:20:44 +02:00
|
|
|
|
|
|
|
const InnerSize = @divExact(@bitSizeOf(Inner), 8);
|
|
|
|
const Inner = packed struct {
|
|
|
|
uid: u32,
|
|
|
|
gid: u32,
|
2022-02-27 15:49:00 +02:00
|
|
|
additional_gids_offset: u29,
|
2022-02-26 11:20:44 +02:00
|
|
|
shell_len_or_idx: u6,
|
2022-02-27 15:49:00 +02:00
|
|
|
shell_here: bool,
|
2022-02-26 11:20:44 +02:00
|
|
|
home_len: u6,
|
|
|
|
name_is_a_suffix: bool,
|
|
|
|
name_len: u5,
|
|
|
|
gecos_len: u8,
|
|
|
|
|
|
|
|
fn homeLen(self: *const Inner) usize {
|
|
|
|
return @as(u32, self.home_len) + 1;
|
2022-02-25 10:48:19 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 11:20:44 +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-24 10:13:31 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
fn nameLen(self: *const Inner) usize {
|
|
|
|
return @as(u32, self.name_len) + 1;
|
2022-02-25 10:48:19 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 11:20:44 +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 13:59:35 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
fn gecosLen(self: *const Inner) usize {
|
|
|
|
return self.gecos_len;
|
|
|
|
}
|
2022-02-25 10:48:19 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
fn maybeShellStart(self: *const Inner) usize {
|
|
|
|
assert(self.shell_here);
|
|
|
|
return self.gecosStart() + self.gecosLen();
|
|
|
|
}
|
2022-02-24 10:13:31 +02:00
|
|
|
|
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
|
|
|
}
|
2022-02-26 11:20:44 +02:00
|
|
|
|
|
|
|
// blobLength returns the length of the blob storing string values.
|
|
|
|
fn blobLength(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;
|
2022-02-24 10:13:31 +02:00
|
|
|
}
|
2022-02-26 11:20:44 +02:00
|
|
|
};
|
2022-02-24 10:13:31 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
const Bytes = if (immutable) []const u8 else []u8;
|
|
|
|
const InnerPointer = if (immutable) *const Inner else *Inner;
|
2022-02-24 10:13:31 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
// PackedUser does not allocate; it re-interprets the "bytes" blob field.
|
|
|
|
// Both of those fields are pointers to "our representation" of that field.
|
|
|
|
inner: InnerPointer,
|
|
|
|
userdata: Bytes,
|
|
|
|
|
|
|
|
pub const Entry = struct {
|
|
|
|
user: Self,
|
|
|
|
next: ?Bytes,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn fromBytes(bytes: Bytes) Entry {
|
2022-02-27 06:09:46 +02:00
|
|
|
const inner = mem.bytesAsValue(
|
2022-02-26 11:20:44 +02:00
|
|
|
Inner,
|
|
|
|
// Should use InnerSize instead of sizeOf, see
|
|
|
|
// https://github.com/ziglang/zig/issues/10958
|
|
|
|
bytes[0..@sizeOf(Inner)],
|
|
|
|
);
|
|
|
|
|
|
|
|
const startBlob = InnerSize;
|
|
|
|
const endBlob = startBlob + inner.blobLength();
|
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
|
2022-02-26 11:20:44 +02:00
|
|
|
var next: ?Bytes = null;
|
|
|
|
if (nextStart < bytes.len) {
|
|
|
|
next = bytes[nextStart..];
|
|
|
|
}
|
2022-02-25 10:48:19 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
return Entry{
|
|
|
|
.user = Self{
|
|
|
|
.inner = inner,
|
|
|
|
.userdata = bytes[startBlob..endBlob],
|
|
|
|
},
|
|
|
|
.next = next,
|
|
|
|
};
|
2022-02-25 10:48:19 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
pub const Iterator = struct {
|
|
|
|
section: ?Bytes,
|
|
|
|
shellIndex: Idx2ShellProto,
|
2022-02-18 20:29:45 +02:00
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
pub fn next(it: *Iterator) ?Self {
|
|
|
|
if (it.section) |section| {
|
|
|
|
const entry = Self.fromBytes(section);
|
|
|
|
it.section = entry.next;
|
|
|
|
return entry.user;
|
|
|
|
}
|
|
|
|
return null;
|
2022-02-26 11:20:44 +02:00
|
|
|
}
|
2022-02-27 15:49:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn iterator(section: Bytes, idxFn: Idx2ShellProto) Iterator {
|
|
|
|
return Iterator{
|
|
|
|
.section = section,
|
|
|
|
.shellIndex = idxFn,
|
|
|
|
};
|
2022-02-23 06:07:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
// 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.
|
2022-02-27 15:49:00 +02:00
|
|
|
const packErr = InvalidRecord || Allocator.Error;
|
2022-02-26 11:20:44 +02:00
|
|
|
pub fn packTo(
|
|
|
|
arr: *ArrayList(u8),
|
|
|
|
user: User,
|
2022-02-27 06:09:46 +02:00
|
|
|
idxFn: shell2idxProto,
|
2022-02-26 11:20:44 +02:00
|
|
|
) packErr!void {
|
|
|
|
// function arguments are consts. We need to mutate the underlying
|
|
|
|
// slice, so passing it via pointer instead.
|
2022-02-27 15:49:00 +02:00
|
|
|
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);
|
2022-02-26 11:20:44 +02:00
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
try validate.utf8(user.home);
|
|
|
|
try validate.utf8(user.name);
|
|
|
|
try validate.utf8(user.shell);
|
|
|
|
try validate.utf8(user.gecos);
|
2022-02-26 11:20:44 +02:00
|
|
|
|
|
|
|
const inner = Inner{
|
|
|
|
.uid = user.uid,
|
|
|
|
.gid = user.gid,
|
|
|
|
.additional_gids_offset = std.math.maxInt(u29),
|
|
|
|
.shell_here = idxFn(user.shell) == null,
|
|
|
|
.shell_len_or_idx = idxFn(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,
|
|
|
|
};
|
|
|
|
const innerBytes = mem.asBytes(&inner);
|
|
|
|
|
|
|
|
// innerBytes.len is longer than InnerSize. We want to copy
|
|
|
|
// only the InnerSize-number of bytes.
|
|
|
|
try arr.*.appendSlice(innerBytes[0..InnerSize]);
|
|
|
|
try arr.*.appendSlice(user.home);
|
|
|
|
|
|
|
|
if (!inner.name_is_a_suffix) {
|
|
|
|
try arr.*.appendSlice(user.name);
|
|
|
|
}
|
|
|
|
try arr.*.appendSlice(user.gecos);
|
2022-02-25 15:17:24 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
if (inner.shell_here) {
|
|
|
|
try arr.*.appendSlice(user.shell);
|
|
|
|
}
|
2022-02-19 11:35:29 +02:00
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
try pad.arrayList(arr, alignmentBits);
|
2022-02-19 11:35:29 +02:00
|
|
|
}
|
2022-02-25 10:48:19 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
// maxSize is the maximum number of records a PackedUser can take
|
|
|
|
// (struct + userdata).
|
|
|
|
pub fn maxSize() usize {
|
|
|
|
comptime {
|
|
|
|
const unpadded = InnerSize +
|
|
|
|
std.math.maxInt(u6) + 1 + // home
|
|
|
|
std.math.maxInt(u5) + 1 + // name
|
|
|
|
std.math.maxInt(u6) + 1 + // shell
|
|
|
|
std.math.maxInt(u8); // gecos
|
2022-02-27 15:49:00 +02:00
|
|
|
return pad.roundUp(u64, alignmentBits, unpadded);
|
2022-02-26 11:20:44 +02:00
|
|
|
}
|
2022-02-19 11:35:29 +02:00
|
|
|
}
|
2022-02-19 18:18:14 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
pub fn uid(self: Self) u32 {
|
|
|
|
return self.inner.uid;
|
|
|
|
}
|
2022-02-24 10:13:31 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
pub fn gid(self: Self) u32 {
|
|
|
|
return self.inner.gid;
|
2022-02-25 10:48:19 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 06:09:46 +02:00
|
|
|
pub fn additionalGidsOffset(self: Self) u29 {
|
|
|
|
return self.inner.additional_gids_offset;
|
|
|
|
}
|
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
pub fn home(self: Self) []const u8 {
|
|
|
|
return self.userdata[0..self.inner.homeLen()];
|
|
|
|
}
|
2022-02-25 10:48:19 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
pub fn name(self: Self) []const u8 {
|
|
|
|
const name_pos = self.inner.nameStart();
|
|
|
|
const name_len = self.inner.nameLen();
|
|
|
|
return self.userdata[name_pos .. name_pos + name_len];
|
|
|
|
}
|
2022-02-24 10:13:31 +02:00
|
|
|
|
2022-02-26 11:20:44 +02:00
|
|
|
pub fn gecos(self: Self) []const u8 {
|
|
|
|
const gecos_pos = self.inner.gecosStart();
|
|
|
|
const gecos_len = self.inner.gecosLen();
|
|
|
|
return self.userdata[gecos_pos .. gecos_pos + gecos_len];
|
|
|
|
}
|
2022-02-24 10:13:31 +02:00
|
|
|
|
2022-02-27 06:09:46 +02:00
|
|
|
pub fn shell(self: Self, idxFn: Idx2ShellProto) []const u8 {
|
2022-02-26 11:20:44 +02:00
|
|
|
if (self.inner.shell_here) {
|
|
|
|
const shell_pos = self.inner.maybeShellStart();
|
|
|
|
const shell_len = self.inner.shellLen();
|
|
|
|
return self.userdata[shell_pos .. shell_pos + shell_len];
|
|
|
|
}
|
|
|
|
return idxFn(self.inner.shell_len_or_idx);
|
|
|
|
}
|
2022-02-24 10:13:31 +02:00
|
|
|
|
2022-02-27 06:09:46 +02:00
|
|
|
pub fn setAdditionalGidsOffset(self: Self, new: u29) void {
|
|
|
|
// TODO(motiejus) how to not declare function for const PackedUser at all?
|
|
|
|
if (immutable) {
|
|
|
|
@compileError("this function is available only for mutable PackedUsers");
|
|
|
|
}
|
|
|
|
self.inner.additional_gids_offset = new;
|
|
|
|
}
|
2022-02-20 12:44:21 +02:00
|
|
|
};
|
2022-02-25 10:48:19 +02:00
|
|
|
}
|
2022-02-20 12:44:21 +02:00
|
|
|
|
2022-02-18 17:34:50 +02:00
|
|
|
const testing = std.testing;
|
|
|
|
|
2022-02-22 15:16:45 +02:00
|
|
|
test "PackedUser internal and external alignment" {
|
2022-02-27 15:49:00 +02:00
|
|
|
// External padding (alignmentBits) must be higher or equal to
|
2022-02-24 05:32:27 +02:00
|
|
|
// the "internal" PackedUser alignment. By aligning PackedUser we are also
|
|
|
|
// working around https://github.com/ziglang/zig/issues/10958 ; PackedUser
|
|
|
|
// cannot be converted from/to [@bitSizeOf(PackedUser)/8]u8;
|
|
|
|
// asBytes/bytesAsValue use @sizeOf, which is larger. Now we are putting no
|
|
|
|
// more than 1, but it probably could be higher.
|
2022-02-27 06:09:46 +02:00
|
|
|
try testing.expectEqual(8, @sizeOf(PackedUserConst.Inner) * 8 - @bitSizeOf(PackedUserConst.Inner));
|
2022-02-18 17:34:50 +02:00
|
|
|
}
|
2022-02-19 11:35:29 +02:00
|
|
|
|
|
|
|
fn testShellIndex(shell: []const u8) ?u6 {
|
2022-02-25 15:20:35 +02:00
|
|
|
if (mem.eql(u8, shell, "/bin/bash")) {
|
2022-02-19 11:35:29 +02:00
|
|
|
return 0;
|
2022-02-25 15:20:35 +02:00
|
|
|
} else if (mem.eql(u8, shell, "/bin/zsh")) {
|
2022-02-19 11:35:29 +02:00
|
|
|
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-23 06:07:53 +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-02-25 15:20:35 +02:00
|
|
|
.gid = std.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
|
|
|
} };
|
2022-02-20 13:17:05 +02:00
|
|
|
for (users) |user| {
|
2022-02-27 06:09:46 +02:00
|
|
|
try PackedUserConst.packTo(&buf, user, testShellIndex);
|
2022-02-20 13:17:05 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 06:09:46 +02:00
|
|
|
var i: u29 = 0;
|
|
|
|
{
|
|
|
|
var it = PackedUserConst.iterator(buf.items, testShell);
|
|
|
|
while (it.next()) |user| : (i += 1) {
|
|
|
|
try testing.expectEqual(users[i].uid, user.uid());
|
|
|
|
try testing.expectEqual(users[i].gid, user.gid());
|
|
|
|
try testing.expectEqual(
|
|
|
|
@as(u29, std.math.maxInt(u29)),
|
|
|
|
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(testShell));
|
|
|
|
}
|
|
|
|
try testing.expectEqual(users.len, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
var it = PackedUserMut.iterator(buf.items, testShell);
|
|
|
|
i = 0;
|
|
|
|
while (it.next()) |user| : (i += 1) {
|
|
|
|
user.setAdditionalGidsOffset(i);
|
|
|
|
}
|
|
|
|
try testing.expectEqual(users.len, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
var it = PackedUserConst.iterator(buf.items, testShell);
|
|
|
|
i = 0;
|
|
|
|
while (it.next()) |user| : (i += 1) {
|
|
|
|
try testing.expectEqual(users[i].gid, user.gid());
|
|
|
|
try testing.expectEqual(i, user.additionalGidsOffset());
|
|
|
|
try testing.expectEqualStrings(users[i].name, user.name());
|
|
|
|
}
|
|
|
|
try testing.expectEqual(users.len, i);
|
2022-02-25 10:48:19 +02:00
|
|
|
}
|
2022-02-19 11:35:29 +02:00
|
|
|
}
|
2022-02-25 15:17:24 +02:00
|
|
|
|
|
|
|
test "PackedUser.maxSize()" {
|
2022-02-25 15:20:35 +02:00
|
|
|
// TODO(motiejus) try using a slice that points to an array in stack.
|
|
|
|
// As of writing, I am getting a stack smashing error.
|
2022-02-26 11:20:44 +02:00
|
|
|
var buf = try ArrayList(u8).initCapacity(testing.allocator, PackedUserConst.maxSize());
|
2022-02-25 15:17:24 +02:00
|
|
|
defer buf.deinit();
|
|
|
|
|
|
|
|
const largeUser = User{
|
2022-02-25 15:20:35 +02:00
|
|
|
.uid = std.math.maxInt(u32),
|
|
|
|
.gid = std.math.maxInt(u32),
|
2022-02-25 15:17:24 +02:00
|
|
|
.name = "Name" ** 8, // 32
|
|
|
|
.gecos = "Gecos" ** 51, // 255
|
|
|
|
.home = "Home" ** 16, // 64
|
|
|
|
.shell = "She.LllL" ** 8, // 64
|
|
|
|
};
|
2022-02-26 11:20:44 +02:00
|
|
|
try PackedUserConst.packTo(&buf, largeUser, testShellIndex);
|
|
|
|
try testing.expectEqual(PackedUserConst.maxSize(), buf.items.len);
|
2022-02-25 15:17:24 +02:00
|
|
|
}
|