#include <stdlib.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
void printAuthorizationAppMenu()
{
printf("Usage: authorization_example <test#> <arg1> <arg2> ... \n");
printf("Test 1: Create authorization list name: authorization_example 1 <author-type> <list-name> \n");
printf("Test 2: Add methods to authorization list: authorization_example 2 <author-type> <list-name> <method1> <method2> <method3> .... \n");
printf("Test 3: Get authorization list names and configured methods of an access type: authorization_example 3 <authorization-type>\n");
printf("Test 4: Set authorization list to an access line: authorization_example 4 <access-line> <author-type> <list-name> \n");
printf("Test 5: Get authorization list name applied to access line: authorization_example 5 <access-line> <author-type> \n");
printf("Test 6: Delete authorization list applied to the access-line: authorization_example 6 <access-line> <author-type> \n");
printf("Test 7: Delete authorization list created: authorization_example 7 <author-type> <list-name> \n");
printf("Test 8: Authorization OpEN APIs sanity: authorization_example 8 \n");
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, listName, (sizeof(str) - 1));
buffDesc.
size = strlen(str)+1;
{
{
printf("Bad return code trying to create authorization list name for type %d. (result = %d) \n", type, result);
}
else
{
printf("Authorization list name %s created successfully. \n", str);
}
}
else
{
printf("Authorization list name is too long or contains invalid characters. \n");
}
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, listName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str)+1;
{
printf("Bad return code trying to add methods to authorization list for type %d. (result = %d) \n", type, result);
}
else
{
printf("Methods added to authorization list successfully. \n");
}
return;
}
{
uint32_t authorListNameSize;
uint32_t maxAuthorMethods;
uint32_t i = 0;
char *str;
char *methodStr;
{
printf("Bad return code trying to get authorization list name size. (result = %d)", result);
return;
}
{
printf("Bad return code trying to get maximum authorization methods supported. (result = %d) \n", result);
}
if ((str = (char *)malloc(authorListNameSize)) == NULL)
{
printf("Could not allocate memory.\n");
return;
}
if ((methodStr = (char *)malloc(maxAuthorMethods)) == NULL)
{
printf("Could not allocate memory.\n");
free(str);
return;
}
memset(str, 0, authorListNameSize);
listName.pstart = str;
listName.size = authorListNameSize;
memset(methodStr, 0, maxAuthorMethods);
methodList.pstart = methodStr;
methodList.size = maxAuthorMethods;
{
printf("Bad return code trying to get first authorization list name for type: %d (result = %d) \n", type, result);
free(methodStr);
free(str);
return;
}
printf("Author_list_name Methods\n");
do
{
printf("%-16s ", str);
{
for (i = 0; i < methodList.size; i++)
{
switch((methodStr[i])-'0')
{
printf("Tacacs ");
break;
printf("Radius ");
break;
printf("None ");
break;
printf("Local ");
break;
default:
break;
}
}
}
else
{
printf("Authorization Method get failed. (result = %d) \n", result);
}
printf("\n");
listName.size = authorListNameSize;
methodList.size = maxAuthorMethods;
free(str);
free(methodStr);
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, listName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str)+1;
{
printf("Bad return code trying to apply authorization list name to access line %d. (result = %d) \n", accessLine, result);
}
else
{
printf("Authorization list name applied to access line successfully \n");
}
return;
}
{
char *str;
uint32_t authorListNameSize;
{
printf("Bad return code trying to get authorization list name size. (result = %d)", result);
return;
}
if ((str = (char *)malloc(authorListNameSize)) == NULL)
{
printf("Could not allocate memory.\n");
return;
}
memset(str, 0, authorListNameSize);
listName.pstart = str;
listName.size = authorListNameSize;
printf(" Getting the authorization list name applied to access line %d\n", accessLine);
{
printf("Bad return code while getting authorization list name applied to access line. (result = %d) \n", result);
}
else
{
printf("Authorization list name: %s \n", str);
}
free(str);
return;
}
{
printf("Deleting authorization list applied to access line %d. \n", accessLine);
{
printf("Bad return code trying to delete authorization list name applied to access line. (result = %d) \n", result);
}
else
{
printf("Deleted the authorization list name from the access line %d. \n", accessLine);
}
return;
}
{
char str[100];
printf("Deleting the authorization list %s \n", listName);
memset(str, 0, sizeof(str));
strncpy(str, listName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str)+1;
{
printf("Bad return code trying to delete authorization list. (result = %d) \n", result);
}
else
{
printf("Authorization list %s deleted successfully. \n", (char *)buffDesc.pstart);
}
return;
}
{
uint32_t authorListNameSize = 0, maxAuthorMethods =0;
char *str, *str1;
printf("\nTesting authorization OpEN APIs sanity\n");
{
printf("Bad return code trying to get authorization name size. (result = %d) \n", result);
return;
}
{
printf("Bad return code trying to get maximum authorization methods supported. (result = %d) \n", result);
return;
}
if ((str = (char *)malloc(authorListNameSize)) == NULL)
{
printf("Could not allocate memory.\n");
return;
}
memset(str, 0, authorListNameSize);
buffDesc1.pstart = str;
buffDesc1.size = authorListNameSize;
if ((str1 = (char *)malloc(maxAuthorMethods)) == NULL)
{
printf("Could not allocate memory.\n");
free(str);
return;
}
memset(str1, 0, maxAuthorMethods);
buffDesc2.pstart = str1;
buffDesc2.size = maxAuthorMethods;
printf("\nTesting openapiAuthorizationListNameSizeGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("NULL string length. (result = %d)\n", result);
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("openapiAuthorizationListCreate() sanity successful.\n");
printf("\nTesting openapiAuthorizationMethodsAdd(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("NULL buf descriptor to authorization method list. (result = %d)\n", result);
printf("openapiAuthorizationMethodsAdd() sanity successful.\n");
printf("\nTesting openapiAuthorizationListDelete(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Invalid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("openapiAuthorizationListDelete() sanity successful.\n");
printf("\nTesting openapiAuthorizationListFirstGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("openapiAuthorizationListFirstGet() sanity successful.\n");
printf("\nTesting openapiAuthorizationListNextGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("openapiAuthorizationListNextGet() sanity successful.\n");
printf("\nTesting openapiAuthorizationMethodListGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("NULL buf descriptor to authorization method list. (result = %d)\n", result);
printf("openapiAuthorizationMethodListGet() sanity successful.\n");
printf("\nTesting openapiAuthorizationListLineSet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid access line. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("openapiAuthorizationListLineSet() sanity successful.\n");
printf("\nTesting openapiAuthorizationListLineGet(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid access line. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("NULL buf descriptor to authorization list name. (result = %d)\n", result);
printf("openapiAuthorizationListLineGet() sanity successful.\n");
printf("\nTesting openapiAuthorizationListLineDelete(): \n");
printf("NULL Client Handle. (result = %d)\n", result);
printf("Inavlid access line. (result = %d)\n", result);
printf("Inavlid authorization type. (result = %d)\n", result);
printf("openapiAuthorizationListLineDelete() sanity successful.\n");
free(str);
free(str1);
return;
}
int main(int argc, char **argv)
{
uint32_t testNum, arg1, arg2;
char switch_os_revision_string[100];
char str[100];
uint32_t i = 0;
if (argc < 2)
{
printAuthorizationAppMenu();
exit(1);
}
testNum = atoi(argv[1]);
l7proc_crashlog_register();
{
printf("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
exit(2);
}
{
sleep(1);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Starting Authorization API example application");
printf("\n");
switch_os_revision.pstart = switch_os_revision_string;
switch_os_revision.size = sizeof(switch_os_revision_string);
printf("Network OS version = %s\n", switch_os_revision_string);
else
printf("Network OS version retrieve error\n");
printf("\n");
switch (testNum)
{
case 1:
if (argc != 4)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
authorizationListCreate(&clientHandle, arg1, argv[3]);
break;
case 2:
if (argc < 5)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
memset(str, 0, sizeof(str));
for (i = 4; i < argc; i++)
{
strncat(str, argv[i], strlen(argv[i]));
}
authorMethods.pstart = str;
authorMethods.size = strlen(str)+1;
authorizationMethodsAdd(&clientHandle, arg1, argv[3], &authorMethods);
break;
case 3:
if (argc != 3)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
authorizationInfoGet(&clientHandle, arg1);
break;
case 4:
if (argc != 5)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
arg2 = atoi(argv[3]);
authorizationListLineApply(&clientHandle, arg1, arg2, argv[4]);
break;
case 5:
if (argc != 4)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
arg2 = atoi(argv[3]);
authorizationListLineGet(&clientHandle, arg1, arg2);
break;
case 6:
if (argc != 4)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
arg2 = atoi(argv[3]);
authorizationListLineDelete(&clientHandle, arg1, arg2);
break;
case 7:
if (argc != 4)
{
printAuthorizationAppMenu();
exit(1);
}
arg1 = atoi(argv[2]);
authorizationListDelete(&clientHandle, arg1, argv[3]);
break;
case 8:
if (argc != 2)
{
printAuthorizationAppMenu();
exit(1);
}
authorizationOpENAPIsSanity(&clientHandle);
break;
default:
printAuthorizationAppMenu();
break;
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Stopping Authorization API example application");
return 0;
}