2013-09-23 11:39:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "rpc/virnetserverclient.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
|
|
2020-03-19 11:02:45 +00:00
|
|
|
#ifndef WIN32
|
2018-02-01 15:32:49 +00:00
|
|
|
|
|
|
|
static void *
|
2019-10-14 12:45:03 +00:00
|
|
|
testClientNew(virNetServerClientPtr client G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2018-02-01 15:32:49 +00:00
|
|
|
{
|
|
|
|
char *dummy;
|
|
|
|
|
2020-09-22 22:42:45 +00:00
|
|
|
dummy = g_new0(char, 1);
|
2018-02-01 15:32:49 +00:00
|
|
|
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
testClientFree(void *opaque)
|
|
|
|
{
|
|
|
|
VIR_FREE(opaque);
|
|
|
|
}
|
|
|
|
|
2019-10-14 12:45:03 +00:00
|
|
|
static int testIdentity(const void *opaque G_GNUC_UNUSED)
|
2013-09-23 11:39:19 +00:00
|
|
|
{
|
|
|
|
int sv[2];
|
|
|
|
int ret = -1;
|
|
|
|
virNetSocketPtr sock = NULL;
|
|
|
|
virNetServerClientPtr client = NULL;
|
2019-10-01 14:37:09 +00:00
|
|
|
g_autoptr(virIdentity) ident = NULL;
|
2013-09-23 11:39:19 +00:00
|
|
|
const char *gotUsername = NULL;
|
2019-07-26 11:21:29 +00:00
|
|
|
uid_t gotUserID;
|
2013-09-23 11:39:19 +00:00
|
|
|
const char *gotGroupname = NULL;
|
2019-07-26 11:21:29 +00:00
|
|
|
gid_t gotGroupID;
|
2013-09-23 11:39:19 +00:00
|
|
|
const char *gotSELinuxContext = NULL;
|
|
|
|
|
|
|
|
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
"Cannot create socket pair");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virNetSocketNewConnectSockFD(sv[0], &sock) < 0) {
|
|
|
|
virDispatchError(NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
sv[0] = -1;
|
|
|
|
|
2016-04-13 18:54:40 +00:00
|
|
|
if (!(client = virNetServerClientNew(1, sock, 0, false, 1,
|
2013-09-23 11:39:19 +00:00
|
|
|
NULL,
|
2018-02-01 15:32:49 +00:00
|
|
|
testClientNew,
|
|
|
|
NULL,
|
|
|
|
testClientFree,
|
|
|
|
NULL))) {
|
2013-09-23 11:39:19 +00:00
|
|
|
virDispatchError(NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ident = virNetServerClientGetIdentity(client))) {
|
|
|
|
fprintf(stderr, "Failed to create identity\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (virIdentityGetUserName(ident, &gotUsername) <= 0) {
|
2013-09-23 11:39:19 +00:00
|
|
|
fprintf(stderr, "Missing username in identity\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (STRNEQ_NULLABLE("astrochicken", gotUsername)) {
|
|
|
|
fprintf(stderr, "Want username 'astrochicken' got '%s'\n",
|
|
|
|
NULLSTR(gotUsername));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (virIdentityGetUNIXUserID(ident, &gotUserID) <= 0) {
|
2013-09-23 11:39:19 +00:00
|
|
|
fprintf(stderr, "Missing user ID in identity\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-07-26 11:21:29 +00:00
|
|
|
if (666 != gotUserID) {
|
|
|
|
fprintf(stderr, "Want username '666' got '%llu'\n",
|
|
|
|
(unsigned long long)gotUserID);
|
2013-09-23 11:39:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (virIdentityGetGroupName(ident, &gotGroupname) <= 0) {
|
2013-09-23 11:39:19 +00:00
|
|
|
fprintf(stderr, "Missing groupname in identity\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (STRNEQ_NULLABLE("fictionalusers", gotGroupname)) {
|
|
|
|
fprintf(stderr, "Want groupname 'fictionalusers' got '%s'\n",
|
|
|
|
NULLSTR(gotGroupname));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (virIdentityGetUNIXGroupID(ident, &gotGroupID) <= 0) {
|
2013-09-23 11:39:19 +00:00
|
|
|
fprintf(stderr, "Missing group ID in identity\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-07-26 11:21:29 +00:00
|
|
|
if (7337 != gotGroupID) {
|
|
|
|
fprintf(stderr, "Want groupname '7337' got '%llu'\n",
|
|
|
|
(unsigned long long)gotGroupID);
|
2013-09-23 11:39:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:30:57 +00:00
|
|
|
if (virIdentityGetSELinuxContext(ident, &gotSELinuxContext) <= 0) {
|
2013-09-23 11:39:19 +00:00
|
|
|
fprintf(stderr, "Missing SELinux context in identity\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (STRNEQ_NULLABLE("foo_u:bar_r:wizz_t:s0-s0:c0.c1023", gotSELinuxContext)) {
|
2019-08-06 15:45:14 +00:00
|
|
|
fprintf(stderr, "Want SELinux context 'foo_u:bar_r:wizz_t:s0-s0:c0.c1023' got '%s'\n",
|
|
|
|
NULLSTR(gotSELinuxContext));
|
2013-09-23 11:39:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(sock);
|
2018-04-19 18:37:00 +00:00
|
|
|
if (client)
|
2018-04-02 11:19:38 +00:00
|
|
|
virNetServerClientClose(client);
|
2013-09-23 11:39:19 +00:00
|
|
|
virObjectUnref(client);
|
|
|
|
VIR_FORCE_CLOSE(sv[0]);
|
|
|
|
VIR_FORCE_CLOSE(sv[1]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Identity",
|
|
|
|
testIdentity, NULL) < 0)
|
2013-09-23 11:39:19 +00:00
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
2019-08-21 16:13:16 +00:00
|
|
|
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virnetserverclient"))
|
2013-09-23 11:39:19 +00:00
|
|
|
#else
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
return EXIT_AM_SKIP;
|
|
|
|
}
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain);
|
2013-09-23 11:39:19 +00:00
|
|
|
#endif
|