From bc7a7a03509c5a22864f1fa015ca9a4ce017bb62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 19 Apr 2022 13:50:29 +0300 Subject: [PATCH] getpwuid --- lib/DB.zig | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/DB.zig b/lib/DB.zig index a4ee78a..e8a959b 100644 --- a/lib/DB.zig +++ b/lib/DB.zig @@ -343,6 +343,16 @@ fn getpwnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory}!?C return try self.getUser(user, buf); } +// get a CUser entry by uid. +fn getpwuid(self: *const DB, uid: u32, buf: *[]u8) error{OutOfMemory}!?CUser { + const idx = bdz.search_u32(self.bdz_uid, uid); + const offset = self.idx_uid2user[idx]; + const nbits = PackedUser.alignment_bits; + const user = PackedUser.fromBytes(self.users[offset << nbits ..]).user; + if (uid != user.uid()) return null; + return try self.getUser(user, buf); +} + fn shellSections( allocator: Allocator, corpus: *const Corpus, @@ -633,8 +643,7 @@ test "getgrnam/getgrgid" { defer testing.allocator.free(buf); { - const doesnotexist = try db.getgrnam("doesnotexist", &buf); - try testing.expectEqual(doesnotexist, null); + try testing.expectEqual(try db.getgrnam("doesnotexist", &buf), null); const all = (try db.getgrnam("all", &buf)).?; try testing.expectEqual(all.gid, 9999); try testing.expectEqualStrings(all.name[0..3], "all"); @@ -648,8 +657,7 @@ test "getgrnam/getgrgid" { } { - const doesnotexist = try db.getgrgid(42, &buf); - try testing.expectEqual(doesnotexist, null); + try testing.expectEqual(try db.getgrgid(42, &buf), null); const all = (try db.getgrgid(9999, &buf)).?; try testing.expectEqual(all.gid, 9999); try testing.expectEqualStrings(all.name[0..3], "all"); @@ -661,18 +669,34 @@ test "getgrnam/getgrgid" { } test "getpwnam/getpwuid" { - var arena = ArenaAllocator.init(testing.allocator); - defer arena.deinit(); - var corpus = try Corpus.testCorpus(arena.allocator()); - var db = try DB.fromCorpus(arena.allocator(), &corpus); - var buf = try arena.allocator().alloc(u8, db.header.getpw_bufsize); + var corpus = try Corpus.testCorpus(testing.allocator); + defer corpus.deinit(); + var db = try DB.fromCorpus(testing.allocator, &corpus); + defer db.deinit(testing.allocator); + var buf = try testing.allocator.alloc(u8, db.header.getpw_bufsize); + defer testing.allocator.free(buf); - const vidmantas = (try db.getpwnam("vidmantas", &buf)).?; - try testing.expectEqual(vidmantas.pw_uid, 128); - try testing.expectEqual(vidmantas.pw_gid, 128); - try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas"); - try testing.expectEqualStrings(vidmantas.pw_gecos[0..19], "Vidmantas Kaminskas"); - try testing.expectEqualStrings(vidmantas.pw_dir[0..15], "/home/vidmantas"); + { + try testing.expectEqual(try db.getpwnam("doesnotexist", &buf), null); + const vidmantas = (try db.getpwnam("vidmantas", &buf)).?; + try testing.expectEqual(vidmantas.pw_uid, 128); + try testing.expectEqual(vidmantas.pw_gid, 128); + try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas"); + try testing.expectEqualStrings(vidmantas.pw_gecos[0..19], "Vidmantas Kaminskas"); + try testing.expectEqualStrings(vidmantas.pw_dir[0..15], "/home/vidmantas"); + } + + { + try testing.expectEqual(try db.getpwuid(123456, &buf), null); + const vidmantas = (try db.getpwuid(128, &buf)).?; + try testing.expectEqual(vidmantas.pw_uid, 128); + try testing.expectEqual(vidmantas.pw_gid, 128); + try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas"); + } + + _ = try db.getpwnam("Name" ** 8, &buf); + buf.len -= 1; + try testing.expectError(error.OutOfMemory, db.getpwnam("Name" ** 8, &buf)); } test "additionalGids" {