1 /*
2  * Copyright (C) 2014, 2015 Red Hat
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of ocserv.
7  *
8  * ocserv is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * ocserv is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <c-ctype.h>
30 #include <occtl/occtl.h>
31 #include <common.h>
32 #include <json.h>
33 #include <vpn.h>
34 #include <c-strcase.h>
35 
36 #define MAX_STR_SIZE 512
37 
38 #define escape_val json_escape_val
39 
print_list_entries(FILE * out,cmd_params_st * params,const char * name,char ** val,unsigned vsize,unsigned have_more)40 int print_list_entries(FILE* out, cmd_params_st *params, const char* name, char **val, unsigned vsize, unsigned have_more)
41 {
42 	const char * tmp;
43 	unsigned int i = 0;
44 
45 	if (HAVE_JSON(params)) {
46 		fprintf(out, "    \"%s\":\t[", name);
47 		for (i=0;i<vsize;i++) {
48 			tmp = val[i];
49 			if (tmp != NULL) {
50 				if (i==0)
51 					fprintf(out, "\"%s\"", tmp);
52 				else
53 					fprintf(out, ", \"%s\"", tmp);
54 			}
55 		}
56 		fprintf(out, "]%s\n", have_more?",":"");
57 	} else {
58 		for (i=0;i<vsize;i++) {
59 			tmp = val[i];
60 			if (tmp != NULL) {
61 				if (i==0)
62 					fprintf(out, "\t%s: %s\n", name, tmp);
63 				else
64 					fprintf(out, "\t\t%s\n", tmp);
65 			}
66 		}
67 	}
68 
69 	return i;
70 }
71 
print_fwport_entries(FILE * out,cmd_params_st * params,const char * name,FwPortSt ** val,unsigned vsize,unsigned have_more)72 int print_fwport_entries(FILE* out, cmd_params_st *params, const char* name, FwPortSt **val, unsigned vsize, unsigned have_more)
73 {
74 	unsigned int i = 0;
75 	char tmp[64];
76 
77 	if (HAVE_JSON(params)) {
78 		fprintf(out, "    \"%s\":\t[", name);
79 		for (i=0;i<vsize;i++) {
80 			if (val[i]->port)
81 				snprintf(tmp, sizeof(tmp), "%s%s(%d)", val[i]->negate?"!":"", proto_to_str(val[i]->proto), val[i]->port);
82 			else
83 				snprintf(tmp, sizeof(tmp), "%s%s()", val[i]->negate?"!":"", proto_to_str(val[i]->proto));
84 
85 			if (i==0)
86 				fprintf(out, "\"%s\"", tmp);
87 			else
88 				fprintf(out, ", \"%s\"", tmp);
89 		}
90 		fprintf(out, "]%s\n", have_more?",":"");
91 	} else {
92 		for (i=0;i<vsize;i++) {
93 			if (val[i]->port)
94 				snprintf(tmp, sizeof(tmp), "%s%s(%d)", val[i]->negate?"!":"", proto_to_str(val[i]->proto), val[i]->port);
95 			else
96 				snprintf(tmp, sizeof(tmp), "%s%s()", val[i]->negate?"!":"", proto_to_str(val[i]->proto));
97 			if (i==0)
98 				fprintf(out, "\t%s: %s", name, tmp);
99 			else
100 				fprintf(out, ", %s", tmp);
101 		}
102 		fprintf(out, "\n");
103 	}
104 
105 	return i;
106 }
107 
print_start_block(FILE * out,cmd_params_st * params)108 void print_start_block(FILE *out, cmd_params_st *params)
109 {
110 	if (HAVE_JSON(params))
111 		fprintf(out, "  {\n");
112 }
113 
print_end_block(FILE * out,cmd_params_st * params,unsigned have_more)114 void print_end_block(FILE *out, cmd_params_st *params, unsigned have_more)
115 {
116 	if (HAVE_JSON(params))
117 		fprintf(out, "  }%s\n", have_more?",":"");
118 }
119 
print_array_block(FILE * out,cmd_params_st * params)120 void print_array_block(FILE *out, cmd_params_st *params)
121 {
122 	if (HAVE_JSON(params))
123 		fprintf(out, "[\n");
124 }
125 
print_end_array_block(FILE * out,cmd_params_st * params)126 void print_end_array_block(FILE *out, cmd_params_st *params)
127 {
128 	if (HAVE_JSON(params))
129 		fprintf(out, "]\n");
130 }
131 
print_separator(FILE * out,cmd_params_st * params)132 void print_separator(FILE *out, cmd_params_st *params)
133 {
134 	if (NO_JSON(params))
135 		fprintf(out, "\n");
136 }
137 
print_single_value(FILE * out,cmd_params_st * params,const char * name,const char * value,unsigned have_more)138 void print_single_value(FILE *out, cmd_params_st *params, const char *name, const char *value, unsigned have_more)
139 {
140 	char tmp[MAX_STR_SIZE];
141 	if (value[0] == 0)
142 		return;
143 
144 	if (HAVE_JSON(params))
145 		fprintf(out, "    \"%s\":  \"%s\"%s\n", name, escape_val(tmp, sizeof(tmp), value), have_more?",":"");
146 	else
147 		fprintf(out, "\t%s: %s\n", name, value);
148 }
149 
print_single_value_int(FILE * out,cmd_params_st * params,const char * name,long i,unsigned have_more)150 void print_single_value_int(FILE *out, cmd_params_st *params, const char *name, long i, unsigned have_more)
151 {
152 	if (HAVE_JSON(params))
153 		fprintf(out, "    \"%s\":  \%lu%s\n", name, i, have_more?",":"");
154 	else
155 		fprintf(out, "\t%s: %lu\n", name, i);
156 }
157 
print_single_value_ex(FILE * out,cmd_params_st * params,const char * name,const char * value,const char * ex,unsigned have_more)158 void print_single_value_ex(FILE *out, cmd_params_st *params, const char *name, const char *value, const char *ex, unsigned have_more)
159 {
160 	char tmp[MAX_STR_SIZE];
161 	if (value[0] == 0)
162 		return;
163 
164 	if (HAVE_JSON(params)) {
165 		fprintf(out, "    \"%s\":  \"%s\",\n", name, escape_val(tmp, sizeof(tmp), value));
166 		fprintf(out, "    \"_%s\":  \"%s\"%s\n", name, escape_val(tmp, sizeof(tmp), ex), have_more?",":"");
167 	} else
168 		fprintf(out, "\t%s: %s (%s)\n", name, value, ex);
169 }
170 
print_pair_value(FILE * out,cmd_params_st * params,const char * name1,const char * value1,const char * name2,const char * value2,unsigned have_more)171 void print_pair_value(FILE *out, cmd_params_st *params, const char *name1, const char *value1, const char *name2, const char *value2, unsigned have_more)
172 {
173 	char tmp[MAX_STR_SIZE];
174 	if (HAVE_JSON(params)) {
175 		if (value1 && value1[0] != 0)
176 			fprintf(out, "    \"%s\":  \"%s\"%s\n", name1, escape_val(tmp, sizeof(tmp), value1), have_more?",":"");
177 		if (value2 && value2[0] != 0)
178 			fprintf(out, "    \"%s\":  \"%s\"%s\n", name2, escape_val(tmp, sizeof(tmp), value2), have_more?",":"");
179 	} else {
180 		if (value1 && value1[0] != 0)
181 			fprintf(out, "\t%s: %s", name1, value1);
182 
183 		if (value2 && value2[0] != 0) {
184 			const char *sep;
185 			if (name1)
186 				sep = "   ";
187 			else
188 				sep = "\t";
189 			fprintf(out, "%s%s: %s", sep, name2, value2);
190 		}
191 		if ((value1 && value1[0] != 0) || (value2 && value2[0] != 0))
192 			fprintf(out, "\n");
193 	}
194 }
195