/*							-- DO NOT EDIT --
 * This file has been generated by smidump
 * version 0.3.1 for the scli package.
 *
 * Derived from SNMP-USER-BASED-SM-MIB:
 *   The management information definitions for the
 *   SNMP User-based Security Model.
 *   
 *
 * Revision 1999-01-20 00:00:
 *   Clarifications, published as RFC2574
 *
 * Revision 1997-11-20 00:00:
 *   Initial version, published as RFC2274
 *
 * $Id: snmp-user-based-sm-mib.c,v 1.18 2002/03/27 13:48:23 schoenw Exp $
 */

#include "snmp-user-based-sm-mib.h"

GSnmpEnum const snmp_user_based_sm_mib_enums_usmUserStorageType[] = {
    { SNMP_USER_BASED_SM_MIB_USMUSERSTORAGETYPE_OTHER,       "other" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTORAGETYPE_VOLATILE,    "volatile" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTORAGETYPE_NONVOLATILE, "nonVolatile" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTORAGETYPE_PERMANENT,   "permanent" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTORAGETYPE_READONLY,    "readOnly" },
    { 0, NULL }
};

GSnmpEnum const snmp_user_based_sm_mib_enums_usmUserStatus[] = {
    { SNMP_USER_BASED_SM_MIB_USMUSERSTATUS_ACTIVE,        "active" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTATUS_NOTINSERVICE,  "notInService" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTATUS_NOTREADY,      "notReady" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTATUS_CREATEANDGO,   "createAndGo" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTATUS_CREATEANDWAIT, "createAndWait" },
    { SNMP_USER_BASED_SM_MIB_USMUSERSTATUS_DESTROY,       "destroy" },
    { 0, NULL }
};


static guint32 const usmNoAuthProtocol[]
	= { SNMP_USER_BASED_SM_MIB_USMNOAUTHPROTOCOL };
static guint32 const usmHMACMD5AuthProtocol[]
	= { SNMP_USER_BASED_SM_MIB_USMHMACMD5AUTHPROTOCOL };
static guint32 const usmHMACSHAAuthProtocol[]
	= { SNMP_USER_BASED_SM_MIB_USMHMACSHAAUTHPROTOCOL };
static guint32 const usmNoPrivProtocol[]
	= { SNMP_USER_BASED_SM_MIB_USMNOPRIVPROTOCOL };
static guint32 const usmDESPrivProtocol[]
	= { SNMP_USER_BASED_SM_MIB_USMDESPRIVPROTOCOL };

GSnmpIdentity const snmp_user_based_sm_mib_identities[] = {
    { usmNoAuthProtocol,
      sizeof(usmNoAuthProtocol)/sizeof(guint32),
      "usmNoAuthProtocol" },
    { usmHMACMD5AuthProtocol,
      sizeof(usmHMACMD5AuthProtocol)/sizeof(guint32),
      "usmHMACMD5AuthProtocol" },
    { usmHMACSHAAuthProtocol,
      sizeof(usmHMACSHAAuthProtocol)/sizeof(guint32),
      "usmHMACSHAAuthProtocol" },
    { usmNoPrivProtocol,
      sizeof(usmNoPrivProtocol)/sizeof(guint32),
      "usmNoPrivProtocol" },
    { usmDESPrivProtocol,
      sizeof(usmDESPrivProtocol)/sizeof(guint32),
      "usmDESPrivProtocol" },
    { 0, 0, NULL }
};


static guint32 usmUserSpinLock_constraints[] = {0L, 2147483647L, 0, 0};
static guint16 usmUserSecurityName_constraints[] = {0, 255, 0, 0};
static guint16 usmUserPublic_constraints[] = {0, 32, 0, 0};


static guint32 const usmStats_oid[] = {1, 3, 6, 1, 6, 3, 15, 1, 1};

