aboutsummaryrefslogtreecommitdiff
path: root/src/pies.h
blob: d78d6d17034d4377b8fd9e9d1c24a23e648a8aa4 (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
/* This file is part of GNU Pies.
   Copyright (C) 2008, 2009 Sergey Poznyakoff

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

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <time.h>
#include <sysexits.h>
#include <argp.h>

#include <grecs.h>
#include <wordsplit.h>

#include "progname.h"
#include "inttostr.h"
#include "c-ctype.h"
#include "xalloc.h"
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
#include "obstack.h"
#include "xvasprintf.h"
#include "quotearg.h"
#include "fprintftime.h"

#include "acl.h"
#include "libpies.h"

#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))

#define TESTTIME  2*60
#define SLEEPTIME 5*60
#define MAXSPAWN 10

#define DEFAULT_PASS_FD_TIMEOUT 5

#define RETR_OUT 0
#define RETR_ERR 1

enum redir_type
{
  redir_null,
  redir_syslog,
  redir_file
};

struct redirector
{
  enum redir_type type;
  union
  {
    int prio;
    char *file;
  } v;
};

typedef struct limits_rec *limits_record_t;

enum return_action
{
  action_restart,
  action_disable,
};

#define STATUS_SIG_BIT 0x80000000
#define STATUS_CODE(c) ((c) & ~STATUS_SIG_BIT)

struct action
{
  struct action *next;
  size_t nstat;
  unsigned *status;
  enum return_action act;  /* Action to take when the component terminates */
  char *addr;              /* Addresses to notify about it. */
  char *message;           /* Notification mail. */
  char *command;           /* Execute this command */    
};


/* user privs */
struct pies_privs
{
  char *user;
  int allgroups;
  gl_list_t groups;
};

enum pies_comp_mode
  {
    /* Execute the component, no sockets are opened. This is the default
       Pies mode. */
    pies_comp_exec,
    /* Open a socket and start a component with stdin/stdout bound to that
       socket. Corresponds to MeTA1 notion of `start_action = accept'.
    */
    pies_comp_accept,
    /* Inetd mode: like above, but start the component only when an
       incoming connection is requested. Corresponds to
       `start_action = nostartaccept' in MeTA1.
    */
    pies_comp_inetd,
    /* Open a socket, start a component, and pass the socket fd to the
       component via the UNIX domain socket. Corresponds to
       `start_action = pass' in MeTA1. */
    pies_comp_pass_fd
  };

#define CF_DISABLED    0x001 /* The componenet is disabled */
#define CF_PRECIOUS    0x002 /* The component is precious (should not
			        be disabled) */
#define CF_WAIT        0x004 /* Wait for the component instance to
			        terminate. */
#define CF_TCPMUX      0x008 /* A plain TCPMUX service */
#define CF_TCPMUXPLUS  0x010 /* A TCPMUX-plus service, i.e. pies
			        must emit a '+' response before starting
			        it */
#define CF_INTERNAL    0x020 /* An internal inetd service */
#define CF_SOCKENV     0x040 /* Component wants socket information in
			        the environment */
#define CF_RESOLVE     0x080 /* Resolve IP addresses */
#define CF_SIGGROUP    0x100 /* Send signals to the process group */

#define ISCF_TCPMUX(f) ((f) & (CF_TCPMUX | CF_TCPMUXPLUS))

struct component
{
  enum pies_comp_mode mode;
  char *tag;               /* Entry tag (for diagnostics purposes) */
  char *program;           /* Program name */
  size_t argc;             /* Number of command line arguments */
  char **argv;             /* Program command line */
  char **env;              /* Program environment */
  char *dir;               /* Working directory */
  gl_list_t prereq;        /* Prerequisites */
  gl_list_t depend;        /* Dependency targets */
  int flags;               /* CF_ bitmask */
  size_t max_instances;    /* Maximum number of simultaneously running
			      instances */
  char *rmfile;            /* Try to remove this file before starting */
  struct pies_privs privs; /* UID/GIDS+groups to run under */
  mode_t umask;            /* Umask to install before starting */
  limits_record_t limits;  /* System limits */

  /* For inetd components */
  size_t max_rate;         /* Maximum number of invocations per minute */
  size_t max_ip_connections; /* Max. number of connections per IP address */
  int socket_type;         /* Socket type */
  struct inetd_builtin *builtin; /* Builtin function */
  char *service;

  struct pies_url *socket_url; /* Socket to listen on
				  (if mode != pies_comp_exec) */
  char *pass_fd_socket;     /* Socket to pass fd on
			       (if mode == pies_comp_pass_fd) */
  unsigned pass_fd_timeout; /* Maximum time to wait for pass_fd socket to
			       become available. */
  pies_acl_t acl;
  char *tcpmux;             /* Master service for TCPMUX */

  /* Optional error messages to be sent back on the socket: */
  char *access_denied_message;
  char *max_instances_message;
  char *max_ip_connections_message;
  
  /* Redirectors: */
  int facility;	         /* Syslog facility. */
  struct redirector redir[2];   /* Repeaters for stdout and stderr */
  /* Actions to execute on various exit codes: */
  struct action *act_head, *act_tail;
  struct action act_temp; /* Auxiliary object used during configuration */
};

union pies_sockaddr_storage
{
  struct sockaddr s;
  struct sockaddr_in s_in;
  struct sockaddr_un s_un;
};

