* The backend socket has now support of credentials of peer clients
* Added: CLICON_NACM_CREDENTIALS and CLICON_NACM_RECOVERY_USER
This commit is contained in:
parent
77b491c568
commit
279614d64f
33 changed files with 951 additions and 145 deletions
184
util/clixon_util_socket.c
Normal file
184
util/clixon_util_socket.c
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
*
|
||||
***** BEGIN LICENSE BLOCK *****
|
||||
|
||||
Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren
|
||||
|
||||
This file is part of CLIXON.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of
|
||||
the GNU General Public License Version 3 or later (the "GPL"),
|
||||
in which case the provisions of the GPL are applicable instead
|
||||
of those above. If you wish to allow use of your version of this file only
|
||||
under the terms of the GPL, and not to allow others to
|
||||
use your version of this file under the terms of Apache License version 2,
|
||||
indicate your decision by deleting the provisions above and replace them with
|
||||
the notice and other provisions required by the GPL. If you do not delete
|
||||
the provisions above, a recipient may use your version of this file under
|
||||
the terms of any one of the Apache License version 2 or the GPL.
|
||||
|
||||
***** END LICENSE BLOCK *****
|
||||
|
||||
* Unit test for testting the backend socket, ie simulating a client by
|
||||
* directly sending XML to the backend.
|
||||
* Precondition:
|
||||
* The backend must have been started using socket path goven as -s
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "clixon_config.h" /* generated by config & autoconf */
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <syslog.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* cligen */
|
||||
#include <cligen/cligen.h>
|
||||
|
||||
/* clixon */
|
||||
#include "clixon/clixon.h"
|
||||
|
||||
static int
|
||||
usage(char *argv0)
|
||||
{
|
||||
fprintf(stderr, "usage:%s [options] with xml on stdin (unless -f)\n"
|
||||
"where options are\n"
|
||||
"\t-h \t\tHelp\n"
|
||||
"\t-D <level> \tDebug\n"
|
||||
"\t-a <family>\tSocket address family (default UNIX)\n"
|
||||
"\t-s <sockpath> \tPath to unix domain socket (or IP addr)\n"
|
||||
"\t-f <file>\tXML input file (overrides stdin)\n"
|
||||
"\t-J \t\tInput as JSON (instead of XML)\n"
|
||||
,
|
||||
argv0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc,
|
||||
char **argv)
|
||||
{
|
||||
int retval = -1;
|
||||
int c;
|
||||
int logdst = CLICON_LOG_STDERR;
|
||||
struct clicon_msg *msg = NULL;
|
||||
char *sockpath;
|
||||
char *retdata = NULL;
|
||||
int jsonin = 0;
|
||||
char *input_filename = NULL;
|
||||
int fd = 0; /* stdin */
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xc;
|
||||
cxobj *xerr = NULL;
|
||||
char *family = "UNIX";
|
||||
int ret;
|
||||
cbuf *cb = cbuf_new();
|
||||
|
||||
/* In the startup, logs to stderr & debug flag set later */
|
||||
clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR);
|
||||
|
||||
optind = 1;
|
||||
opterr = 0;
|
||||
while ((c = getopt(argc, argv, "hD:s:f:Ja:")) != -1)
|
||||
switch (c) {
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
break;
|
||||
case 'D':
|
||||
if (sscanf(optarg, "%d", &debug) != 1)
|
||||
usage(argv[0]);
|
||||
break;
|
||||
case 's':
|
||||
sockpath = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
input_filename = optarg;
|
||||
break;
|
||||
case 'J':
|
||||
jsonin++;
|
||||
break;
|
||||
case 'a':
|
||||
family = optarg;
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
break;
|
||||
}
|
||||
clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, logdst);
|
||||
if (sockpath == NULL){
|
||||
fprintf(stderr, "Mandatory option missing: -s <sockpath>\n");
|
||||
usage(argv[0]);
|
||||
}
|
||||
if (input_filename){
|
||||
if ((fd = open(input_filename, O_RDONLY)) < 0){
|
||||
clicon_err(OE_YANG, errno, "open(%s)", input_filename);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
/* 2. Parse data (xml/json) */
|
||||
if (jsonin){
|
||||
if ((ret = json_parse_file(fd, NULL, &xt, &xerr)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
fprintf(stderr, "Invalid JSON\n");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if (xml_parse_file(fd, NULL, NULL, &xt) < 0){
|
||||
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if ((xc = xml_child_i(xt, 0)) == NULL){
|
||||
fprintf(stderr, "No xml\n");
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml2cbuf(cb, xc, 0, 0, -1) < 0)
|
||||
goto done;
|
||||
if ((msg = clicon_msg_encode("%s", cbuf_get(cb))) < 0)
|
||||
goto done;
|
||||
if (strcmp(family, "UNIX")==0){
|
||||
if (clicon_rpc_connect_unix(msg, sockpath, &retdata, NULL) < 0)
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
if (clicon_rpc_connect_inet(msg, sockpath, 4535, &retdata, NULL) < 0)
|
||||
goto done;
|
||||
fprintf(stdout, "%s\n", retdata);
|
||||
retval = 0;
|
||||
done:
|
||||
if (xerr)
|
||||
xml_free(xerr);
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (msg)
|
||||
free(msg);
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue