1
Fork 0
turbonss/src/User.zig

260 lines
7.9 KiB
Zig
Raw Normal View History

const std = @import("std");
const mem = std.mem;
2022-04-10 21:58:43 +03:00
const fmt = std.fmt;
const maxInt = std.math.maxInt;
const Allocator = mem.Allocator;
2022-04-08 20:05:30 +03:00
const ArrayList = std.ArrayList;
2022-04-10 21:58:43 +03:00
const BoundedArray = std.BoundedArray;
2022-04-19 13:17:23 +03:00
const pw_passwd = "x\x00";
const User = @This();
2022-06-15 12:56:19 +03:00
const ErrCtx = @import("ErrCtx.zig");
2022-04-10 21:58:43 +03:00
const PackedUser = @import("PackedUser.zig");
const validate = @import("validate.zig");
uid: u32,
gid: u32,
name: []const u8,
gecos: []const u8,
home: []const u8,
shell: []const u8,
// deep-clones a User record with a given Allocator.
pub fn clone(
self: *const User,
allocator: Allocator,
) error{OutOfMemory}!User {
2022-06-15 12:56:19 +03:00
const name = try allocator.dupe(u8, self.name);
errdefer allocator.free(name);
const gecos = try allocator.dupe(u8, self.gecos);
errdefer allocator.free(gecos);
const home = try allocator.dupe(u8, self.home);
errdefer allocator.free(home);
const shell = try allocator.dupe(u8, self.shell);
errdefer allocator.free(shell);
return User{
.uid = self.uid,
.gid = self.gid,
2022-06-15 12:56:19 +03:00
.name = name,
.gecos = gecos,
.home = home,
.shell = shell,
};
}
2022-04-10 21:58:43 +03:00
// fromLine accepts a line of /etc/passwd (with or without the EOL) and makes a
// User.
2022-06-29 13:36:41 +03:00
fn fromLine(allocator: Allocator, err: *ErrCtx, line: []const u8) error{ InvalidRecord, OutOfMemory }!User {
2022-04-10 21:58:43 +03:00
var it = mem.split(u8, line, ":");
2022-06-29 13:55:53 +03:00
const name = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord);
_ = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord); // password
const uids = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord);
const gids = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord);
const gecos = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord);
const home = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord);
const shell = it.next() orelse return err.returnf("too few fields", .{}, error.InvalidRecord);
2022-07-02 09:14:02 +03:00
if (it.next() != null) return err.returnf("too many fields", .{}, error.InvalidRecord);
2022-06-15 13:34:37 +03:00
2022-06-29 13:55:53 +03:00
const uid = fmt.parseInt(u32, uids, 10) catch
return err.returnf("bad uid: {s}", .{uids}, error.InvalidRecord);
2022-06-15 13:34:37 +03:00
2022-06-29 13:55:53 +03:00
const gid = fmt.parseInt(u32, gids, 10) catch
return err.returnf("bad gid: {s}", .{gids}, error.InvalidRecord);
2022-06-15 13:34:37 +03:00
2022-06-29 13:55:53 +03:00
validate.name(name, err) catch
return err.returnf("invalid name '{s}'", .{name}, error.InvalidRecord);
validate.gecos(gecos, err) catch
return err.returnf("invalid gecos '{s}'", .{gecos}, error.InvalidRecord);
validate.path(home, err) catch
return err.returnf("invalid home '{s}'", .{home}, error.InvalidRecord);
validate.path(shell, err) catch
return err.returnf("invalid shell '{s}'", .{shell}, error.InvalidRecord);
2022-04-10 21:58:43 +03:00
const user = User{
.uid = uid,
.gid = gid,
2022-06-29 13:55:53 +03:00
.name = name,
.gecos = gecos,
.home = home,
.shell = shell,
2022-04-10 21:58:43 +03:00
};
return try user.clone(allocator);
}
fn strlen(self: *const User) usize {
return self.name.len +
self.gecos.len +
self.home.len +
self.shell.len;
}
2022-06-07 05:32:21 +03:00
const line_fmt = "{s}:x:{d}:{d}:{s}:{s}:{s}\n";
2022-07-11 14:14:21 +03:00
pub const max_line_len = fmt.count(line_fmt, .{
2022-06-05 23:56:45 +03:00
max_user.name,
max_user.uid,
max_user.gid,
max_user.gecos,
max_user.home,
max_user.shell,
});
2022-06-07 13:14:50 +03:00
// toLine formats the user in /etc/passwd format, including the EOL
pub fn toLine(self: *const User) BoundedArray(u8, max_line_len) {
2022-07-02 16:24:47 +03:00
var result = BoundedArray(u8, max_line_len).init(0) catch unreachable;
result.writer().print(line_fmt, .{
2022-06-07 13:14:50 +03:00
self.name,
2022-06-05 23:56:45 +03:00
self.uid,
self.gid,
self.gecos,
self.home,
self.shell,
2022-06-07 13:14:50 +03:00
}) catch unreachable;
2022-06-05 23:56:45 +03:00
return result;
}
2022-04-18 13:11:20 +03:00
// length of all string-data fields, assuming they are zero-terminated.
// Does not include password, since that's always static 'x\00'.
2022-04-18 13:11:20 +03:00
pub fn strlenZ(self: *const User) usize {
return self.strlen() + 4; // '\0' of name, gecos, home and shell
2022-04-18 13:11:20 +03:00
}
pub fn deinit(self: *User, allocator: Allocator) void {
2022-06-12 13:38:10 +03:00
allocator.free(self.name);
allocator.free(self.gecos);
allocator.free(self.home);
allocator.free(self.shell);
self.* = undefined;
}
2022-06-29 13:36:41 +03:00
pub fn fromReader(allocator: Allocator, err: *ErrCtx, reader: anytype) ![]User {
2022-04-08 20:05:30 +03:00
var users = ArrayList(User).init(allocator);
2022-06-15 12:56:19 +03:00
errdefer {
for (users.items) |*user| user.deinit(allocator);
users.deinit();
}
2022-07-11 14:14:21 +03:00
var buf: [max_line_len]u8 = undefined;
2022-06-05 23:56:45 +03:00
while (try reader.readUntilDelimiterOrEof(buf[0..], '\n')) |line| {
2022-06-29 13:36:41 +03:00
var user = try fromLine(allocator, err, line);
2022-06-05 23:56:45 +03:00
try users.append(user);
2022-04-08 20:05:30 +03:00
}
2022-06-05 23:56:45 +03:00
return users.toOwnedSlice();
2022-04-08 20:05:30 +03:00
}
2022-04-19 13:17:23 +03:00
pub const CUser = extern struct {
2022-07-06 16:54:43 +03:00
pw_name: [*:0]const u8,
pw_passwd: [*:0]const u8 = pw_passwd,
2022-04-19 13:17:23 +03:00
pw_uid: u32,
pw_gid: u32,
2022-07-06 16:54:43 +03:00
pw_gecos: [*:0]const u8,
pw_dir: [*:0]const u8,
pw_shell: [*:0]const u8,
2022-04-19 13:17:23 +03:00
};
const testing = std.testing;
2022-04-10 21:58:43 +03:00
pub const max_user = User{
.uid = maxInt(u32),
.gid = maxInt(u32),
.name = "Name" ** 8,
.gecos = "realname" ** 255 ++ "realnam",
.home = "Home" ** 16,
.shell = "She.LllL" ** 32,
};
2022-07-02 09:14:02 +03:00
test "User.fromLine" {
const examples = [_]struct {
line: []const u8,
want: ?User = null,
wantErr: []const u8 = &[_]u8{},
}{
.{
.line = "root:x:0:0:root:/root:/bin/bash",
.want = User{
.uid = 0,
.gid = 0,
.name = "root",
.gecos = "root",
.home = "/root",
.shell = "/bin/bash",
},
2022-06-28 19:55:33 +03:00
},
2022-07-02 09:14:02 +03:00
.{
.line = "žemas:x:0:0:root:/root:/bin/bash",
2022-07-02 16:17:10 +03:00
.wantErr = "invalid name 'žemas': invalid character 0xC5 at position 0",
2022-07-02 09:14:02 +03:00
},
.{
.line = "root:x:-1:0:root:/root:/bin/bash",
.wantErr = "bad uid: -1",
},
.{
.line = "root:x:0:-1:root:/root:/bin/bash",
.wantErr = "bad gid: -1",
},
.{
.line = "root:x:0:0:root:/root:bin/bash",
.wantErr = "invalid shell 'bin/bash': must start with /",
},
.{
.line = "root:x:0:0:root:/root:/bin/bash:redundant",
.wantErr = "too many fields",
},
.{
.line = "",
.wantErr = "too few fields",
},
.{
.line = "root:x:0:0:root:/root",
.wantErr = "too few fields",
},
};
2022-06-28 19:55:33 +03:00
const allocator = testing.allocator;
2022-07-02 09:14:02 +03:00
for (examples) |tt| {
2022-06-29 13:36:41 +03:00
var err = ErrCtx{};
2022-06-29 10:48:37 +03:00
2022-06-28 19:55:33 +03:00
if (tt.want) |want_user| {
2022-06-29 13:36:41 +03:00
var got = try fromLine(allocator, &err, tt.line);
2022-06-28 19:55:33 +03:00
defer got.deinit(allocator);
try testing.expectEqual(want_user.uid, got.uid);
try testing.expectEqual(want_user.gid, got.gid);
try testing.expectEqualStrings(want_user.name, got.name);
try testing.expectEqualStrings(want_user.gecos, got.gecos);
try testing.expectEqualStrings(want_user.home, got.home);
try testing.expectEqualStrings(want_user.shell, got.shell);
2022-07-02 09:14:02 +03:00
continue;
2022-06-28 19:55:33 +03:00
}
2022-07-02 09:14:02 +03:00
const got = fromLine(allocator, &err, tt.line);
try testing.expectError(error.InvalidRecord, got);
try testing.expectEqualStrings(tt.wantErr, err.unwrap().constSlice());
2022-06-28 19:55:33 +03:00
}
}
2022-07-12 12:59:47 +03:00
test "User max_user and max_str_len are consistent" {
2022-04-10 21:58:43 +03:00
const total_len = max_user.name.len +
max_user.gecos.len +
max_user.home.len +
max_user.shell.len;
try testing.expectEqual(total_len, PackedUser.max_str_len);
}
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");
}