main.cc 8.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id$

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
Jeremy C. Reed's avatar
Jeremy C. Reed committed
21
#include <netinet/in.h>
22
#include <stdlib.h>
23
#include <errno.h>
24

25
#include <cassert>
26 27 28
#include <iostream>

#include <boost/foreach.hpp>
29 30

#include <exceptions/exceptions.h>
31

32 33
#include <dns/buffer.h>
#include <dns/message.h>
34
#include <dns/messagerenderer.h>
35

36 37 38
#include <cc/session.h>
#include <cc/data.h>
#include <config/ccsession.h>
39

40 41
#include <xfr/xfrout_client.h>

42 43
#include <auth/spec_config.h>
#include <auth/common.h>
JINMEI Tatuya's avatar
JINMEI Tatuya committed
44
#include <auth/change_user.h>
45
#include <auth/auth_srv.h>
46
#include <asiolink/asiolink.h>
47 48

using namespace std;
49 50 51
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
52
using namespace isc::dns;
53
using namespace isc::xfr;
54
using namespace asiolink;
55

56 57
namespace {

58
static bool verbose_mode = false;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
59

60 61
static const string PROGRAM = "Auth";
static const char* DNSPORT = "5300";
62 63 64 65

/* need global var for config/command handlers.
 * todo: turn this around, and put handlers in the authserver
 * class itself? */
66
static AuthSrv *auth_server;
67

68
static IOService* io_service;
69

70 71
ConstElementPtr
my_config_handler(ConstElementPtr new_config) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
72
    return (auth_server->updateConfig(new_config));
73 74
}

