| 1 | package maclogin |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/xml" |
| 6 | "io" |
| 7 | "os" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/google/uuid" |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | func permittedLoginScreen(data []byte, user string) string { |
| 18 | if bytes.Equal(data, []byte("system-login")) { |
| 19 | return "system-login" |
| 20 | } |
| 21 | header, payload, ok := bytes.Cut(data, []byte("\n")) |
| 22 | fields := strings.Fields(string(header)) |
| 23 | if !ok || len(fields) != 3 || fields[1] != user || fields[0] != fields[2] { |
| 24 | return "" |
| 25 | } |
| 26 | uid, err := strconv.ParseUint(fields[0], 10, 32) |
| 27 | if err != nil || uid < 500 || len(payload) > 32<<10 { |
| 28 | return "" |
| 29 | } |
| 30 | type item struct { |
| 31 | XMLName xml.Name |
| 32 | Value string `xml:",chardata"` |
| 33 | } |
| 34 | var doc struct { |
| 35 | XMLName xml.Name `xml:"plist"` |
| 36 | Values []struct { |
| 37 | XMLName xml.Name |
| 38 | Records []struct { |
| 39 | XMLName xml.Name |
| 40 | Items []item `xml:",any"` |
| 41 | } `xml:",any"` |
| 42 | } `xml:",any"` |
| 43 | } |
| 44 | decoder := xml.NewDecoder(bytes.NewReader(bytes.TrimSpace(payload))) |
| 45 | if decoder.Decode(&doc) != nil || len(doc.Values) != 1 || doc.Values[0].XMLName.Local != "array" { |
| 46 | return "" |
| 47 | } |
| 48 | if _, err := decoder.Token(); err != io.EOF { |
| 49 | return "" |
| 50 | } |
| 51 | key := "" |
| 52 | for _, record := range doc.Values[0].Records { |
| 53 | if record.XMLName.Local != "dict" || len(record.Items)%2 != 0 { |
| 54 | return "" |
| 55 | } |
| 56 | values := map[string]item{} |
| 57 | for i := 0; i < len(record.Items); i += 2 { |
| 58 | k, v := record.Items[i], record.Items[i+1] |
| 59 | if k.XMLName.Local != "key" || k.Value == "" || values[k.Value].XMLName.Local != "" { |
| 60 | return "" |
| 61 | } |
| 62 | values[k.Value] = v |
| 63 | } |
| 64 | on := values["kCGSSessionOnConsoleKey"] |
| 65 | if on.XMLName.Local == "false" && on.Value == "" { |
| 66 | continue |
| 67 | } |
| 68 | boolTrue := func(k string) bool { v := values[k]; return v.XMLName.Local == "true" && v.Value == "" } |
| 69 | id, name := values["kCGSSessionUserIDKey"], values["kCGSSessionUserNameKey"] |
| 70 | session := values["CGSSessionUniqueSessionUUID"] |
| 71 | sessionID, err := uuid.Parse(session.Value) |
| 72 | if key != "" || !boolTrue("kCGSSessionOnConsoleKey") || !boolTrue("kCGSessionLoginDoneKey") || |
| 73 | !boolTrue("CGSSessionScreenIsLocked") || id.XMLName.Local != "integer" || id.Value != fields[0] || |
| 74 | name.XMLName.Local != "string" || name.Value != user || session.XMLName.Local != "string" || err != nil || sessionID == uuid.Nil { |
| 75 | return "" |
| 76 | } |
| 77 | key = "locked-" + sessionID.String() |
| 78 | } |
| 79 | return key |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | func sameGuestDirectory(actual, expected string) bool { |
| 85 | a, err := os.Stat(actual) |
| 86 | if err != nil || !a.IsDir() { |
| 87 | return false |
| 88 | } |
| 89 | b, err := os.Stat(expected) |
| 90 | return err == nil && b.IsDir() && os.SameFile(a, b) |
| 91 | } |