1 /*
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)
3  *
4  * (c) by the OLSR project
5  *
6  * See our Git repository to find out who worked on this file
7  * and thus is a copyright holder on it.
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above copyright
18  *   notice, this list of conditions and the following disclaimer in
19  *   the documentation and/or other materials provided with the
20  *   distribution.
21  * * Neither the name of olsr.org, olsrd nor the names of its
22  *   contributors may be used to endorse or promote products derived
23  *   from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Visit http://www.olsr.org for more information.
39  *
40  * If you find this software useful feel free to make a donation
41  * to the project. For more information see the website or contact
42  * the copyright holders.
43  *
44  */
45 
46 /*
47  * Dynamic linked library for the olsr.org olsr daemon
48  */
49 
50 #include "olsr.h"
51 #include "olsrd_httpinfo.h"
52 #include "olsr_cfg.h"
53 #include "admin_interface.h"
54 #include "net_olsr.h"
55 #include "ipcalc.h"
56 
57 #include <stdio.h>
58 #include <string.h>
59 #include <stdlib.h>
60 
61 static const char admin_basic_setting_int[] =
62   "<td><strong>%s</strong></td><td><input type=\"text\" name=\"%s\" maxlength=\"%d\" class=\"input_text\" value=\"%d\"></td>\n";
63 static const char admin_basic_setting_float[] =
64   "<td><strong>%s</strong></td><td><input type=\"text\" name=\"%s\" maxlength=\"%d\" class=\"input_text\" value=\"%0.2f\"></td>\n";
65 static const char admin_basic_setting_string[] =
66   "<td><strong>%s</strong></td><td><input type=\"text\" name=\"%s\" maxlength=\"%d\" class=\"input_text\" value=\"%s\"></td>\n";
67 
68 static const char admin_frame_prolog[] =
69   "<strong>Administrator interface</strong><hr>\n" "<h2>Change basic settings</h2>\n"
70   "<form action=\"set_values\" method=\"post\">\n" "<table width=\"100%%\">\n";
71 
72 static const char admin_frame_mid[] =
73   "</table>\n<br>\n" "<center><input type=\"submit\" value=\"Submit\" class=\"input_button\">\n"
74   "<input type=\"reset\" value=\"Reset\" class=\"input_button\"></center>\n" "</form>\n" "<h2>Add/remove local HNA entries</h2>\n"
75   "<form action=\"set_values\" method=\"post\">\n" "<table width=\"100%%\"><tr><td><strong>Network:</strong></td>\n"
76   "<td><input type=\"text\" name=\"hna_new_net\" maxlength=\"16\" class=\"input_text\" value=\"0.0.0.0\"></td>\n"
77   "<td><strong>Netmask/Prefix:</strong></td>\n"
78   "<td><input type=\"text\" name=\"hna_new_netmask\" maxlength=\"16\" class=\"input_text\" value=\"0.0.0.0\"></td>\n"
79   "<td><input type=\"submit\" value=\"Add entry\" class=\"input_button\"></td></form>\n" "</table><hr>\n"
80   "<form action=\"set_values\" method=\"post\">\n" "<table width=\"100%%\">\n"
81   "<tr><th width=50 halign=\"middle\">Delete</th><th>Network</th><th>Netmask</th></tr>\n";
82 
83 static const char admin_frame_epilog[] =
84   "</table>\n<br>\n" "<center><input type=\"submit\" value=\"Delete selected\" class=\"input_button\"></center>\n" "</form>\n";
85 
86 int
build_admin_body(char * buf,uint32_t bufsize)87 build_admin_body(char *buf, uint32_t bufsize __attribute__ ((unused)))
88 {
89   int size = 0;
90   size += snprintf(&buf[size], bufsize - size, admin_frame_prolog);
91 
92   size += snprintf(&buf[size], bufsize - size, "<tr>\n");
93 
94   size += snprintf(&buf[size], bufsize - size, admin_basic_setting_int, "Debug level:", "debug_level", 2, olsr_cnf->debug_level);
95   size += snprintf(&buf[size], bufsize - size, admin_basic_setting_float, "Pollrate:", "pollrate", 4, (double)olsr_cnf->pollrate);
96   size += snprintf(&buf[size], bufsize - size, admin_basic_setting_string, "TOS:", "tos", 6, "TBD");
97 
98   size += snprintf(&buf[size], bufsize - size, "</tr>\n" "<tr>\n");
99 
100   size +=
101     snprintf(&buf[size], bufsize - size, admin_basic_setting_int, "TC redundancy:", "tc_redundancy", 1, olsr_cnf->tc_redundancy);
102   size += snprintf(&buf[size], bufsize - size, admin_basic_setting_int, "MPR coverage:", "mpr_coverage", 1, olsr_cnf->mpr_coverage);
103   size += snprintf(&buf[size], bufsize - size, admin_basic_setting_int, "Willingness:", "willingness", 1, olsr_cnf->willingness);
104 
105   size += snprintf(&buf[size], bufsize - size, "</tr>\n" "<tr>\n");
106 
107   if (olsr_cnf->use_hysteresis) {
108     size +=
109       snprintf(&buf[size], bufsize - size, admin_basic_setting_float, "Hyst scaling:", "hyst_scaling", 4,
110     		  (double)olsr_cnf->hysteresis_param.scaling);
111 
112     size +=
113       snprintf(&buf[size], bufsize - size, admin_basic_setting_float, "Lower thr:", "hyst_lower", 4,
114     		  (double)olsr_cnf->hysteresis_param.thr_low);
115     size +=
116       snprintf(&buf[size], bufsize - size, admin_basic_setting_float, "Upper thr:", "hyst_upper", 4,
117     		  (double)olsr_cnf->hysteresis_param.thr_high);
118   } else {
119     size += snprintf(&buf[size], bufsize - size, "<td>Hysteresis disabled</td>\n");
120   }
121 
122   size += snprintf(&buf[size], bufsize - size, "</tr>\n" "<tr>\n");
123 
124   if (olsr_cnf->lq_level) {
125     size += snprintf(&buf[size], bufsize - size, admin_basic_setting_int, "LQ level:", "lq_level", 1, olsr_cnf->lq_level);
126     size += snprintf(&buf[size], bufsize - size, admin_basic_setting_float, "LQ aging:", "lq_aging", 2, (double)olsr_cnf->lq_aging);
127   } else {
128     size += snprintf(&buf[size], bufsize - size, "<td>LQ disabled</td>\n");
129   }
130 
131   size += snprintf(&buf[size], bufsize - size, "</tr>\n" "<tr>\n");
132   size += snprintf(&buf[size], bufsize - size, "</tr>\n");
133 
134   size += snprintf(&buf[size], bufsize - size, admin_frame_mid);
135 
136   if (olsr_cnf->hna_entries) {
137     struct ip_prefix_list *hna;
138     for (hna = olsr_cnf->hna_entries; hna; hna = hna->next) {
139       struct ipaddr_str netbuf;
140       olsr_ip_to_string(&netbuf, &hna->net.prefix);
141       size +=
142         snprintf(&buf[size], bufsize - size,
143                  "<tr><td halign=\"middle\"><input type=\"checkbox\" name=\"del_hna%s*%d\" class=\"input_checkbox\"></td><td>%s</td><td>%d</td></tr>\n",
144                  netbuf.buf, hna->net.prefix_len, netbuf.buf, hna->net.prefix_len);
145     }
146   }
147   size += snprintf(&buf[size], bufsize - size, admin_frame_epilog);
148   return size;
149 }
150 
151 #ifdef ADMIN_INTERFACE
152 
153 int
process_param(char * key,char * value)154 process_param(char *key, char *value)
155 {
156   static union olsr_ip_addr curr_hna_net;
157   static bool curr_hna_ok = false;
158 
159   if (!strcmp(key, "debug_level")) {
160     int ival = atoi(value);
161     if ((ival < 0) || (ival > 9))
162       return -1;
163 
164     olsr_cnf->debug_level = ival;
165     return 1;
166   }
167 
168   if (!strcmp(key, "tc_redundancy")) {
169     int ival = atoi(value);
170     if ((ival < 0) || (ival > 3))
171       return -1;
172 
173     olsr_cnf->tc_redundancy = ival;
174     return 1;
175   }
176 
177   if (!strcmp(key, "mpr_coverage")) {
178     int ival = atoi(value);
179     if (ival < 0)
180       return -1;
181 
182     olsr_cnf->mpr_coverage = ival;
183     return 1;
184   }
185 
186   if (!strcmp(key, "willingness")) {
187     int ival = atoi(value);
188     if ((ival < 0) || (ival > 7))
189       return -1;
190 
191     olsr_cnf->willingness = ival;
192     return 1;
193   }
194 
195   if (!strcmp(key, "lq_level")) {
196     int ival = atoi(value);
197     if ((ival < 0) || (ival > 2))
198       return -1;
199 
200     olsr_cnf->lq_level = ival;
201     return 1;
202   }
203 
204   if (!strcmp(key, "lq_wsize")) {
205     int ival = atoi(value);
206     if ((ival < 0) || (ival > 10))
207       return -1;
208 
209     olsr_cnf->lq_wsize = ival;
210     return 1;
211   }
212 
213   if (!strcmp(key, "hyst_scaling")) {
214     float fval = 1.1;
215     sscanf(value, "%f", &fval);
216     if ((fval < 0.0) || (fval > 1.0))
217       return -1;
218 
219     olsr_cnf->hysteresis_param.scaling = fval;
220     return 1;
221   }
222 
223   if (!strcmp(key, "hyst_scaling")) {
224     float fval = 1.1;
225     sscanf(value, "%f", &fval);
226     if ((fval < 0.0) || (fval > 1.0))
227       return -1;
228 
229     olsr_cnf->hysteresis_param.scaling = fval;
230     return 1;
231   }
232 
233   if (!strcmp(key, "hyst_lower")) {
234     float fval = 1.1;
235     sscanf(value, "%f", &fval);
236     if ((fval < 0.0) || (fval > 1.0))
237       return -1;
238 
239     olsr_cnf->hysteresis_param.thr_low = fval;
240     return 1;
241   }
242 
243   if (!strcmp(key, "hyst_upper")) {
244     float fval = 1.1;
245     sscanf(value, "%f", &fval);
246     if ((fval < 0.0) || (fval > 1.0))
247       return -1;
248 
249     olsr_cnf->hysteresis_param.thr_high = fval;
250     return 1;
251   }
252 
253   if (!strcmp(key, "pollrate")) {
254     float fval = 1.1;
255     sscanf(value, "%f", &fval);
256     if ((fval < 0.0) || (fval > 1.0))
257       return -1;
258 
259     olsr_cnf->pollrate = fval;
260     return 1;
261   }
262 
263   if (!strcmp(key, "hna_new_net")) {
264     if (inet_pton(olsr_cnf->ipsize, value, &curr_hna_net.v4) == 0) {
265       fprintf(stderr, "Failed converting new HNA net %s\n", value);
266       return -1;
267     }
268     curr_hna_ok = true;
269     return 1;
270   }
271 
272   if (!strcmp(key, "hna_new_netmask")) {
273     struct in_addr in;
274     uint8_t prefixlen;
275 
276     if (!curr_hna_ok)
277       return -1;
278 
279     curr_hna_ok = false;
280 
281     if (inet_aton(value, &in) == 0) {
282       fprintf(stderr, "Failed converting new HNA netmask %s\n", value);
283       return -1;
284     }
285     prefixlen = netmask_to_prefix((uint8_t *) & in, olsr_cnf->ipsize);
286     if (prefixlen == UCHAR_MAX) {
287       fprintf(stderr, "Failed converting new HNA netmask %s\n", value);
288       return -1;
289     }
290     ip_prefix_list_add(&olsr_cnf->hna_entries, &curr_hna_net, prefixlen);
291     return 1;
292   }
293 
294   if (!strncmp(key, "del_hna", 7) && !strcmp(value, "on")) {
295     struct in_addr net, mask;
296     char ip_net[16], ip_mask[16];
297     int seperator = 0;
298     uint8_t prefixlen;
299 
300     while (key[7 + seperator] != '*') {
301       seperator++;
302     }
303     memcpy(ip_net, &key[7], seperator);
304     ip_net[seperator] = 0;
305     memcpy(ip_mask, &key[7 + seperator + 1], 16);
306     olsr_printf(1, "Deleting HNA %s/%s\n", ip_net, ip_mask);
307 
308     if (inet_aton(ip_net, &net) == 0) {
309       fprintf(stderr, "Failed converting HNA net %s for deletion\n", ip_net);
310       return -1;
311     }
312 
313     if (inet_aton(ip_mask, &mask) == 0) {
314       fprintf(stderr, "Failed converting HNA netmask %s for deletion\n", ip_mask);
315       return -1;
316     }
317     prefixlen = netmask_to_prefix((uint8_t *) & mask, olsr_cnf->ipsize);
318     if (prefixlen == UCHAR_MAX) {
319       fprintf(stderr, "Failed converting new HNA netmask %s\n", value);
320       return -1;
321     }
322     ip_prefix_list_add(&olsr_cnf->hna_entries, &curr_hna_net, prefixlen);
323     return 1;
324   }
325 
326   return 0;
327 }
328 
329 int
process_set_values(char * data,uint32_t data_size,char * buf,uint32_t bufsize)330 process_set_values(char *data, uint32_t data_size, char *buf, uint32_t bufsize __attribute__ ((unused)))
331 {
332   int size = 0;
333   int val_start, key_start;
334   uint32_t i;
335 
336   size += sprintf(buf, "<html>\n<head><title>olsr.org httpinfo plugin</title></head>\n<body>\n");
337 
338   key_start = 0;
339   val_start = 0;
340 
341   for (i = 0; i < data_size; i++) {
342     if (data[i] == '=') {
343       data[i] = '\0';
344       val_start = i + 1;
345     }
346 
347     if (data[i] == '&') {
348       data[i] = '\0';
349       if (!process_param(&data[key_start], &data[val_start])) {
350         size +=
351           snprintf(&buf[size], bufsize - size, "<h2>FAILED PROCESSING!</h2><br>Key: %s Value: %s<br>\n", &data[key_start],
352                    &data[val_start]);
353         return -1;
354       }
355 
356       key_start = i + 1;
357     }
358   }
359 
360   if (!process_param(&data[key_start], &data[val_start])) {
361     size +=
362       snprintf(&buf[size], bufsize - size, "<b>FAILED PROCESSING!</b><br>Key: %s Value: %s<br>\n", &data[key_start],
363                &data[val_start]);
364     return -1;
365   }
366 
367   size +=
368     snprintf(&buf[size], bufsize - size,
369              "<h2>UPDATE SUCESSFULL!</h2><br>Press BACK and RELOAD in your browser to return to the plugin<br>\n</body>\n</html>\n");
370   size += snprintf(&buf[size], bufsize - size, "\n</body>\n</html>\n");
371 
372   return size;
373 }
374 #endif /* ADMIN_INTERFACE */
375 
376 /*
377  * Local Variables:
378  * c-basic-offset: 2
379  * indent-tabs-mode: nil
380  * End:
381  */
382