Commit 271d62e9 271d62e93d6dc12f80d785d680df34cc35fddd90 by Sergey Poznyakoff

New mu submode: acl. Supersedes examples/aclck.c

* mu/acl.c: New file.
* mu/Makefile.am: Add acl.c
* po/POTFILES.in: Add acl.c
* mu/mu.c (mutool_action_tab): Add acl mode.
* mu/mu.h (mutool_acl): New proto.
* mu/flt2047.c: Fix docstrings.
* mu/pop.c: Likewise.
1 parent 96e44735
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
17 17
18 bin_PROGRAMS = mu 18 bin_PROGRAMS = mu
19 mu_SOURCES = \ 19 mu_SOURCES = \
20 acl.c\
20 info.c\ 21 info.c\
21 mu.h\ 22 mu.h\
22 mu.c\ 23 mu.c\
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #if defined(HAVE_CONFIG_H)
18 # include <config.h>
19 #endif
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <sys/un.h>
25 #include <arpa/inet.h>
26 #include <mailutils/mailutils.h>
27 #include <mailutils/libcfg.h>
28 #include "argp.h"
29 #include "mu.h"
30 #include "xalloc.h"
31
32 static char acl_doc[] = N_("mu acl - test access control lists.");
33 static char acl_args_doc[] = N_("ADDRESS [ADDRESS...]");
34
35 static struct argp_option acl_options[] = {
36 { "file", 'f', N_("FILE"), 0, N_("read ACLs from FILE") },
37 { "path", 'p', N_("PATH"), 0,
38 N_("path to the ACL in the configuration tree") },
39 { NULL }
40 };
41
42 static char *input_file_name;
43 static struct sockaddr *target_sa;
44 static socklen_t target_salen;
45 static mu_acl_t acl;
46 static const char *path = "acl";
47
48 static struct sockaddr *
49 parse_address (socklen_t *psalen, const char *str)
50 {
51 struct sockaddr_in in;
52 struct sockaddr *sa;
53
54 in.sin_family = AF_INET;
55 if (inet_aton (str, &in.sin_addr) == 0)
56 {
57 mu_error ("Invalid IPv4: %s", str);
58 exit (1);
59 }
60 in.sin_port = 0;
61 *psalen = sizeof (in);
62 sa = malloc (*psalen);
63 if (!sa)
64 {
65 mu_error ("%s", mu_strerror (errno));
66 exit (1);
67 }
68
69 memcpy (sa, &in, sizeof (in));
70 return sa;
71 }
72
73 static error_t
74 acl_parse_opt (int key, char *arg, struct argp_state *state)
75 {
76 switch (key)
77 {
78 case 'f':
79 input_file_name = arg;
80 break;
81
82 case 'p':
83 path = arg;
84 break;
85
86 default:
87 return ARGP_ERR_UNKNOWN;
88 }
89 return 0;
90 }
91
92 static struct argp acl_argp = {
93 acl_options,
94 acl_parse_opt,
95 acl_args_doc,
96 acl_doc,
97 NULL,
98 NULL,
99 NULL
100 };
101
102
103 static struct mu_cfg_param acl_cfg_param[] = {
104 { "acl", mu_cfg_section, &acl, 0, NULL, "access control list" },
105 { NULL }
106 };
107
108 int
109 mutool_acl (int argc, char **argv)
110 {
111 int rc, index;
112 mu_acl_result_t result;
113 mu_cfg_tree_t *tree = NULL, *temp_tree = NULL;
114 mu_cfg_node_t *node;
115 int flags = 0;
116
117 if (argp_parse (&acl_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
118 return 1;
119
120 argc -= index;
121 argv += index;
122
123 if (argc == 0)
124 {
125 mu_error (_("not enough arguments"));
126 return 1;
127 }
128
129 if (input_file_name)
130 {
131 mu_load_site_rcfile = 0;
132 mu_load_user_rcfile = 0;
133 mu_load_rcfile = input_file_name;
134 }
135
136 mu_acl_cfg_init ();
137 if (mu_libcfg_parse_config (&tree))
138 return 1;
139 if (!tree)
140 return 0;
141
142 if (mu_cfg_find_node (tree, path, &node))
143 {
144 mu_error (_("cannot find node: %s"), path);
145 return 1;
146 }
147
148 mu_cfg_tree_create (&temp_tree);
149 mu_cfg_tree_add_node (temp_tree, node);
150 rc = mu_cfg_tree_reduce (temp_tree, mu_program_name, acl_cfg_param,
151 flags, NULL);
152 if (rc)
153 return 1;
154 if (!acl)
155 {
156 mu_error (_("No ACL found in config"));
157 return 1;
158 }
159
160 while (argc--)
161 {
162 const char *ap = *argv++;
163
164 target_sa = parse_address (&target_salen, ap);
165 printf ("Testing %s:\n", ap);
166 rc = mu_acl_check_sockaddr (acl, target_sa, target_salen, &result);
167 if (rc)
168 {
169 mu_error ("mu_acl_check_sockaddr failed: %s", mu_strerror (rc));
170 return 1;
171 }
172
173 switch (result)
174 {
175 case mu_acl_result_undefined:
176 printf ("%s: undefined\n", ap);
177 break;
178
179 case mu_acl_result_accept:
180 printf ("%s: accept\n", ap);
181 break;
182
183 case mu_acl_result_deny:
184 printf ("%s: deny\n", ap);
185 break;
186 }
187 }
188
189 mu_cfg_destroy_tree (&tree);
190 mu_cfg_destroy_tree (&temp_tree);
191
192 return 0;
193 }
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
23 #include "argp.h" 23 #include "argp.h"
24 #include "mu.h" 24 #include "mu.h"
25 25
26 static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers"); 26 static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers.");
27 static char flt2047_args_doc[] = N_("[text]"); 27 static char flt2047_args_doc[] = N_("[text]");
28 28
29 static struct argp_option flt2047_options[] = { 29 static struct argp_option flt2047_options[] = {
......
...@@ -32,6 +32,7 @@ Commands are:\n\ ...@@ -32,6 +32,7 @@ Commands are:\n\
32 mu pop - POP3 client program\n\ 32 mu pop - POP3 client program\n\
33 mu filter - filter program\n\ 33 mu filter - filter program\n\
34 mu 2047 - decode/encode message headers as per RFC 2047\n\ 34 mu 2047 - decode/encode message headers as per RFC 2047\n\
35 mu acl - test access control lists\n\
35 \n\ 36 \n\
36 Try `mu COMMAND --help' to get help on a particular COMMAND.\n\ 37 Try `mu COMMAND --help' to get help on a particular COMMAND.\n\
37 \n\ 38 \n\
...@@ -92,6 +93,7 @@ struct mutool_action_tab mutool_action_tab[] = { ...@@ -92,6 +93,7 @@ struct mutool_action_tab mutool_action_tab[] = {
92 { "filter", mutool_filter }, 93 { "filter", mutool_filter },
93 { "2047", mutool_flt2047 }, 94 { "2047", mutool_flt2047 },
94 { "query", mutool_query }, 95 { "query", mutool_query },
96 { "acl", mutool_acl },
95 { NULL } 97 { NULL }
96 }; 98 };
97 99
......
...@@ -33,6 +33,7 @@ int mutool_filter (int argc, char **argv); ...@@ -33,6 +33,7 @@ int mutool_filter (int argc, char **argv);
33 int mutool_flt2047 (int argc, char **argv); 33 int mutool_flt2047 (int argc, char **argv);
34 int mutool_info (int argc, char **argv); 34 int mutool_info (int argc, char **argv);
35 int mutool_query (int argc, char **argv); 35 int mutool_query (int argc, char **argv);
36 int mutool_acl (int argc, char **argv);
36 37
37 extern char *mutool_shell_prompt; 38 extern char *mutool_shell_prompt;
38 extern mu_vartab_t mutool_prompt_vartab; 39 extern mu_vartab_t mutool_prompt_vartab;
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
27 #include "argp.h" 27 #include "argp.h"
28 #include "xalloc.h" 28 #include "xalloc.h"
29 29
30 static char pop_doc[] = N_("mu pop - POP3 client shell"); 30 static char pop_doc[] = N_("mu pop - POP3 client shell.");
31 static char pop_args_doc[] = ""; 31 static char pop_args_doc[] = "";
32 32
33 static struct argp_option pop_options[] = { 33 static struct argp_option pop_options[] = {
......
...@@ -197,6 +197,7 @@ sieve/sieve.c ...@@ -197,6 +197,7 @@ sieve/sieve.c
197 197
198 sql/mysql.c 198 sql/mysql.c
199 199
200 mu/acl.c
200 mu/filter.c 201 mu/filter.c
201 mu/flt2047.c 202 mu/flt2047.c
202 mu/info.c 203 mu/info.c
......