Map Windows SIDs to Linux uids and gids, and back, in pure Go.
When users are managed centrally in Active Directory and your Linux hosts are joined to the domain, every user and group has two identities: a SID on the Windows side and a uid or gid on the Linux side. sidmap answers the two questions that come up:
- What is the uid or gid of this SID?
- What is the SID of this uid or gid?
It asks the identity service already running on the host (SSSD or winbind), so the answers are the same ones the host itself uses. There is no cgo, so it builds with CGO_ENABLED=0 and works in static binaries.
go get github.com/bete7512/sidmapRequires Go 1.22 or later and Linux.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/bete7512/sidmap"
)
func main() {
// Use whichever service the host runs: SSSD first, then winbind.
r, err := sidmap.Detect()
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Optional: remember answers for 5 minutes, misses for 30 seconds.
r = sidmap.WithCache(r, 5*time.Minute, 30*time.Second)
ctx := context.Background()
id, err := r.SIDToID(ctx, "S-1-5-21-1111-2222-3333-1104")
if err != nil {
log.Fatal(err)
}
fmt.Println(id.Kind, id.ID) // uid 10000
sid, err := r.IDToSID(ctx, sidmap.UID(10000))
if err != nil {
log.Fatal(err)
}
fmt.Println(sid) // S-1-5-21-1111-2222-3333-1104
}Use sidmap.UID(n) for a user and sidmap.GID(n) for a group. They are kept apart because a uid and a gid can share the same number and still belong to different objects.
To pick a backend yourself, call sidmap.NewSSSD() or sidmap.NewWinbind() instead of Detect().
| Backend | How it asks | Needs |
|---|---|---|
| SSSD | Speaks the SSSD protocol over /var/lib/sss/pipes/nss |
SSSD running |
| winbind | Runs Samba's wbinfo program |
wbinfo on PATH, winbindd running |
The winbind backend starts one wbinfo process per lookup, so wrap it in WithCache if you do many lookups.
Check errors with errors.Is:
| Error | Meaning |
|---|---|
sidmap.ErrNotFound |
The service answered: there is no mapping. |
sidmap.ErrUnavailable |
The service could not be reached. Nothing is known about the mapping. |
sidmap.ErrInvalid |
The SID or ID you passed is malformed. |
These are separate on purpose. A stopped service is never reported as a missing user.
SIDToID can return the kind sidmap.Both when the host uses one number as both the uid and the gid of a SID.
sidmap.WithSSSDSocket("/custom/path/nss") // SSSD socket path
sidmap.WithWbinfo("/usr/local/bin/wbinfo") // wbinfo program
sidmap.WithTimeout(2 * time.Second) // limit per lookup, default 5sYou don't need a real domain to test code that uses sidmap. The sidmaptest package runs a fake SSSD on a temporary socket:
func TestMyCode(t *testing.T) {
fake := sidmaptest.NewSSSD(t)
fake.AddUser(10000, "S-1-5-21-1111-2222-3333-1104")
r, err := sidmap.NewSSSD(sidmap.WithSSSDSocket(fake.Socket))
if err != nil {
t.Fatal(err)
}
defer r.Close()
// use r ...
}New and small. The tests run against the fake SSSD and a fake wbinfo; the library has not yet been verified against a live domain. Reports from real SSSD and winbind hosts are welcome.
Reverse lookups on SSSD use its uid-specific and gid-specific commands, which need SSSD 2.x.
MIT. See LICENSE.