vendor/OPENSSH: upgrade from 8.8p1 top 9.1p1
[dragonfly.git] / crypto / openssh / sftp-usergroup.c
blob083930a4a327c9bc017e9c20d84917c447479071
1 /*
2 * Copyright (c) 2022 Damien Miller <djm@mindrot.org>
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.
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.
17 /* sftp client user/group lookup and caching */
19 #include "includes.h"
21 #include <sys/types.h>
22 #include <openbsd-compat/sys-tree.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
28 #include "log.h"
29 #include "xmalloc.h"
31 #include "sftp-common.h"
32 #include "sftp-client.h"
33 #include "sftp-usergroup.h"
35 /* Tree of id, name */
36 struct idname {
37 u_int id;
38 char *name;
39 RB_ENTRY(idname) entry;
40 /* XXX implement bounded cache as TAILQ */
42 static int
43 idname_cmp(struct idname *a, struct idname *b)
45 if (a->id == b->id)
46 return 0;
47 return a->id > b->id ? 1 : -1;
49 RB_HEAD(idname_tree, idname);
50 RB_GENERATE_STATIC(idname_tree, idname, entry, idname_cmp)
52 static struct idname_tree user_idname = RB_INITIALIZER(&user_idname);
53 static struct idname_tree group_idname = RB_INITIALIZER(&group_idname);
55 static void
56 idname_free(struct idname *idname)
58 if (idname == NULL)
59 return;
60 free(idname->name);
61 free(idname);
64 static void
65 idname_enter(struct idname_tree *tree, u_int id, const char *name)
67 struct idname *idname;
69 if ((idname = xcalloc(1, sizeof(*idname))) == NULL)
70 fatal_f("alloc");
71 idname->id = id;
72 idname->name = xstrdup(name);
73 if (RB_INSERT(idname_tree, tree, idname) != NULL)
74 idname_free(idname);
77 static const char *
78 idname_lookup(struct idname_tree *tree, u_int id)
80 struct idname idname, *found;
82 memset(&idname, 0, sizeof(idname));
83 idname.id = id;
84 if ((found = RB_FIND(idname_tree, tree, &idname)) != NULL)
85 return found->name;
86 return NULL;
89 static void
90 freenames(char **names, u_int nnames)
92 u_int i;
94 if (names == NULL)
95 return;
96 for (i = 0; i < nnames; i++)
97 free(names[i]);
98 free(names);
101 static void
102 lookup_and_record(struct sftp_conn *conn,
103 u_int *uids, u_int nuids, u_int *gids, u_int ngids)
105 int r;
106 u_int i;
107 char **usernames = NULL, **groupnames = NULL;
109 if ((r = do_get_users_groups_by_id(conn, uids, nuids, gids, ngids,
110 &usernames, &groupnames)) != 0) {
111 debug_fr(r, "do_get_users_groups_by_id");
112 return;
114 for (i = 0; i < nuids; i++) {
115 if (usernames[i] == NULL) {
116 debug3_f("uid %u not resolved", uids[i]);
117 continue;
119 debug3_f("record uid %u => \"%s\"", uids[i], usernames[i]);
120 idname_enter(&user_idname, uids[i], usernames[i]);
122 for (i = 0; i < ngids; i++) {
123 if (groupnames[i] == NULL) {
124 debug3_f("gid %u not resolved", gids[i]);
125 continue;
127 debug3_f("record gid %u => \"%s\"", gids[i], groupnames[i]);
128 idname_enter(&group_idname, gids[i], groupnames[i]);
130 freenames(usernames, nuids);
131 freenames(groupnames, ngids);
134 static int
135 has_id(u_int id, u_int *ids, u_int nids)
137 u_int i;
139 if (nids == 0)
140 return 0;
142 /* XXX O(N^2) */
143 for (i = 0; i < nids; i++) {
144 if (ids[i] == id)
145 break;
147 return i < nids;
150 static void
151 collect_ids_from_glob(glob_t *g, int user, u_int **idsp, u_int *nidsp)
153 u_int id, i, n = 0, *ids = NULL;
155 for (i = 0; g->gl_pathv[i] != NULL; i++) {
156 if (user) {
157 if (ruser_name(g->gl_statv[i]->st_uid) != NULL)
158 continue; /* Already seen */
159 id = (u_int)g->gl_statv[i]->st_uid;
160 } else {
161 if (rgroup_name(g->gl_statv[i]->st_gid) != NULL)
162 continue; /* Already seen */
163 id = (u_int)g->gl_statv[i]->st_gid;
165 if (has_id(id, ids, n))
166 continue;
167 ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
168 ids[n++] = id;
170 *idsp = ids;
171 *nidsp = n;
174 void
175 get_remote_user_groups_from_glob(struct sftp_conn *conn, glob_t *g)
177 u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
179 if (!can_get_users_groups_by_id(conn))
180 return;
182 collect_ids_from_glob(g, 1, &uids, &nuids);
183 collect_ids_from_glob(g, 0, &gids, &ngids);
184 lookup_and_record(conn, uids, nuids, gids, ngids);
185 free(uids);
186 free(gids);
189 static void
190 collect_ids_from_dirents(SFTP_DIRENT **d, int user, u_int **idsp, u_int *nidsp)
192 u_int id, i, n = 0, *ids = NULL;
194 for (i = 0; d[i] != NULL; i++) {
195 if (user) {
196 if (ruser_name((uid_t)(d[i]->a.uid)) != NULL)
197 continue; /* Already seen */
198 id = d[i]->a.uid;
199 } else {
200 if (rgroup_name((gid_t)(d[i]->a.gid)) != NULL)
201 continue; /* Already seen */
202 id = d[i]->a.gid;
204 if (has_id(id, ids, n))
205 continue;
206 ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
207 ids[n++] = id;
209 *idsp = ids;
210 *nidsp = n;
213 void
214 get_remote_user_groups_from_dirents(struct sftp_conn *conn, SFTP_DIRENT **d)
216 u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
218 if (!can_get_users_groups_by_id(conn))
219 return;
221 collect_ids_from_dirents(d, 1, &uids, &nuids);
222 collect_ids_from_dirents(d, 0, &gids, &ngids);
223 lookup_and_record(conn, uids, nuids, gids, ngids);
224 free(uids);
225 free(gids);
228 const char *
229 ruser_name(uid_t uid)
231 return idname_lookup(&user_idname, (u_int)uid);
234 const char *
235 rgroup_name(uid_t gid)
237 return idname_lookup(&group_idname, (u_int)gid);