1*a6c5e0d9Schristos /*	$NetBSD: sftp-usergroup.c,v 1.2 2022/10/05 22:39:36 christos Exp $	*/
2*a6c5e0d9Schristos 
376728a53Schristos /*
476728a53Schristos  * Copyright (c) 2022 Damien Miller <djm@mindrot.org>
576728a53Schristos  *
676728a53Schristos  * Permission to use, copy, modify, and distribute this software for any
776728a53Schristos  * purpose with or without fee is hereby granted, provided that the above
876728a53Schristos  * copyright notice and this permission notice appear in all copies.
976728a53Schristos  *
1076728a53Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1176728a53Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1276728a53Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1376728a53Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1476728a53Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1576728a53Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1676728a53Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1776728a53Schristos  */
1876728a53Schristos 
1976728a53Schristos /* sftp client user/group lookup and caching */
20*a6c5e0d9Schristos #include "includes.h"
21*a6c5e0d9Schristos __RCSID("$NetBSD: sftp-usergroup.c,v 1.2 2022/10/05 22:39:36 christos Exp $");
2276728a53Schristos 
2376728a53Schristos #include <sys/types.h>
2476728a53Schristos #include <sys/tree.h>
2576728a53Schristos 
2676728a53Schristos #include <glob.h>
2776728a53Schristos #include <stdlib.h>
2876728a53Schristos #include <stdarg.h>
2976728a53Schristos #include <string.h>
3076728a53Schristos 
3176728a53Schristos #include "log.h"
3276728a53Schristos #include "xmalloc.h"
3376728a53Schristos 
3476728a53Schristos #include "sftp-common.h"
3576728a53Schristos #include "sftp-client.h"
3676728a53Schristos #include "sftp-usergroup.h"
3776728a53Schristos 
3876728a53Schristos /* Tree of id, name */
3976728a53Schristos struct idname {
4076728a53Schristos         u_int id;
4176728a53Schristos 	char *name;
4276728a53Schristos         RB_ENTRY(idname) entry;
4376728a53Schristos 	/* XXX implement bounded cache as TAILQ */
4476728a53Schristos };
4576728a53Schristos static int
idname_cmp(struct idname * a,struct idname * b)4676728a53Schristos idname_cmp(struct idname *a, struct idname *b)
4776728a53Schristos {
4876728a53Schristos 	if (a->id == b->id)
4976728a53Schristos 		return 0;
5076728a53Schristos 	return a->id > b->id ? 1 : -1;
5176728a53Schristos }
5276728a53Schristos RB_HEAD(idname_tree, idname);
5376728a53Schristos RB_GENERATE_STATIC(idname_tree, idname, entry, idname_cmp)
5476728a53Schristos 
5576728a53Schristos static struct idname_tree user_idname = RB_INITIALIZER(&user_idname);
5676728a53Schristos static struct idname_tree group_idname = RB_INITIALIZER(&group_idname);
5776728a53Schristos 
5876728a53Schristos static void
idname_free(struct idname * idname)5976728a53Schristos idname_free(struct idname *idname)
6076728a53Schristos {
6176728a53Schristos 	if (idname == NULL)
6276728a53Schristos 		return;
6376728a53Schristos 	free(idname->name);
6476728a53Schristos 	free(idname);
6576728a53Schristos }
6676728a53Schristos 
6776728a53Schristos static void
idname_enter(struct idname_tree * tree,u_int id,const char * name)6876728a53Schristos idname_enter(struct idname_tree *tree, u_int id, const char *name)
6976728a53Schristos {
7076728a53Schristos 	struct idname *idname;
7176728a53Schristos 
7276728a53Schristos 	if ((idname = xcalloc(1, sizeof(*idname))) == NULL)
7376728a53Schristos 		fatal_f("alloc");
7476728a53Schristos 	idname->id = id;
7576728a53Schristos 	idname->name = xstrdup(name);
7676728a53Schristos 	if (RB_INSERT(idname_tree, tree, idname) != NULL)
7776728a53Schristos 		idname_free(idname);
7876728a53Schristos }
7976728a53Schristos 
8076728a53Schristos static const char *
idname_lookup(struct idname_tree * tree,u_int id)8176728a53Schristos idname_lookup(struct idname_tree *tree, u_int id)
8276728a53Schristos {
8376728a53Schristos 	struct idname idname, *found;
8476728a53Schristos 
8576728a53Schristos 	memset(&idname, 0, sizeof(idname));
8676728a53Schristos 	idname.id = id;
8776728a53Schristos 	if ((found = RB_FIND(idname_tree, tree, &idname)) != NULL)
8876728a53Schristos 		return found->name;
8976728a53Schristos 	return NULL;
9076728a53Schristos }
9176728a53Schristos 
9276728a53Schristos static void
freenames(char ** names,u_int nnames)9376728a53Schristos freenames(char **names, u_int nnames)
9476728a53Schristos {
9576728a53Schristos 	u_int i;
9676728a53Schristos 
9776728a53Schristos 	if (names == NULL)
9876728a53Schristos 		return;
9976728a53Schristos 	for (i = 0; i < nnames; i++)
10076728a53Schristos 		free(names[i]);
10176728a53Schristos 	free(names);
10276728a53Schristos }
10376728a53Schristos 
10476728a53Schristos static void
lookup_and_record(struct sftp_conn * conn,u_int * uids,u_int nuids,u_int * gids,u_int ngids)10576728a53Schristos lookup_and_record(struct sftp_conn *conn,
10676728a53Schristos     u_int *uids, u_int nuids, u_int *gids, u_int ngids)
10776728a53Schristos {
10876728a53Schristos 	int r;
10976728a53Schristos 	u_int i;
11076728a53Schristos 	char **usernames = NULL, **groupnames = NULL;
11176728a53Schristos 
11276728a53Schristos 	if ((r = do_get_users_groups_by_id(conn, uids, nuids, gids, ngids,
11376728a53Schristos 	    &usernames, &groupnames)) != 0) {
11476728a53Schristos 		debug_fr(r, "do_get_users_groups_by_id");
11576728a53Schristos 		return;
11676728a53Schristos 	}
11776728a53Schristos 	for (i = 0; i < nuids; i++) {
11876728a53Schristos 		if (usernames[i] == NULL) {
11976728a53Schristos 			debug3_f("uid %u not resolved", uids[i]);
12076728a53Schristos 			continue;
12176728a53Schristos 		}
12276728a53Schristos 		debug3_f("record uid %u => \"%s\"", uids[i], usernames[i]);
12376728a53Schristos 		idname_enter(&user_idname, uids[i], usernames[i]);
12476728a53Schristos 	}
12576728a53Schristos 	for (i = 0; i < ngids; i++) {
12676728a53Schristos 		if (groupnames[i] == NULL) {
12776728a53Schristos 			debug3_f("gid %u not resolved", gids[i]);
12876728a53Schristos 			continue;
12976728a53Schristos 		}
13076728a53Schristos 		debug3_f("record gid %u => \"%s\"", gids[i], groupnames[i]);
13176728a53Schristos 		idname_enter(&group_idname, gids[i], groupnames[i]);
13276728a53Schristos 	}
13376728a53Schristos 	freenames(usernames, nuids);
13476728a53Schristos 	freenames(groupnames, ngids);
13576728a53Schristos }
13676728a53Schristos 
13776728a53Schristos static int
has_id(u_int id,u_int * ids,u_int nids)13876728a53Schristos has_id(u_int id, u_int *ids, u_int nids)
13976728a53Schristos {
14076728a53Schristos 	u_int i;
14176728a53Schristos 
14276728a53Schristos 	if (nids == 0)
14376728a53Schristos 		return 0;
14476728a53Schristos 
14576728a53Schristos 	/* XXX O(N^2) */
14676728a53Schristos 	for (i = 0; i < nids; i++) {
14776728a53Schristos 		if (ids[i] == id)
14876728a53Schristos 			break;
14976728a53Schristos 	}
15076728a53Schristos 	return i < nids;
15176728a53Schristos }
15276728a53Schristos 
15376728a53Schristos static void
collect_ids_from_glob(glob_t * g,int user,u_int ** idsp,u_int * nidsp)15476728a53Schristos collect_ids_from_glob(glob_t *g, int user, u_int **idsp, u_int *nidsp)
15576728a53Schristos {
15676728a53Schristos 	u_int id, i, n = 0, *ids = NULL;
15776728a53Schristos 
15876728a53Schristos 	for (i = 0; g->gl_pathv[i] != NULL; i++) {
159*a6c5e0d9Schristos 		struct stat *stp;
160*a6c5e0d9Schristos #if GLOB_KEEPSTAT != 0
161*a6c5e0d9Schristos 		stp = g->gl_statv[i];
162*a6c5e0d9Schristos #else
163*a6c5e0d9Schristos 		struct stat st;
164*a6c5e0d9Schristos 		if (lstat(g->gl_pathv[i], stp = &st) == -1) {
165*a6c5e0d9Schristos 			error("no stat information for %s", g->gl_pathv[i]);
166*a6c5e0d9Schristos 			continue;
167*a6c5e0d9Schristos 		}
168*a6c5e0d9Schristos #endif
16976728a53Schristos 		if (user) {
170*a6c5e0d9Schristos 			if (ruser_name(stp->st_uid) != NULL)
17176728a53Schristos 				continue; /* Already seen */
172*a6c5e0d9Schristos 			id = (u_int)stp->st_uid;
17376728a53Schristos 		} else {
174*a6c5e0d9Schristos 			if (rgroup_name(stp->st_gid) != NULL)
17576728a53Schristos 				continue; /* Already seen */
176*a6c5e0d9Schristos 			id = (u_int)stp->st_gid;
17776728a53Schristos 		}
17876728a53Schristos 		if (has_id(id, ids, n))
17976728a53Schristos 			continue;
18076728a53Schristos 		ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
18176728a53Schristos 		ids[n++] = id;
18276728a53Schristos 	}
18376728a53Schristos 	*idsp = ids;
18476728a53Schristos 	*nidsp = n;
18576728a53Schristos }
18676728a53Schristos 
18776728a53Schristos void
get_remote_user_groups_from_glob(struct sftp_conn * conn,glob_t * g)18876728a53Schristos get_remote_user_groups_from_glob(struct sftp_conn *conn, glob_t *g)
18976728a53Schristos {
19076728a53Schristos 	u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
19176728a53Schristos 
19276728a53Schristos 	if (!can_get_users_groups_by_id(conn))
19376728a53Schristos 		return;
19476728a53Schristos 
19576728a53Schristos 	collect_ids_from_glob(g, 1, &uids, &nuids);
19676728a53Schristos 	collect_ids_from_glob(g, 0, &gids, &ngids);
19776728a53Schristos 	lookup_and_record(conn, uids, nuids, gids, ngids);
19876728a53Schristos 	free(uids);
19976728a53Schristos 	free(gids);
20076728a53Schristos }
20176728a53Schristos 
20276728a53Schristos static void
collect_ids_from_dirents(SFTP_DIRENT ** d,int user,u_int ** idsp,u_int * nidsp)20376728a53Schristos collect_ids_from_dirents(SFTP_DIRENT **d, int user, u_int **idsp, u_int *nidsp)
20476728a53Schristos {
20576728a53Schristos 	u_int id, i, n = 0, *ids = NULL;
20676728a53Schristos 
20776728a53Schristos 	for (i = 0; d[i] != NULL; i++) {
20876728a53Schristos 		if (user) {
20976728a53Schristos 			if (ruser_name((uid_t)(d[i]->a.uid)) != NULL)
21076728a53Schristos 				continue; /* Already seen */
21176728a53Schristos 			id = d[i]->a.uid;
21276728a53Schristos 		} else {
21376728a53Schristos 			if (rgroup_name((gid_t)(d[i]->a.gid)) != NULL)
21476728a53Schristos 				continue; /* Already seen */
21576728a53Schristos 			id = d[i]->a.gid;
21676728a53Schristos 		}
21776728a53Schristos 		if (has_id(id, ids, n))
21876728a53Schristos 			continue;
21976728a53Schristos 		ids = xrecallocarray(ids, n, n + 1, sizeof(*ids));
22076728a53Schristos 		ids[n++] = id;
22176728a53Schristos 	}
22276728a53Schristos 	*idsp = ids;
22376728a53Schristos 	*nidsp = n;
22476728a53Schristos }
22576728a53Schristos 
22676728a53Schristos void
get_remote_user_groups_from_dirents(struct sftp_conn * conn,SFTP_DIRENT ** d)22776728a53Schristos get_remote_user_groups_from_dirents(struct sftp_conn *conn, SFTP_DIRENT **d)
22876728a53Schristos {
22976728a53Schristos 	u_int *uids = NULL, nuids = 0, *gids = NULL, ngids = 0;
23076728a53Schristos 
23176728a53Schristos 	if (!can_get_users_groups_by_id(conn))
23276728a53Schristos 		return;
23376728a53Schristos 
23476728a53Schristos 	collect_ids_from_dirents(d, 1, &uids, &nuids);
23576728a53Schristos 	collect_ids_from_dirents(d, 0, &gids, &ngids);
23676728a53Schristos 	lookup_and_record(conn, uids, nuids, gids, ngids);
23776728a53Schristos 	free(uids);
23876728a53Schristos 	free(gids);
23976728a53Schristos }
24076728a53Schristos 
24176728a53Schristos const char *
ruser_name(uid_t uid)24276728a53Schristos ruser_name(uid_t uid)
24376728a53Schristos {
24476728a53Schristos 	return idname_lookup(&user_idname, (u_int)uid);
24576728a53Schristos }
24676728a53Schristos 
24776728a53Schristos const char *
rgroup_name(uid_t gid)24876728a53Schristos rgroup_name(uid_t gid)
24976728a53Schristos {
25076728a53Schristos 	return idname_lookup(&group_idname, (u_int)gid);
25176728a53Schristos }
25276728a53Schristos 
253