static GSnmpAttribute usmStats_attr[] = {
    { 1, G_SNMP_COUNTER32,
      SNMP_USER_BASED_SM_MIB_USMSTATSUNSUPPORTEDSECLEVELS, "usmStatsUnsupportedSecLevels",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmStats_t, usmStatsUnsupportedSecLevels),
      0,
      0 },
    { 2, G_SNMP_COUNTER32,
      SNMP_USER_BASED_SM_MIB_USMSTATSNOTINTIMEWINDOWS, "usmStatsNotInTimeWindows",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmStats_t, usmStatsNotInTimeWindows),
      0,
      0 },
    { 3, G_SNMP_COUNTER32,
      SNMP_USER_BASED_SM_MIB_USMSTATSUNKNOWNUSERNAMES, "usmStatsUnknownUserNames",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmStats_t, usmStatsUnknownUserNames),
      0,
      0 },
    { 4, G_SNMP_COUNTER32,
      SNMP_USER_BASED_SM_MIB_USMSTATSUNKNOWNENGINEIDS, "usmStatsUnknownEngineIDs",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmStats_t, usmStatsUnknownEngineIDs),
      0,
      0 },
    { 5, G_SNMP_COUNTER32,
      SNMP_USER_BASED_SM_MIB_USMSTATSWRONGDIGESTS, "usmStatsWrongDigests",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmStats_t, usmStatsWrongDigests),
      0,
      0 },
    { 6, G_SNMP_COUNTER32,
      SNMP_USER_BASED_SM_MIB_USMSTATSDECRYPTIONERRORS, "usmStatsDecryptionErrors",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmStats_t, usmStatsDecryptionErrors),
      0,
      0 },
    { 0, 0, 0, NULL }
};

static guint32 const usmUser_oid[] = {1, 3, 6, 1, 6, 3, 15, 1, 2};

static GSnmpAttribute usmUser_attr[] = {
    { 1, G_SNMP_INTEGER32,
      SNMP_USER_BASED_SM_MIB_USMUSERSPINLOCK, "usmUserSpinLock",
       usmUserSpinLock_constraints,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUser_t, usmUserSpinLock),
      0,
      GSNMP_ATTR_FLAG_WRITABLE },
    { 0, 0, 0, NULL }
};

static guint32 const usmUserEntry_oid[] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1};

static GSnmpAttribute usmUserEntry_attr[] = {
    { 3, G_SNMP_OCTETSTRING,
      SNMP_USER_BASED_SM_MIB_USMUSERSECURITYNAME, "usmUserSecurityName",
       usmUserSecurityName_constraints,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserSecurityName),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserSecurityNameLength),
      0 },
    { 4, G_SNMP_OBJECTID,
      SNMP_USER_BASED_SM_MIB_USMUSERCLONEFROM, "usmUserCloneFrom",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserCloneFrom),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserCloneFromLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 5, G_SNMP_OBJECTID,
      SNMP_USER_BASED_SM_MIB_USMUSERAUTHPROTOCOL, "usmUserAuthProtocol",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserAuthProtocol),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserAuthProtocolLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 6, G_SNMP_OCTETSTRING,
      SNMP_USER_BASED_SM_MIB_USMUSERAUTHKEYCHANGE, "usmUserAuthKeyChange",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserAuthKeyChange),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserAuthKeyChangeLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 7, G_SNMP_OCTETSTRING,
      SNMP_USER_BASED_SM_MIB_USMUSEROWNAUTHKEYCHANGE, "usmUserOwnAuthKeyChange",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserOwnAuthKeyChange),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserOwnAuthKeyChangeLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 8, G_SNMP_OBJECTID,
      SNMP_USER_BASED_SM_MIB_USMUSERPRIVPROTOCOL, "usmUserPrivProtocol",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserPrivProtocol),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserPrivProtocolLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 9, G_SNMP_OCTETSTRING,
      SNMP_USER_BASED_SM_MIB_USMUSERPRIVKEYCHANGE, "usmUserPrivKeyChange",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserPrivKeyChange),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserPrivKeyChangeLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 10, G_SNMP_OCTETSTRING,
      SNMP_USER_BASED_SM_MIB_USMUSEROWNPRIVKEYCHANGE, "usmUserOwnPrivKeyChange",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserOwnPrivKeyChange),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserOwnPrivKeyChangeLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 11, G_SNMP_OCTETSTRING,
      SNMP_USER_BASED_SM_MIB_USMUSERPUBLIC, "usmUserPublic",
       usmUserPublic_constraints,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserPublic),
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, _usmUserPublicLength),
      GSNMP_ATTR_FLAG_WRITABLE },
    { 12, G_SNMP_INTEGER32,
      SNMP_USER_BASED_SM_MIB_USMUSERSTORAGETYPE, "usmUserStorageType",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserStorageType),
      0,
      GSNMP_ATTR_FLAG_WRITABLE },
    { 13, G_SNMP_INTEGER32,
      SNMP_USER_BASED_SM_MIB_USMUSERSTATUS, "usmUserStatus",
       NULL,
      G_STRUCT_OFFSET(snmp_user_based_sm_mib_usmUserEntry_t, usmUserStatus),
      0,
      GSNMP_ATTR_FLAG_WRITABLE },
    { 0, 0, 0, NULL }
};


