main.cc 6.64 KB
Newer Older
1
// Copyright (C) 2009-2011  Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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.

15 16
#include <config.h>

17 18 19 20
#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
#include <iostream>

28
#include <exceptions/exceptions.h>
29

30
#include <util/buffer.h>
31

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

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

39 40
#include <xfr/xfrout_client.h>

41 42
#include <auth/spec_config.h>
#include <auth/common.h>
43
#include <auth/auth_config.h>
44
#include <auth/command.h>
JINMEI Tatuya's avatar
JINMEI Tatuya committed
45
#include <auth/change_user.h>
46
#include <auth/auth_srv.h>
47
#include <asiodns/asiodns.h>
48
#include <asiolink/asiolink.h>
49
#include <log/dummylog.h>
50 51

using namespace std;
52 53 54
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
55
using namespace isc::dns;
56
using namespace isc::util;
57
using namespace isc::xfr;
58 59
using namespace isc::asiolink;
using namespace isc::asiodns;
60

61 62
namespace {

63
bool verbose_mode = false;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
64

65 66 67
/* need global var for config/command handlers.
 * todo: turn this around, and put handlers in the authserver
 * class itself? */
68
AuthSrv *auth_server;
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
ConstElementPtr
my_command_handler(const string& command, ConstElementPtr args) {
77 78
    assert(auth_server != NULL);
    return (execAuthServerCommand(*auth_server, command, args));
79 80
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
81
void
82
usage() {
83
    cerr << "Usage:  b10-auth [-u user] [-nv]"
Evan Hunt's avatar
Evan Hunt committed
84
         << endl;
85 86 87
    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;
88
    exit(1);
89
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
90
} // end of anonymous namespace
91 92 93 94

int
main(int argc, char* argv[]) {
    int ch;
95
    const char* uid = NULL;
96
    bool cache = true;
97

98
    while ((ch = getopt(argc, argv, ":nu:v")) != -1) {
99
        switch (ch) {
Evan Hunt's avatar
Evan Hunt committed
100 101 102
        case 'n':
            cache = false;
            break;
103 104 105
        case 'u':
            uid = optarg;
            break;
106 107
        case 'v':
            verbose_mode = true;
108
            isc::log::denabled = true;
109
            break;
110 111 112 113 114 115
        case '?':
        default:
            usage();
        }
    }

116
    if (argc - optind > 0) {
117
        usage();
118 119 120
    }

    int ret = 0;
121

122
    // XXX: we should eventually pass io_service here.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
123
    Session* cc_session = NULL;
124
    Session* xfrin_session = NULL;
125
    Session* statistics_session = NULL;
126
    bool xfrin_session_established = false; // XXX (see Trac #287)
127
    bool statistics_session_established = false; // XXX (see Trac #287)
128
    ModuleCCSession* config_session = NULL;
129
    XfroutClient xfrout_client(getXfroutSocketPath());
130
    try {
131
        string specfile;
132 133
        if (getenv("B10_FROM_BUILD")) {
            specfile = string(getenv("B10_FROM_BUILD")) +
134
                "/src/bin/auth/auth.spec";
135
        } else {
136
            specfile = string(AUTH_SPECFILE_LOCATION);
137
        }
138

139
        auth_server = new AuthSrv(cache, xfrout_client);
140
        auth_server->setVerbose(verbose_mode);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
141
        cout << "[b10-auth] Server created." << endl;
142

143
        SimpleCallback* checkin = auth_server->getCheckinProvider();
144
        IOService& io_service = auth_server->getIOService();
145 146
        DNSLookup* lookup = auth_server->getDNSLookupProvider();
        DNSAnswer* answer = auth_server->getDNSAnswerProvider();
147

148 149
        DNSService dns_service(io_service, checkin, lookup, answer);
        auth_server->setDNSService(dns_service);
150
        cout << "[b10-auth] DNSServices created." << endl;
151

152
        cc_session = new Session(io_service.get_io_service());
153
        cout << "[b10-auth] Configuration session channel created." << endl;
154

155 156 157
        config_session = new ModuleCCSession(specfile, *cc_session,
                                             my_config_handler,
                                             my_command_handler);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
158 159
        cout << "[b10-auth] Configuration channel established." << endl;

160
        xfrin_session = new Session(io_service.get_io_service());
161 162 163 164 165
        cout << "[b10-auth] Xfrin session channel created." << endl;
        xfrin_session->establish(NULL);
        xfrin_session_established = true;
        cout << "[b10-auth] Xfrin session channel established." << endl;

166
        statistics_session = new Session(io_service.get_io_service());
167 168 169 170
        cout << "[b10-auth] Statistics session channel created." << endl;
        statistics_session->establish(NULL);
        statistics_session_established = true;
        cout << "[b10-auth] Statistics session channel established." << endl;
171

172
        auth_server->setXfrinSession(xfrin_session);
173
        auth_server->setStatisticsSession(statistics_session);
174 175 176 177 178

        // Configure the server.  configureAuthServer() is expected to install
        // all initial configurations, but as a short term workaround we
        // handle the traditional "database_file" setup by directly calling
        // updateConfig().
179 180
        // if server load configure failed, we won't exit, give user second chance
        // to correct the configure.
181
        auth_server->setConfigSession(config_session);
182 183 184
        try {
            configureAuthServer(*auth_server, config_session->getFullConfig());
            auth_server->updateConfig(ElementPtr());
185
        } catch (const AuthConfigError& ex) {
186 187
            cout << "[bin10-auth] Server load config failed:" << ex.what() << endl;
        }
188

189 190 191 192
        if (uid != NULL) {
            changeUser(uid);
        }

193
        cout << "[b10-auth] Server started." << endl;
194
        io_service.run();
Jelte Jansen's avatar
Jelte Jansen committed
195

196
    } catch (const std::exception& ex) {
197
        cerr << "[b10-auth] Server failed: " << ex.what() << endl;
198
        ret = 1;
199
    }
200

201 202
    if (statistics_session_established) {
        statistics_session->disconnect();
203 204
    }

205 206 207 208
    if (xfrin_session_established) {
        xfrin_session->disconnect();
    }

209
    delete statistics_session;
210 211
    delete xfrin_session;
    delete config_session;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
212
    delete cc_session;
213
    delete auth_server;
214

215
    return (ret);
216
}