mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-04-01 20:05:19 +00:00
Turn virNetSASLContext and virNetSASLSession into virObject instances
Make virNetSASLContext and virNetSASLSession use virObject APIs for reference counting Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
parent
e10e1969d5
commit
0b4d3fe556
2
cfg.mk
2
cfg.mk
@ -156,8 +156,6 @@ useless_free_options = \
|
||||
--name=virNetServerProgramFree \
|
||||
--name=virNetServerServiceFree \
|
||||
--name=virNetSocketFree \
|
||||
--name=virNetSASLContextFree \
|
||||
--name=virNetSASLSessionFree \
|
||||
--name=virNWFilterDefFree \
|
||||
--name=virNWFilterEntryFree \
|
||||
--name=virNWFilterHashTableFree \
|
||||
|
@ -2325,7 +2325,7 @@ authfail:
|
||||
PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
|
||||
"client=%p auth=%d",
|
||||
client, REMOTE_AUTH_SASL);
|
||||
virNetSASLSessionFree(sasl);
|
||||
virObjectUnref(sasl);
|
||||
virMutexUnlock(&priv->lock);
|
||||
return -1;
|
||||
}
|
||||
@ -2369,7 +2369,7 @@ remoteSASLFinish(virNetServerClientPtr client)
|
||||
"client=%p auth=%d identity=%s",
|
||||
client, REMOTE_AUTH_SASL, identity);
|
||||
|
||||
virNetSASLSessionFree(priv->sasl);
|
||||
virObjectUnref(priv->sasl);
|
||||
priv->sasl = NULL;
|
||||
|
||||
return 0;
|
||||
@ -2467,7 +2467,7 @@ authdeny:
|
||||
goto error;
|
||||
|
||||
error:
|
||||
virNetSASLSessionFree(priv->sasl);
|
||||
virObjectUnref(priv->sasl);
|
||||
priv->sasl = NULL;
|
||||
virResetLastError();
|
||||
virReportError(VIR_ERR_AUTH_FAILED, "%s",
|
||||
@ -2565,7 +2565,7 @@ authdeny:
|
||||
goto error;
|
||||
|
||||
error:
|
||||
virNetSASLSessionFree(priv->sasl);
|
||||
virObjectUnref(priv->sasl);
|
||||
priv->sasl = NULL;
|
||||
virResetLastError();
|
||||
virReportError(VIR_ERR_AUTH_FAILED, "%s",
|
||||
|
@ -1470,27 +1470,13 @@ xdr_virNetMessageError;
|
||||
|
||||
|
||||
# virnetsaslcontext.h
|
||||
virNetSASLContextCheckIdentity;
|
||||
virNetSASLContextFree;
|
||||
virNetSASLContextNewClient;
|
||||
virNetSASLContextNewServer;
|
||||
virNetSASLContextRef;
|
||||
virNetSASLSessionClientStart;
|
||||
virNetSASLSessionClientStep;
|
||||
virNetSASLSessionDecode;
|
||||
virNetSASLSessionEncode;
|
||||
virNetSASLSessionExtKeySize;
|
||||
virNetSASLSessionFree;
|
||||
virNetSASLSessionGetIdentity;
|
||||
virNetSASLSessionGetKeySize;
|
||||
virNetSASLSessionGetMaxBufSize;
|
||||
virNetSASLSessionListMechanisms;
|
||||
virNetSASLSessionNewClient;
|
||||
virNetSASLSessionNewServer;
|
||||
virNetSASLSessionRef;
|
||||
virNetSASLSessionSecProps;
|
||||
virNetSASLSessionServerStart;
|
||||
virNetSASLSessionServerStep;
|
||||
|
||||
|
||||
# virnetserver.h
|
||||
@ -1542,7 +1528,6 @@ virNetServerClientSetCloseHook;
|
||||
virNetServerClientSetDispatcher;
|
||||
virNetServerClientSetIdentity;
|
||||
virNetServerClientSetPrivateData;
|
||||
virNetServerClientSetSASLSession;
|
||||
virNetServerClientStartKeepAlive;
|
||||
virNetServerClientWantClose;
|
||||
|
||||
|
@ -6,7 +6,6 @@
|
||||
virNetSASLContextCheckIdentity;
|
||||
virNetSASLContextNewServer;
|
||||
virNetSASLSessionExtKeySize;
|
||||
virNetSASLSessionFree;
|
||||
virNetSASLSessionGetIdentity;
|
||||
virNetSASLSessionGetKeySize;
|
||||
virNetSASLSessionListMechanisms;
|
||||
|
@ -3397,8 +3397,8 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv,
|
||||
|
||||
remoteAuthInteractStateClear(&state, true);
|
||||
VIR_FREE(saslcb);
|
||||
virNetSASLSessionFree(sasl);
|
||||
virNetSASLContextFree(saslCtxt);
|
||||
virObjectUnref(sasl);
|
||||
virObjectUnref(saslCtxt);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -497,7 +497,7 @@ void virNetClientFree(virNetClientPtr client)
|
||||
virNetSocketFree(client->sock);
|
||||
virObjectUnref(client->tls);
|
||||
#if HAVE_SASL
|
||||
virNetSASLSessionFree(client->sasl);
|
||||
virObjectUnref(client->sasl);
|
||||
#endif
|
||||
|
||||
virNetMessageClear(&client->msg);
|
||||
@ -535,7 +535,7 @@ virNetClientCloseLocked(virNetClientPtr client)
|
||||
virObjectUnref(client->tls);
|
||||
client->tls = NULL;
|
||||
#if HAVE_SASL
|
||||
virNetSASLSessionFree(client->sasl);
|
||||
virObjectUnref(client->sasl);
|
||||
client->sasl = NULL;
|
||||
#endif
|
||||
ka = client->keepalive;
|
||||
@ -607,8 +607,7 @@ void virNetClientSetSASLSession(virNetClientPtr client,
|
||||
virNetSASLSessionPtr sasl)
|
||||
{
|
||||
virNetClientLock(client);
|
||||
client->sasl = sasl;
|
||||
virNetSASLSessionRef(sasl);
|
||||
client->sasl = virObjectRef(sasl);
|
||||
virNetSocketSetSASLSession(client->sock, client->sasl);
|
||||
virNetClientUnlock(client);
|
||||
}
|
||||
|
@ -33,24 +33,52 @@
|
||||
#define VIR_FROM_THIS VIR_FROM_RPC
|
||||
|
||||
struct _virNetSASLContext {
|
||||
virObject object;
|
||||
|
||||
virMutex lock;
|
||||
const char *const*usernameWhitelist;
|
||||
int refs;
|
||||
};
|
||||
|
||||
struct _virNetSASLSession {
|
||||
virObject object;
|
||||
|
||||
virMutex lock;
|
||||
sasl_conn_t *conn;
|
||||
int refs;
|
||||
size_t maxbufsize;
|
||||
};
|
||||
|
||||
|
||||
static virClassPtr virNetSASLContextClass;
|
||||
static virClassPtr virNetSASLSessionClass;
|
||||
static void virNetSASLContextDispose(void *obj);
|
||||
static void virNetSASLSessionDispose(void *obj);
|
||||
|
||||
static int virNetSASLContextOnceInit(void)
|
||||
{
|
||||
if (!(virNetSASLContextClass = virClassNew("virNetSASLContext",
|
||||
sizeof(virNetSASLContext),
|
||||
virNetSASLContextDispose)))
|
||||
return -1;
|
||||
|
||||
if (!(virNetSASLSessionClass = virClassNew("virNetSASLSession",
|
||||
sizeof(virNetSASLSession),
|
||||
virNetSASLSessionDispose)))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
VIR_ONCE_GLOBAL_INIT(virNetSASLContext)
|
||||
|
||||
|
||||
virNetSASLContextPtr virNetSASLContextNewClient(void)
|
||||
{
|
||||
virNetSASLContextPtr ctxt;
|
||||
int err;
|
||||
|
||||
if (virNetSASLContextInitialize() < 0)
|
||||
return NULL;
|
||||
|
||||
err = sasl_client_init(NULL);
|
||||
if (err != SASL_OK) {
|
||||
virReportError(VIR_ERR_AUTH_FAILED,
|
||||
@ -59,10 +87,8 @@ virNetSASLContextPtr virNetSASLContextNewClient(void)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (VIR_ALLOC(ctxt) < 0) {
|
||||
virReportOOMError();
|
||||
if (!(ctxt = virObjectNew(virNetSASLContextClass)))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (virMutexInit(&ctxt->lock) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
@ -71,8 +97,6 @@ virNetSASLContextPtr virNetSASLContextNewClient(void)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctxt->refs = 1;
|
||||
|
||||
return ctxt;
|
||||
}
|
||||
|
||||
@ -81,6 +105,9 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char *const*usernameWhitel
|
||||
virNetSASLContextPtr ctxt;
|
||||
int err;
|
||||
|
||||
if (virNetSASLContextInitialize() < 0)
|
||||
return NULL;
|
||||
|
||||
err = sasl_server_init(NULL, "libvirt");
|
||||
if (err != SASL_OK) {
|
||||
virReportError(VIR_ERR_AUTH_FAILED,
|
||||
@ -89,10 +116,8 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char *const*usernameWhitel
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (VIR_ALLOC(ctxt) < 0) {
|
||||
virReportOOMError();
|
||||
if (!(ctxt = virObjectNew(virNetSASLContextClass)))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (virMutexInit(&ctxt->lock) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
@ -102,7 +127,6 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char *const*usernameWhitel
|
||||
}
|
||||
|
||||
ctxt->usernameWhitelist = usernameWhitelist;
|
||||
ctxt->refs = 1;
|
||||
|
||||
return ctxt;
|
||||
}
|
||||
@ -152,28 +176,11 @@ cleanup:
|
||||
}
|
||||
|
||||
|
||||
void virNetSASLContextRef(virNetSASLContextPtr ctxt)
|
||||
void virNetSASLContextDispose(void *obj)
|
||||
{
|
||||
virMutexLock(&ctxt->lock);
|
||||
ctxt->refs++;
|
||||
virMutexUnlock(&ctxt->lock);
|
||||
}
|
||||
virNetSASLContextPtr ctxt = obj;
|
||||
|
||||
void virNetSASLContextFree(virNetSASLContextPtr ctxt)
|
||||
{
|
||||
if (!ctxt)
|
||||
return;
|
||||
|
||||
virMutexLock(&ctxt->lock);
|
||||
ctxt->refs--;
|
||||
if (ctxt->refs > 0) {
|
||||
virMutexUnlock(&ctxt->lock);
|
||||
return;
|
||||
}
|
||||
|
||||
virMutexUnlock(&ctxt->lock);
|
||||
virMutexDestroy(&ctxt->lock);
|
||||
VIR_FREE(ctxt);
|
||||
}
|
||||
|
||||
virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt ATTRIBUTE_UNUSED,
|
||||
@ -186,10 +193,8 @@ virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt ATTRIB
|
||||
virNetSASLSessionPtr sasl = NULL;
|
||||
int err;
|
||||
|
||||
if (VIR_ALLOC(sasl) < 0) {
|
||||
virReportOOMError();
|
||||
goto cleanup;
|
||||
}
|
||||
if (!(sasl = virObjectNew(virNetSASLSessionClass)))
|
||||
return NULL;
|
||||
|
||||
if (virMutexInit(&sasl->lock) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
@ -198,7 +203,6 @@ virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt ATTRIB
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sasl->refs = 1;
|
||||
/* Arbitrary size for amount of data we can encode in a single block */
|
||||
sasl->maxbufsize = 1 << 16;
|
||||
|
||||
@ -219,7 +223,7 @@ virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt ATTRIB
|
||||
return sasl;
|
||||
|
||||
cleanup:
|
||||
virNetSASLSessionFree(sasl);
|
||||
virObjectUnref(sasl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -231,10 +235,8 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr ctxt ATTRIB
|
||||
virNetSASLSessionPtr sasl = NULL;
|
||||
int err;
|
||||
|
||||
if (VIR_ALLOC(sasl) < 0) {
|
||||
virReportOOMError();
|
||||
goto cleanup;
|
||||
}
|
||||
if (!(sasl = virObjectNew(virNetSASLSessionClass)))
|
||||
return NULL;
|
||||
|
||||
if (virMutexInit(&sasl->lock) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
@ -243,7 +245,6 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr ctxt ATTRIB
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sasl->refs = 1;
|
||||
/* Arbitrary size for amount of data we can encode in a single block */
|
||||
sasl->maxbufsize = 1 << 16;
|
||||
|
||||
@ -265,17 +266,10 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr ctxt ATTRIB
|
||||
return sasl;
|
||||
|
||||
cleanup:
|
||||
virNetSASLSessionFree(sasl);
|
||||
virObjectUnref(sasl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void virNetSASLSessionRef(virNetSASLSessionPtr sasl)
|
||||
{
|
||||
virMutexLock(&sasl->lock);
|
||||
sasl->refs++;
|
||||
virMutexUnlock(&sasl->lock);
|
||||
}
|
||||
|
||||
int virNetSASLSessionExtKeySize(virNetSASLSessionPtr sasl,
|
||||
int ssf)
|
||||
{
|
||||
@ -712,22 +706,12 @@ cleanup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
void virNetSASLSessionFree(virNetSASLSessionPtr sasl)
|
||||
void virNetSASLSessionDispose(void *obj)
|
||||
{
|
||||
if (!sasl)
|
||||
return;
|
||||
|
||||
virMutexLock(&sasl->lock);
|
||||
sasl->refs--;
|
||||
if (sasl->refs > 0) {
|
||||
virMutexUnlock(&sasl->lock);
|
||||
return;
|
||||
}
|
||||
virNetSASLSessionPtr sasl = obj;
|
||||
|
||||
if (sasl->conn)
|
||||
sasl_dispose(&sasl->conn);
|
||||
|
||||
virMutexUnlock(&sasl->lock);
|
||||
virMutexDestroy(&sasl->lock);
|
||||
VIR_FREE(sasl);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@
|
||||
# include <sasl/sasl.h>
|
||||
|
||||
# include "internal.h"
|
||||
# include "virobject.h"
|
||||
|
||||
typedef struct _virNetSASLContext virNetSASLContext;
|
||||
typedef virNetSASLContext *virNetSASLContextPtr;
|
||||
@ -43,9 +44,6 @@ virNetSASLContextPtr virNetSASLContextNewServer(const char *const*usernameWhitel
|
||||
int virNetSASLContextCheckIdentity(virNetSASLContextPtr ctxt,
|
||||
const char *identity);
|
||||
|
||||
void virNetSASLContextRef(virNetSASLContextPtr sasl);
|
||||
void virNetSASLContextFree(virNetSASLContextPtr sasl);
|
||||
|
||||
virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt,
|
||||
const char *service,
|
||||
const char *hostname,
|
||||
@ -59,8 +57,6 @@ virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr ctxt,
|
||||
|
||||
char *virNetSASLSessionListMechanisms(virNetSASLSessionPtr sasl);
|
||||
|
||||
void virNetSASLSessionRef(virNetSASLSessionPtr sasl);
|
||||
|
||||
int virNetSASLSessionExtKeySize(virNetSASLSessionPtr sasl,
|
||||
int ssf);
|
||||
|
||||
@ -114,6 +110,4 @@ ssize_t virNetSASLSessionDecode(virNetSASLSessionPtr sasl,
|
||||
const char **output,
|
||||
size_t *outputlen);
|
||||
|
||||
void virNetSASLSessionFree(virNetSASLSessionPtr sasl);
|
||||
|
||||
#endif /* __VIR_NET_CLIENT_SASL_CONTEXT_H__ */
|
||||
|
@ -474,8 +474,7 @@ void virNetServerClientSetSASLSession(virNetServerClientPtr client,
|
||||
* operation do we switch to SASL mode
|
||||
*/
|
||||
virNetServerClientLock(client);
|
||||
client->sasl = sasl;
|
||||
virNetSASLSessionRef(sasl);
|
||||
client->sasl = virObjectRef(sasl);
|
||||
virNetServerClientUnlock(client);
|
||||
}
|
||||
#endif
|
||||
@ -591,7 +590,7 @@ void virNetServerClientFree(virNetServerClientPtr client)
|
||||
|
||||
VIR_FREE(client->identity);
|
||||
#if HAVE_SASL
|
||||
virNetSASLSessionFree(client->sasl);
|
||||
virObjectUnref(client->sasl);
|
||||
#endif
|
||||
if (client->sockTimer > 0)
|
||||
virEventRemoveTimeout(client->sockTimer);
|
||||
@ -1009,7 +1008,7 @@ virNetServerClientDispatchWrite(virNetServerClientPtr client)
|
||||
*/
|
||||
if (client->sasl) {
|
||||
virNetSocketSetSASLSession(client->sock, client->sasl);
|
||||
virNetSASLSessionFree(client->sasl);
|
||||
virObjectUnref(client->sasl);
|
||||
client->sasl = NULL;
|
||||
}
|
||||
#endif
|
||||
|
@ -750,7 +750,7 @@ void virNetSocketFree(virNetSocketPtr sock)
|
||||
virNetTLSSessionSetIOCallbacks(sock->tlsSession, NULL, NULL, NULL);
|
||||
virObjectUnref(sock->tlsSession);
|
||||
#if HAVE_SASL
|
||||
virNetSASLSessionFree(sock->saslSession);
|
||||
virObjectUnref(sock->saslSession);
|
||||
#endif
|
||||
|
||||
VIR_FORCE_CLOSE(sock->fd);
|
||||
@ -924,9 +924,8 @@ void virNetSocketSetSASLSession(virNetSocketPtr sock,
|
||||
virNetSASLSessionPtr sess)
|
||||
{
|
||||
virMutexLock(&sock->lock);
|
||||
virNetSASLSessionFree(sock->saslSession);
|
||||
sock->saslSession = sess;
|
||||
virNetSASLSessionRef(sess);
|
||||
virObjectUnref(sock->saslSession);
|
||||
sock->saslSession = virObjectRef(sess);
|
||||
virMutexUnlock(&sock->lock);
|
||||
}
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user