lease_cmds.cc 18.5 KB
Newer Older
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
// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <lease_cmds.h>
#include <config/command_mgr.h>
#include <lease_parser.h>
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <asiolink/io_address.h>
#include <lease_cmds_log.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcp/duid.h>
#include <util/encode/hex.h>
#include <util/strutil.h>
#include <exceptions/exceptions.h>
#include <boost/bind.hpp>
#include <string>

using namespace isc::dhcp;
using namespace isc::data;
using namespace isc::config;
using namespace isc::asiolink;
using namespace std;

namespace isc {
namespace lease_cmds {

/// @brief Wrapper class around reservation command handlers.
class LeaseCmdsImpl {
public:
    LeaseCmdsImpl();

    ~LeaseCmdsImpl();

/// @brief Parameters specified for reservation-get and reservation-del
///
/// As both call types (get and delete) need specify which reservation to
/// act on, they have the same set of parameters. In particular, those
/// two call types support the following sets of parameters:
47
48
49
/// - address
/// - subnet-id, identifier-type, identifier-value (v4)
/// - subnet-id, lease-type, iaid, identifier-type, identifier-value (v6)
50
51
52
53
54
55
56
57
58
59
60
///
/// This class stores those parameters and is used to pass them around.
class Parameters {
public:

    /// @brief specifies type of query (by IP addr, by hwaddr, by DUID)
    typedef enum {
        TYPE_ADDR,    ///< query by IP address (either v4 or v6)
        TYPE_HWADDR,  ///< query by hardware address (v4 only)
        TYPE_DUID     ///< query by DUID (v6 only)
    } Type;
61

62
63
64
65
66
67
68
69
70
71
72
73
74
    /// @brief Specifies subnet-id (always used)
    SubnetID subnet_id;

    /// @brief Specifies IPv4 or IPv6 address (used when query_by_addr is true)
    IOAddress addr;

    /// @brief Specifies identifier type (usually FLEX_ID, used when
    ///        query_by_addr is false)
    HWAddrPtr hwaddr;

    /// @brief Specifies identifier value (used when query_by_addr is false)
    isc::dhcp::DuidPtr duid;

75
76
77
78
79
80
81
82
83
84
85
86
87
88
    static Type txtToType(const std::string& txt) {
        if (txt == "address") {
            return (Parameters::TYPE_ADDR);
        } else if (txt == "hw-address") {
            return (Parameters::TYPE_HWADDR);
        } else if (txt == "duid") {
            return (Parameters::TYPE_DUID);
        } else {
            isc_throw(BadValue, "Incorrect identifier type: "
                      << txt << ", the only supported values are: "
                      "address, hw-address, duid");
        }
    }

89
    /// @brief specifies parameter types (true = query by address, false =
Francis Dupont's avatar
Francis Dupont committed
90
    ///         query by identifier-type,identifier)
91
92
    Type query_type;

93
94
95
96
    Lease::Type lease_type;

    uint32_t iaid;

Francis Dupont's avatar
Francis Dupont committed
97
    /// @brief Default constructor.
98
    Parameters()
99
        :addr("::"), query_type(TYPE_ADDR), lease_type(Lease::TYPE_NA), iaid(0) {
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    }
};

private:

