main.cc 7.18 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 46
#include <auth/auth_srv.h>
#include <auth/asio_link.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

55 56
namespace {

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

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

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

67
asio_link::IOService* io_service;
68

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

JINMEI Tatuya's avatar
JINMEI Tatuya committed
74
ElementPtr
75
my_command_handler(const string& command, const ElementPtr args) {
76
    ElementPtr answer = createAnswer();
77

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

JINMEI Tatuya's avatar
JINMEI Tatuya committed
89
void
90
usage() {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
91
    cerr << "Usage: b10-auth [-a address] [-p port] [-4|-6] [-nv]" << endl;
92
    exit(1);
93
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
94
} // end of anonymous namespace
95 96 97 98

int
main(int argc, char* argv[]) {
    int ch;
99
    const char* port = DNSPORT;
Evan Hunt's avatar
Evan Hunt committed
100
    const char* address = NULL;
101
    const char* uid = NULL;
Evan Hunt's avatar
Evan Hunt committed
102
    bool use_ipv4 = true, use_ipv6 = true, cache = true;
103

104
    while ((ch = getopt(argc, argv, "46a:np:u:v")) != -1) {
105
        switch (ch) {
106
        case '4':
107 108 109 110 111
            // 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;
112 113
            break;
        case '6':
114 115
            // The same note as -4 applies.
            use_ipv4 = false;
116
            break;
Evan Hunt's avatar
Evan Hunt committed
117 118 119
        case 'n':
            cache = false;
            break;
Evan Hunt's avatar
Evan Hunt committed
120 121 122
        case 'a':
            address = optarg;
            break;
123
        case 'p':
124
            port = optarg;
125
            break;
126 127 128
        case 'u':
            uid = optarg;
            break;
129 130 131
        case 'v':
            verbose_mode = true;
            break;
132 133 134 135 136 137
        case '?':
        default:
            usage();
        }
    }

138
    if (argc - optind > 0) {
139
        usage();
140 141
    }

142
    if (!use_ipv4 && !use_ipv6) {
143
        cerr << "[b10-auth] Error: -4 and -6 can't coexist" << endl;
144 145 146
        usage();
    }

147 148 149 150 151
    if ((!use_ipv4 || !use_ipv6) && address != NULL) {
        cerr << "[b10-auth] Error: -4|-6 and -a can't coexist" << endl;
        usage();
    }

152
    int ret = 0;
153

154
    // XXX: we should eventually pass io_service here.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
155
    Session* cc_session = NULL;
156 157 158
    Session* xfrin_session = NULL;
    bool xfrin_session_established = false; // XXX (see Trac #287)
    ModuleCCSession* config_session = NULL;
159 160 161 162 163 164 165
    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);
166
    try {
167
        string specfile;
168 169
        if (getenv("B10_FROM_BUILD")) {
            specfile = string(getenv("B10_FROM_BUILD")) +
170
                "/src/bin/auth/auth.spec";
171
        } else {
172
            specfile = string(AUTH_SPECFILE_LOCATION);
173
        }
174

175
        auth_server = new AuthSrv(cache, xfrout_client);
176
        auth_server->setVerbose(verbose_mode);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
177
        cout << "[b10-auth] Server created." << endl;
178

179
        if (address != NULL) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
180 181 182 183 184 185
            // 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.
186 187 188 189 190 191
            io_service = new asio_link::IOService(auth_server, *port,
                                                  *address);
        } else {
            io_service = new asio_link::IOService(auth_server, *port,
                                                  use_ipv4, use_ipv6);
        }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
192
        cout << "[b10-auth] IOService created." << endl;
193

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

197 198 199
        config_session = new ModuleCCSession(specfile, *cc_session,
                                             my_config_handler,
                                             my_command_handler);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
200 201
        cout << "[b10-auth] Configuration channel established." << endl;

202 203 204 205
        if (uid != NULL) {
            changeUser(uid);
        }

206 207 208 209 210 211
        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;

212 213 214 215
        // XXX: with the current interface to asio_link we have to create
        // auth_server before io_service while Session needs io_service.
        // In a next step of refactoring we should make asio_link independent
        // from auth_server, and create io_service, auth_server, and
216 217 218
        // sessions in that order.
        auth_server->setXfrinSession(xfrin_session);
        auth_server->setConfigSession(config_session);
219 220
        auth_server->updateConfig(ElementPtr());

221
        cout << "[b10-auth] Server started." << endl;
222
        io_service->run();
223
    } catch (const std::exception& ex) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
224
        cerr << "[b10-auth] Initialization failed: " << ex.what() << endl;
225
        ret = 1;
226
    }
227

228 229 230 231 232 233
    if (xfrin_session_established) {
        xfrin_session->disconnect();
    }

    delete xfrin_session;
    delete config_session;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
234
    delete cc_session;
235
    delete io_service;
236
    delete auth_server;
237

238
    return (ret);
239
}