extern char *log_tag;
extern int log_facility;
extern unsigned long shutdown_timeout;
extern struct component default_component;
extern pies_acl_t pies_acl;
extern limits_record_t pies_limits;
extern char *mailer_program;
extern char *mailer_command_line;
extern int mailer_argc;
extern char **mailer_argv;
extern size_t default_max_rate;
extern char *qotdfile;

void register_prog (struct component *comp);
size_t progman_running_count (void);
void progman_start (void);
void progman_wake_sleeping (int);
void progman_stop (void);
void progman_cleanup (int expect_term);
void progman_stop_component (const char *name);
void progman_dump_stats (const char *filename);
void progman_dump_prereq (void);
void progman_dump_depmap (void);
int progman_accept (int socket);
int progman_build_depmap (void);
void progman_create_sockets (void);
struct component *progman_lookup_component (const char *tag);
struct component *progman_lookup_tcpmux (const char *service,
					 const char *master);

void progman_run_comp (struct component *comp, int fd,
		       union pies_sockaddr_storage *sa, socklen_t salen);

void progman_iterate_comp (int (*fun) (struct component *, void *),
			   void *data);
void fd_report (int fd, const char *msg);

int check_acl (pies_acl_t acl, struct sockaddr *s, socklen_t salen);

void log_setup (int want_stderr);
void signal_setup (RETSIGTYPE (*sf)(int));

typedef struct pies_depmap *pies_depmap_t;
typedef struct pies_depmap_pos *pies_depmap_pos_t;
enum pies_depmap_direction
  {
    depmap_row = 0,
    depmap_col = !depmap_row
  };

pies_depmap_t depmap_alloc (size_t count);
pies_depmap_t depmap_copy (pies_depmap_t dpm);
void depmap_set (pies_depmap_t dmap, size_t row, size_t col);
int depmap_isset (pies_depmap_t dmap, size_t row, size_t col);
void depmap_tc (pies_depmap_t dmap);
size_t depmap_first (pies_depmap_t dmap, enum pies_depmap_direction dir,
		     size_t coord, pies_depmap_pos_t *ppos);
size_t depmap_next (pies_depmap_t dmap, pies_depmap_pos_t pos);

int assert_grecs_value_type (grecs_locus_t *locus,
			     const grecs_value_t *value, int type);

int str_to_socket_type (const char *str, int *pret);
int socket_type_to_str (int socket_type, const char **pres);

struct component *component_create (const char *name);

void component_finish (struct component *comp, grecs_locus_t *locus);
struct grecs_keyword *find_component_keyword (const char *ident);


/* url.c */
struct pies_url
{
  char *string;
  char *scheme;
  char *host;
  char *port_s;
  int port;
  char *proto_s;
  int proto;
  char *path;
  char *user;
  char *passwd;
  int argc;
  char **argv;
};

int pies_url_create (struct pies_url **purl, const char *str);
void pies_url_destroy (struct pies_url **purl);
const char * pies_url_get_arg (struct pies_url *url, const char *argname);

void pies_pause (void);
int register_socket (int socktype, int fd);
int pass_fd (const char *socket, int fd, unsigned time_out);
int create_socket (struct pies_url *url, int socket_type,
		   const char *user, mode_t umask);
void disable_socket (int fd);
void enable_socket (int fd);


int parse_limits (limits_record_t *plrec, char *str, char **endp);
int set_limits (const char *name, limits_record_t lrec);


void meta1_parser_set_debug (void);
int meta1lex (void);
int meta1error (char *s);
int meta1parse (void);


/* diag.c */
#define DIAG_TO_SYSLOG 0x1
#define DIAG_TO_STDERR 0x2

extern int diag_output;

#define DIAG_OUTPUT(x) (diag_output & (x))

void diag_setup (int flags);

void logmsg (int prio, const char *fmt, ...);
void logmsg_printf (int prio, const char *fmt, ...);
void logmsg_vprintf (int prio, const char *fmt, va_list ap);

extern unsigned debug_level;
extern int source_info_option;
void debug_msg (const char *fmt, ...);

#define debug(lev, args)						\
  do									\
    if (debug_level >= lev)						\
      {									\
	if (source_info_option)						\
	  logmsg_printf (LOG_DEBUG, "%s:%lu:%s: ",			\
			 __FILE__, __LINE__, __FUNCTION__);		\
	debug_msg args;							\
      }									\
  while (0)


/* meta.c */
struct metadef
{
  char *kw;
  char *value;
  const char *(*expand) (struct metadef *, void *);
  char *storage;
  void *data;
};

char *meta_expand_string (const char *string, struct metadef *def, void *data);
void meta_free (struct metadef *def);


/* addrfmt.c */
void sockaddr_to_str (const struct sockaddr *sa, int salen,
		      char *bufptr, size_t buflen,
		      size_t *plen);
char *sockaddr_to_astr (const struct sockaddr *sa, int salen);


/* userprivs.c */
int switch_to_privs (uid_t uid, gid_t gid, gl_list_t retain_groups);

void pies_priv_setup (struct pies_privs *);
void pies_epriv_setup (struct pies_privs *);

/* inetd.c */
int inetd_parse_conf (const char *file);

/* inetd-bi.c */
struct inetd_builtin
{
  const char *service;
  int socktype;
  int single_process;
  int flags;
  void (*fun) (int, struct component const *);
};

struct inetd_builtin *inetd_builtin_lookup (const char *service, int socktype);


Return to:

Send suggestions and report system problems to the System administrator.