    /// @brief Registers commands:
    ///
    /// Registers:
    /// - lease4-add
    /// - lease6-add
    /// - lease4-get
    /// - lease6-get
    /// - lease4-del
    /// - lease6-del
    /// - lease4-update
    /// - lease6-update
    /// - lease4-del-all
    /// - lease6-del-all
118

119
120
121
    /// @throw Unexpected if CommandMgr is not available (should not happen)
    void registerCommands();

Francis Dupont's avatar
Francis Dupont committed
122
    /// @brief Deregisters commands:
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    ///
    /// Deregisters:
    /// - lease4-add
    /// - lease6-add
    /// - lease4-get
    /// - lease6-get
    /// - lease4-del
    /// - lease6-del
    /// - lease4-update
    /// - lease6-update
    /// - lease4-del-all
    /// - lease6-del-all
    ///
    /// @throw Unexpected if CommandMgr is not available (should not happen)
    void deregisterCommands();

139
    /// @brief lease4-add, lease6-add command handler
140
141
142
143
144
    ///
    /// This command attempts to add a lease.
    ///
    /// An example full command looks as follows. Note that the args
    /// parameter is expected to contain the "arguments" portion of it.
145
    /// This function covers both v4 and v6 leases.
146
    ///
147
    /// Example command for v4:
148
149
150
151
152
153
154
    /// {
    ///     "command": "lease4-add",
    ///     "parameters": {
    ///         "address": "192.0.2.1",
    ///         "hwaddr": "00:01:02:03:04:05",
    ///         "client-id": "this-is-a-client",
    ///         "valid-lft": 3600,
155
    ///         "expire": 12345678,
156
    ///         "subnet-id": 1,
Francis Dupont's avatar
Francis Dupont committed
157
    ///         "fqdn-fwd": true,
158
    ///         "fqdn-rev": true,
159
160
161
162
    ///         "hostname": "myhost.example.org",
    ///         "state": 0
    ///     }
    /// }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    /// Example command for v6:
    /// {
    ///     "command": "lease6-add",
    ///     "arguments": {
    ///         "subnet-id": 66,
    ///         "ip-address": "2001:db8:abcd::",
    ///         "type": "IA_PD",
    ///         "prefix-len": 48,
    ///         "duid": "01:02:03:04:05:06:07:08",
    ///         "iaid": 1234,
    ///         "preferred-lft": 500,
    ///         "valid-lft": 1000,
    ///         "expire": 12345678,
    ///         "fqdn-fwd": true,
    ///         "fqdn-rev": true,
    ///         "hostname": "urania.example.org""
    ///     }
    /// }

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
    ///
    /// @param command should be 'reservation-add' (but it's ignored)
    /// @param args must contain host reservation definition.
    /// @return result of the operation
    static ConstElementPtr
    leaseAddHandler(const string& command, ConstElementPtr args);

    /// @brief reservation-get command handler
    ///
    /// This command attempts to retrieve a host that match selected criteria.
    /// Two types of parameters are supported: (subnet-id, address) or
    /// (subnet-id, identifier-type, identifier).
    ///
    /// Example command for query by (subnet-id, address):
    /// {
    ///     "command": "reservation-get",
    ///     "arguments": {
    ///         "subnet-id": 1,
    ///         "ip-address": "192.0.2.202"
    ///     }
    /// }
    ///
    /// Example command for query by (subnet-id, identifier-type, identifier)
    /// {
    ///     "command": "reservation-get",
    ///     "arguments": {
    ///         "subnet-id": 1,
    ///         "identifier-type": "hw-address",
    ///         "identifier": "00:01:02:03:04:05"
    ///     }
    /// }";
    /// @param command should be 'reservation-get' (but it's ignored)
    /// @param args must contain host reservation definition.
    /// @return result of the operation (host will be included as parameters, if found)
    static ConstElementPtr
    leaseGetHandler(const string& command, ConstElementPtr args);

    /// @brief reservation-del command handler
    ///
    /// This command attempts to delete a host that match selected criteria.
    /// Two types of parameters are supported: (subnet-id, address) or
    /// (subnet-id, identifier-type, identifier).
    ///
    /// Note: for this operation to work, hosts-database must be specified
    /// in your configuration file (or from code point of view, alternate_source_
    /// must be set in HostMgr).
    ///
    /// Example command for query by (subnet-id, address):
    /// {
    ///     "command": "reservation-get",
    ///     "arguments": {
    ///         "subnet-id": 1,
    ///         "ip-address": "192.0.2.202"
    ///     }
    /// }
    ///
    /// Example command for query by (subnet-id, identifier-type, identifier)
    /// {
    ///     "command": "reservation-get",
    ///     "arguments": {
    ///         "subnet-id": 1,
    ///         "identifier-type": "hw-address",
    ///         "identifier": "00:01:02:03:04:05"
    ///     }
    /// }";
    /// @param command should be 'reservation-add' (but it's ignored)
    /// @param args must contain host reservation definition.
    /// @return result of the operation (host will be included as parameters, if found)
    static ConstElementPtr
    leaseDelHandler(const string& command, ConstElementPtr args);

253

254
255
256
257
258
    static ConstElementPtr
    leaseUpdateHandler(const string& command, ConstElementPtr args);