snmp_user_based_sm_mib_usmStats_t *
snmp_user_based_sm_mib_new_usmStats()
{
    snmp_user_based_sm_mib_usmStats_t *usmStats;

    usmStats = (snmp_user_based_sm_mib_usmStats_t *) g_malloc0(sizeof(snmp_user_based_sm_mib_usmStats_t) + sizeof(gpointer));
    return usmStats;
}

static snmp_user_based_sm_mib_usmStats_t *
assign_usmStats(GSList *vbl)
{
    snmp_user_based_sm_mib_usmStats_t *usmStats;
    char *p;

    usmStats = snmp_user_based_sm_mib_new_usmStats();
    if (! usmStats) {
        return NULL;
    }

    p = (char *) usmStats + sizeof(snmp_user_based_sm_mib_usmStats_t);
    * (GSList **) p = vbl;

    gsnmp_attr_assign(vbl, usmStats_oid, sizeof(usmStats_oid)/sizeof(guint32),
                      usmStats_attr, usmStats);

    return usmStats;
}

void
snmp_user_based_sm_mib_get_usmStats(GSnmpSession *s, snmp_user_based_sm_mib_usmStats_t **usmStats, gint mask)
{
    GSList *in = NULL, *out = NULL;
    static guint32 base[] = {1, 3, 6, 1, 6, 3, 15, 1, 1, 0};

    *usmStats = NULL;

    gsnmp_attr_get(s, &in, base, 10, 9, usmStats_attr, mask);

    out = g_snmp_session_sync_getnext(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        if (s->error_status != G_SNMP_ERR_NOERROR) {
            g_snmp_vbl_free(out);
            return;
        }
        *usmStats = assign_usmStats(out);
    }
}

void
snmp_user_based_sm_mib_free_usmStats(snmp_user_based_sm_mib_usmStats_t *usmStats)
{
    GSList *vbl;
    char *p;

    if (usmStats) {
        p = (char *) usmStats + sizeof(snmp_user_based_sm_mib_usmStats_t);
        vbl = * (GSList **) p;
        g_snmp_vbl_free(vbl);
        g_free(usmStats);
    }
}

snmp_user_based_sm_mib_usmUser_t *
snmp_user_based_sm_mib_new_usmUser()
{
    snmp_user_based_sm_mib_usmUser_t *usmUser;

    usmUser = (snmp_user_based_sm_mib_usmUser_t *) g_malloc0(sizeof(snmp_user_based_sm_mib_usmUser_t) + sizeof(gpointer));
    return usmUser;
}

static snmp_user_based_sm_mib_usmUser_t *
assign_usmUser(GSList *vbl)
{
    snmp_user_based_sm_mib_usmUser_t *usmUser;
    char *p;

    usmUser = snmp_user_based_sm_mib_new_usmUser();
    if (! usmUser) {
        return NULL;
    }

    p = (char *) usmUser + sizeof(snmp_user_based_sm_mib_usmUser_t);
    * (GSList **) p = vbl;

    gsnmp_attr_assign(vbl, usmUser_oid, sizeof(usmUser_oid)/sizeof(guint32),
                      usmUser_attr, usmUser);

    return usmUser;
}

void
snmp_user_based_sm_mib_get_usmUser(GSnmpSession *s, snmp_user_based_sm_mib_usmUser_t **usmUser, gint mask)
{
    GSList *in = NULL, *out = NULL;
    static guint32 base[] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 0};

    *usmUser = NULL;

    gsnmp_attr_get(s, &in, base, 10, 9, usmUser_attr, mask);

    out = g_snmp_session_sync_getnext(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        if (s->error_status != G_SNMP_ERR_NOERROR) {
            g_snmp_vbl_free(out);
            return;
        }
        *usmUser = assign_usmUser(out);
    }
}

