1
Fork 0
main
Motiejus Jakštys 2022-04-20 07:19:34 +03:00 committed by Motiejus Jakštys
parent 1a01175c46
commit ee603c47ca
4 changed files with 110 additions and 16 deletions

35
lib/File.zig Normal file
View File

@ -0,0 +1,35 @@
const std = @import("std");
const os = std.os;
const DB = @import("DB.zig");
const InvalidHeader = @import("header.zig").Invalid;
const File = @This();
db: DB,
ptr: []align(4096) const u8,
pub const Error = os.OpenError || os.FStatError || os.MMapError || InvalidHeader;
pub fn open(fname: []const u8) Error!File {
const fd = try os.open(fname, os.O.RDONLY, 0);
var fd_open = true;
errdefer {
if (fd_open) os.close(fd);
}
const st = try os.fstat(fd);
const size = @intCast(u64, st.size);
const ptr = try os.mmap(null, size, os.PROT.READ, os.MAP.SHARED, fd, 0);
errdefer os.munmap(ptr);
os.close(fd);
fd_open = false;
const db = try DB.fromBytes(ptr);
return File{ .db = db, .ptr = ptr };
}
pub fn close(self: *File) void {
os.munmap(self.ptr);
self.* = undefined;
}

View File

@ -1,15 +0,0 @@
const std = @import("std");
const os = std.os;
const DB = @import("DB.zig");
const PackedUser = @import("PackedUser.zig");
// State is a type of the global variable holding the process state:
// the DB handle and all the iterators.
const State = struct {
DB: *const DB,
getpwent_iterator: *PackedUser.Iterator,
};
// state is initialized on library startup.
var state: State = undefined;

74
lib/libnss.zig Normal file
View File

@ -0,0 +1,74 @@
const std = @import("std");
const os = std.os;
const fmt = std.fmt;
const mem = std.mem;
const DB = @import("DB.zig");
const File = @import("File.zig");
const CGroup = @import("Group.zig").CGroup;
const PackedGroup = @import("PackedGroup.zig");
const CUser = @import("User.zig").CUser;
const PackedUser = @import("PackedUser.zig");
const c = @cImport({
@cInclude("stdlib.h");
@cInclude("nss.h");
});
export const turbonss_default_path: [:0]const u8 = "/etc/turbonss/db.turbo";
// State is a type of the global variable holding the process state:
// the DB handle and all the iterators.
const State = struct {
file: ?File,
getpwent_iterator: ?PackedUser.Iterator,
getgrent_iterator: ?PackedGroup.Iterator,
omit_members: bool,
err_msg: [1024]u8,
};
// state is initialized on library startup.
var state: State = undefined;
// constructor
export fn _turbo_init() void {
const fname = os.getenvZ("TURBONSS_DB") orelse turbonss_default_path[0..];
state.file = File.open(fname) catch |err| {
_ = fmt.bufPrint(&state.err_msg, "open {s}: {s}", .{ fname, @errorName(err) }) catch return;
return;
};
const omit_members_env = os.getenvZ("TURBONSS_OMIT_MEMBERS") orelse "auto";
state.omit_members = shouldOmitMembers(omit_members_env, os.argv);
return;
}
fn shouldOmitMembers(env: []const u8, argv: [][*:0]u8) bool {
if (mem.eql(u8, env, "1")) return true;
if (mem.eql(u8, env, "0")) return false;
if (argv.len == 0) return false;
return mem.eql(u8, mem.sliceTo(argv[0], 0), "id");
}
// destructor
export fn _turbo_fini() void {
if (state.file) |*fooo|
fooo.close();
}
export fn _nss_turbo_getpwuid_r(
uid: c_uint,
res: *CUser,
buf: [*]u8,
len: usize,
errnop: *c_int,
) c.enum_nss_status {
_ = uid;
_ = res;
_ = buf;
_ = len;
_ = errnop;
return 0;
}

View File

@ -6,11 +6,11 @@ test "turbonss test suite" {
_ = @import("DB.zig");
_ = @import("Group.zig");
_ = @import("header.zig");
_ = @import("libnss.zig");
_ = @import("PackedGroup.zig");
_ = @import("PackedUser.zig");
_ = @import("padding.zig");
_ = @import("shell.zig");
_ = @import("State.zig");
_ = @import("User.zig");
_ = @import("validate.zig");
}