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