void
snmp_user_based_sm_mib_set_usmUser(GSnmpSession *s, snmp_user_based_sm_mib_usmUser_t *usmUser, gint mask)
{
    GSList *in = NULL, *out = NULL;
    static guint32 base[] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 0, 0};

    gsnmp_attr_set(s, &in, base, 11, 9, usmUser_attr, mask, usmUser);

    out = g_snmp_session_sync_set(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        g_snmp_vbl_free(out);
    }
}

void
snmp_user_based_sm_mib_free_usmUser(snmp_user_based_sm_mib_usmUser_t *usmUser)
{
    GSList *vbl;
    char *p;

    if (usmUser) {
        p = (char *) usmUser + sizeof(snmp_user_based_sm_mib_usmUser_t);
        vbl = * (GSList **) p;
        g_snmp_vbl_free(vbl);
        g_free(usmUser);
    }
}

snmp_user_based_sm_mib_usmUserEntry_t *
snmp_user_based_sm_mib_new_usmUserEntry()
{
    snmp_user_based_sm_mib_usmUserEntry_t *usmUserEntry;

    usmUserEntry = (snmp_user_based_sm_mib_usmUserEntry_t *) g_malloc0(sizeof(snmp_user_based_sm_mib_usmUserEntry_t) + sizeof(gpointer));
    return usmUserEntry;
}

static int
unpack_usmUserEntry(GSnmpVarBind *vb, snmp_user_based_sm_mib_usmUserEntry_t *usmUserEntry)
{
    guint8 idx = 12;
    guint16 i, len;

    if (vb->id_len < idx) return -1;
    len = vb->id[idx++];
    if (len < 5 || len > 32) return -1;
    if (vb->id_len < idx + len) return -1;
    for (i = 0; i < len; i++) {
        usmUserEntry->usmUserEngineID[i] = vb->id[idx++];
    }
    usmUserEntry->_usmUserEngineIDLength = len;
    if (vb->id_len < idx) return -1;
    len = vb->id[idx++];
    if (len < 1 || len > 32) return -1;
    if (vb->id_len < idx + len) return -1;
    for (i = 0; i < len; i++) {
        usmUserEntry->usmUserName[i] = vb->id[idx++];
    }
    usmUserEntry->_usmUserNameLength = len;
    if (vb->id_len > idx) return -1;
    return 0;
}

static int
pack_usmUserEntry(guint32 *base, guchar *usmUserEngineID, guint16 _usmUserEngineIDLength, guchar *usmUserName, guint16 _usmUserNameLength)
{
    guint8 idx = 12;
    guint16 i, len;

    len = _usmUserEngineIDLength;
    base[idx++] = len;
    if (len < 5 || len > 32) return -1;
    for (i = 0; i < len; i++) {
        base[idx++] = usmUserEngineID[i];
        if (idx >= 128) return -1;
    }
    len = _usmUserNameLength;
    base[idx++] = len;
    if (len < 1 || len > 32) return -1;
    for (i = 0; i < len; i++) {
        base[idx++] = usmUserName[i];
        if (idx >= 128) return -1;
    }
    return idx;
}

static snmp_user_based_sm_mib_usmUserEntry_t *
assign_usmUserEntry(GSList *vbl)
{
    snmp_user_based_sm_mib_usmUserEntry_t *usmUserEntry;
    char *p;

    usmUserEntry = snmp_user_based_sm_mib_new_usmUserEntry();
    if (! usmUserEntry) {
        return NULL;
    }

    p = (char *) usmUserEntry + sizeof(snmp_user_based_sm_mib_usmUserEntry_t);
    * (GSList **) p = vbl;

    if (unpack_usmUserEntry((GSnmpVarBind *) vbl->data, usmUserEntry) < 0) {
        g_warning("%s: invalid instance identifier", "usmUserEntry");
        g_free(usmUserEntry);
        return NULL;
    }

    gsnmp_attr_assign(vbl, usmUserEntry_oid, sizeof(usmUserEntry_oid)/sizeof(guint32),
                      usmUserEntry_attr, usmUserEntry);

    return usmUserEntry;
}

