Skip to reference

std.os.users

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.os.users — the machine's ACCOUNTS, as its two plain files write them: a user line of passwd(5) and a group line of group(5). Stability tier: core.

Pure parsing, and nothing else. Every function here takes TEXT the caller already holds and answers from it. Nothing in this module opens a file, asks a name service (NSS, a directory server, a cache), or reaches the host at all: it carries no capability and it runs at comptime. The program that wants the machine's accounts reads them itself and says where from — users.user_named(fs.read_text("/etc/passwd")?, "wolf") — so the file it trusted is in its own source, which is what su, login, id and ls -l need and all a module without a name service can honestly offer. A host that keeps its accounts somewhere else (a directory service, macOS's for most of its users) is not answered by these files, and this module does not pretend otherwise — the next paragraph says exactly what that costs.

What the two files cannot see (wolf-std#68, ruling #99 R2: the limit is documented, and the fix is a builtin, not a better reader). macOS keeps every ordinary user — every account System Settings makes — and every local user's group memberships in its directory service (Open Directory, whose local node is /var/db/dslocal/nodes/Default). Its /etc/passwd and /etc/group carry root, nobody, daemon and the system's _ accounts, and the password file's own header says it is "consulted directly only when the system is running in single-user mode". On linux the same is true of every account nsswitch.conf sends elsewhere: LDAP, NIS, SSSD, systemd's dynamic users. For such an account each lookup here answers none, users and groups leave it out, and groups_of gives the primary id it was handed and nothing more — so id NAME, whoami, chown user:group, chgrp and groups written over this module say "no such user" where the host's own tools answer. bu24 measured it: 30 name-printing cases skip on CI's macOS runner, whose own account is the directory service's, and tests/os/users/directory_accounts.lu holds it as a row there: the host names the runner's uid, /etc/passwd answers none for it by id and by name, and groups_of misses groups the process holds.

A pure reader cannot see a name service, so nothing here will grow one. The future fix is a host account lookup — the C library's getpwnam, getpwuid, getgrnam, getgrgid and getgrouplist, which ask whatever the host is configured to ask — as wolf builtins, asked of wolf-lang from wolf-std#68; when they land, host lookups ship BESIDE these functions and every function here keeps its answer. Until then two things still work on every host: the NUMBERS (std.env.uid, std.env.groups, std.fs.stat answer ids whatever keeps the names, and printing the number when a lookup is none is what id itself does for an id no name service knows), and the host's own tool, started through std.process — /usr/bin/id -un is how the row above learns the name the files do not hold.

What it is for. std.env.uid and std.fs.stat answer NUMBERS; this is where a number gets its name and a name its number, and where the three arguments of std.process.set_ids come from: user_named gives the user and group ids, groups_of the supplementary groups.

The two lines.

name:password:uid:gid:comment:home:shell         a user  (7 fields)
name:password:gid:member,member,…                a group (4 fields)

The fields are cut at : and at nothing else; none is trimmed. A line is well formed when it has exactly that many fields, its name is not empty and its ids are decimal. The password field is carried by neither type: on every current host it is a placeholder (x, *), the real one being in a file this module does not read.

An id is 0..=4294967294. A leading - is read the way the host reads it — as that many below 4294967296 — because macOS writes nobody as -2, which is 4294967294; -1 is therefore no id (it is every host call's "leave this one alone") and a line carrying it is malformed.

Errors are rows, never traps (D30, §2). Two tags:

- parse — one line is not a user (or a group) line: parse_user, parse_group. - none — the text holds no such account: the four lookups.

The readers of a whole text (users, groups, the lookups, groups_of) SKIP what is not an account line — a blank line, a # comment (macOS's file opens with ten), a +/- compatibility entry, a malformed line — instead of failing the file for it: one bad line must not lock every user out, and parse_user is there for the program that wants to report it.

Types and traits

User

struct · Source line 94

pub struct User { name: str, uid: int, gid: int, comment: str, home: str, shell: str, }

One user: a line of passwd(5), less its password field.

Group

struct · Source line 110

pub struct Group { name: str, gid: int, members: List[str], }

One group: a line of group(5), less its password field.

Functions and methods

parse_user

fn · Source line 172

pub fn parse_user(line: str) -> User ! {parse}

One user line, parsed.

Total over text and PURE — comptime-safe, reaching no host. The line is taken as written, without its newline.

Rows: parse when line does not have exactly seven :-separated fields, its name is empty, or its user or group id is not a decimal id (the module header says what an id is). Never traps.

let u = users.parse_user("wolf:x:1000:1000:A Wolf:/home/wolf:/bin/sh")?
u.name == "wolf" && u.uid == 1000 && u.gid == 1000
u.comment == "A Wolf" && u.home == "/home/wolf" && u.shell == "/bin/sh"
let nobody = users.parse_user("nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false")?
nobody.uid == 4294967294

parse_group

fn · Source line 197

pub fn parse_group(line: str) -> Group ! {parse}

One group line, parsed.

Total over text and PURE. The member list is cut at ,; an empty piece (a trailing comma, an empty list) is not a member.

Rows: parse when line does not have exactly four :-separated fields, its name is empty, or its group id is not a decimal id. Never traps.

let g = users.parse_group("pack:x:2000:wolf,cub")?
g.name == "pack" && g.gid == 2000 && g.members.len == 2 && g.members[1] == "cub"
let empty = users.parse_group("lone:x:2001:")?
empty.members.len == 0

users

fn · Source line 226

pub fn users(text: str) -> List[User]

Every user in text — the text of a passwd(5) file — in the file's order.

Total and PURE. A line that is not a user line (blank, a # comment, a +/- entry, malformed) is passed over; parse_user says why one line is refused. Every user the FILE holds, not every user the host has: on macOS the list read from /etc/passwd is root, nobody, daemon and the _ system accounts, never the people (the module header's "What the two files cannot see").

let all = users.users("# accounts\nroot:x:0:0:root:/root:/bin/sh\nbroken line\nwolf:x:1000:1000::/home/wolf:\n")
all.len == 2 && all[0].name == "root" && all[1].uid == 1000

groups

fn · Source line 252

pub fn groups(text: str) -> List[Group]

Every group in text — the text of a group(5) file — in the file's order.

Total and PURE. Lines that are not group lines are passed over, as users passes them. Every group the FILE holds, with the members the file writes: a group or a membership the host keeps elsewhere (macOS's directory service, LDAP, NIS) is not in it.

let all = users.groups("root:x:0:\npack:x:2000:wolf,cub\n\n")
all.len == 2 && all[1].name == "pack" && all[1].members[0] == "wolf"

user_named

fn · Source line 283

pub fn user_named(text: str, name: str) -> User ! {none}

The FIRST user in text named name — what su name looks up.

PURE. The first line wins, as it does for the host's own reader.

Rows: none when no user line carries that name — which is also the answer for an account the host keeps outside the file (macOS's directory service, LDAP, NIS), however well the host knows it; the module header says what that costs su, chown and id. Never traps.

let text = "root:x:0:0:root:/root:/bin/sh\nwolf:x:1000:1000::/home/wolf:/bin/sh\n"
let u = users.user_named(text, "wolf")?
u.uid == 1000 && u.home == "/home/wolf"
let missing = users.user_named(text, "cub") else |_| users.parse_user("none:x:7:7:::")?
missing.uid == 7

user_by_id

fn · Source line 307

pub fn user_by_id(text: str, uid: int) -> User ! {none}

The FIRST user in text whose id is uid — the name ls -l and id print for a number.

PURE. Two names may share an id; the first line wins.

Rows: none when no user line carries that id — also for an id whose account the host keeps outside the file, which on macOS is the id of every ordinary user (id and ls -l print the NUMBER then). Never traps.

let text = "root:x:0:0:root:/root:/bin/sh\nwolf:x:1000:1000::/home/wolf:/bin/sh\n"
let u = users.user_by_id(text, 0)?
u.name == "root"

group_named

fn · Source line 327

pub fn group_named(text: str, name: str) -> Group ! {none}

The FIRST group in text named name — what chgrp name looks up.

PURE.

Rows: none when no group line carries that name, including a group the host keeps outside the file (the module header). Never traps.

let g = users.group_named("root:x:0:\npack:x:2000:wolf\n", "pack")?
g.gid == 2000

group_by_id

fn · Source line 347

pub fn group_by_id(text: str, gid: int) -> Group ! {none}

The FIRST group in text whose id is gid.

PURE.

Rows: none when no group line carries that id, including a group the host keeps outside the file (the module header). Never traps.

let g = users.group_by_id("root:x:0:\npack:x:2000:wolf\n", 2000)?
g.name == "pack"

groups_of

fn · Source line 381

pub fn groups_of(text: str, name: str, gid: int) -> List[int]

The groups user name belongs to, by the group file's text: gid — the user's own group, from their User.gid — first, then the id of every group that lists name as a member, in the file's order, each id once.

This is the list a login gives a process (initgroups, as far as the file alone decides it), and the third argument of std.process.set_ids: process.set_ids(u.uid, u.gid, users.groups_of(group_text, u.name, u.gid)).

As far as the file alone decides it, and no further. A membership the host records elsewhere is not here: macOS writes no local user into /etc/group (the directory service holds every membership), so for such a user this answers [gid] alone while the process the host started holds more (std.env.groups() reads those). A program that drops privileges to a directory-service user over this list drops them further than a login would.

Total and PURE.

let text = "root:x:0:\nwolf:x:1000:\npack:x:2000:cub,wolf\nden:x:3000:wolf\nfox:x:4000:fox\n"
let gs = users.groups_of(text, "wolf", 1000)
gs.len == 3 && gs[0] == 1000 && gs[1] == 2000 && gs[2] == 3000

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: none, parse. Their conditions are documented on the functions that return them.

No public declarations in this section.