]> git.stg.codes - stg.git/blob - rlm_stg/iface.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / rlm_stg / iface.cpp
1 #include "iface.h"
2
3 #include "stg_client.h"
4 #include "types.h"
5 #include "radlog.h"
6
7 #include <stdexcept>
8 #include <cstring>
9
10 #include <strings.h>
11
12 namespace RLM = STG::RLM;
13
14 using RLM::Client;
15 using RLM::PAIRS;
16 using RLM::RESULT;
17 using RLM::REQUEST_TYPE;
18
19 namespace
20 {
21
22 STG_PAIR* toSTGPairs(const PAIRS& source)
23 {
24     STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
25     for (size_t pos = 0; pos < source.size(); ++pos) {
26         bzero(pairs[pos].key, sizeof(pairs[pos].key));
27         bzero(pairs[pos].value, sizeof(pairs[pos].value));
28         strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(pairs[pos].key));
29         strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(pairs[pos].value));
30     }
31     bzero(pairs[source.size()].key, sizeof(pairs[source.size()].key));
32     bzero(pairs[source.size()].value, sizeof(pairs[source.size()].value));
33
34     return pairs;
35 }
36
37 PAIRS fromSTGPairs(const STG_PAIR* pairs)
38 {
39     const STG_PAIR* pair = pairs;
40     PAIRS res;
41
42     while (!emptyPair(pair)) {
43         res.push_back(std::pair<std::string, std::string>(pair->key, pair->value));
44         ++pair;
45     }
46
47     return res;
48 }
49
50 STG_RESULT toResult(const RESULT& source)
51 {
52     STG_RESULT result;
53     result.modify = toSTGPairs(source.modify);
54     result.reply = toSTGPairs(source.reply);
55     result.returnCode = source.returnCode;
56     return result;
57 }
58
59 STG_RESULT emptyResult()
60 {
61     STG_RESULT result = {NULL, NULL, STG_REJECT};
62     return result;
63 }
64
65 std::string toString(const char* value)
66 {
67     if (value == NULL)
68         return "";
69     else
70         return value;
71 }
72
73 STG_RESULT stgRequest(REQUEST_TYPE type, const char* userName, const char* password, const STG_PAIR* pairs)
74 {
75     Client* client = Client::get();
76     if (client == NULL) {
77         RadLog("Client is not configured.");
78         return emptyResult();
79     }
80     try {
81         return toResult(client->request(type, toString(userName), toString(password), fromSTGPairs(pairs)));
82     } catch (const std::runtime_error& ex) {
83         RadLog("Error: '%s'.", ex.what());
84         return emptyResult();
85     }
86 }
87
88 }
89
90 int stgInstantiateImpl(const char* address)
91 {
92     if (Client::configure(toString(address)))
93         return 1;
94
95     return 0;
96 }
97
98 STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs)
99 {
100     return stgRequest(RLM::AUTHORIZE, userName, password, pairs);
101 }
102
103 STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs)
104 {
105     return stgRequest(RLM::AUTHENTICATE, userName, password, pairs);
106 }
107
108 STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs)
109 {
110     return stgRequest(RLM::POST_AUTH, userName, password, pairs);
111 }
112
113 STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs)
114 {
115     return stgRequest(RLM::PRE_ACCT, userName, password, pairs);
116 }
117
118 STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs)
119 {
120     return stgRequest(RLM::ACCOUNT, userName, password, pairs);
121 }