314 lines
9.7 KiB
C
314 lines
9.7 KiB
C
/*
|
|
******************************************************************************
|
|
* INTERPEAK SOURCE FILE
|
|
*
|
|
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipdhcps6_user.c,v $ $Revision: 1.7.54.1 $
|
|
* $Source: /home/interpeak/CVSRoot/ipdhcps6/src/ipdhcps6_user.c,v $
|
|
* $Author: ulf $
|
|
* $State: Exp $ $Locker: $
|
|
*
|
|
* INTERPEAK_COPYRIGHT_STRING
|
|
* Design and implementation by Kenneth Jonsson <kenneth@interpeak.se>
|
|
******************************************************************************
|
|
*/
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 1 DESCRIPTION
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 2 CONFIGURATION
|
|
****************************************************************************
|
|
*/
|
|
|
|
#include "ipdhcps6_config.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 3 INCLUDE FILES
|
|
****************************************************************************
|
|
*/
|
|
|
|
#define IPCOM_USE_CLIB_PROTO
|
|
#include <ipcom_clib.h>
|
|
#include <ipcom_cstyle.h>
|
|
#include <ipcom_err.h>
|
|
#include <ipcom_sysvar.h>
|
|
#include <ipcom_type.h>
|
|
#include <ipcom_syslog.h>
|
|
|
|
#include "ipdhcps6.h"
|
|
#include "ipdhcps6_h.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 4 DEFINES
|
|
****************************************************************************
|
|
*/
|
|
|
|
#define IPDHCPS6_USER_SYSVAR_NAME (36 + IPCOM_AUTH_USERNAME_MAX)
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 5 TYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
|
|
typedef struct Ipdhcps6_list_cb_data_struct
|
|
{
|
|
Ipdhcps6_user *users;
|
|
int users_len;
|
|
int num_users;
|
|
}
|
|
Ipdhcps6_list_cb_data;
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 6 EXTERN PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 7 LOCAL PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 8 DATA
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 9 STATIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipdhcps6_user_prefix_ids_from_str
|
|
*===========================================================================
|
|
* Description:
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC Ip_err
|
|
ipdhcps6_user_prefix_ids_from_str(Ipdhcps6_user *user, const char *str_prefixes)
|
|
{
|
|
int i, j;
|
|
|
|
for (i = 0; i < IPDHCPS6_PREFIX_PER_USER; i++)
|
|
ipcom_memset(user->prefix_ids[i], 0, IPDHCPS6_KEY_LEN);
|
|
|
|
for (i = 0, j = 0; *str_prefixes != '\0'; str_prefixes++)
|
|
{
|
|
if (*str_prefixes == ' ')
|
|
{
|
|
i = 0;
|
|
j++;
|
|
continue;
|
|
}
|
|
if (j >= IPDHCPS6_PREFIX_PER_USER)
|
|
{
|
|
IPCOM_LOG2(ERR, "Too many prefixes for user %s, max is %d",
|
|
user->name, IPDHCPS6_PREFIX_PER_USER);
|
|
return IPCOM_ERR_NO_MEMORY;
|
|
}
|
|
|
|
user->prefix_ids[j][i++] = *str_prefixes;
|
|
}
|
|
return IPCOM_SUCCESS;
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipdhcps6_user_list_cb
|
|
*===========================================================================
|
|
* Description:
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC int
|
|
ipdhcps6_user_list_cb(const char *name, const char *value, int flags,
|
|
Ipdhcps6_list_cb_data *cb_data)
|
|
{
|
|
const char *username;
|
|
const char *username_end;
|
|
|
|
(void) flags;
|
|
username_end = ipcom_strstr(name, ".ipdhcps6.prefix");
|
|
if (username_end == IP_NULL)
|
|
return 0;
|
|
|
|
if (++cb_data->num_users * sizeof(Ipdhcps6_user) >= (Ip_size_t) cb_data->users_len
|
|
&& cb_data->users)
|
|
{
|
|
Ipdhcps6_user *user = &cb_data->users[cb_data->num_users - 1];
|
|
int i = 0;
|
|
|
|
username = username_end - 1;
|
|
while (*username != '.')
|
|
--username;
|
|
while (++username != username_end)
|
|
user->name[i++] = *username;
|
|
user->name[i] = '\0';
|
|
|
|
(void) ipdhcps6_user_prefix_ids_from_str(user, value);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 10 GLOBAL FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipdhcps6_user_get
|
|
*===========================================================================
|
|
* Description: Adds a DHCPv6 host to the database.
|
|
* Parameters: user - The username must be filled in, this function will
|
|
* fill in all prefixes assigned to this user.
|
|
* Returns: IPCOM_SUCCESS - operation was successful.
|
|
* IPCOM_ERR_NOT_FOUND - the user was not found.
|
|
*
|
|
*/
|
|
IP_GLOBAL Ip_err
|
|
ipdhcps6_user_get(Ipdhcps6_user *user)
|
|
{
|
|
char sysvar_name[IPDHCPS6_USER_SYSVAR_NAME];
|
|
char prefix_ids[IPDHCPS6_PREFIX_PER_USER * IPDHCPS6_KEY_LEN];
|
|
Ip_size_t ids_len = sizeof(prefix_ids);
|
|
|
|
if (user->name[0] == '\0')
|
|
return IPCOM_ERR_INVALID_ARG;
|
|
|
|
ipcom_sprintf(sysvar_name, "ipcom.users.%s.ipdhcps6.prefix_list", user->name);
|
|
if (ipcom_sysvar_get(sysvar_name, prefix_ids, &ids_len) == IP_NULL)
|
|
{
|
|
IPCOM_LOG1(DEBUG, "No prefix configuration found for user %s", user->name);
|
|
return IPCOM_ERR_NOT_FOUND;
|
|
}
|
|
|
|
return ipdhcps6_user_prefix_ids_from_str(user, prefix_ids);
|
|
}
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 11 PUBLIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipdhcps6_user_add
|
|
*===========================================================================
|
|
* Description: Fills in all user information for the user 'user->name'.
|
|
* Parameters: user - The user to find information for.
|
|
* Returns: IPCOM_SUCCESS - operation was successful.
|
|
*
|
|
*/
|
|
IP_PUBLIC Ip_err
|
|
ipdhcps6_user_add(Ipdhcps6_user *user)
|
|
{
|
|
char sysvar_name[IPDHCPS6_USER_SYSVAR_NAME];
|
|
char prefix_ids[IPDHCPS6_PREFIX_PER_USER * IPDHCPS6_KEY_LEN];
|
|
int i;
|
|
|
|
ipcom_sprintf(sysvar_name, "ipcom.users.%s.ipdhcps6.prefix_list", user->name);
|
|
prefix_ids[0] = '\0';
|
|
|
|
for (i = 0; i < IPDHCPS6_PREFIX_PER_USER; i++)
|
|
{
|
|
if (ipcom_strlen(user->prefix_ids[i]) == 0)
|
|
continue;
|
|
if (prefix_ids[0] != '\0')
|
|
ipcom_strcat(prefix_ids, " ");
|
|
ipcom_strcat(prefix_ids, user->prefix_ids[i]);
|
|
}
|
|
|
|
return ipcom_sysvar_set(sysvar_name, prefix_ids, IPCOM_SYSVAR_FLAG_OVERWRITE);
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipdhcps6_user_delete
|
|
*===========================================================================
|
|
* Description: Deletes a DHCPv6 user from the database.
|
|
* Parameters: username - The name of the user to delete DHCPv6 config for.
|
|
* Returns: IPCOM_SUCCESS - operation was successful.
|
|
* IPCOM_ERR_NOT_FOUND - the subnet or option was not found.
|
|
*
|
|
*/
|
|
IP_PUBLIC Ip_err
|
|
ipdhcps6_user_delete(const char *username)
|
|
{
|
|
char sysvar_name[IPDHCPS6_USER_SYSVAR_NAME];
|
|
|
|
ipcom_sprintf(sysvar_name, "ipcom.users.%s.ipdhcps6.prefix_list", username);
|
|
return ipcom_sysvar_unset(sysvar_name);
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipdhcps6_user_list
|
|
*===========================================================================
|
|
* Description: Lists all DHCPv6 users in the database.
|
|
* Parameters: users - Buffer that will contain the client ID:s.
|
|
* Can be IP_NULL if the required length of the 'user' buffer
|
|
* (in bytes) should be returned in 'user_len'.
|
|
* users_len - The length of the 'users' buffer. Will contain
|
|
* the actual number of bytes copied to 'user' after return.
|
|
* Returns: IPCOM_SUCCESS - operation was successful.
|
|
* IPCOM_ERR_NO_MEMORY - the 'users' buffer was not large enough.
|
|
*
|
|
*/
|
|
IP_PUBLIC Ip_err
|
|
ipdhcps6_user_list(Ipdhcps6_user *users, int *users_len)
|
|
{
|
|
Ipdhcps6_list_cb_data cb_data;
|
|
Ip_err err = IPCOM_SUCCESS;
|
|
|
|
cb_data.users = users;
|
|
cb_data.users_len = *users_len;
|
|
cb_data.num_users = 0;
|
|
ipcom_sysvar_for_each("ipcom.users",
|
|
(int (*)(const char *, const char *, int, void *)) ipdhcps6_user_list_cb,
|
|
&cb_data);
|
|
|
|
if (users && (Ip_size_t) *users_len < cb_data.num_users * sizeof(Ipdhcps6_user))
|
|
err = IPCOM_ERR_NO_MEMORY;
|
|
*users_len = cb_data.num_users * sizeof(Ipdhcps6_user);
|
|
return err;
|
|
}
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* END OF FILE
|
|
****************************************************************************
|
|
*/
|
|
|