-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
201 lines (166 loc) · 6.18 KB
/
commands.cpp
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
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (April 2006)
Copyright (c) 2006, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
#include "ibp_server.h"
#include "log.h"
//***************************************************************************
// generate_command_acl - Fills in the command ACL list for a new connection
//***************************************************************************
void generate_command_acl(char *peer_name, int *acl)
{
int i;
command_t *command;
log_printf(15, "generate_command_acl: peer_name=%s\n", peer_name);
for (i=0; i<COMMAND_TABLE_MAX+1; i++) {
command = &(global_config->command[i]);
if (command->used == 1) {
acl[i] = subnet_list_validate(command->subnet, peer_name);
} else {
acl[i] = 0;
}
}
}
//*************************************************************************
// add_command - Adds a command to the vec table
// If a collision occurs with a command occurs the program aborts.
//*************************************************************************
void add_command(int cmd, const char *cmd_keyword, GKeyFile *kf,
void (*load_config)(GKeyFile *keyfile),
void (*init)(void),
void (*destroy)(void),
void (*print_cmd)(FILE *fd),
int (*parse)(ibp_task_t *task, char **bstate),
int (*execute)(ibp_task_t *task)) {
gsize n;
char **list;
if ((cmd < 0) || (cmd > COMMAND_TABLE_MAX)) {
printf("add_command: Invalid commands slot. Requested %d. Should be between 0 and %d\n", cmd, COMMAND_TABLE_MAX);
fflush(stdout);
abort();
}
command_t *command = &(global_config->command[cmd]);
//** check for a collision
if (command->used != 0) {
printf("add_command: Command collision for slot %d!\n", cmd);
fflush(stdout);
abort();
}
//*** Ok now install it in the table ***
command->used = 1;
strncpy(command->name, cmd_keyword, sizeof(command->name)); command->name[sizeof(command->name)-1] = '\0';
command->command = cmd;
command->load_config = load_config;
command->init = init;
command->destroy = destroy;
command->print = print_cmd;
command->read = parse;
command->execute = execute;
command->acl = g_key_file_get_string_list(kf, "access_control", cmd_keyword, &n, NULL);
//*** Load the ip table filter ***
list = command->acl;
if (list == NULL) {
log_printf(0, "add_command: Using default ACL for command %s!\n", cmd_keyword);
list = global_config->server.default_acl;
if (list == NULL) {
log_printf(0, "add_command: No default access_control flag found!\n");
abort();
}
}
//log_printf(15, "add_command: initial acl=%s\n", list[0]);
command->subnet = new_subnet_list(list);
// g_strfreev(list);
//** Lastly load the config
if (load_config != NULL) command->load_config(kf);
}
//*************************************************************************
// print_command_config - Prints the config for all commands
//*************************************************************************
void print_command_config(FILE *fd) {
int i, j;
command_t *cmd;
char **text;
for (i=0; i<=COMMAND_TABLE_MAX; i++) {
cmd = &(global_config->command[i]);
if (cmd->used == 1) {
if (cmd->print != NULL) {
cmd->print(fd);
fprintf(fd, "\n");
}
}
}
//** Now print the access_control information
fprintf(fd, "\n");
fprintf(fd, "[access_control]\n");
text = global_config->server.default_acl;
if (text != NULL) {
fprintf(fd, "default = ");
for (i=0; text[i] != NULL; i++) {
fprintf(fd, "%s", text[i]);
if (text[i+1] != NULL) fprintf(fd, ";");
}
fprintf(fd, "\n");
}
for (i=0; i<=COMMAND_TABLE_MAX; i++) {
cmd = &(global_config->command[i]);
if (cmd->used == 1) {
text = cmd->acl;
//log_printf(15, "print_command_config: cmd=%s\n", cmd->name);
if (text != NULL) {
fprintf(fd, "%s = ", cmd->name);
for (j=0; text[j] != NULL; j++) {
fprintf(fd, "%s", text[j]);
if (text[j+1] != NULL) fprintf(fd, ";");
}
fprintf(fd, "\n");
}
}
}
fprintf(fd, "\n");
}
//*************************************************************************
// initialize_commands - Executes each commands init routine
//*************************************************************************
void initialize_commands() {
int i;
command_t *cmd;
for (i=0; i<=COMMAND_TABLE_MAX; i++) {
cmd = &(global_config->command[i]);
if (cmd->used == 1) {
if (cmd->init != NULL) cmd->init();
}
}
}
//*************************************************************************
// destroy_commands - Executes each commands destroy routine
//*************************************************************************
void destroy_commands() {
int i;
command_t *cmd;
for (i=0; i<=COMMAND_TABLE_MAX; i++) {
cmd = &(global_config->command[i]);
if (cmd->used == 1) {
if (cmd->destroy != NULL) cmd->destroy();
}
}
}