source: rtems-libbsd/testsuite/pf02/test_main.c @ 32a3dd1

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 32a3dd1 was 32a3dd1, checked in by Christian Mauderer <Christian.Mauderer@…>, on 08/04/16 at 06:02:24

pf: Add RTEMS_BSD_CONFIG_FIREWALL_xxx options.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <assert.h>
33#include <fcntl.h>
34#include <stdlib.h>
35
36#include <rtems/telnetd.h>
37#include <rtems/ftpd.h>
38#include <rtems/shell.h>
39
40#define TEST_NAME "LIBBSD PF 2"
41
42/* Block all input except telnet. */
43#define ETC_PF_CONF "/etc/pf.conf"
44#define ETC_PF_CONF_CONTENT \
45        "block all\n" \
46        "pass in inet proto { tcp } from any to any port { telnet } keep state\n" \
47        "pass out all\n"
48
49/* pf.os */
50#define ETC_PF_OS "/etc/pf.os"
51#define ETC_PF_OS_CONTENT "# empty"
52
53/* protocols */
54#define ETC_PROTOCOLS "/etc/protocols"
55#define ETC_PROTOCOLS_CONTENT \
56        "ip     0       IP              # internet protocol, pseudo protocol number\n" \
57        "tcp    6       TCP             # transmission control protocol\n" \
58        "udp    17      UDP             # user datagram protocol\n"
59
60/* services */
61#define ETC_SERVICES "/etc/services"
62#define ETC_SERVICES_CONTENT \
63        "ftp-data        20/sctp   #File Transfer [Default Data]\n" \
64        "ftp-data        20/tcp    #File Transfer [Default Data]\n" \
65        "ftp-data        20/udp    #File Transfer [Default Data]\n" \
66        "ftp             21/sctp   #File Transfer [Control]\n" \
67        "ftp             21/tcp    #File Transfer [Control]\n" \
68        "ftp             21/udp    #File Transfer [Control]\n" \
69        "ssh             22/tcp    #Secure Shell Login\n" \
70        "telnet          23/tcp\n" \
71        "telnet          23/udp\n" \
72        "http            80/tcp    www www-http #World Wide Web HTTP\n"
73
74static const struct {
75        const char *name;
76        const char *content;
77} init_files[] = {
78        {.name = ETC_PF_CONF, .content = ETC_PF_CONF_CONTENT},
79        {.name = ETC_PF_OS, .content = ETC_PF_OS_CONTENT},
80        {.name = ETC_PROTOCOLS, .content = ETC_PROTOCOLS_CONTENT},
81        {.name = ETC_SERVICES, .content = ETC_SERVICES_CONTENT},
82};
83
84/* Create all necessary files */
85static void
86prepare_files()
87{
88        size_t i;
89        struct stat sb;
90        int rv;
91        int fd;
92        size_t written;
93
94        /* Create /etc if necessary */
95        rv = mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO);
96        /* ignore errors, check the dir after. */
97        assert(stat("/etc", &sb) == 0);
98        assert(S_ISDIR(sb.st_mode));
99
100        /* Create files */
101        for(i = 0; i < (sizeof(init_files)/sizeof(init_files[0])); ++i) {
102                const char *content;
103                size_t len;
104
105                content = init_files[i].content;
106                len = strlen(content);
107
108                fd = open(init_files[i].name, O_WRONLY | O_CREAT,
109                    S_IRWXU | S_IRWXG | S_IRWXO);
110                assert(fd != -1);
111
112                written = write(fd, content, len);
113                assert(written == len);
114
115                rv = close(fd);
116                assert(rv == 0);
117        }
118}
119
120static void
121telnet_shell(char *name, void *arg)
122{
123        rtems_shell_env_t env;
124
125        memset(&env, 0, sizeof(env));
126
127        env.devname = name;
128        env.taskname = "TLNT";
129        env.login_check = NULL;
130        env.forever = false;
131
132        rtems_shell_main_loop(&env);
133}
134
135rtems_telnetd_config_table rtems_telnetd_config = {
136        .command = telnet_shell,
137        .arg = NULL,
138        .priority = 0,
139        .stack_size = 0,
140        .login_check = NULL,
141        .keep_stdio = false
142};
143
144struct rtems_ftpd_configuration rtems_ftpd_configuration = {
145        .priority = 100,
146        .max_hook_filesize = 0,
147        .port = 21,
148        .hooks = NULL,
149        .root = NULL,
150        .tasks_count = 4,
151        .idle = 5 * 60,
152        .access = 0
153};
154
155static void
156test_main(void)
157{
158        rtems_status_code sc;
159        int rv;
160
161        prepare_files();
162
163        sc = rtems_telnetd_initialize();
164        assert(sc == RTEMS_SUCCESSFUL);
165
166        rv = rtems_initialize_ftpd();
167        assert(rv == 0);
168
169        rtems_shell_env_t env;
170
171        memset(&env, 0, sizeof(env));
172        rtems_shell_main_loop( &env );
173
174        exit(0);
175}
176
177#include <machine/rtems-bsd-sysinit.h>
178
179#define RTEMS_BSD_CONFIG_FIREWALL_PF
180#define RTEMS_BSD_CONFIG_FIREWALL_PFLOG
181#define CONFIGURE_MAXIMUM_DRIVERS 32
182
183#include <rtems/bsd/test/default-network-init.h>
184
185/* Shell config */
186#include <rtems/netcmds-config.h>
187
188#define CONFIGURE_SHELL_COMMANDS_INIT
189#define CONFIGURE_SHELL_COMMANDS_ALL
190
191#define CONFIGURE_SHELL_USER_COMMANDS \
192    &rtems_shell_PING_Command, \
193    &rtems_shell_IFCONFIG_Command, \
194    &rtems_shell_PFCTL_Command
195
196#include <rtems/shellconfig.h>
Note: See TracBrowser for help on using the repository browser.