- OCaml 85%
- Go 13%
- Shell 2%
passwd #
Parse the Unix account databases /etc/passwd and /etc/group, and resolve a
Docker/runc-style user[:group] specifier to numeric ids.
Installation #
Install with opam:
$ opam install passwd
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install passwd
Overview #
A container image records the user its processes should run as: USER opam in
a Dockerfile, or "User": "opam" in an OCI image config. Turning that name into
the uid, primary gid and supplementary group ids the kernel needs means
reading /etc/passwd and /etc/group inside the image, exactly as
opencontainers/runc does at container start.
This library performs that resolution step. Parsing is pure: it works on the file contents and does no I/O, so the caller reads the databases (from a container root filesystem, for instance) and passes the bytes.
Usage #
let passwd = "root:x:0:0:root:/root:/bin/sh\nopam:x:1000:1000::/home/opam:/bin/sh\n"
let group = "opam:x:1000:\nwheel:x:10:opam\n"
let resolved =
let db = Passwd.v ~passwd ~group () in
match Passwd.resolve db "opam" with
| Ok { Passwd.uid; gid; additional_gids } -> Some (uid, gid, additional_gids)
| Error (`Msg _m) -> None
Resolution rules #
Following libcontainer/user.GetExecUser, with defaults uid = 0, gid = 0
and no supplementary groups:
- The
userpart, and the optionalgroupafter a:, may each be a name or a numeric id. - A
usermatching a passwd entry takes that entry'suidandgid. A numericuserwith no entry keeps thatuid; a nameduserwith no entry is an error. - An explicit
groupoverrides the primarygid. A numericgroupis accepted even with no group entry; a namedgroupwith no entry is an error. - With no
groupgiven, the supplementary gids are those of every group listing the resolved user as a member. With agroupgiven, there are none.
Related work #
- opencontainers/runc (Go) implements these rules in
libcontainer/user, which Docker and containerd use. peercredreads theuid,gidandpidof a connected Unix-socket peer. It identifies a running process, wherepasswdresolves the user an image is configured with.
License #
ISC. See LICENSE.md.