summaryrefslogtreecommitdiff
path: root/src/daemon.c
blob: ff84d973a1a72e304143051c5291423e4a9603a2 (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
/* -*- c-file-style: "java"; indent-tabs-mode: nil; tab-width: 4; fill-column: 78 -*-
 *
 * distcc -- A simple distributed compiler system
 *
 * Copyright (C) 2002, 2003 by Martin Pool <mbp@samba.org>
 * Copyright 2007 Google Inc.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */


                /* "Just like distributed.net, only useful!" */

/**
 * @file
 *
 * distcc volunteer server.  Accepts and serves requests to compile
 * files.
 *
 * May be run from inetd (default if stdin is a socket), or as a
 * daemon by itself.
 *
 * distcc has an adequate but perhaps not optimal system for deciding
 * where to send files.  The general principle is that the server
 * should say how many jobs it is willing to accept, rather than the
 * client having to know.  This is probably good in two ways: it
 * allows for people in the future to impose limits on how much work
 * their contributed machine will do, and secondly it seems better to
 * put this information in one place rather than on every client.
 **/

#include <config.h>

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#ifdef HAVE_ARPA_NAMESER_H
#  include <arpa/nameser.h>
#endif

#include <arpa/inet.h>


#include "exitcode.h"
#include "distcc.h"
#include "trace.h"
#include "util.h"
#include "dopt.h"
#include "srvnet.h"
#include "daemon.h"
#include "types.h"
#ifdef HAVE_GSSAPI
#include "auth.h"
#endif


/* for trace.c */
char const *rs_program_name = "distccd";

/* for serve.c */
char const *dcc_daemon_wd;  /* The working directory for the server. */


static int dcc_inetd_server(void);
static void dcc_setup_real_log(void);


/**
 * Errors during startup (e.g. bad options) need to be reported somewhere,
 * although we have not yet parsed the options to work out where the user
 * wants them.
 *
 * In inetd mode, we can't write to stderr because that will corrupt the
 * stream, so if it looks like stderr is a socket we go to syslog instead.
 **/
static int dcc_setup_startup_log(void)
{
    rs_trace_set_level(RS_LOG_INFO);
    if (!is_a_socket(STDERR_FILENO)) {
        rs_add_logger(rs_logger_file, RS_LOG_DEBUG, 0, STDERR_FILENO);
    } else {
        openlog("distccd", LOG_PID, LOG_DAEMON);
        rs_trace_syslog = TRUE;
        rs_add_logger(rs_logger_syslog, RS_LOG_DEBUG, NULL, 0);
    }

    return 0;
}


static int dcc_should_be_inetd(void)
{
    /* Work out if we ought to serve stdin or be a standalone daemon */
    if (opt_inetd_mode)
        return 1;
    else if (opt_daemon_mode)
        return 0;
    else if (is_a_socket(STDIN_FILENO)) {
        rs_log_info("stdin is socket; assuming --inetd mode");
        return 1;
    } else if (isatty(STDIN_FILENO)) {
        rs_log_info("stdin is a tty; assuming --daemon mode");
        return 0;
    } else {
        rs_log_info("stdin is neither a tty nor a socket; assuming --daemon mode");
        return 0;
    }
}


static int dcc_setup_daemon_path(void)
{
    int ret;
    const char *path;

    if ((path = getenv("DISTCCD_PATH")) != NULL) {
        if ((ret = dcc_set_path(path)))
            return ret;

        return 0;
    } else {
        path = getenv("PATH");
        rs_log_info("daemon's PATH is %s", path ? path : "(NULL)");
        return 0;
    }
}

static void dcc_warn_masquerade_whitelist(void) {
    DIR *d, *e;
    const char *warn = "You must set up masquerade" \
                       " (see distcc(1)) to list whitelisted compilers or pass" \
                       " --enable-tcp-insecure. To set up masquerade automatically" \
                       " run update-distcc-symlinks.";

    e = opendir("/usr/lib/distcc");
    d = opendir(LIBDIR "/distcc");
    if (!e && !d) {
        rs_log_crit(LIBDIR "/distcc not found. %s", warn);
        dcc_exit(EXIT_COMPILER_MISSING);
    }
    if ((!e || !readdir(e)) && (!d || !readdir(d))) {
        rs_log_crit(LIBDIR "/distcc empty. %s", warn);
        dcc_exit(EXIT_COMPILER_MISSING);
    }
    if (d) {
        closedir(d);
    }
    if (e) {
        closedir(e);
    }
}

/**
 * distcc daemon.  May run from inetd, or standalone.  Accepts
 * requests from clients to compile files.
 **/
int main(int argc, char *argv[])
{
    int ret;

    dcc_setup_startup_log();

    if (distccd_parse_options(argc, (const char **) argv))
        dcc_exit(EXIT_DISTCC_FAILED);

    /* check this before redirecting the logs, so that it's really obvious */
    if (!dcc_should_be_inetd())
        if (opt_allowed == NULL) {
            rs_log_warning("No --allow option specified. Defaulting to --allow-private."
                         " Allowing non-Internet (globally"
                         " routable) addresses.");
            opt_allow_private = 1;
        }

    if ((ret = dcc_set_lifetime()) != 0)
        dcc_exit(ret);

    /* do this before giving away root */
    if (nice(opt_niceness) == -1) {
        rs_log_warning("nice %d failed: %s", opt_niceness,
                       strerror(errno));
        /* continue anyhow */
    }

    if ((ret = dcc_discard_root()) != 0)
        dcc_exit(ret);

    /* Discard privileges before opening log so that if it's created, it has
     * the right ownership. */
    dcc_setup_real_log();

    /* Do everything from $TMPDIR directory.  Allows start directory to be
     * unmounted. */
    if ((ret = dcc_get_tmp_top(&dcc_daemon_wd)))
        goto out;

    if (chdir(dcc_daemon_wd) == -1) {
        rs_log_error("failed to chdir to %s: %s",
                     dcc_daemon_wd, strerror(errno));
        ret = EXIT_IO_ERROR;
        goto out;
    } else {
        rs_trace("chdir to %s", dcc_daemon_wd);
    }

    if ((ret = dcc_setup_daemon_path()))
        goto out;

#ifdef HAVE_GSSAPI
    /* Obtain credentials if authentication is requested. */
    if (dcc_auth_enabled) {
        if ((ret = dcc_gssapi_acquire_credentials()) != 0) {
            goto out;
        }

        /* Read contents of list file into an array and apply qsort. */
        if (opt_blacklist_enabled || opt_whitelist_enabled) {
            if ((ret = dcc_gssapi_obtain_list((opt_blacklist_enabled) ? 1 : 0)) != 0) {
	            goto out;
	        }
        }
    }
#endif

    /* Initialize the distcc io timeout value */
    dcc_get_io_timeout();

    if (!opt_enable_tcp_insecure)
        dcc_warn_masquerade_whitelist();

    if (dcc_should_be_inetd())
        ret = dcc_inetd_server();
    else
        ret = dcc_standalone_server();

    out:
    dcc_exit(ret);
}


/**
 * If a --lifetime options was specified, set up a timer that will kill the
 * daemon when it expires.
 **/
int dcc_set_lifetime(void)
{
    if (opt_lifetime) {
        alarm(opt_lifetime);
/*         rs_trace("set alarm for %+d seconds", opt_lifetime); */
    }
    return 0;
}


/**
 * Set log to the final destination after options have been read.
 **/
static void dcc_setup_real_log(void)
{
    int fd;

    /* Even in inetd mode, we might want to log to stderr, because that will
     * work OK for ssh connections. */

    if (opt_log_stderr) {
        rs_remove_all_loggers();
        rs_add_logger(rs_logger_file, opt_log_level_num, 0, STDERR_FILENO);
        return;
    }

    if (arg_log_file) {
        /* Don't remove loggers yet, in case this fails and needs to go to the
         * default. */
        if ((fd = open(arg_log_file, O_CREAT|O_APPEND|O_WRONLY, 0666)) == -1) {
            rs_log_error("failed to open %s: %s", arg_log_file,
                         strerror(errno));
            /* continue and use syslog */
        } else {
            rs_remove_all_loggers();
            rs_add_logger(rs_logger_file, opt_log_level_num, NULL, fd);
            return;
        }
    }

    rs_remove_all_loggers();
    openlog("distccd", LOG_PID, LOG_DAEMON);
    rs_trace_syslog = TRUE;
    rs_add_logger(rs_logger_syslog, opt_log_level_num, NULL, 0);
}


int dcc_log_daemon_started(const char *role)
{
    rs_log_info("%s started (%s %s, built %s %s)",
                role,
                PACKAGE_VERSION,
                GNU_HOST,
                __DATE__, __TIME__);

    return 0;
}


/**
 * Serve a single file on stdin, and then exit.
 **/
static int dcc_inetd_server(void)
{
    int ret, close_ret;
    struct dcc_sockaddr_storage ss;
    struct sockaddr *psa = (struct sockaddr *) &ss;
    socklen_t len = sizeof ss;

    dcc_log_daemon_started("inetd server");

    if ((getpeername(STDIN_FILENO, psa, &len) == -1)) {
        /* This can fail with ENOTSOCK if e.g. sshd has started us on a pipe,
         * not on a socket.  I think it's harmless. */
        rs_log_notice("failed to get peer name: %s", strerror(errno));
        psa = NULL;             /* make sure we don't refer to uninitialized mem */
        len = 0;
    }

    ret = dcc_service_job(STDIN_FILENO, STDOUT_FILENO, psa, len);

    close_ret = dcc_close(STDIN_FILENO);

#ifdef HAVE_GSSAPI
    if (dcc_auth_enabled) {
        dcc_gssapi_release_credentials();

        if (opt_blacklist_enabled || opt_whitelist_enabled) {
            dcc_gssapi_free_list();
	    }
    }
#endif

    if (ret)
        return ret;
    else
        return close_ret;
}