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
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <radius_request.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <radius_access.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <radius_accounting.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <radius_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::radius;
using namespace std;
namespace ph = std::placeholders;

namespace isc {
namespace radius {

uint32_t getNASPort(uint32_t subnet_id) {
    const map<uint32_t, uint32_t>& remap = RadiusImpl::instance().remap_;
    auto const by_id = remap.find(subnet_id);
    if (by_id != remap.end()) {
        return (by_id->second);
    }
    auto const by_id0 = remap.find(SUBNET_ID_DEFAULT);
    if (by_id0 != remap.end()) {
        return (by_id0->second);
    }
    return (subnet_id);
}

RadiusRequest::RadiusRequest(const MsgCode code,
                             uint32_t subnet_id,
                             const AttributesPtr& send_attrs,
                             bool sync,
                             const Exchange::Handler& handler) {
    AttributesPtr attrs;
    if (send_attrs) {
        attrs.reset(new Attributes(*send_attrs));
    } else {
        attrs.reset(new Attributes());
    }
    nas_port_ = getNASPort(subnet_id);
    if (!attrs->get(PW_NAS_PORT)) {
        attrs->add(Attribute::fromInt(PW_NAS_PORT, nas_port_));
    }
    MessagePtr request(new Message(code, 0, vector<uint8_t>(),
                                   "to-be-set", attrs));
    unsigned maxretries = RadiusImpl::instance().retries_;
    Servers servers;
    if (code == PW_ACCESS_REQUEST) {
        servers = RadiusImpl::instance().auth_->servers_;
    } else {
        servers = RadiusImpl::instance().acct_->servers_;
    }
    if (sync) {
        exchange_.reset(new Exchange(request, maxretries, servers));
    } else {
        exchange_.reset(new Exchange(RadiusImpl::instance().getIOContext(),
                                     request, maxretries, servers,
                                     handler));
    }
}

void
RadiusSyncAuth::start() {
    AttributesPtr send_attrs;
    MessagePtr request = exchange_->getRequest();
    if (request) {
        send_attrs = request->getAttributes();
    }
    LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE, RADIUS_AUTHENTICATION_SYNC)
        .arg(nas_port_)
        .arg(send_attrs ? send_attrs->toText() : "no attributes");

    RadiusRequest::start();

    int result = getRC();
    AttributesPtr recv_attrs = getRespAttrs();
    if (result == OK_RC) {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_AUTHENTICATION_SYNC_ACCEPTED)
            .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
    } else if (result == REJECT_RC) {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_AUTHENTICATION_SYNC_REJECTED)
            .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
    } else {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_AUTHENTICATION_SYNC_FAILED)
            .arg(result)
            .arg(exchangeRCtoText(result));
    }

    if (callback_) {
        try {
            callback_(result, recv_attrs);
        } catch (...) {
        }
    }
    exchange_->shutdown();
}

RadiusAsyncAuth::RadiusAsyncAuth(uint32_t subnet_id,
                                 const AttributesPtr& send_attrs,
                                 const CallbackAuth& callback)
    : RadiusAuth(subnet_id, send_attrs, false,
                 std::bind(&RadiusAsyncAuth::invokeCallback,
                           callback, ph::_1)) {
}

void
RadiusAsyncAuth::start() {
    AttributesPtr send_attrs;
    MessagePtr request = exchange_->getRequest();
    if (request) {
        send_attrs = request->getAttributes();
    }
    LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE, RADIUS_AUTHENTICATION_ASYNC)
        .arg(nas_port_)
        .arg(send_attrs ? send_attrs->toText() : "no attributes");

    RadiusRequest::start();
}

void
RadiusAsyncAuth::invokeCallback(const CallbackAuth& callback,
                                const ExchangePtr exchange) {
    int result = ERROR_RC;
    AttributesPtr recv_attrs;
    if (exchange) {
        result = exchange->getRC();
        MessagePtr response = exchange->getResponse();
        if (response) {
            recv_attrs = response->getAttributes();
        }
    }
    if (result == OK_RC) {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_AUTHENTICATION_ASYNC_ACCEPTED)
            .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
    } else if (result == REJECT_RC) {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_AUTHENTICATION_ASYNC_REJECTED)
            .arg(recv_attrs ? recv_attrs->toText() : "no attributes");
    } else {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_AUTHENTICATION_ASYNC_FAILED)
            .arg(result)
            .arg(exchangeRCtoText(result));
    }

    if (callback) {
        try {
            callback(result, recv_attrs);
        } catch (...) {
        }
    }
    exchange->shutdown();
    RadiusImpl::instance().unregisterExchange(exchange);
}

void
RadiusSyncAcct::start() {
    AttributesPtr send_attrs;
    MessagePtr request = exchange_->getRequest();
    if (request) {
        send_attrs = request->getAttributes();
    }
    LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE, RADIUS_ACCOUNTING_SYNC)
        .arg(nas_port_)
        .arg(send_attrs ? send_attrs->toText() : "no attributes");

    RadiusRequest::start();

    int result = getRC();
    if (result == OK_RC) {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_ACCOUNTING_SYNC_SUCCEED);
    } else {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_ACCOUNTING_SYNC_FAILED)
            .arg(result)
            .arg(exchangeRCtoText(result));
    }

    if (callback_) {
        try {
            callback_(result);
        } catch (...) {
        }
    }
    exchange_->shutdown();
}

RadiusAsyncAcct::RadiusAsyncAcct(uint32_t subnet_id,
                                 const AttributesPtr& send_attrs,
                                 const CallbackAcct& callback)
    : RadiusAcct(subnet_id, send_attrs, false,
                 std::bind(&RadiusAsyncAcct::invokeCallback,
                           callback, ph::_1)) {
}

void
RadiusAsyncAcct::start() {
    AttributesPtr send_attrs;
    MessagePtr request = exchange_->getRequest();
    if (request) {
        send_attrs = request->getAttributes();
    }
    LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE, RADIUS_ACCOUNTING_ASYNC)
        .arg(nas_port_)
        .arg(send_attrs ? send_attrs->toText() : "no attributes");

    RadiusRequest::start();
}

void
RadiusAsyncAcct::invokeCallback(const CallbackAcct& callback,
                                const ExchangePtr exchange) {
    int result = ERROR_RC;
    if (exchange) {
        result = exchange->getRC();
    }
    if (result == OK_RC) {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_ACCOUNTING_ASYNC_SUCCEED);
    } else {
        LOG_DEBUG(radius_logger, RADIUS_DBG_TRACE,
                  RADIUS_ACCOUNTING_ASYNC_FAILED)
            .arg(result)
            .arg(exchangeRCtoText(result));
    }

    if (callback) {
        try {
            callback(result);
        } catch (...) {
        }
    }
    exchange->shutdown();
    RadiusImpl::instance().unregisterExchange(exchange);
}

} // end of namespace radius
} // end of namespace isc