75 76 77
ConstElementPtr
my_command_handler(const string& command, ConstElementPtr args) {
    ConstElementPtr answer = createAnswer();
78

79
    if (command == "print_message") {
80 81
        cout << args << endl;
        /* let's add that message to our answer as well */
82
        answer = createAnswer(0, args);
83
    } else if (command == "shutdown") {
84
        io_service->stop();
85
    }
86
    
JINMEI Tatuya's avatar
JINMEI Tatuya committed
87
    return (answer);
88 89
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
90
void
91
usage() {
92 93 94 95 96 97 98 99
    cerr << "Usage:  b10-auth [-a address] [-p port] [-4|-6] [-nv]" << endl;
    cerr << "\t-a: specify the address to listen on (default: all) " << endl;
    cerr << "\t-p: specify the port to listen on (default: 5300)" << endl;
    cerr << "\t-4: listen on all IPv4 addresses (incompatible with -a)" << endl;
    cerr << "\t-4: listen on all IPv6 addresses (incompatible with -a)" << endl;
    cerr << "\t-n: do not cache answers in memory" << endl;
    cerr << "\t-u: change process UID to the specified user" << endl;
    cerr << "\t-v: verbose output" << endl;
100
    exit(1);
101
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
102
} // end of anonymous namespace
103 104 105 106

int
main(int argc, char* argv[]) {
    int ch;
107
    const char* port = DNSPORT;
Evan Hunt's avatar
Evan Hunt committed
108
    const char* address = NULL;
109
    const char* uid = NULL;
Evan Hunt's avatar
Evan Hunt committed
110
    bool use_ipv4 = true, use_ipv6 = true, cache = true;
111

112
    while ((ch = getopt(argc, argv, "46a:np:u:v")) != -1) {
113
        switch (ch) {
114
        case '4':
115 116 117 118 119
            // Note that -4 means "ipv4 only", we need to set "use_ipv6" here,
            // not "use_ipv4".  We could use something like "ipv4_only", but
            // we found the negatively named variable could confuse the code
            // logic.
            use_ipv6 = false;
120 121
            break;
        case '6':
122 123
            // The same note as -4 applies.
            use_ipv4 = false;
124
            break;
Evan Hunt's avatar
Evan Hunt committed
125 126 127
        case 'n':
            cache = false;
            break;
Evan Hunt's avatar
Evan Hunt committed
128 129 130
        case 'a':
            address = optarg;
            break;
131
        case 'p':
132
            port = optarg;
133
            break;
134 135 136
        case 'u':
            uid = optarg;
            break;
137 138 139
        case 'v':
            verbose_mode = true;
            break;
140 141 142 143 144 145
        case '?':
        default:
            usage();
        }
    }

146
    if (argc - optind > 0) {
147
        usage();
148 149
    }

150
    if (!use_ipv4 && !use_ipv6) {
151 152
        cerr << "[b10-auth] Error: Cannot specify both -4 and -6 "
             << "at the same time" << endl;
153 154 155
        usage();
    }

156
    if ((!use_ipv4 || !use_ipv6) && address != NULL) {
157 158
        cerr << "[b10-auth] Error: Cannot specify -4 or -6 "
             << "at the same time as -a" << endl;
159 160 161
        usage();
    }

162
    int ret = 0;
163

164
    // XXX: we should eventually pass io_service here.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
165
    Session* cc_session = NULL;
166 167 168
    Session* xfrin_session = NULL;
    bool xfrin_session_established = false; // XXX (see Trac #287)
    ModuleCCSession* config_session = NULL;
169 170 171 172 173 174 175
    string xfrout_socket_path;
    if (getenv("B10_FROM_BUILD") != NULL) {
        xfrout_socket_path = string(getenv("B10_FROM_BUILD")) + "/auth_xfrout_conn";
    } else {
        xfrout_socket_path = UNIX_SOCKET_FILE;
    }
    XfroutClient xfrout_client(xfrout_socket_path);
176
    try {
177
        string specfile;
178 179
        if (getenv("B10_FROM_BUILD")) {
            specfile = string(getenv("B10_FROM_BUILD")) +
180
                "/src/bin/auth/auth.spec";
181
        } else {
182
            specfile = string(AUTH_SPECFILE_LOCATION);
183
        }
184

185
        auth_server = new AuthSrv(cache, xfrout_client);
186
        auth_server->setVerbose(verbose_mode);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
187
        cout << "[b10-auth] Server created." << endl;
188

189
        SimpleCallback* checkin = auth_server->getCheckinProvider();
190 191
        DNSLookup* lookup = auth_server->getDNSLookupProvider();
        DNSAnswer* answer = auth_server->getDNSAnswerProvider();
192

193
        if (address != NULL) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
194 195 196 197 198 199
            // XXX: we can only specify at most one explicit address.
            // This also means the server cannot run in the dual address
            // family mode if explicit addresses need to be specified.
            // We don't bother to fix this problem, however.  The -a option
            // is a short term workaround until we support dynamic listening
            // port allocation.
200
            io_service = new IOService(*port, *address,
201
                                       checkin, lookup, answer);
202
        } else {
203
            io_service = new IOService(*port, use_ipv4, use_ipv6,
204
                                       checkin, lookup, answer);
205
        }
206
        auth_server->setIOService(*io_service);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
207
        cout << "[b10-auth] IOService created." << endl;
208

JINMEI Tatuya's avatar
JINMEI Tatuya committed
209
        cc_session = new Session(io_service->get_io_service());
210
        cout << "[b10-auth] Configuration session channel created." << endl;
211

212 213 214
        config_session = new ModuleCCSession(specfile, *cc_session,
                                             my_config_handler,
                                             my_command_handler);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
215 216
        cout << "[b10-auth] Configuration channel established." << endl;

217 218 219 220
        if (uid != NULL) {
            changeUser(uid);
        }

221 222 223 224 225 226
        xfrin_session = new Session(io_service->get_io_service());
        cout << "[b10-auth] Xfrin session channel created." << endl;
        xfrin_session->establish(NULL);
        xfrin_session_established = true;
        cout << "[b10-auth] Xfrin session channel established." << endl;

227
        // XXX: with the current interface to asiolink we have to create
228
        // auth_server before io_service while Session needs io_service.
229
        // In a next step of refactoring we should make asiolink independent
230
        // from auth_server, and create io_service, auth_server, and
231 232 233
        // sessions in that order.
        auth_server->setXfrinSession(xfrin_session);
        auth_server->setConfigSession(config_session);
234 235
        auth_server->updateConfig(ElementPtr());

236
        cout << "[b10-auth] Server started." << endl;
237
        io_service->run();
238
    } catch (const std::exception& ex) {
239
        cerr << "[b10-auth] Server failed: " << ex.what() << endl;
240
        ret = 1;
241
    }
242

243 244 245 246 247 248
    if (xfrin_session_established) {
        xfrin_session->disconnect();
    }

    delete xfrin_session;
    delete config_session;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
249
    delete cc_session;
250
    delete io_service;
251
    delete auth_server;
252

253
    return (ret);
254
}