1 /*
2 * Copyright (c) 2022 Damien Miller <djm@mindrot.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /* sftp client user/group lookup and caching */
18
19 #include "includes.h"
20
21 #include <sys/types.h>
22 #include <sys/tree.h>
23
24 #include <glob.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28
29 #include "log.h"
30 #include "xmalloc.h"
31
32 #include "sftp-common.h"
33 #include "sftp-client.h"
34 #include "sftp-usergroup.h"
35
36 /* Tree of id, name */
37 struct idname {
38 u_int id;
39 char *name;
40 RB_ENTRY(idname) entry;
41 /* XXX implement bounded cache as TAILQ */
42 };
43 static int
idname_cmp(struct idname * a,struct idname * b)44 idname_cmp(struct idname *a, struct idname *b)
45 {
46 if (a->id == b->id)
47 return 0;
48 return a->id > b->id ? 1 : -1;
49 }
50 RB_HEAD(idname_tree, idname);
51 RB_GENERATE_STATIC(idname_tree, idname, entry, idname_cmp)
52
53 static struct idname_tree user_idname = RB_INITIALIZER(&user_idname);
54 static struct idname_tree group_idname = RB_INITIALIZER(&group_idname);
55
56 static void
idname_free(struct idname * idname)57 idname_free(struct idname *idname)
58 {
59 if (idname == NULL)
60 return;
61 free(idname->name);
62 free(idname);
63 }
64
65 static void
idname_enter(struct idname_tree * tree,u_int id,const char * name)66 idname_enter(struct idname_tree *tree, u_int id, const char *name)
67 {
68 struct idname *idname;
69
70 if ((idname = xcalloc(1, sizeof(*idname))) == NULL)
71 fatal_f("alloc");
72 idname->id = id;
73 idname->name = xstrdup(name);
74 if (RB_INSERT(idname_tree, tree, idname) != NULL)
75 idname_free(idname);
76 }
77
78 static const char *
idname_lookup(struct idname_tree * tree,u_int id)79 idname_lookup(struct idname_tree *tree, u_int id)
80 {
81 struct idname idname, *found;
82
83 memset(&idname, 0, sizeof(idname));
84 idname.id = id;
85 if ((found = RB_FIND(idname_tree, tree, &idname)) != NULL)
86 return found->name;
87 return NULL;
88 }
89
90 static void
freenames(char ** names,u_int nnames)91 freenames(char **names, u_int nnames)
92 {
93 u_int i;
94
95 if (names == NULL)
96 return;
97 for (i = 0; i < nnames; i++)
98 free(names[i]);
99 free(names);
100 }
101
102 static void
lookup_and_record(struct sftp_conn * conn,u_int * uids,u_int nuids,u_int * gids,u_int ngids)103 lookup_and_record(struct sftp_conn *conn,
104 u_int *uids, u_int nuids, u_int *gids, u_int ngids)
105 {
106 int r;
107 u_int i;
108 char **usernames = NULL, **groupnames = NULL;
109
110 if ((r = sftp_get_users_groups_by_id(conn, uids, nuids, gids, ngids,
111 &usernames, &groupnames)) != 0) {
112 debug_fr(r, "sftp_get_users_groups_by_id");
113 return;
114 }
115 for (i = 0; i < nuids; i++) {
116 if (usernames[i] == NULL) {
117 debug3_f("uid %u not resolved", uids[i]);
118 continue;
119 }
120 debug3_f("record uid %u => \"%s\"", uids[i], usernames[i]);
121 idname_enter(&user_idname, uids[i], usernames[i]);
122 }
123 for (i = 0; i < ngids; i++) {
124 if (groupnames[i] == NULL) {
125 debug3_f("gid %u not resolved", gids[i]);
126 continue;
127 }
128 debug3_f("record gid %u => \"%s\"", gids[i], groupnames[i]);
129 idname_enter(&group_idname, gids[i], groupnames[i]);
130 }
131 freenames(usernames, nuids);
132 freenames(groupnames, ngids);
133 }
134
135 static int
has_id(u_int id,u_int * ids,u_int nids)136 has_id(u_int id, u_int *ids, u_int nids)
137 {
138 u_int i;
139
140 if (nids == 0)
141 return 0;
142
143 /* XXX O(N^2) */
144 for (i = 0; i < nids; i++) {
145 if (ids[i] == id)
146 break;
147 }
148 return i < nids;
149 }
150
151 static void
collect_ids_from_glob(glob_t * g,int user,u_int ** idsp,u_int * nidsp)152 collect_ids_from_glob(glob_t *g, int user, u_int **idsp, u_int *nidsp)
153 {
154 u_int id, i, n = 0, *ids = NULL;
155
156 for (i = 0; g->gl_pathv[i] != NULL; i++) {
157 if (user) {
158 if (ruser_name(g->gl_statv[i]->st_uid) != NULL)
159 continue; /* Already seen */
160 id = (u_int)g->gl_statv[i]->st_uid;
161 } else {
162 if (rgroup_name(g->gl_statv[i]->st_gid) != NULL)
163 continue; /* Already seen */
164 id = (u_int)g->gl_statv[i]->st_gid;
165 }
166 if (has_id(id, ids, n))
167 continue;
168 ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
169 ids[n++] = id;
170 }
171 *idsp = ids;
172 *nidsp = n;
173 }
174
175 void
get_remote_user_groups_from_glob(struct sftp_conn * conn,glob_t * g)176 get_remote_user_groups_from_glob(struct sftp_conn *conn, glob_t *g)
177 {
178 u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
179
180 if (!sftp_can_get_users_groups_by_id(conn))
181 return;
182
183 collect_ids_from_glob(g, 1, &uids, &nuids);
184 collect_ids_from_glob(g, 0, &gids, &ngids);
185 lookup_and_record(conn, uids, nuids, gids, ngids);
186 free(uids);
187 free(gids);
188 }
189
190 static void
collect_ids_from_dirents(SFTP_DIRENT ** d,int user,u_int ** idsp,u_int * nidsp)191 collect_ids_from_dirents(SFTP_DIRENT **d, int user, u_int **idsp, u_int *nidsp)
192 {
193 u_int id, i, n = 0, *ids = NULL;
194
195 for (i = 0; d[i] != NULL; i++) {
196 if (user) {
197 if (ruser_name((uid_t)(d[i]->a.uid)) != NULL)
198 continue; /* Already seen */
199 id = d[i]->a.uid;
200 } else {
201 if (rgroup_name((gid_t)(d[i]->a.gid)) != NULL)
202 continue; /* Already seen */
203 id = d[i]->a.gid;
204 }
205 if (has_id(id, ids, n))
206 continue;
207 ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
208 ids[n++] = id;
209 }
210 *idsp = ids;
211 *nidsp = n;
212 }
213
214 void
get_remote_user_groups_from_dirents(struct sftp_conn * conn,SFTP_DIRENT ** d)215 get_remote_user_groups_from_dirents(struct sftp_conn *conn, SFTP_DIRENT **d)
216 {
217 u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
218
219 if (!sftp_can_get_users_groups_by_id(conn))
220 return;
221
222 collect_ids_from_dirents(d, 1, &uids, &nuids);
223 collect_ids_from_dirents(d, 0, &gids, &ngids);
224 lookup_and_record(conn, uids, nuids, gids, ngids);
225 free(uids);
226 free(gids);
227 }
228
229 const char *
ruser_name(uid_t uid)230 ruser_name(uid_t uid)
231 {
232 return idname_lookup(&user_idname, (u_int)uid);
233 }
234
235 const char *
rgroup_name(uid_t gid)236 rgroup_name(uid_t gid)
237 {
238 return idname_lookup(&group_idname, (u_int)gid);
239 }
240
241