xref: /openbsd-src/usr.bin/ssh/sftp-usergroup.c (revision 19766334a853933b1a69938aae799a7959154ecf)
138e6e118Sdjm /*
238e6e118Sdjm  * Copyright (c) 2022 Damien Miller <djm@mindrot.org>
338e6e118Sdjm  *
438e6e118Sdjm  * Permission to use, copy, modify, and distribute this software for any
538e6e118Sdjm  * purpose with or without fee is hereby granted, provided that the above
638e6e118Sdjm  * copyright notice and this permission notice appear in all copies.
738e6e118Sdjm  *
838e6e118Sdjm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
938e6e118Sdjm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1038e6e118Sdjm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1138e6e118Sdjm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1238e6e118Sdjm  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1338e6e118Sdjm  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1438e6e118Sdjm  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1538e6e118Sdjm  */
1638e6e118Sdjm 
1738e6e118Sdjm /* sftp client user/group lookup and caching */
1838e6e118Sdjm 
1938e6e118Sdjm #include <sys/types.h>
2038e6e118Sdjm #include <sys/tree.h>
2138e6e118Sdjm 
2238e6e118Sdjm #include <glob.h>
2338e6e118Sdjm #include <stdlib.h>
2438e6e118Sdjm #include <stdarg.h>
2538e6e118Sdjm #include <string.h>
2638e6e118Sdjm 
2738e6e118Sdjm #include "log.h"
2838e6e118Sdjm #include "xmalloc.h"
2938e6e118Sdjm 
3038e6e118Sdjm #include "sftp-common.h"
3138e6e118Sdjm #include "sftp-client.h"
3238e6e118Sdjm #include "sftp-usergroup.h"
3338e6e118Sdjm 
3438e6e118Sdjm /* Tree of id, name */
3538e6e118Sdjm struct idname {
3638e6e118Sdjm         u_int id;
3738e6e118Sdjm 	char *name;
3838e6e118Sdjm         RB_ENTRY(idname) entry;
3938e6e118Sdjm 	/* XXX implement bounded cache as TAILQ */
4038e6e118Sdjm };
4138e6e118Sdjm static int
idname_cmp(struct idname * a,struct idname * b)4238e6e118Sdjm idname_cmp(struct idname *a, struct idname *b)
4338e6e118Sdjm {
4438e6e118Sdjm 	if (a->id == b->id)
4538e6e118Sdjm 		return 0;
4638e6e118Sdjm 	return a->id > b->id ? 1 : -1;
4738e6e118Sdjm }
4838e6e118Sdjm RB_HEAD(idname_tree, idname);
4938e6e118Sdjm RB_GENERATE_STATIC(idname_tree, idname, entry, idname_cmp)
5038e6e118Sdjm 
5138e6e118Sdjm static struct idname_tree user_idname = RB_INITIALIZER(&user_idname);
5238e6e118Sdjm static struct idname_tree group_idname = RB_INITIALIZER(&group_idname);
5338e6e118Sdjm 
5438e6e118Sdjm static void
idname_free(struct idname * idname)5538e6e118Sdjm idname_free(struct idname *idname)
5638e6e118Sdjm {
5738e6e118Sdjm 	if (idname == NULL)
5838e6e118Sdjm 		return;
5938e6e118Sdjm 	free(idname->name);
6038e6e118Sdjm 	free(idname);
6138e6e118Sdjm }
6238e6e118Sdjm 
6338e6e118Sdjm static void
idname_enter(struct idname_tree * tree,u_int id,const char * name)6438e6e118Sdjm idname_enter(struct idname_tree *tree, u_int id, const char *name)
6538e6e118Sdjm {
6638e6e118Sdjm 	struct idname *idname;
6738e6e118Sdjm 
6838e6e118Sdjm 	if ((idname = xcalloc(1, sizeof(*idname))) == NULL)
6938e6e118Sdjm 		fatal_f("alloc");
7038e6e118Sdjm 	idname->id = id;
7138e6e118Sdjm 	idname->name = xstrdup(name);
7238e6e118Sdjm 	if (RB_INSERT(idname_tree, tree, idname) != NULL)
7338e6e118Sdjm 		idname_free(idname);
7438e6e118Sdjm }
7538e6e118Sdjm 
7638e6e118Sdjm static const char *
idname_lookup(struct idname_tree * tree,u_int id)7738e6e118Sdjm idname_lookup(struct idname_tree *tree, u_int id)
7838e6e118Sdjm {
7938e6e118Sdjm 	struct idname idname, *found;
8038e6e118Sdjm 
8138e6e118Sdjm 	memset(&idname, 0, sizeof(idname));
8238e6e118Sdjm 	idname.id = id;
8338e6e118Sdjm 	if ((found = RB_FIND(idname_tree, tree, &idname)) != NULL)
8438e6e118Sdjm 		return found->name;
8538e6e118Sdjm 	return NULL;
8638e6e118Sdjm }
8738e6e118Sdjm 
8838e6e118Sdjm static void
freenames(char ** names,u_int nnames)8938e6e118Sdjm freenames(char **names, u_int nnames)
9038e6e118Sdjm {
9138e6e118Sdjm 	u_int i;
9238e6e118Sdjm 
9338e6e118Sdjm 	if (names == NULL)
9438e6e118Sdjm 		return;
9538e6e118Sdjm 	for (i = 0; i < nnames; i++)
9638e6e118Sdjm 		free(names[i]);
9738e6e118Sdjm 	free(names);
9838e6e118Sdjm }
9938e6e118Sdjm 
10038e6e118Sdjm static void
lookup_and_record(struct sftp_conn * conn,u_int * uids,u_int nuids,u_int * gids,u_int ngids)10138e6e118Sdjm lookup_and_record(struct sftp_conn *conn,
10238e6e118Sdjm     u_int *uids, u_int nuids, u_int *gids, u_int ngids)
10338e6e118Sdjm {
10438e6e118Sdjm 	int r;
10538e6e118Sdjm 	u_int i;
10638e6e118Sdjm 	char **usernames = NULL, **groupnames = NULL;
10738e6e118Sdjm 
108*19766334Sdjm 	if ((r = sftp_get_users_groups_by_id(conn, uids, nuids, gids, ngids,
10938e6e118Sdjm 	    &usernames, &groupnames)) != 0) {
110*19766334Sdjm 		debug_fr(r, "sftp_get_users_groups_by_id");
11138e6e118Sdjm 		return;
11238e6e118Sdjm 	}
11338e6e118Sdjm 	for (i = 0; i < nuids; i++) {
11438e6e118Sdjm 		if (usernames[i] == NULL) {
11538e6e118Sdjm 			debug3_f("uid %u not resolved", uids[i]);
11638e6e118Sdjm 			continue;
11738e6e118Sdjm 		}
11838e6e118Sdjm 		debug3_f("record uid %u => \"%s\"", uids[i], usernames[i]);
11938e6e118Sdjm 		idname_enter(&user_idname, uids[i], usernames[i]);
12038e6e118Sdjm 	}
12138e6e118Sdjm 	for (i = 0; i < ngids; i++) {
12238e6e118Sdjm 		if (groupnames[i] == NULL) {
12338e6e118Sdjm 			debug3_f("gid %u not resolved", gids[i]);
12438e6e118Sdjm 			continue;
12538e6e118Sdjm 		}
12638e6e118Sdjm 		debug3_f("record gid %u => \"%s\"", gids[i], groupnames[i]);
12738e6e118Sdjm 		idname_enter(&group_idname, gids[i], groupnames[i]);
12838e6e118Sdjm 	}
12938e6e118Sdjm 	freenames(usernames, nuids);
13038e6e118Sdjm 	freenames(groupnames, ngids);
13138e6e118Sdjm }
13238e6e118Sdjm 
13338e6e118Sdjm static int
has_id(u_int id,u_int * ids,u_int nids)13438e6e118Sdjm has_id(u_int id, u_int *ids, u_int nids)
13538e6e118Sdjm {
13638e6e118Sdjm 	u_int i;
13738e6e118Sdjm 
13838e6e118Sdjm 	if (nids == 0)
13938e6e118Sdjm 		return 0;
14038e6e118Sdjm 
14138e6e118Sdjm 	/* XXX O(N^2) */
14238e6e118Sdjm 	for (i = 0; i < nids; i++) {
14338e6e118Sdjm 		if (ids[i] == id)
14438e6e118Sdjm 			break;
14538e6e118Sdjm 	}
14638e6e118Sdjm 	return i < nids;
14738e6e118Sdjm }
14838e6e118Sdjm 
14938e6e118Sdjm static void
collect_ids_from_glob(glob_t * g,int user,u_int ** idsp,u_int * nidsp)15038e6e118Sdjm collect_ids_from_glob(glob_t *g, int user, u_int **idsp, u_int *nidsp)
15138e6e118Sdjm {
15238e6e118Sdjm 	u_int id, i, n = 0, *ids = NULL;
15338e6e118Sdjm 
15438e6e118Sdjm 	for (i = 0; g->gl_pathv[i] != NULL; i++) {
15538e6e118Sdjm 		if (user) {
15638e6e118Sdjm 			if (ruser_name(g->gl_statv[i]->st_uid) != NULL)
15738e6e118Sdjm 				continue; /* Already seen */
15838e6e118Sdjm 			id = (u_int)g->gl_statv[i]->st_uid;
15938e6e118Sdjm 		} else {
16038e6e118Sdjm 			if (rgroup_name(g->gl_statv[i]->st_gid) != NULL)
16138e6e118Sdjm 				continue; /* Already seen */
16238e6e118Sdjm 			id = (u_int)g->gl_statv[i]->st_gid;
16338e6e118Sdjm 		}
16438e6e118Sdjm 		if (has_id(id, ids, n))
16538e6e118Sdjm 			continue;
16638e6e118Sdjm 		ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
16738e6e118Sdjm 		ids[n++] = id;
16838e6e118Sdjm 	}
16938e6e118Sdjm 	*idsp = ids;
17038e6e118Sdjm 	*nidsp = n;
17138e6e118Sdjm }
17238e6e118Sdjm 
17338e6e118Sdjm void
get_remote_user_groups_from_glob(struct sftp_conn * conn,glob_t * g)17438e6e118Sdjm get_remote_user_groups_from_glob(struct sftp_conn *conn, glob_t *g)
17538e6e118Sdjm {
17638e6e118Sdjm 	u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
17738e6e118Sdjm 
178*19766334Sdjm 	if (!sftp_can_get_users_groups_by_id(conn))
17938e6e118Sdjm 		return;
18038e6e118Sdjm 
18138e6e118Sdjm 	collect_ids_from_glob(g, 1, &uids, &nuids);
18238e6e118Sdjm 	collect_ids_from_glob(g, 0, &gids, &ngids);
18338e6e118Sdjm 	lookup_and_record(conn, uids, nuids, gids, ngids);
18438e6e118Sdjm 	free(uids);
18538e6e118Sdjm 	free(gids);
18638e6e118Sdjm }
18738e6e118Sdjm 
18838e6e118Sdjm static void
collect_ids_from_dirents(SFTP_DIRENT ** d,int user,u_int ** idsp,u_int * nidsp)18938e6e118Sdjm collect_ids_from_dirents(SFTP_DIRENT **d, int user, u_int **idsp, u_int *nidsp)
19038e6e118Sdjm {
19138e6e118Sdjm 	u_int id, i, n = 0, *ids = NULL;
19238e6e118Sdjm 
19338e6e118Sdjm 	for (i = 0; d[i] != NULL; i++) {
19438e6e118Sdjm 		if (user) {
19538e6e118Sdjm 			if (ruser_name((uid_t)(d[i]->a.uid)) != NULL)
19638e6e118Sdjm 				continue; /* Already seen */
19738e6e118Sdjm 			id = d[i]->a.uid;
19838e6e118Sdjm 		} else {
19938e6e118Sdjm 			if (rgroup_name((gid_t)(d[i]->a.gid)) != NULL)
20038e6e118Sdjm 				continue; /* Already seen */
20138e6e118Sdjm 			id = d[i]->a.gid;
20238e6e118Sdjm 		}
20338e6e118Sdjm 		if (has_id(id, ids, n))
20438e6e118Sdjm 			continue;
20538e6e118Sdjm 		ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
20638e6e118Sdjm 		ids[n++] = id;
20738e6e118Sdjm 	}
20838e6e118Sdjm 	*idsp = ids;
20938e6e118Sdjm 	*nidsp = n;
21038e6e118Sdjm }
21138e6e118Sdjm 
21238e6e118Sdjm void
get_remote_user_groups_from_dirents(struct sftp_conn * conn,SFTP_DIRENT ** d)21338e6e118Sdjm get_remote_user_groups_from_dirents(struct sftp_conn *conn, SFTP_DIRENT **d)
21438e6e118Sdjm {
21538e6e118Sdjm 	u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
21638e6e118Sdjm 
217*19766334Sdjm 	if (!sftp_can_get_users_groups_by_id(conn))
21838e6e118Sdjm 		return;
21938e6e118Sdjm 
22038e6e118Sdjm 	collect_ids_from_dirents(d, 1, &uids, &nuids);
22138e6e118Sdjm 	collect_ids_from_dirents(d, 0, &gids, &ngids);
22238e6e118Sdjm 	lookup_and_record(conn, uids, nuids, gids, ngids);
22338e6e118Sdjm 	free(uids);
22438e6e118Sdjm 	free(gids);
22538e6e118Sdjm }
22638e6e118Sdjm 
22738e6e118Sdjm const char *
ruser_name(uid_t uid)22838e6e118Sdjm ruser_name(uid_t uid)
22938e6e118Sdjm {
23038e6e118Sdjm 	return idname_lookup(&user_idname, (u_int)uid);
23138e6e118Sdjm }
23238e6e118Sdjm 
23338e6e118Sdjm const char *
rgroup_name(uid_t gid)23438e6e118Sdjm rgroup_name(uid_t gid)
23538e6e118Sdjm {
23638e6e118Sdjm 	return idname_lookup(&group_idname, (u_int)gid);
23738e6e118Sdjm }
23838e6e118Sdjm 
239