virtio-9p: Add minimal set of FileOperations
Add minimal set of FileOperations and the corresponding implementations for local fstype. These will be required for the FID management patches later on. [aneesh.kumar@linux.vnet.ibm.com: rpath fix ] Signed-off-by: Anthony Liguori <aliguori@us.ibm.com> Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
parent
a03f787439
commit
131dcb2521
|
@ -27,6 +27,11 @@ typedef struct FsContext
|
||||||
|
|
||||||
typedef struct FileOperations
|
typedef struct FileOperations
|
||||||
{
|
{
|
||||||
|
int (*lstat)(FsContext *, const char *, struct stat *);
|
||||||
|
ssize_t (*readlink)(FsContext *, const char *, char *, size_t);
|
||||||
|
int (*setuid)(FsContext *, uid_t);
|
||||||
|
int (*close)(FsContext *, int);
|
||||||
|
int (*closedir)(FsContext *, DIR *);
|
||||||
void *opaque;
|
void *opaque;
|
||||||
} FileOperations;
|
} FileOperations;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -12,6 +12,84 @@
|
||||||
*/
|
*/
|
||||||
#include "virtio.h"
|
#include "virtio.h"
|
||||||
#include "virtio-9p.h"
|
#include "virtio-9p.h"
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <grp.h>
|
||||||
|
|
||||||
|
static const char *rpath(FsContext *ctx, const char *path)
|
||||||
|
{
|
||||||
|
/* FIXME: so wrong... */
|
||||||
|
static char buffer[4096];
|
||||||
|
snprintf(buffer, sizeof(buffer), "%s/%s", ctx->fs_root, path);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int local_lstat(FsContext *ctx, const char *path, struct stat *stbuf)
|
||||||
|
{
|
||||||
|
return lstat(rpath(ctx, path), stbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int local_setuid(FsContext *ctx, uid_t uid)
|
||||||
|
{
|
||||||
|
struct passwd *pw;
|
||||||
|
gid_t groups[33];
|
||||||
|
int ngroups;
|
||||||
|
static uid_t cur_uid = -1;
|
||||||
|
|
||||||
|
if (cur_uid == uid) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setreuid(0, 0)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pw = getpwuid(uid);
|
||||||
|
if (pw == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ngroups = 33;
|
||||||
|
if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setgroups(ngroups, groups)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setregid(-1, pw->pw_gid)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setreuid(-1, uid)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur_uid = uid;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t local_readlink(FsContext *ctx, const char *path,
|
||||||
|
char *buf, size_t bufsz)
|
||||||
|
{
|
||||||
|
return readlink(rpath(ctx, path), buf, bufsz);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int local_close(FsContext *ctx, int fd)
|
||||||
|
{
|
||||||
|
return close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int local_closedir(FsContext *ctx, DIR *dir)
|
||||||
|
{
|
||||||
|
return closedir(dir);
|
||||||
|
}
|
||||||
|
|
||||||
FileOperations local_ops = {
|
FileOperations local_ops = {
|
||||||
|
.lstat = local_lstat,
|
||||||
|
.setuid = local_setuid,
|
||||||
|
.readlink = local_readlink,
|
||||||
|
.close = local_close,
|
||||||
|
.closedir = local_closedir,
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,6 +21,41 @@
|
||||||
int dotu = 1;
|
int dotu = 1;
|
||||||
int debug_9p_pdu;
|
int debug_9p_pdu;
|
||||||
|
|
||||||
|
static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
|
||||||
|
{
|
||||||
|
return s->ops->lstat(&s->ctx, path->data, stbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int v9fs_do_setuid(V9fsState *s, uid_t uid)
|
||||||
|
{
|
||||||
|
return s->ops->setuid(&s->ctx, uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
|
||||||
|
{
|
||||||
|
ssize_t len;
|
||||||
|
|
||||||
|
buf->data = qemu_malloc(1024);
|
||||||
|
|
||||||
|
len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
|
||||||
|
if (len > -1) {
|
||||||
|
buf->size = len;
|
||||||
|
buf->data[len] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int v9fs_do_close(V9fsState *s, int fd)
|
||||||
|
{
|
||||||
|
return s->ops->close(&s->ctx, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int v9fs_do_closedir(V9fsState *s, DIR *dir)
|
||||||
|
{
|
||||||
|
return s->ops->closedir(&s->ctx, dir);
|
||||||
|
}
|
||||||
|
|
||||||
static void v9fs_string_init(V9fsString *str)
|
static void v9fs_string_init(V9fsString *str)
|
||||||
{
|
{
|
||||||
str->data = NULL;
|
str->data = NULL;
|
||||||
|
@ -437,9 +472,13 @@ static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu)
|
||||||
(void) v9fs_string_sprintf;
|
(void) v9fs_string_sprintf;
|
||||||
(void) v9fs_string_copy;
|
(void) v9fs_string_copy;
|
||||||
(void) v9fs_string_size;
|
(void) v9fs_string_size;
|
||||||
|
(void) v9fs_do_lstat;
|
||||||
|
(void) v9fs_do_setuid;
|
||||||
|
(void) v9fs_do_readlink;
|
||||||
|
(void) v9fs_do_close;
|
||||||
|
(void) v9fs_do_closedir;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
|
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
|
||||||
{
|
{
|
||||||
if (debug_9p_pdu) {
|
if (debug_9p_pdu) {
|
||||||
|
|
Loading…
Reference in New Issue