123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /*
- * clish_config_callback.c
- *
- *
- * Callback hook to execute config operations.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <assert.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include "private.h"
- #include "konf/net.h"
- #include "konf/buf.h"
- #include "konf/query.h"
- #include "clish/variable.h"
- static int send_request(konf_client_t * client, char *command);
- static int receive_answer(konf_client_t * client, konf_buf_t **data);
- static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
- static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
- /*--------------------------------------------------------- */
- bool_t
- clish_config_callback(const clish_shell_t * shell,
- const clish_command_t * cmd, clish_pargv_t * pargv)
- {
- unsigned i;
- char *line;
- char *command = NULL;
- konf_client_t *client;
- konf_buf_t *buf = NULL;
- const char *viewid = NULL;
- if (shell)
- viewid = clish_shell__get_viewid(shell);
- switch (clish_command__get_cfg_op(cmd)) {
- case CLISH_CONFIG_NONE:
- return BOOL_TRUE;
- case CLISH_CONFIG_SET:
- {
- char tmp[100];
- char *pattern;
- lub_string_cat(&command, "-s");
- pattern = clish_command__get_pattern(cmd, viewid, pargv);
- if (!pattern) {
- lub_string_free(command);
- return BOOL_FALSE;
- }
- line = clish_variable__get_line(cmd, pargv);
- lub_string_cat(&command, " -l \"");
- lub_string_cat(&command, line);
- lub_string_cat(&command, "\"");
- lub_string_free(line);
- lub_string_cat(&command, " -r \"");
- lub_string_cat(&command, pattern);
- lub_string_cat(&command, "\"");
- lub_string_free(pattern);
- if (clish_command__get_splitter(cmd) == BOOL_FALSE)
- lub_string_cat(&command, " -i");
- snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
- clish_command__get_priority(cmd));
- tmp[sizeof(tmp) - 1] = '\0';
- lub_string_cat(&command, tmp);
- if (clish_command__get_seq(cmd) == BOOL_TRUE) {
- lub_string_cat(&command, " -q");
- if (clish_command__get_seq_num(cmd,
- viewid, pargv) != 0) {
- snprintf(tmp, sizeof(tmp) - 1, " -n %u",
- clish_command__get_seq_num(cmd,
- viewid, pargv));
- tmp[sizeof(tmp) - 1] = '\0';
- lub_string_cat(&command, tmp);
- }
- }
- for (i = 0; i < clish_command__get_depth(cmd); i++) {
- const char *str =
- clish_shell__get_pwd_line(shell, i);
- if (!str)
- return BOOL_FALSE;
- lub_string_cat(&command, " \"");
- lub_string_cat(&command, str);
- lub_string_cat(&command, "\"");
- }
- break;
- }
- case CLISH_CONFIG_UNSET:
- {
- char tmp[100];
- char *pattern;
- lub_string_cat(&command, "-u");
- pattern = clish_command__get_pattern(cmd, viewid, pargv);
- if (!pattern) {
- lub_string_free(command);
- return BOOL_FALSE;
- }
- lub_string_cat(&command, " -r \"");
- lub_string_cat(&command, pattern);
- lub_string_cat(&command, "\"");
- lub_string_free(pattern);
- if (clish_command__get_seq(cmd) == BOOL_TRUE) {
- /* Send priority too */
- snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
- clish_command__get_priority(cmd));
- tmp[sizeof(tmp) - 1] = '\0';
- lub_string_cat(&command, tmp);
- lub_string_cat(&command, " -q");
- if (clish_command__get_seq_num(cmd,
- viewid, pargv) != 0) {
- snprintf(tmp, sizeof(tmp) - 1, " -n %u",
- clish_command__get_seq_num(cmd,
- viewid, pargv));
- tmp[sizeof(tmp) - 1] = '\0';
- lub_string_cat(&command, tmp);
- }
- }
- for (i = 0; i < clish_command__get_depth(cmd); i++) {
- const char *str =
- clish_shell__get_pwd_line(shell, i);
- if (!str)
- return BOOL_FALSE;
- lub_string_cat(&command, " \"");
- lub_string_cat(&command, str);
- lub_string_cat(&command, "\"");
- }
- break;
- }
- case CLISH_CONFIG_DUMP:
- {
- char *file;
- char tmp[100];
- lub_string_cat(&command, "-d");
- file = clish_command__get_file(cmd, viewid, pargv);
- if (file) {
- lub_string_cat(&command, " -f \"");
- if (file[0] != '\0')
- lub_string_cat(&command, file);
- else
- lub_string_cat(&command, "/tmp/running-config");
- lub_string_cat(&command, "\"");
- }
- if (clish_command__get_seq(cmd) == BOOL_TRUE) {
- lub_string_cat(&command, " -q");
- if (clish_command__get_seq_num(cmd,
- viewid, pargv) != 0) {
- snprintf(tmp, sizeof(tmp) - 1, " -n %u",
- clish_command__get_seq_num(cmd,
- viewid, pargv));
- tmp[sizeof(tmp) - 1] = '\0';
- lub_string_cat(&command, tmp);
- }
- }
- break;
- }
- case CLISH_CONFIG_COPY:
- {
- char *file;
- lub_string_cat(&command, "-d -f ");
- file = clish_command__get_file(cmd, viewid, pargv);
- lub_string_cat(&command, file);
- lub_string_free(file);
- break;
- }
- default:
- return BOOL_FALSE;
- };
- client = clish_shell__get_client(shell);
- #ifdef DEBUG
- fprintf(stderr, "CONFIG request: %s\n", command);
- #endif
- if (send_request(client, command) < 0) {
- fprintf(stderr, "Cannot write to the running-config.\n");
- }
- if (receive_answer(client, &buf) < 0) {
- fprintf(stderr, "Cannot get answer from config daemon.\n");
- }
- lub_string_free(command);
- switch (clish_command__get_cfg_op(cmd)) {
- case CLISH_CONFIG_DUMP:
- {
- char *filename;
- filename = clish_command__get_file(cmd, viewid, pargv);
- if (filename) {
- FILE *fd;
- char str[1024];
- if (!(fd = fopen(filename, "r")))
- break;
- while (fgets(str, sizeof(str), fd))
- fprintf(clish_shell__get_ostream(shell),
- "%s", str);
- fclose(fd);
- }
- if (buf) {
- char *str;
- konf_buf_lseek(buf, 0);
- while ((str = konf_buf_preparse(buf))) {
- if (strlen(str) == 0) {
- lub_string_free(str);
- break;
- }
- fprintf(clish_shell__get_ostream(shell),
- "%s\n", str);
- lub_string_free(str);
- }
- konf_buf_delete(buf);
- }
- break;
- }
- default:
- break;
- };
- return BOOL_TRUE;
- }
- /*--------------------------------------------------------- */
- static int send_request(konf_client_t * client, char *command)
- {
- if ((konf_client_connect(client) < 0))
- return -1;
- if (konf_client_send(client, command) < 0) {
- if (konf_client_reconnect(client) < 0)
- return -1;
- if (konf_client_send(client, command) < 0)
- return -1;
- }
- return 0;
- }
- static int receive_answer(konf_client_t * client, konf_buf_t **data)
- {
- konf_buf_t *buf;
- int nbytes;
- char *str;
- int retval = 0;
- int processed = 0;
- if ((konf_client_connect(client) < 0))
- return -1;
- buf = konf_buf_new(konf_client__get_sock(client));
- while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
- while ((str = konf_buf_parse(buf))) {
- konf_buf_t *tmpdata = NULL;
- retval = process_answer(client, str, buf, &tmpdata);
- lub_string_free(str);
- if (retval < 0)
- return retval;
- if (retval == 0)
- processed = 1;
- if (tmpdata) {
- if (*data)
- konf_buf_delete(*data);
- *data = tmpdata;
- }
- }
- }
- konf_buf_delete(buf);
- return retval;
- }
- static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
- {
- konf_buf_t *tmpdata;
- char *str;
- int retval = 0;
- int processed = 0;
- if ((konf_client_connect(client) < 0))
- return -1;
- tmpdata = konf_buf_new(konf_client__get_sock(client));
- do {
- while ((str = konf_buf_parse(buf))) {
- #ifdef DEBUG
- fprintf(stderr, "RECV DATA: [%s]\n", str);
- #endif
- konf_buf_add(tmpdata, str, strlen(str) + 1);
- if (strlen(str) == 0) {
- processed = 1;
- lub_string_free(str);
- break;
- }
- lub_string_free(str);
- }
- } while ((!processed) && (konf_buf_read(buf)) > 0);
- if (!processed) {
- konf_buf_delete(tmpdata);
- *data = NULL;
- return -1;
- }
- *data = tmpdata;
- return 0;
- }
- static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
- {
- int res;
- konf_query_t *query;
- /* Parse query */
- query = konf_query_new();
- res = konf_query_parse_str(query, str);
- if (res < 0) {
- konf_query_free(query);
- #ifdef DEBUG
- fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
- #endif
- return -1;
- }
- #ifdef DEBUG
- fprintf(stderr, "CONFIG answer: %s\n", str);
- // konf_query_dump(query);
- #endif
- switch (konf_query__get_op(query)) {
- case KONF_QUERY_OP_OK:
- res = 0;
- break;
- case KONF_QUERY_OP_ERROR:
- res = -1;
- break;
- case KONF_QUERY_OP_STREAM:
- if (receive_data(client, buf, data) < 0)
- res = -1;
- else
- res = 1; /* wait for another answer */
- break;
- default:
- res = -1;
- break;
- }
- /* Free resources */
- konf_query_free(query);
- return res;
- }
|