From 4822725962d817d645cc9809d7a780210ff73d42 Mon Sep 17 00:00:00 2001 From: yoe Date: Sun, 19 Aug 2007 16:05:48 +0000 Subject: [PATCH] r278: Set group _after_ setting user, since setting the user may make us lose rights to set a group --- nbd-server.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/nbd-server.c b/nbd-server.c index 09a5256..4c5792c 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -68,6 +68,7 @@ #include /* For BLKGETSIZE */ #endif #include /* sigaction */ +#include #include #include /* sockaddr_in, htons, in_addr */ #include /* hostent, gethostby*, getservby* */ @@ -1052,7 +1053,7 @@ void negotiate(CLIENT *client) { /** sending macro. */ #define SEND(net,reply) writeit( net, &reply, sizeof( reply )); /** error macro. */ -#define ERROR(client,reply) { reply.error = htonl(-1); SEND(client->net,reply); reply.error = 0; } +#define ERROR(client,reply,errcode) { reply.error = htonl(errcode); SEND(client->net,reply); reply.error = 0; } /** * Serve a file to a single client. * @@ -1112,13 +1113,13 @@ int mainloop(CLIENT *client) { memcpy(reply.handle, request.handle, sizeof(reply.handle)); if ((request.from + len) > (OFFT_MAX)) { DEBUG("[Number too large!]"); - ERROR(client, reply); + ERROR(client, reply, EINVAL); continue; } if (((ssize_t)((off_t)request.from + len) > client->exportsize)) { DEBUG("[RANGE!]"); - ERROR(client, reply); + ERROR(client, reply, EINVAL); continue; } @@ -1129,12 +1130,12 @@ int mainloop(CLIENT *client) { if ((client->server->flags & F_READONLY) || (client->server->flags & F_AUTOREADONLY)) { DEBUG("[WRITE to READONLY!]"); - ERROR(client, reply); + ERROR(client, reply, EPERM); continue; } if (expwrite(request.from, buf, len, client)) { DEBUG("Write failed: %m" ); - ERROR(client, reply); + ERROR(client, reply, errno); continue; } SEND(client->net, reply); @@ -1146,7 +1147,7 @@ int mainloop(CLIENT *client) { DEBUG("exp->buf, "); if (expread(request.from, buf + sizeof(struct nbd_reply), len, client)) { DEBUG("Read failed: %m"); - ERROR(client, reply); + ERROR(client, reply, errno); continue; } @@ -1549,16 +1550,16 @@ int serveloop(GArray* servers) { void dousers(void) { struct passwd *pw; struct group *gr; - if(runuser) { - pw=getpwnam(runuser); - if(setuid(pw->pw_uid)<0) - msg3(LOG_DEBUG, "Could not set UID: %s", strerror(errno)); - } if(rungroup) { gr=getgrnam(rungroup); if(setgid(gr->gr_gid)<0) msg3(LOG_DEBUG, "Could not set GID: %s", strerror(errno)); } + if(runuser) { + pw=getpwnam(runuser); + if(setuid(pw->pw_uid)<0) + msg3(LOG_DEBUG, "Could not set UID: %s", strerror(errno)); + } } /** -- 1.7.10.4