-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.c
1127 lines (905 loc) · 25.9 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002, 2003, 2004 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <sys/param.h>
#include "config.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/stat.h>
#include <sys/tree.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <dnet.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <fnmatch.h>
#include <pcap.h>
#undef timeout_pending
#undef timeout_initialized
#include <event.h>
#include "honeyd.h"
#include "personality.h"
#include "template.h"
#include "subsystem.h"
#include "condition.h"
#include "interface.h"
#include "parser.h"
#include "ethernet.h"
#include "arp.h"
#include "pool.h"
#include "dhcpclient.h"
#include "util.h"
#include "log.h"
/* Tailq that holds all subsystems */
struct subsystemqueue subsystems;
/* Tree that contains all templates */
struct templtree templates;
/* Counter for addresses in 169.254/16 that we assign for DHCP */
static uint16_t privip_counter = 1;
int
templ_compare(struct template *a, struct template *b)
{
return (strcmp(a->name, b->name));
}
SPLAY_GENERATE(templtree, template, node, templ_compare);
int
port_compare(struct port *a, struct port *b)
{
int diff;
diff = a->proto - b->proto;
if (diff) {
return (diff);
} else {
/* safe because number in uint16_t */
return ((int)a->number - (int)b->number);
}
}
SPLAY_PROTOTYPE(porttree, port, node, port_compare);
SPLAY_GENERATE(porttree, port, node, port_compare);
void
config_init(void)
{
TAILQ_INIT(&subsystems);
SPLAY_INIT(&templates);
no_spoof.new_src.addr_type = ADDR_TYPE_NONE; /* default is no source spoofing... */
no_spoof.new_dst.addr_type = ADDR_TYPE_NONE; /* ... and no destination spoofing */
}
void
config_read(char *config)
{
extern int honeyd_ignore_parse_errors;
FILE *fp;
if ((fp = fopen(config, "r")) == NULL)
err(1, "fopen(%s)", config);
if (parse_configuration(fp, config) == -1 &&
!honeyd_ignore_parse_errors)
errx(1, "parsing configuration file failed");
fclose(fp);
}
struct template *
template_find(const char *name)
{
struct template tmp;
tmp.name = (char *)name;
return (SPLAY_FIND(templtree, &templates, &tmp));
}
void
template_list_glob(struct evbuffer *buffer, const char *pattern)
{
struct template *tmpl, *last = NULL;
struct evbuffer *tmp = NULL;
int count = 0;
tmpl = template_find(pattern);
if (tmpl != NULL) {
template_print(buffer, tmpl);
return;
}
if ((tmp = evbuffer_new()) == NULL)
err(1, "%s: malloc");
SPLAY_FOREACH(tmpl, templtree, &templates) {
/* Ignore it if it does not match */
if (fnmatch(pattern, tmpl->name, 0))
continue;
count++;
evbuffer_add_printf(tmp, "%4d. %s (%s)\n",
count,
tmpl->name,
tmpl->person != NULL ? tmpl->person->name : "undefined");
last = tmpl;
}
if (count == 1) {
template_print(buffer, last);
} else {
evbuffer_add_buffer(buffer, tmp);
}
evbuffer_free(tmp);
}
/*
* Checks if condition for each template in the list is matched.
* Return the first template that we match.
*/
struct template *
template_dynamic(const struct template *tmpl, const struct ip_hdr *ip,
u_short iplen)
{
struct template *save = NULL;
struct condition *cond;
TAILQ_FOREACH(cond, &tmpl->dynamic, next) {
if (save == NULL)
save = cond->tmpl;
/* See if we match this template and return it on success */
if (cond->match == NULL ||
cond->match(cond->tmpl, ip, iplen, cond->match_arg))
return (cond->tmpl);
}
/* We need to return something, so return the first non-NULL */
return (save);
}
struct template *
template_find_best(const char *addr, const struct ip_hdr *ip, u_short iplen)
{
struct template *tmpl;
tmpl = template_find(addr);
if (tmpl == NULL)
tmpl = template_find("default");
if (tmpl != NULL && tmpl->flags & TEMPLATE_DYNAMIC)
tmpl = template_dynamic(tmpl, ip, iplen);
return (tmpl);
}
struct template *
template_create(const char *name)
{
extern rand_t *honeyd_rand;
struct template *tmpl;
if (template_find(name))
return (NULL);
if ((tmpl = calloc(1, sizeof(struct template))) == NULL)
err(1, "%s: calloc", __func__);
tmpl->name = strdup(name);
/* UDP ports are closed by default */
tmpl->udp.status = PORT_RESET;
SPLAY_INIT(&tmpl->ports);
SPLAY_INSERT(templtree, &templates, tmpl);
/* Configured subsystems */
TAILQ_INIT(&tmpl->subsystems);
TAILQ_INIT(&tmpl->dynamic);
/* No spoofing, by default */
tmpl->spoof = no_spoof;
/* Crank ref counter */
tmpl->refcnt++;
/* Create a drift */
tmpl->drift = 1.0 + (rand_uint16(honeyd_rand) % 140 - 70)/100000.0;
return (tmpl);
}
int
template_iterate(int (*f)(struct template *, void *), void *arg)
{
struct template *tmpl;
SPLAY_FOREACH(tmpl, templtree, &templates) {
if ((*f)(tmpl, arg) == -1)
return (-1);
}
return (0);
}
/*
* Removes all configured templates from the system, so that the
* configuration file can be re-read.
*/
void
template_free_all(int how)
{
struct template *tmpl;
while ((tmpl = SPLAY_ROOT(&templates)) != NULL) {
SPLAY_REMOVE(templtree, &templates, tmpl);
if (how == TEMPLATE_FREE_REGULAR)
template_free(tmpl);
else if (!(tmpl->flags & TEMPLATE_DYNAMIC_CHILD))
template_deallocate(tmpl);
}
}
/* Remove a template from the system */
void
template_remove(struct template *tmpl)
{
/* Remove ourselves from the searchable index */
if (template_find(tmpl->name) == tmpl)
SPLAY_REMOVE(templtree, &templates, tmpl);
}
/* Insert a template into the system */
int
template_insert(struct template *tmpl)
{
/* Insert ourselves into the searchable index */
if (template_find(tmpl->name) != NULL)
return (-1);
SPLAY_INSERT(templtree, &templates, tmpl);
return (0);
}
void
template_deallocate(struct template *tmpl)
{
struct condition *cond;
struct port *port;
/* Remove ourselves from the searchable index */
if (template_find(tmpl->name) == tmpl)
SPLAY_REMOVE(templtree, &templates, tmpl);
/* Free conditions for dynamic templates */
for (cond = TAILQ_FIRST(&tmpl->dynamic); cond != NULL;
cond = TAILQ_FIRST(&tmpl->dynamic)) {
TAILQ_REMOVE(&tmpl->dynamic, cond, next);
template_free(cond->tmpl);
if (cond->match_arg)
free(cond->match_arg);
free(cond);
}
/* Remove ports from template */
while ((port = SPLAY_ROOT(&tmpl->ports)) != NULL)
port_free(tmpl, port);
if (tmpl->person != NULL)
personality_declone(tmpl->person);
if (tmpl->ethernet_addr != NULL) {
struct arp_req *req = arp_find(tmpl->ethernet_addr);
/*
* Templates that are not bound to IP addresses do not
* have an associated arp request object.
*/
if (req != NULL)
arp_free(req);
free(tmpl->ethernet_addr);
}
if (tmpl->dhcp_req != NULL) {
dhcp_release(tmpl);
dhcp_abort(tmpl);
free(tmpl->dhcp_req);
}
free(tmpl->name);
free(tmpl);
}
struct port *
port_find(struct template *tmpl, int proto, int number)
{
struct port tmpport;
tmpport.proto = proto;
tmpport.number = number;
return (SPLAY_FIND(porttree, &tmpl->ports, &tmpport));
}
void
port_action_clone(struct action *dst, const struct action *src)
{
*dst = *src;
if (src->action) {
dst->action = strdup(src->action);
if (dst->action == NULL)
err(1, "%s: strdup", __func__);
}
if (src->aitop != NULL) {
struct addrinfo *ai = src->aitop;
char addr[NI_MAXHOST];
char port[NI_MAXSERV];
short nport;
if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
addr, sizeof(addr), port, sizeof(port),
NI_NUMERICHOST|NI_NUMERICSERV) != 0)
err(1, "%s: getnameinfo", __func__);
nport = atoi(port);
dst->aitop = cmd_proxy_getinfo(addr, ai->ai_socktype, nport);
if (dst->aitop == NULL)
errx(1, "%s: cmd_proxy_getinfo failed", __func__);
}
}
void
port_encapsulation_free(struct port_encapsulate *tmp)
{
struct port *port = tmp->port;
TAILQ_REMOVE(&port->pending, tmp, next);
event_del(&tmp->ev);
/* Remove the reference to the pending connection */
if (tmp->hdr != NULL)
tmp->hdr->pending = NULL;
free(tmp);
}
void
port_free(struct template *tmpl, struct port *port)
{
struct port_encapsulate *tmp;
/* Remove pending connections */
while ((tmp = TAILQ_FIRST(&port->pending)) != NULL) {
/* This might not be the correct way to clean this up */
if (tmp->hdr != NULL && tmp->hdr->type == SOCK_STREAM)
tcp_connectfail(tmp->con);
port_encapsulation_free(tmp);
}
SPLAY_REMOVE(porttree, &tmpl->ports, port);
if (port->sub_conport != NULL) {
/* Back pointer to connection object.
* It allows us to remove the reference to this object
* in the connection.
* However, at this point we really need to tear down
* that connection, too.
*/
*port->sub_conport = NULL;
}
if (port->sub != NULL)
TAILQ_REMOVE(&port->sub->ports, port, next);
if (port->sub_fd != -1)
fdshare_close(port->sub_fd);
if (port->action.action != NULL)
free (port->action.action);
if (port->action.aitop != NULL)
freeaddrinfo(port->action.aitop);
free(port);
}
struct port *
port_insert(struct template *tmpl, int proto, int number,
struct action *action)
{
struct port *port, tmpport;
tmpport.proto = proto;
tmpport.number = number;
if (SPLAY_FIND(porttree, &tmpl->ports, &tmpport) != NULL)
return (NULL);
if ((port = calloc(1, sizeof(struct port))) == NULL)
err(1, "%s: calloc", __func__);
TAILQ_INIT(&port->pending);
port->sub = NULL;
port->sub_fd = -1;
port->proto = proto;
port->number = number;
port_action_clone(&port->action, action);
SPLAY_INSERT(porttree, &tmpl->ports, port);
return (port);
}
/* Create a random port in a certain range */
struct port *
port_random(struct template *tmpl, int proto, struct action *action,
int min, int max)
{
extern rand_t *honeyd_rand;
struct port *port = NULL;
int count = 100;
int number;
while (count-- && port == NULL) {
number = rand_uint16(honeyd_rand) % (max - min) + min;
port = port_insert(tmpl, proto, number, action);
}
return (port);
}
int
template_add(struct template *tmpl, int proto, int number,
struct action *action)
{
return (port_insert(tmpl, proto, number, action) == NULL ? -1 : 0);
}
void
template_insert_subsystem(struct template *tmpl, struct subsystem *sub)
{
struct subsystem_container *container;
if ((container = malloc(sizeof(struct subsystem_container))) == NULL)
err(1, "%s: malloc", __func__);
container->sub = sub;
TAILQ_INSERT_TAIL(&tmpl->subsystems, container, next);
}
/* This function is slow, but should only called on SIGHUP */
void
template_remove_subsystem(struct template *tmpl, struct subsystem *sub)
{
struct subsystem_container *container;
TAILQ_FOREACH(container, &tmpl->subsystems, next) {
if (container->sub == sub)
break;
}
if (container == NULL)
errx(1, "%s: could not remove subsystem %p from %s",
sub, tmpl->name);
TAILQ_REMOVE(&tmpl->subsystems, container, next);
free(container);
}
void
template_post_arp(struct template *tmpl, struct addr *ipaddr)
{
struct arp_req *req;
/* Register this mac address as our own */
req = arp_new(tmpl->inter, NULL, NULL, ipaddr, tmpl->ethernet_addr);
if (req == NULL)
errx(1, "%s: cannot create arp entry");
req->flags |= ARP_INTERNAL;
req->owner = tmpl;
}
void
template_remove_arp(struct template *tmpl)
{
struct arp_req *arp;
if (tmpl->ethernet_addr == NULL)
return;
/* Check if we have an ARP entry */
arp = arp_find(tmpl->ethernet_addr);
assert(arp != NULL);
arp_free(arp);
}
/*
* When the interface is specified, we do not need to do an address lookup
* for a corresponding interface. We use the interface for DHCP.
*/
struct template *
template_clone(const char *newname, const struct template *tmpl,
struct interface *inter, int start)
{
struct subsystem_container *container;
struct condition *condition;
struct template *newtmpl;
struct port *port;
struct addr addr;
int isipaddr = 0;
if ((newtmpl = template_create(newname)) == NULL)
return (NULL);
SPLAY_FOREACH(port, porttree, (struct porttree *)&tmpl->ports) {
if (port_insert(newtmpl, port->proto, port->number,
&port->action) == NULL)
return (NULL);
}
if (tmpl->person)
newtmpl->person = personality_clone(tmpl->person);
/* Keeps track of the type of template */
isipaddr = addr_aton(newtmpl->name, &addr) != -1;
if (tmpl->ethernet_addr) {
newtmpl->ethernet_addr = ethernetcode_clone(tmpl->ethernet_addr);
/*
* This template wants to have its own ethernet address,
* so we need to register it with our arp code.
*
* DHCP templates get a temporary IP address assigned.
*/
if (isipaddr) {
if (inter == NULL) {
inter = interface_find_responsible(&addr);
if (inter == NULL)
errx(1, "%s: cannot find interface");
}
newtmpl->inter = inter;
/* Register this mac address as our own */
template_post_arp(newtmpl, &addr);
}
}
port_action_clone(&newtmpl->tcp, &tmpl->tcp);
port_action_clone(&newtmpl->udp, &tmpl->udp);
port_action_clone(&newtmpl->icmp, &tmpl->icmp);
newtmpl->timestamp = tmpl->timestamp;
newtmpl->uid = tmpl->uid;
newtmpl->gid = tmpl->gid;
newtmpl->max_nofiles = tmpl->max_nofiles;
newtmpl->drop_inrate = tmpl->drop_inrate;
newtmpl->drop_synrate = tmpl->drop_synrate;
newtmpl->flags = tmpl->flags;
newtmpl->spoof = tmpl->spoof;
/* We need to remove this when cloning */
newtmpl->flags &= ~TEMPLATE_DYNAMIC_CHILD;
/* Clone dynamics */
TAILQ_FOREACH(condition, &tmpl->dynamic, next) {
if (template_insert_dynamic(newtmpl,
condition->tmpl, condition) == -1)
warn("%s: couldn't insert dynamic cond %s into %s",
condition->tmpl->name, tmpl->name);
}
/* Clone subsystems */
TAILQ_FOREACH(container, &tmpl->subsystems, next) {
struct subsystem *sub = container->sub;
/*
* Create a new subsystem structure only if the
* subsystem has not been specified as shared in the
* configuration.
*/
if (!(sub->flags & SUBSYSTEM_SHARED)) {
template_subsystem(newtmpl, sub->cmdstring,sub->flags);
continue;
}
/* Otherwise, just create references */
subsystem_insert_template(sub, newtmpl);
template_insert_subsystem(newtmpl, sub);
}
/* Start subsystems if we are the master template for a subsystem */
if (!start)
return (newtmpl);
/* Start background processes */
TAILQ_FOREACH(container, &newtmpl->subsystems, next) {
template_subsystem_start(newtmpl, container->sub);
}
return (newtmpl);
}
int
template_subsystem(struct template *tmpl, char *subsystem, int flags)
{
struct subsystem *sub;
if ((sub = calloc(1, sizeof(struct subsystem))) == NULL)
err(1, "%s: calloc", __func__);
if ((sub->cmdstring = strdup(subsystem)) == NULL)
err(1, "%s: strdup", __func__);
/* Initializes subsystem data structures */
TAILQ_INIT(&sub->ports);
SPLAY_INIT(&sub->root);
TAILQ_INIT(&sub->templates);
subsystem_insert_template(sub, tmpl);
sub->cmd.pid = -1;
sub->flags |= flags;
template_insert_subsystem(tmpl, sub);
/* Remember this subsystem for lookup through the UI */
TAILQ_INSERT_TAIL(&subsystems, sub, next);
return (0);
}
void
template_subsystem_free_ports(struct subsystem *sub)
{
struct port *port;
for (port = TAILQ_FIRST(&sub->ports); port;
port = TAILQ_FIRST(&sub->ports)) {
/* Free all ports for this subsystem */
port_free(port->subtmpl, port);
}
}
struct subsystem *
template_subsystem_find(const char *name)
{
struct subsystem *sub;
TAILQ_FOREACH(sub, &subsystems, next) {
if (!strcmp(name, sub->cmdstring))
return (sub);
}
return (NULL);
}
void
template_subsystem_list_glob(struct evbuffer *buffer, const char *pattern)
{
struct subsystem *sub, *last = NULL;
struct evbuffer *tmp = NULL;
int count = 0;
sub = template_subsystem_find(pattern);
if (sub != NULL) {
subsystem_print(buffer, sub);
return;
}
if ((tmp = evbuffer_new()) == NULL)
err(1, "%s: malloc");
TAILQ_FOREACH(sub, &subsystems, next) {
/* Ignore it if it does not match */
if (fnmatch(pattern, sub->cmdstring, 0))
continue;
count++;
evbuffer_add_printf(tmp, "%4d. %s (%d)\n",
count, sub->cmdstring, sub->cmd.pid);
last = sub;
}
if (count == 1) {
subsystem_print(buffer, last);
} else {
evbuffer_add_buffer(buffer, tmp);
}
evbuffer_free(tmp);
}
void
template_subsystem_free(struct subsystem *sub)
{
struct template_container *cont;
struct template *tmpl;
TAILQ_REMOVE(&subsystems, sub, next);
template_subsystem_free_ports(sub);
/*
* As we are removing all templates for this subsystem, we
* actually do not need to remove them from the Splay.
*/
for (cont = TAILQ_FIRST(&sub->templates); cont;
cont = TAILQ_FIRST(&sub->templates)) {
TAILQ_REMOVE(&sub->templates, cont, next);
tmpl = cont->tmpl;
free(cont);
template_remove_subsystem(tmpl, sub);
template_free(tmpl);
}
cmd_free(&sub->cmd);
free(sub->cmdstring);
free(sub);
}
/*
* Inserts a new conditional template into a dynamic template.
* The condition gets cloned in the process.
*/
int
template_insert_dynamic(struct template *tmpl, struct template *child,
struct condition *condition)
{
char newname[1024];
struct condition *cond;
if ((cond = calloc(1, sizeof(struct condition))) == NULL)
err(1, "%s: calloc", __func__);
if (condition != NULL)
*cond = *condition;
/* Con up a new template name */
snprintf(newname, sizeof(newname), "%s_%d_%s",
tmpl->name, tmpl->dynamic_rulenr++, child->name);
if ((cond->tmpl = template_clone(newname, child, NULL, 0)) == NULL) {
fprintf(stderr, "Failed to clone %s from %s\n",
newname, child->name);
free(cond);
return (-1);
}
cond->tmpl->flags |= TEMPLATE_DYNAMIC_CHILD;
TAILQ_INSERT_TAIL(&tmpl->dynamic, cond, next);
/* Do we need to copy the match arg, too? */
if (condition == NULL || condition->match_arg == NULL)
return (0);
if ((cond->match_arg = malloc(cond->match_arglen)) == NULL)
err(1, "%s: malloc", __func__);
memcpy(cond->match_arg, condition->match_arg, cond->match_arglen);
return (0);
}
int
template_get_dhcp_address(struct addr *addr)
{
static char address[16];
snprintf(address, sizeof(address),
"169.254.%d.%d", privip_counter / 256 + 1, privip_counter % 256);
if (++privip_counter > 255 * 255)
errx(1, "%s: out of temporary IP addresses", __func__);
return (addr_aton(address, addr));
}
void
template_subsystem_start(struct template *tmpl, struct subsystem *sub)
{
extern int honeyd_verify_config;
char *p;
char *argv[4];
char line[512]; /* for command replacement */
char *name = tmpl->name;
struct addr addr;
int isipaddr = addr_aton(name, &addr) != -1;
/* Has the subsystem started already? */
if (sub->cmd.pid != -1 || honeyd_verify_config)
return;
gettimeofday(&sub->tv_restart, NULL);
argv[0] = "/bin/sh";
argv[1] = "-c";
argv[3] = NULL;
strlcpy(line, sub->cmdstring, sizeof(line));
if (isipaddr) {
/*
* Provide a subsystem with it's own IP address.
* For a shared subsystem, this is going to fail.
*/
while (strrpl(line, sizeof(line), "$ipsrc", name) != NULL)
;
}
p = honeyd_logdate();
while (strrpl(line, sizeof(line), "$date", p) != NULL)
;
argv[2] = line;
if (cmd_subsystem(tmpl, sub, "/bin/sh", argv) == -1)
errx(1, "%s: can not start subsystem \"%s\" for %s",
sub->cmdstring, name);
}
void
template_print(struct evbuffer *buffer, struct template *tmpl)
{
struct port *port;
evbuffer_add_printf(buffer, "template %s:\n", tmpl->name);
evbuffer_add_printf(buffer, " personality: %s\n",
tmpl->person != NULL ? tmpl->person->name : "undefined");
if (tmpl->ethernet_addr != NULL)
evbuffer_add_printf(buffer, " ethernet address: %s\n",
addr_ntoa(tmpl->ethernet_addr));
evbuffer_add_printf(buffer, " IP id: %u\n", tmpl->id);
evbuffer_add_printf(buffer, " TCP seq: %lx\n", tmpl->seq);
evbuffer_add_printf(buffer, " TCP drop: in: %d syn: %d\n",
tmpl->drop_inrate, tmpl->drop_synrate);
evbuffer_add_printf(buffer, " refcnt: %d\n", tmpl->refcnt);
evbuffer_add_printf(buffer, " ports:\n");
SPLAY_FOREACH(port, porttree, &tmpl->ports) {
char *type;
switch (port->action.status) {
case PORT_OPEN:
type = "open";
break;
case PORT_PROXY:
type = "proxy";
break;
case PORT_BLOCK:
type = "block";
break;
case PORT_RESET:
type = "reset";
break;
case PORT_SUBSYSTEM:
type = "subsystem";
break;
case PORT_PYTHON:
type = "python";
break;
default:
type = "reserved";
break;
}
evbuffer_add_printf(buffer, " %s %5d %s",
port->proto == IP_PROTO_TCP ? "tcp" : "udp",
port->number, type);
evbuffer_add_printf(buffer, "\n");
if (port->action.status == PORT_SUBSYSTEM) {
evbuffer_add_printf(buffer, "\t%s\n",
port->sub->cmdstring);
} else if (port->action.status == PORT_OPEN &&
port->action.action != NULL) {
evbuffer_add_printf(buffer, "\t%s\n",
port->action.action);
}
}
}
/***************************************************************************
* Everything is unittest related below this
***************************************************************************/
void
template_delay_cb(int fd, short which, void *arg)
{
extern struct pool *pool_pkt;
extern struct pool *pool_delay;
struct delay *delay = arg;
struct ip_hdr *ip = delay->ip;
struct template *tmpl = delay->tmpl;
u_int iplen = delay->iplen;
if (ip->ip_ttl &&
!(delay->flags & (DELAY_UNREACH|DELAY_EXTERNAL|DELAY_TUNNEL|DELAY_ETHERNET))) {
struct addr addr;
template_free(tmpl);
addr_pack(&addr, ADDR_TYPE_IP, IP_ADDR_BITS,
&ip->ip_dst, IP_ADDR_LEN);
/* Internal delivery */
tmpl = template_find_best(addr_ntoa(&addr), ip, iplen);
tmpl = template_ref(tmpl);
/* No Check for fragmentation */
honeyd_dispatch(tmpl, ip, iplen);
}
if (delay->flags & DELAY_FREEPKT)
pool_free(pool_pkt, ip);
template_free(tmpl);
if (delay->flags & DELAY_NEEDFREE)
pool_free(pool_delay, delay);
}
void