summaryrefslogtreecommitdiffabout
path: root/dicod/dicod.h
blob: 13303d7822d1d1230f8349e555ce5a38c8e5364a (plain)
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
/* This file is part of GNU Dico.
   Copyright (C) 1998-2019 Sergey Poznyakoff

   GNU Dico 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 3, or (at your option)
   any later version.

   GNU Dico 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 GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <xalloc.h>
#include <ctype.h>
#include <syslog.h>
#include <inttypes.h>
#include <limits.h>
#include <size_max.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <sysexits.h>
#include <ltdl.h>
#include "grecs.h"
#include "wordsplit.h"

#include <xdico.h>
#include <inttostr.h>
#include <c-strcase.h>
#include <quotearg.h>
#include <appi18n.h>

#define EXIT_TIMEOUT 2
#define UINTMAX_STRSIZE_BOUND INT_BUFSIZE_BOUND(uintmax_t)

extern int mode;
extern int foreground;     /* Run in foreground mode */
extern int single_process; /* Single process mode */
extern int log_to_stderr;  /* Log to stderr */
extern char *config_file;
extern int config_lint_option;
extern char *pidfile_name;
extern dico_list_t listen_addr;
extern uid_t user_id;
extern gid_t group_id;
extern dico_list_t /* of gid_t */ group_list;
extern unsigned int max_children;
extern unsigned int shutdown_timeout;
extern unsigned int inactivity_timeout;
extern char *hostname;
extern const char *program_version;
extern char *initial_banner_text;
extern int got_quit;
extern char *help_text;
extern const char *server_info;
extern char *msg_id;
extern dico_list_t module_load_path;
extern dico_list_t prepend_load_path;
extern dico_list_t modinst_list;
extern dico_list_t database_list;
extern int timing_option;
extern char *client_id;
extern char *user_name;
extern dico_list_t user_groups;
extern int transcript;

extern char *debug_level_str;
extern unsigned long total_forks;
extern char *access_log_format;
extern char *access_log_file;
extern int identity_check;
extern char *identity_name;
extern char *ident_keyfile;
extern long ident_timeout;

extern struct sockaddr server_addr;
extern socklen_t server_addrlen;
extern struct sockaddr_storage client_addr;
extern socklen_t client_addrlen;

extern const char *program_version;

struct dico_stat {
    unsigned long defines;
    unsigned long matches;
    unsigned long compares;
};

extern struct dico_stat current_stat, total_stat;

#ifndef LOG_FACILITY
# define LOG_FACILITY LOG_LOCAL1
#endif

#define DICTD_DEFAULT_STRATEGY "lev"

#define DICTD_LOGGING_ENVAR "__DICTD_LOGGING__"

#define MODE_DAEMON  0
#define MODE_INETD   1
#define MODE_PREPROC 2
#define MODE_TEST    3

struct dicod_conf_override {
    int transcript;
};

void get_options(int *argc, char **argv[], struct dicod_conf_override *conf);


/* acl.c */
typedef struct dicod_acl *dicod_acl_t;

dicod_acl_t dicod_acl_create(const char *name, grecs_locus_t *locus);
int dicod_acl_check(dicod_acl_t acl, int res);

int parse_acl_line(grecs_locus_t *locus, int allow, dicod_acl_t acl,
		   grecs_value_t *value);

int dicod_acl_install(dicod_acl_t acl, grecs_locus_t *locus);
dicod_acl_t dicod_acl_lookup(const char *name);

extern dicod_acl_t connect_acl;


/* */

typedef struct dicod_module_instance {
    char *ident;
    char *command;
    struct dico_database_module *module;
    lt_dlhandle handle;
} dicod_module_instance_t;

#define DICO_DBF_LANG    0x10000

struct dicod_database {
    int flags;

    char *name;        /* Dictionary name */
    char *descr;       /* Description (SHOW DB) */
    char *info;        /* Info (SHOW INFO) */

    dico_list_t langlist[2]; /* List of "source/dest" languages */

    dicod_acl_t  acl;  /* ACL for this database */
    int visible;         /* Is this database (administratively) visible */
    int session_visible; /* Is this database visible in the current session.
			    This depends on the value of visible and on the
			    result of dicod_acl_check and dicod_lang_check */

    dico_handle_t mod_handle;        /* Dico module handle */

    dico_assoc_list_t mime_headers;