void
snmp_user_based_sm_mib_get_usmUserTable(GSnmpSession *s, snmp_user_based_sm_mib_usmUserEntry_t ***usmUserEntry, gint mask)
{
    GSList *in = NULL, *out = NULL;
    GSList *row;
    int i;
    static guint32 base[] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 0};

    *usmUserEntry = NULL;

    gsnmp_attr_get(s, &in, base, 12, 11, usmUserEntry_attr, mask);

    out = gsnmp_gettable(s, in);
    /* g_snmp_vbl_free(in); */

    if (out) {
        *usmUserEntry = (snmp_user_based_sm_mib_usmUserEntry_t **) g_malloc0((g_slist_length(out) + 1) * sizeof(snmp_user_based_sm_mib_usmUserEntry_t *));
        if (! *usmUserEntry) {
            s->error_status = G_SNMP_ERR_INTERNAL;
            g_snmp_vbl_free(out);
            return;
        }
        for (row = out, i = 0; row; row = g_slist_next(row), i++) {
            (*usmUserEntry)[i] = assign_usmUserEntry(row->data);
        }
    }
}

void
snmp_user_based_sm_mib_get_usmUserEntry(GSnmpSession *s, snmp_user_based_sm_mib_usmUserEntry_t **usmUserEntry, guchar *usmUserEngineID, guint16 _usmUserEngineIDLength, guchar *usmUserName, guint16 _usmUserNameLength, gint mask)
{
    GSList *in = NULL, *out = NULL;
    guint32 base[128];
    gint8 len;

    memset(base, 0, sizeof(base));
    memcpy(base, usmUserEntry_oid, sizeof(usmUserEntry_oid));
    len = pack_usmUserEntry(base, usmUserEngineID, _usmUserEngineIDLength, usmUserName, _usmUserNameLength);
    if (len < 0) {
        g_warning("%s: invalid index values", "usmUserEntry");
        s->error_status = G_SNMP_ERR_INTERNAL;
        return;
    }

    *usmUserEntry = NULL;

    gsnmp_attr_get(s, &in, base, len, 11, usmUserEntry_attr, mask);

    out = g_snmp_session_sync_get(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        if (s->error_status != G_SNMP_ERR_NOERROR) {
            g_snmp_vbl_free(out);
            return;
        }
        *usmUserEntry = assign_usmUserEntry(out);
    }
}

void
snmp_user_based_sm_mib_set_usmUserEntry(GSnmpSession *s, snmp_user_based_sm_mib_usmUserEntry_t *usmUserEntry, gint mask)
{
    GSList *in = NULL, *out = NULL;
    guint32 base[128];
    gint8 len;

    memset(base, 0, sizeof(base));
    memcpy(base, usmUserEntry_oid, sizeof(usmUserEntry_oid));
    len = pack_usmUserEntry(base, usmUserEntry->usmUserEngineID, usmUserEntry->_usmUserEngineIDLength, usmUserEntry->usmUserName, usmUserEntry->_usmUserNameLength);
    if (len < 0) {
        g_warning("%s: invalid index values", "usmUserEntry");
        s->error_status = G_SNMP_ERR_INTERNAL;
        return;
    }

    gsnmp_attr_set(s, &in, base, len, 11, usmUserEntry_attr, mask, usmUserEntry);

    out = g_snmp_session_sync_set(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        g_snmp_vbl_free(out);
    }
}

void
snmp_user_based_sm_mib_free_usmUserEntry(snmp_user_based_sm_mib_usmUserEntry_t *usmUserEntry)
{
    GSList *vbl;
    char *p;

    if (usmUserEntry) {
        p = (char *) usmUserEntry + sizeof(snmp_user_based_sm_mib_usmUserEntry_t);
        vbl = * (GSList **) p;
        g_snmp_vbl_free(vbl);
        g_free(usmUserEntry);
    }
}

void
snmp_user_based_sm_mib_free_usmUserTable(snmp_user_based_sm_mib_usmUserEntry_t **usmUserEntry)
{
    int i;

    if (usmUserEntry) {
        for (i = 0; usmUserEntry[i]; i++) {
            snmp_user_based_sm_mib_free_usmUserEntry(usmUserEntry[i]);
        }
        g_free(usmUserEntry);
    }
}


