This library is pretty good so far for my use cases, but it doesn't seem to generate the indexes that are specified in the libnss-cache documentation. I can PR in this functionality if you'd like, or you can use the functions I've provided below which can be used to generate the indexes.
func genIndex(lines []string, indexCol int) map[string]int {
iMap := make(map[string]int)
offset := 0
for _, l := range lines {
key := strings.Split(l, ":")[indexCol]
iMap[key] = offset
offset += len(l) + 1
}
return iMap
}
func writeIndex(index map[string]int, location string) error {
keyList := []string{}
for k := range index {
keyList = append(keyList, k)
}
sort.Strings(keyList)
var b bytes.Buffer
for _, key := range keyList {
value := index[key]
fmt.Fprintf(&b, "%s", key)
fmt.Fprintf(&b, "\x00")
fmt.Fprintf(&b, "%08d", int64(value))
for i := 0; i < 32-len(key)-1; i++ {
fmt.Fprint(&b, "\x00")
}
fmt.Fprintf(&b, "\n")
}
return ioutil.WriteFile(location, b.Bytes(), 0644)
}
Four indexes in total need to be generated, two each for passwd and group, each by both the string representation and the number. The reason this works right now is that if the indexes aren't present libnss-cache will fall back to trying a linear search through the maps, but this will fail for maps beyond a certain size.