| 1 | package receiver |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os/user" |
| 6 | "strconv" |
| 7 | |
| 8 | "github.com/gokrazy/rsync/internal/rsyncopts" |
| 9 | ) |
| 10 | |
| 11 | type mapping struct { |
| 12 | Name string |
| 13 | LocalId int32 |
| 14 | } |
| 15 | |
| 16 | func (rt *Transfer) recvIdMapping1(localId func(id int32, name string) int32) (map[int32]mapping, error) { |
| 17 | idMapping := make(map[int32]mapping) |
| 18 | for { |
| 19 | id, err := rt.Conn.ReadInt32() |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | if id == 0 { |
| 24 | break |
| 25 | } |
| 26 | length, err := rt.Conn.ReadByte() |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | name := make([]byte, length) |
| 31 | if _, err := io.ReadFull(rt.Conn.Reader, name); err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | idMapping[id] = mapping{ |
| 35 | Name: string(name), |
| 36 | LocalId: localId(id, string(name)), |
| 37 | } |
| 38 | } |
| 39 | return idMapping, nil |
| 40 | } |
| 41 | |
| 42 | // rsync/uidlist.c:recv_id_list |
| 43 | func (rt *Transfer) RecvIdList() (users map[int32]mapping, groups map[int32]mapping, _ error) { |
| 44 | if rt.Opts.PreserveUid { |
| 45 | var err error |
| 46 | users, err = rt.recvIdMapping1(func(remoteUid int32, remoteUsername string) int32 { |
| 47 | u, err := user.Lookup(remoteUsername) |
| 48 | if err != nil { |
| 49 | return remoteUid |
| 50 | } |
| 51 | uid, err := strconv.ParseInt(u.Uid, 0, 32) |
| 52 | if err != nil { |
| 53 | return remoteUid |
| 54 | } |
| 55 | return int32(uid) |
| 56 | }) |
| 57 | if err != nil { |
| 58 | return nil, nil, err |
| 59 | } |
| 60 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_FLIST, 2) { |
| 61 | for remoteUid, mapping := range users { |
| 62 | rt.Logger.Printf("remote uid %d(%s) maps to local uid %d", remoteUid, mapping.Name, mapping.LocalId) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if rt.Opts.PreserveGid { |
| 68 | var err error |
| 69 | groups, err = rt.recvIdMapping1(func(remoteGid int32, remoteGroupname string) int32 { |
| 70 | g, err := user.LookupGroup(remoteGroupname) |
| 71 | if err != nil { |
| 72 | return remoteGid |
| 73 | } |
| 74 | gid, err := strconv.ParseInt(g.Gid, 0, 32) |
| 75 | if err != nil { |
| 76 | return remoteGid |
| 77 | } |
| 78 | return int32(gid) |
| 79 | }) |
| 80 | if err != nil { |
| 81 | return nil, nil, err |
| 82 | } |
| 83 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_FLIST, 2) { |
| 84 | for remoteGid, mapping := range groups { |
| 85 | rt.Logger.Printf("remote gid %d(%s) maps to local gid %d", remoteGid, mapping.Name, mapping.LocalId) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return users, groups, nil |
| 91 | } |