    dicod_module_instance_t *instance; /* Pointer to the module instance
					  structure */
    int argc;                 /* Handler arguments: count */
    char **argv;              /*  ... and pointers */
    char *command;            /* Handler command line (for diagnostics) */
    void *extra;
};

#define CONTENT_TRANSFER_ENCODING_HEADER "Content-transfer-encoding"

/* dicod.c */
void dicod_server(int argc, char **argv);
void replace_io_stream(dico_stream_t str);
dico_stream_t dicod_iostream(int ifd, int ofd);
int dicod_loop(dico_stream_t stream);
int dicod_inetd(void);
void dicod_init_strategies(void);
void dicod_server_init(void);
void dicod_server_cleanup(void);
int get_input_line(dico_stream_t str, char **buf, size_t *size,
		   size_t *rdbytes);

dicod_database_t *find_database(const char *name);
void database_remove_dependent(dicod_module_instance_t *inst);
size_t database_count(void);
int database_iterate(dico_list_iterator_t fun, void *data);
int show_sys_info_p(void);
void dicod_log_setup(void);
void dicod_log_pre_setup(void);
void dicod_log_encode_envar(void);
char *get_full_hostname(void);
void check_db_visibility(void);
void reset_db_visibility(void);
static inline int
database_is_visible(dicod_database_t const *db)
{
    return db->session_visible;
}
static inline int
database_is_virtual(dicod_database_t const *db)
{
    return db->flags & DICO_DBF_VIRTUAL;
}
int dicod_any_lang_list_p(dico_list_t list);


typedef void (*dicod_cmd_fn) (dico_stream_t str, int argc, char **argv);

struct dicod_command {
    const char *keyword;
    int minparam;
    int maxparam;
    char *param;
    char *help;
    dicod_cmd_fn handler;
};

#define DICOD_MAXPARAM_INF (-1)

void dicod_handle_command(dico_stream_t str, int argc, char **argv);
void dicod_init_command_tab(void);
void dicod_add_command(struct dicod_command *cmd);
void dicod_remove_command(const char *name);

/* capa.c */
void dicod_capa_register(const char *name, struct dicod_command *cmd,
			 int (*init)(void*), void *closure);
int dicod_capa_add(const char *name);
void dicod_capa_iterate(int (*fun)(const char*, int, void *), void *closure);
int dicod_capa_flush(void);
int dicod_capa_is_enabled(const char *name);

/* lang.c */
void register_lang(void);
int dicod_lang_check(dico_list_t list[2]);

/* mime.c */
extern int option_mime;
void register_mime(void);

/* markup.c */
void register_markup(void);
void markup_flush_capa(void);

/* xidle.c */
void register_xidle(void);

/* xversion.c */
void register_xversion(void);

/* lev.c */
void register_lev(void);

/* regex.c */
void register_regex(void);

/* dbtext.c */
struct dico_udb_def text_udb_def;
extern dico_udb_t user_db;

/* auth.c */
void register_auth(void);
void init_auth_data(void);

/* loader.c */
void dicod_loader_init(void);
int dicod_load_module(dicod_module_instance_t *hptr);

dico_list_t dicod_langlist_copy(dico_list_t src);

void dicod_match_word_db(dicod_database_t *db, dico_stream_t stream,
			 const dico_strategy_t strat, const char *word);
void dicod_match_word_first(dico_stream_t stream,
			    const dico_strategy_t strat, const char *word);
void dicod_match_word_all(dico_stream_t stream,
			  const dico_strategy_t strat, const char *word);
void dicod_define_word_db(dicod_database_t *db, dico_stream_t stream,
			  const char *word);
void dicod_define_word_first(dico_stream_t stream, const char *word);
void dicod_define_word_all(dico_stream_t stream, const char *word);

int dicod_module_test(int argc, char **argv);

void dicod_builtin_module_init(void);

/* result.c */
typedef struct dicod_db_result {
    int flags;
    dicod_database_t *db;
    dico_result_t res;
    size_t rcount;
    size_t ccount;
} dicod_db_result_t;

dicod_db_result_t *dicod_db_result_alloc(dicod_database_t *db,
					 dico_result_t res);
void dicod_db_result_free(dicod_db_result_t *dbr);
size_t dicod_db_result_count(dicod_db_result_t *dbr);
size_t dicod_db_result_compare_count(dicod_db_result_t *dbr);
int dicod_db_result_output(dicod_db_result_t *dbr, size_t n, dico_stream_t str);
dico_assoc_list_t dicod_db_result_mime_header(dicod_db_result_t *dbr, size_t n);