    static ConstElementPtr
    leaseWipeHandler(const string& command, ConstElementPtr args);
259

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    /// @brief Extracts parameters required for reservation-get and reservation-del
    ///
    /// See @ref Parameters class for detailed description of what is expected
    /// in the args structure.
    ///
    /// @param args - arguments passed to command
    /// @return parsed parameters
    /// @throw BadValue if input arguments don't make sense.
    static Parameters getParameters(const ConstElementPtr& args);
};

LeaseCmdsImpl::LeaseCmdsImpl() {
    registerCommands();
}

LeaseCmdsImpl::~LeaseCmdsImpl() {
    deregisterCommands();
}

void LeaseCmdsImpl::registerCommands() {
280
281
    /// @todo: Use registration mechanism once #5314 is merged.
    /// See #5321 discussion.
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
    CommandMgr::instance().registerCommand("lease4-add",
        boost::bind(&LeaseCmdsImpl::leaseAddHandler, _1, _2));
    CommandMgr::instance().registerCommand("lease6-add",
        boost::bind(&LeaseCmdsImpl::leaseAddHandler, _1, _2));

    CommandMgr::instance().registerCommand("lease4-get",
        boost::bind(&LeaseCmdsImpl::leaseGetHandler, _1, _2));
    CommandMgr::instance().registerCommand("lease6-get",
        boost::bind(&LeaseCmdsImpl::leaseGetHandler, _1, _2));

    CommandMgr::instance().registerCommand("lease4-del",
    boost::bind(&LeaseCmdsImpl::leaseDelHandler, _1, _2));
    CommandMgr::instance().registerCommand("lease6-del",
    boost::bind(&LeaseCmdsImpl::leaseDelHandler, _1, _2));

    CommandMgr::instance().registerCommand("lease4-update",
    boost::bind(&LeaseCmdsImpl::leaseUpdateHandler, _1, _2));
    CommandMgr::instance().registerCommand("lease6-update",
    boost::bind(&LeaseCmdsImpl::leaseUpdateHandler, _1, _2));

    CommandMgr::instance().registerCommand("lease4-del-all",
    boost::bind(&LeaseCmdsImpl::leaseWipeHandler, _1, _2));
    CommandMgr::instance().registerCommand("lease6-del-all",
    boost::bind(&LeaseCmdsImpl::leaseWipeHandler, _1, _2));
}

void LeaseCmdsImpl::deregisterCommands() {
    /// @todo: Use deregistration mechanism once #5321 discussion is done
    CommandMgr::instance().deregisterCommand("lease4-add");
    CommandMgr::instance().deregisterCommand("lease6-add");

    CommandMgr::instance().deregisterCommand("lease4-get");
    CommandMgr::instance().deregisterCommand("lease6-get");

    CommandMgr::instance().deregisterCommand("lease4-del");
    CommandMgr::instance().deregisterCommand("lease6-del");

    CommandMgr::instance().deregisterCommand("lease4-update");
    CommandMgr::instance().deregisterCommand("lease6-update");

    CommandMgr::instance().deregisterCommand("lease4-del-all");
    CommandMgr::instance().deregisterCommand("lease6-del-all");
}

ConstElementPtr
LeaseCmdsImpl::leaseAddHandler(const std::string& name,
                               ConstElementPtr params) {
    bool v4 = (name == "lease4-add");
330

331
332
333
334
335
336
337
338
339
340
    string txt = "(missing parameters)";
    if (params) {
        txt = params->str();
    }

    try {
        if (!params) {
            isc_throw(isc::BadValue, "no parameters specified for the command");
        }

341
342
        ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();

343
344
345
        Lease4Ptr lease4;
        Lease6Ptr lease6;
        if (v4) {
346
            Lease4Parser parser;
347
            lease4 = parser.parse(config, params);
348

349
            // checkLeaseIntegrity(config, lease4);
350
351

            if (lease4) {
352
                LeaseMgrFactory::instance().addLease(lease4);
353
            }
354

355
        } else {
356
            Lease6Parser parser;
357
            lease6 = parser.parse(config, params);
358

359
            // checkLeaseIntegrity(config, lease6);
360
361

            if (lease6) {
362
                LeaseMgrFactory::instance().addLease(lease6);
363
364
365
366
367
            }
        }


    } catch (const std::exception& ex) {
368
        LOG_ERROR(lease_cmds_logger, v4 ? LEASE_CMDS_ADD4_FAILED : LEASE_CMDS_ADD6_FAILED)
369
370
371
372
            .arg(txt)
            .arg(ex.what());
        return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
    }
Francis Dupont's avatar
Francis Dupont committed
373
374
    LOG_INFO(lease_cmds_logger,
             v4 ? LEASE_CMDS_ADD4 : LEASE_CMDS_ADD6).arg(txt);
375
376
377
378
379
380
381
382
383
384
385
    return (createAnswer(CONTROL_RESULT_SUCCESS, "Lease added."));
}

LeaseCmdsImpl::Parameters
LeaseCmdsImpl::getParameters(const ConstElementPtr& params) {
    Parameters x;

    if (!params || params->getType() != Element::map) {
        isc_throw(BadValue, "Parameters missing or are not a map.");
    }

386
387
    // We support several sets of parameters for leaseX-get/lease-del:
    // lease-get(type, address)
Francis Dupont's avatar
Francis Dupont committed
388
    // lease-get(type, subnet-id, identifier-type, identifier)
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

    if (params->contains("type")) {
        string t = params->get("type")->stringValue();
        if (t == "IA_NA" || t == "0") {
            x.lease_type = Lease::TYPE_NA;
        } else if (t == "IA_TA" || t == "1") {
            x.lease_type = Lease::TYPE_TA;
        } else if (t == "IA_PD" || t == "2") {
            x.lease_type = Lease::TYPE_PD;
        } else if (t == "V4" || t == "3") {
            x.lease_type = Lease::TYPE_V4;
        } else {
            isc_throw(BadValue, "Invalid lease type specified: "
                      << t << ", only supported values are: IA_NA, IA_TA,"
                      << " IA_PD and V4");
        }
405
406
    }

407
    ConstElementPtr tmp = params->get("ip-address");
408
409
    if (tmp) {
        if (tmp->getType() != Element::string) {
410
            isc_throw(BadValue, "'ip-address' is not a string.");
411
412
413
        }

        x.addr = IOAddress(tmp->stringValue());
414
        x.query_type = Parameters::TYPE_ADDR;
415
416
417
        return (x);
    }

418
419
420
421
422
423
424
425
426
427
428
429
430
    tmp = params->get("subnet-id");
    if (!tmp) {
        isc_throw(BadValue, "Mandatory 'subnet-id' parameter missing.");
    }
    if (tmp->getType() != Element::integer) {
        isc_throw(BadValue, "'subnet-id' parameter is not integer.");
    }
    x.subnet_id = tmp->intValue();

    if (params->contains("iaid")) {
        x.iaid = params->get("iaid")->intValue();
    }

431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    // No address specified. Ok, so it must be identifier based query.
    // "identifier-type": "duid",
    // "identifier": "aa:bb:cc:dd:ee:..."

    ConstElementPtr type = params->get("identifier-type");
    ConstElementPtr ident = params->get("identifier");
    if (!type || type->getType() != Element::string) {
        isc_throw(BadValue, "No 'ip-address' provided"
                  " and 'identifier-type' is either missing or not a string.");
    }
    if (!ident || ident->getType() != Element::string) {
        isc_throw(BadValue, "No 'ip-address' provided"
                  " and 'identifier' is either missing or not a string.");
    }

    // Got the parameters. Let's see if their values make sense.
447
448
449
450
451
452
453
454
    // Try to convert identifier-type
    x.query_type = Parameters::txtToType(type->stringValue());

    switch (x.query_type) {
    case Parameters::TYPE_HWADDR: {
        HWAddr hw = HWAddr::fromText(ident->stringValue());
        x.hwaddr = HWAddrPtr(new HWAddr(hw));
        break;
455
    }
456
457
458
    case Parameters::TYPE_DUID: {
        DUID duid = DUID::fromText(ident->stringValue());
        x.duid = DuidPtr(new DUID(duid));
459
        break;
460
461
462
463
464
465
466
467
468
    }
    case Parameters::TYPE_ADDR: {
        // We should never get here. The address clause should have been caught
        // earlier.
        return (x);
    }
    default: {
        isc_throw(BadValue, "Identifier type " << type->stringValue() <<
                  " is not supported.");
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    }
    }
    return (x);
}

ConstElementPtr
LeaseCmdsImpl::leaseGetHandler(const std::string& name, ConstElementPtr params) {
    Parameters p;
    Lease4Ptr lease4;
    Lease6Ptr lease6;
    bool v4 = (name == "lease4-get");
    try {
        p = getParameters(params);

        switch (p.query_type) {
484
        case Parameters::TYPE_ADDR: {
485
486
            // Query by address
            if (v4) {
487
                lease4 = LeaseMgrFactory::instance().getLease4(p.addr);
488
            } else {
489
                lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, p.addr);
490
491
492
            }
            break;
        }
493
        case Parameters::TYPE_HWADDR:
494
            if (v4) {
495
496
497
498
499
500
                if (!p.hwaddr) {
                    return (createAnswer(CONTROL_RESULT_ERROR,
                                         "Program error: Query by hw-address "
                                         "requires hwaddr to be specified"));
                }
                lease4 = LeaseMgrFactory::instance().getLease4(*p.hwaddr, p.subnet_id);
501
502
503
504
            } else {
                return (createAnswer(CONTROL_RESULT_ERROR,
                                     "Query by hw-address is not allowed in v6."));
            }
505
            break;
506
507
508
509
510
511
512
513
514
        case Parameters::TYPE_DUID:
            if (!v4) {
                if (!p.duid) {
                    return (createAnswer(CONTROL_RESULT_ERROR,
                                         "Program error: Query by duid "
                                         "requires duid to be specified"));
                }
                lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, *p.duid,
                                                               p.iaid, p.subnet_id);
515
516
517
518
            } else {
                return (createAnswer(CONTROL_RESULT_ERROR,
                                     "Query by duid is not allowed in v4."));
            }
519
            break;
520
521
522
523
524
        default: {
            stringstream tmp;
            tmp << "Unknown query type: " << static_cast<int>(p.query_type);
            return (createAnswer(CONTROL_RESULT_ERROR, tmp.str()));
        }
525
526
        }
    } catch (const std::exception& ex) {
527
        return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
528
529
530
531
    }

    ElementPtr lease_json;
    if (v4 && lease4) {
532
        lease_json = lease4->toElement();
533
        return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv4 lease found.", lease_json));
534
535
    }
    if (!v4 && lease6) {
536
        lease_json = lease6->toElement();
537
538
        return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv6 lease found.", lease_json));

539
    }
540
541
542

    // If we got here, the lease has not been found.
    return (createAnswer(CONTROL_RESULT_EMPTY, "Lease not found."));
543
544
545
}

ConstElementPtr
546
547
LeaseCmdsImpl::leaseDelHandler(const std::string& cmd, ConstElementPtr args) {
    return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
548
549
}

550
ConstElementPtr
551
LeaseCmdsImpl::leaseUpdateHandler(const string& cmd, ConstElementPtr args) {
552
553
554
555
    return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
}

ConstElementPtr
556
LeaseCmdsImpl::leaseWipeHandler(const string& cmd, ConstElementPtr args) {
557
558
559
    return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
}

560
561
562
563
564
565
566
567
568
569
LeaseCmds::LeaseCmds()
    :impl_(new LeaseCmdsImpl()) {
}

LeaseCmds::~LeaseCmds() {
    impl_.reset();
}

};
};