From 9126c9c0366028cb799978d145fd361c6635b5f4 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Mon, 23 Dec 2013 13:35:31 +0100 Subject: [PATCH] add uid and gid URL param to specify alternate uid and/or gid This allows to connect with an alternate uid or gid than that of the current user. Example: examples/nfs-ls nfs://10.0.0.1/export?uid=1000&gid=33 Signed-off-by: Peter Lieven --- include/libnfs-private.h | 4 ++++ include/nfsc/libnfs.h | 1 - lib/init.c | 25 +++++++++++++++++++++++++ lib/libnfs.c | 4 ++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/libnfs-private.h b/include/libnfs-private.h index 6b64033..a2f3e1a 100644 --- a/include/libnfs-private.h +++ b/include/libnfs-private.h @@ -97,6 +97,8 @@ struct rpc_context { /* parameters passable via URL */ int tcp_syncnt; + int uid; + int gid; }; struct rpc_pdu { @@ -140,6 +142,8 @@ void rpc_set_autoreconnect(struct rpc_context *rpc); void rpc_unset_autoreconnect(struct rpc_context *rpc); void rpc_set_tcp_syncnt(struct rpc_context *rpc, int v); +void rpc_set_uid(struct rpc_context *rpc, int uid); +void rpc_set_gid(struct rpc_context *rpc, int gid); int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size); void rpc_free_all_fragments(struct rpc_context *rpc); diff --git a/include/nfsc/libnfs.h b/include/nfsc/libnfs.h index 1035810..1ea7561 100644 --- a/include/nfsc/libnfs.h +++ b/include/nfsc/libnfs.h @@ -77,7 +77,6 @@ EXTERN int nfs_queue_length(struct nfs_context *nfs); struct AUTH; EXTERN void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth); - /* * When an operation failed, this function can extract a detailed error string. */ diff --git a/lib/init.c b/lib/init.c index bb2fe17..df3c2aa 100644 --- a/lib/init.c +++ b/lib/init.c @@ -74,6 +74,13 @@ struct rpc_context *rpc_init_context(void) salt += 0x01000000; rpc->fd = -1; rpc->tcp_syncnt = RPC_PARAM_UNDEFINED; +#ifdef WIN32 + rpc->uid = 65534; + rpc->gid = 65534; +#else + rpc->uid = getuid(); + rpc->gid = getgid(); +#endif return rpc; } @@ -101,6 +108,24 @@ void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) rpc->auth = auth; } +static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) { + if (uid != rpc->uid || gid != rpc->gid) { + struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL); + if (auth != NULL) { + rpc_set_auth(rpc, auth); + rpc->uid = uid; + rpc->gid = gid; + } + } +} + +void rpc_set_uid(struct rpc_context *rpc, int uid) { + rpc_set_uid_gid(rpc, uid, rpc->gid); +} + +void rpc_set_gid(struct rpc_context *rpc, int gid) { + rpc_set_uid_gid(rpc, rpc->uid, gid); +} void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) { diff --git a/lib/libnfs.c b/lib/libnfs.c index cd15fb0..f7a2bf0 100644 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -282,6 +282,10 @@ flags: strp2++; if (!strncmp(strp, "tcp-syncnt", 10)) { rpc_set_tcp_syncnt(nfs->rpc, atoi(strp2)); + } else if (!strncmp(strp, "uid", 3)) { + rpc_set_uid(nfs->rpc, atoi(strp2)); + } else if (!strncmp(strp, "gid", 3)) { + rpc_set_gid(nfs->rpc, atoi(strp2)); } } } -- 2.34.1