/* Flag values for dicod_db_result_db: */
enum {
    result_db_all,      /* Return the database no matter what its visibility */
    result_db_visible   /* Return only visible databases */
};

dicod_database_t *dicod_db_result_db(dicod_db_result_t *dbr, size_t n,
				     int flag);

/* database.c */
int dicod_database_init(dicod_database_t *dp);
int dicod_database_open(dicod_database_t *dp);
int dicod_database_close(dicod_database_t *dp);
int dicod_database_deinit(dicod_database_t *dp);

void dicod_database_free(dicod_database_t *dp);

char *dicod_database_get_descr(dicod_database_t *db);
void dicod_database_free_descr(dicod_database_t *db, char *descr);
char *dicod_database_get_info(dicod_database_t *db);
void dicod_database_free_info(dicod_database_t *db, char *info);
void dicod_database_get_languages(dicod_database_t *db, dico_list_t list[]);

dicod_db_result_t *dicod_database_match(dicod_database_t *db,
					const dico_strategy_t strat,
					const char *word);
dicod_db_result_t *dicod_database_define(dicod_database_t *db,
					 const char *word);

char *dicod_database_get_info(dicod_database_t *db);
void dicod_database_free_info(dicod_database_t *db, char *info);

char *dicod_database_get_descr(dicod_database_t *db);
void dicod_database_free_descr(dicod_database_t *db, char *descr);

int dicod_database_flags(dicod_database_t const *db);

void dicod_database_print_definitions(const char *word,
				      dicod_db_result_t *res, size_t count,
				      dico_stream_t stream);

/* ostream.c */
extern off_t total_bytes_out;
dico_stream_t dicod_ostream_create(dico_stream_t str,
				   dico_assoc_list_t headers);

/* stat.c */
void begin_timing(const char *name);
void report_timing(dico_stream_t stream, xdico_timer_t t,
		   struct dico_stat *sp);
void report_current_timing(dico_stream_t stream, const char *name);

/* xscript.c */
dico_stream_t transcript_stream_create(dico_stream_t transport,
				       dico_stream_t logstr,
				       const char *prefix[]);

/* getopt.m4 */
extern void print_help(void);
extern void print_usage(void);

/* pp.c */
void include_path_setup(void);
void add_include_dir(char *dir);
int pp_init(const char *name);
void pp_done(void);
int preprocess_config(const char *extpp);
void pp_make_argcv(int *pargc, const char ***pargv);
FILE *pp_extrn_start(int argc, const char **argv, pid_t *ppid);
void pp_extrn_shutdown(pid_t pid);
size_t pp_fill_buffer(char *buf, size_t size);
void run_lint();
char *install_text(const char *str);

/* accesslog.c */
void access_log_status(const char *first, const char *last);
void access_log(int argc, char **argv);
void compile_access_log(void);
void access_log_free_cache(void);

/* ident.c */
char *query_ident_name(struct sockaddr_in *srv_addr,
		       struct sockaddr_in *clt_addr);

/* alias.c */
int alias_install(const char *kw, int argc, char **argv, grecs_locus_t *ploc);
int alias_expand(int argc, char **argv, int *pargc, char ***pargv);

/* gsasl.c */
extern int sasl_enable;
extern dico_list_t sasl_enabled_mech;
extern dico_list_t sasl_disabled_mech;
extern char *sasl_service;
extern char *sasl_realm;
extern dico_list_t sasl_anon_groups;

void register_sasl(void);

/* stratcl.c */
enum cmp_op {
    cmp_eq,
    cmp_ne,
    cmp_lt,
    cmp_le,
    cmp_gt,
    cmp_ge
};

void stratcl_add_word(dico_list_t list, const char *word);
void stratcl_add_cmp(dico_list_t list, enum cmp_op op, size_t len);
void stratcl_add_disable(dico_list_t list);
int stratcl_check_word(dico_list_t list, const char *word);

/* ckpass.c */
int dicod_check_password(const char *db_pass, const char *pass);

/* main.c */
void config_help(void);
int database_session_visibility(dicod_database_t *db);


/* virtual.c */
extern struct dico_database_module virtual_builtin_module;

enum mime_cond {
    cond_any = -1,
    cond_mime,
    cond_nomime
};

struct vdb_member {
    char *name;
    dicod_database_t *db;
    int   cond;
};

dico_list_t vdb_list_create(void);

Return to:

Send suggestions and report system problems to the System administrator.