-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli-sub.h
376 lines (261 loc) · 8.53 KB
/
cli-sub.h
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
#ifndef ___CNAMEjj_CLI_SUB_H__
#define ___CNAMEjj_CLI_SUB_H__
/*
* Revision history
* ----------------
* 8/16/13
* -- Parts added for CGI support routines
* 6/10/13 -jj
* -- Include bits needed for "xml2-util" routines.
* 1/21/13 -jj
* -- Combine with other header files for "libCLI" routines, and rename...
* 12/11/12
* -- Copied from "junction-ac" code and tweaked to add in error value constants
*
* Todo
* ----
*/
#pragma GCC diagnostic ignored "-Wlong-long"
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#ifndef SKIP_XML_PARTS
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#endif
#include <poll.h>
#include <time.h>
#ifdef DEBUG_MALLOC
#include "bug_malloc.h"
#endif
/* --- */
#ifdef DEBUG_TRACE_CALLS
#include <time.h>
# define ENTER( NAME ) \
time_t dbg_ct_start = time(0); \
fprintf(stderr, "Enter: %s\n", NAME ); \
# define LEAVE( NAME ) fprintf(stderr, "Leave: %s (%ld)\n", NAME, time(0) - dbg_ct_start );
# define INSUB( NAME, MSG ) fprintf(stderr, "InSub: %s (%s) (%ld)\n", NAME, MSG, time(0) - dbg_ct_start );
#else
# define ENTER( NAME )
# define LEAVE( NAME )
# define INSUB( NAME, MSG )
#endif
/* --- */
#define SUBSET_READ 0x4
#define SUBSET_WRITE 0x2
#define SUBSET_EXEC 0x1
#define OP_TYPE_CHAR 1
#define OP_TYPE_FLAG 2
#define OP_TYPE_INT 3
#define OP_TYPE_VOID 4
#define OP_TYPE_FLOAT 5
#define OP_TYPE_LAST 6
#define OP_FL_BLANK 0x00
#define OP_FL_FOUND 0x01
#define OP_FL_SET 0x02
#define OP_FL_INVALID 0x04
#define OP_FL_REPEATS 0x08
#define EXT_FLAG_PREFIX "--"
#define EXT_FLAG_NEGATE "--no-"
#define INT_DISPLAY_LEN 11
#define HEX_BASE 16
#define GNM_FIRST 0
#define GNM_NEXT 1
#define GNM_LAST 2
#define ENV_QUERY_STRING "QUERY_STRING"
#define ENV_SCRIPT_NAME "SCRIPT_NAME"
#define ENV_SERVER_NAME "SERVER_NAME"
#define ENV_SERVER_PORT "SERVER_PORT"
#define CHUNK_MAX 4096
#define BYTE_SIZE 1
#define EOS_CH '\0'
#define PERCENT_CH '%'
#define BLANK_CH ' '
#define PLUS_CH '+'
#define TAB_CH '\t'
#define LF_CH '\n'
#define CR_CH '\r'
#define COLON_CH ':'
#define GT_CH '>'
#define LT_CH '<'
#define DQUOTE_CH '"'
#define AMPER_CH '&'
#define CGI_OPT_DELIM '&'
#define CGI_OPT_EQUAL '='
#define IS_SET_TRUE 1
#define IS_SET_FALSE 0
#define IS_SET_INVALID -1
#define OP_IS_SET_TRUE1 "1"
#define OP_IS_SET_TRUE2 "ON"
#define OP_IS_SET_TRUE3 "YES"
#define OP_IS_SET_FALSE1 "0"
#define OP_IS_SET_FALSE2 "OFF"
#define OP_IS_SET_FALSE3 "NO"
#define COLON_ST ":"
#define EMPTY_ST ""
#define DEFAULT_HTTP_PORT "80"
#define NO_PORT -1
#define NO_SOCK -1
#define URL_VALID 1
#define URL_ERROR 0
#define PROTOCOL_DELIM "://"
#define DOMAIN_DELIM "/"
#define PORT_DELIM ":"
#define QUERY_DELIM "?"
#define USER_DELIM "@"
#define IPV6_PORT_DELIM "#"
#define BASE10 10
#define SC_FORM_PATT_SERVER "<:SERVER:>"
#define SC_FORM_PATT_SCRIPT "<:SCRIPT:>"
#define SC_FORM_PATT_PORT "<:PORT:>"
/* -- Can't use this constants from "in6.h", so hardcode tem
#define SCOPE_LOOP __IPV6_ADDR_SCOPE_INTFACELOCAL
#define SCOPE_LINK __IPV6_ADDR_SCOPE_LINKLOCAL
#define SCOPE_SITE __IPV6_ADDR_SCOPE_SITELOCAL
#define SCOPE_GLOBAL __IPV6_ADDR_SCOPE_GLOBAL
*/
#define SCOPE_LOOP 0x01
#define SCOPE_LINK 0x02
#define SCOPE_SITE 0x05
#define SCOPE_GLOBAL 0x0e
#define POLL_EVENTS_READ POLLIN | POLLRDNORM | POLLPRI
#define POLL_EVENTS_WRITE POLLOUT | POLLWRNORM
#define POLL_EVENTS_ANY POLL_EVENTS_READ | POLL_EVENTS_WRITE | POLLERR | POLLHUP | POLLNVAL | POLLRDBAND | POLLWRBAND
#define POLL_EVENTS_ERROR POLLERR | POLLHUP | POLLNVAL
#ifdef linux
#define SOCKET_OPTS SOCK_STREAM | SOCK_NONBLOCK
#else
#define SOCKET_OPTS SOCK_STREAM
#endif
#define DEF_CLIENT_PORT 80
#define SSL_PROT_PREFIX "https"
#define CTX_MODES SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
#define SSL_MAX_READ_AHEAD 1
#define SSL_TRUSTED_CERT_PATH "/etc/ssl/certs"
#define DEF_INT_DISPLAY_FORMAT "%d"
#define DEF_FLOAT_DISPLAY_FORMAT "%.1f"
#define INT_DISPLAY_SIZE 20
#define FLOAT_DISPLAY_SIZE 30
#define NO_PATT_MATCH -1
#define DTYPE_AUTO -1
#define DTYPE_FLOAT 0
#define DTYPE_FIXED 1
#define DTYPE_TIME 2
#define IS_DTYPE_FLOAT "float:"
#define IS_DTYPE_FIXED "int:"
#define IS_DTYPE_TIME "time:"
#define CLOCK_DISPLAY_SIZE 128
/* --- */
struct value_chain {
unsigned int opt_num, flags;
char *val;
void *parsed;
struct value_chain *next;
};
struct option_set {
unsigned int num, type, flags;
char *name, *val, *def;
void *parsed;
int opt_num;
};
struct word_chain {
char *opt;
struct word_chain *next;
};
struct word_list {
int count;
char **words;
};
struct sub_list {
char *from, *to;
struct sub_list *next;
};
struct data_chunk {
char *data;
int size;
struct data_chunk *next;
};
struct url_breakout {
int status, port, use_ssl;
char *protocol, *target, *user, *host, *ip4, *ip6, *uri, *query;
};
struct interface_info {
char *name;
unsigned int flags;
struct sockaddr *addr;
};
struct http_status_response {
int code;
char *version, *reason;
};
struct string_parts {
int np;
char **list;
};
struct sub_list_reference {
struct sub_list *patt;
char *ref;
};
/* --- */
struct word_chain *parse_command_options( int *rc, struct option_set *plist, int nopt, int narg, char **opts);
struct option_set *get_matching_option( int flag_num, struct option_set *opset, int nflags);
void print_parse_summary( struct word_chain *, struct option_set *, int);
mode_t convert_to_mode( int);
char *gsub_string( int *rc, char *template, struct sub_list *patts);
char *get_groupname( int *rc, uid_t gid);
char *get_username( int *rc, uid_t uid);
int switch_run_user( char *user);
int switch_run_group( char *group);
char *int_to_str( char *buff, int buff_len, int source, char *format);
char *build_syscall_errmsg( char *syscall, int sysrc);
char *hexdigits_to_string( int *rc, int *msglen, char *hex);
#ifndef SKIP_XML_PARTS
xmlNodePtr search_node_list( xmlNodePtr head, char *node_name, char *attrib_name, char *attrib_val);
char *dup_attrib_value( int *rc, xmlNodePtr curr, char *attrib_name);
char *get_attrib_value( int *rc, xmlNodePtr curr, char *attrib_name);
xmlAttr *search_attrib_list( xmlAttr *head, char *attrib_name, char *attrib_val);
xmlNodePtr get_node_match( xmlNodePtr head, char *node_name, int match_type);
xmlNodePtr source_config_file( int *rc, char *config);
#endif
int called_as_cgi();
struct data_chunk *allocate_one_chunk( struct data_chunk *current, int max_chunk_length);
void free_chunk_chain( struct data_chunk *current);
char *get_cgi_data( int *rc);
char *decode_url_str( char *encoded);
int process_parsed_command_options( struct option_set *plist, int nopt, int narg, char **opts);
struct word_chain *add_to_word_chain( struct word_chain *anchor, char *data);
int is_option_set( char *setting);
struct word_chain *parse_cgi_options( int *rc, struct option_set *plist,
int nopt, char *cgi_data);
struct value_chain *add_option_to_chain( struct value_chain *spot, int opt_num, unsigned int flag,
char *val, void *parsed);
struct option_set *cond_get_matching_option( int *rc, int which_flag, struct option_set *opset, int nflags);
struct url_breakout *alloc_url_breakout();
void free_url_breakout( struct url_breakout *plan);
struct url_breakout *parse_url_string( char *url);
int is_ipv4_address( char *ip);
int is_ipv6_address( char *ip);
void print_option_settings( FILE *out, int nflags, struct option_set *opset);
char *dup_memory( char *start, char *end);
float get_scaled_number( char *mark, float figure);
char *sys_call_fail_msg( const char *routine);
char *errmsg_with_string( const char *emsg_template, const char *detail);
struct interface_info *get_matching_interface( const char *if_name, int protocol, unsigned int flags);
int is_reserved_uri_char( int spot);
struct http_status_response *parse_http_status( char *line);
char *construct_entry_form( char *template);
int connect_host( int *rc, char *host, int port, int timeout, int protocol);
int wait_until_sock_ready( int sock, int event, int max_wait);
char *combine_strings( int *rc, char *lead, char *trail);
char *string_from_int( int *rc, int number, char *format);
char *string_from_float( int *rc, float number, char *format);
char *string_from_clock( int *rc, time_t number, char *format);
char *string_from_typed_format( int *rc, void *number, int dtype, char *format);
struct string_parts *explode_string( int *rc, char *string, char *delim);
void remove_empty_strings( struct string_parts *plist);
struct value_chain *dup_value_chain( int *rc, struct value_chain *orig);
void free_value_chain( struct value_chain *vchain);
/* --- */
#endif