All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rlm_test.c
Go to the documentation of this file.
1 /*
2  * This program is is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or (at
5  * your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: 0cceba3d9a53e401b3df5f5246dfcb2d7892d30e $
19  * @file rlm_test.c
20  * @brief test module code.
21  *
22  * @copyright 2013 The FreeRADIUS server project
23  * @copyright 2013 your name <your address>
24  */
25 RCSID("$Id: 0cceba3d9a53e401b3df5f5246dfcb2d7892d30e $")
26 
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 
31 /*
32  * Define a structure for our module configuration.
33  *
34  * These variables do not need to be in a structure, but it's
35  * a lot cleaner to do so, and a pointer to the structure can
36  * be used as the instance handle.
37  */
38 typedef struct rlm_test_t {
39  bool boolean;
40  uint32_t value;
41  char const *string;
43 } rlm_test_t;
44 
45 /*
46  * A mapping of configuration file names to internal variables.
47  */
48 static const CONF_PARSER module_config[] = {
49  { FR_CONF_OFFSET("integer", PW_TYPE_INTEGER, rlm_test_t, value), .dflt = "1" },
50  { FR_CONF_OFFSET("boolean", PW_TYPE_BOOLEAN, rlm_test_t, boolean), .dflt = "no" },
51  { FR_CONF_OFFSET("string", PW_TYPE_STRING, rlm_test_t, string) },
52  { FR_CONF_OFFSET("ipaddr", PW_TYPE_IPV4_ADDR, rlm_test_t, ipaddr), .dflt = "*" },
54 };
55 
56 static int rlm_test_cmp(UNUSED void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check,
57  UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
58 {
59  rad_assert(check->da->type == PW_TYPE_STRING);
60 
61  RINFO("test-Paircmp called with \"%s\"", check->vp_strvalue);
62 
63  if (strcmp(check->vp_strvalue, "yes") == 0) return 0;
64  return 1;
65 }
66 
67 /*
68  * Do any per-module initialization that is separate to each
69  * configured instance of the module. e.g. set up connections
70  * to external databases, read configuration files, set up
71  * dictionary entries, etc.
72  *
73  * If configuration information is given in the config section
74  * that must be referenced in later calls, store a handle to it
75  * in *instance otherwise put a null pointer there.
76  */
77 static int mod_instantiate(UNUSED CONF_SECTION *conf, void *instance)
78 {
79  rlm_test_t *inst = instance;
80 
81  paircompare_register_byname("test-Paircmp", fr_dict_attr_by_num(NULL, 0, PW_USER_NAME), false,
82  rlm_test_cmp, inst);
83 
84  /*
85  * Log some messages
86  */
87  INFO("rlm_test: Informational message");
88  WARN("rlm_test: Warning message");
89  ERROR("rlm_test: Error message");
90  DEBUG("rlm_test: Debug message");
91  DEBUG2("rlm_test: Debug2 message");
92  DEBUG3("rlm_test: Debug3 message");
93  DEBUG4("rlm_test: Debug4 message");
94  AUTH("rlm_test: Auth message");
95  ACCT("rlm_test: Acct message");
96  PROXY("rlm_test: Proxy message");
97 
98  return 0;
99 }
100 
101 /*
102  * Find the named user in this modules database. Create the set
103  * of attribute-value pairs to check and reply with for this user
104  * from the database. The authentication code only needs to check
105  * the password, the rest is done here.
106  */
107 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(UNUSED void *instance, REQUEST *request)
108 {
109  RINFO("RINFO message");
110  RDEBUG("RDEBUG message");
111  RDEBUG2("RDEBUG2 message");
112 
113  RWARN("RWARN message");
114  RWDEBUG("RWDEBUG message");
115  RWDEBUG("RWDEBUG2 message");
116 
117  RAUTH("RAUTH message");
118  RACCT("RACCT message");
119  RPROXY("RPROXY message");
120 
121  /*
122  * Should appear wavy
123  */
124  RERROR("RERROR error message");
125  RINDENT();
126  REDEBUG("RDEBUG error message");
127  REXDENT();
128  REDEBUG2("RDEBUG2 error message");
129  RINDENT();
130  REDEBUG3("RDEBUG3 error message");
131  REXDENT();
132  REDEBUG4("RDEBUG4 error message");
133 
134  return RLM_MODULE_OK;
135 }
136 
137 /*
138  * Authenticate the user with the given password.
139  */
140 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(UNUSED void *instance, UNUSED REQUEST *request)
141 {
142  return RLM_MODULE_OK;
143 }
144 
145 #ifdef WITH_ACCOUNTING
146 /*
147  * Massage the request before recording it or proxying it
148  */
149 static rlm_rcode_t CC_HINT(nonnull) mod_preacct(UNUSED void *instance, UNUSED REQUEST *request)
150 {
151  return RLM_MODULE_OK;
152 }
153 
154 /*
155  * Write accounting information to this modules database.
156  */
157 static rlm_rcode_t CC_HINT(nonnull) mod_accounting(UNUSED void *instance, UNUSED REQUEST *request)
158 {
159  return RLM_MODULE_OK;
160 }
161 
162 /*
163  * See if a user is already logged in. Sets request->simul_count to the
164  * current session count for this user and sets request->simul_mpp to 2
165  * if it looks like a multilink attempt based on the requested IP
166  * address, otherwise leaves request->simul_mpp alone.
167  *
168  * Check twice. If on the first pass the user exceeds his
169  * max. number of logins, do a second pass and validate all
170  * logins by querying the terminal server (using eg. SNMP).
171  */
172 static rlm_rcode_t CC_HINT(nonnull) mod_checksimul(UNUSED void *instance, REQUEST *request)
173 {
174  request->simul_count=0;
175 
176  return RLM_MODULE_OK;
177 }
178 #endif
179 
180 
181 /*
182  * Only free memory we allocated. The strings allocated via
183  * cf_section_parse() do not need to be freed.
184  */
185 static int mod_detach(UNUSED void *instance)
186 {
187  /* free things here */
188  return 0;
189 }
190 
191 /*
192  * The module name should be the only globally exported symbol.
193  * That is, everything else should be 'static'.
194  *
195  * If the module needs to temporarily modify it's instantiation
196  * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
197  * The server will then take care of ensuring that the module
198  * is single-threaded.
199  */
200 extern module_t rlm_test;
201 module_t rlm_test = {
203  .name = "test",
204  .type = RLM_TYPE_THREAD_SAFE,
205  .inst_size = sizeof(rlm_test_t),
206  .config = module_config,
207  .instantiate = mod_instantiate,
208  .detach = mod_detach,
209  .methods = {
212 #ifdef WITH_ACCOUNTING
213  [MOD_PREACCT] = mod_preacct,
216 #endif
217  },
218 };
#define REDEBUG4(fmt,...)
Definition: log.h:257
module_t rlm_test
Definition: rlm_test.c:201
#define RINDENT()
Indent R* messages by one level.
Definition: log.h:265
#define RACCT(fmt,...)
Definition: log.h:203
#define DEBUG3(fmt,...)
Definition: log.h:177
#define RERROR(fmt,...)
Definition: log.h:207
#define AUTH(fmt,...)
Definition: log.h:139
static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
Write accounting data to Couchbase documents.
static int rlm_test_cmp(UNUSED void *instance, REQUEST *request, UNUSED VALUE_PAIR *thing, VALUE_PAIR *check, UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
Definition: rlm_test.c:56
The module is OK, continue.
Definition: radiusd.h:91
Metadata exported by the module.
Definition: modules.h:134
static const CONF_PARSER module_config[]
Definition: rlm_test.c:48
#define RWARN(fmt,...)
Definition: log.h:206
#define INFO(fmt,...)
Definition: log.h:143
static rlm_rcode_t CC_HINT(nonnull)
Definition: rlm_test.c:107
static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
Handle authorization requests using Couchbase document data.
#define RLM_TYPE_THREAD_SAFE
Module is threadsafe.
Definition: modules.h:75
#define UNUSED
Definition: libradius.h:134
#define RLM_MODULE_INIT
Definition: modules.h:86
#define REDEBUG2(fmt,...)
Definition: log.h:255
#define CONF_PARSER_TERMINATOR
Definition: conffile.h:289
#define inst
bool boolean
Definition: rlm_test.c:39
fr_ipaddr_t ipaddr
Definition: rlm_test.c:42
static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request) CC_HINT(nonnull)
Defines a CONF_PAIR to C data type mapping.
Definition: conffile.h:267
#define rad_assert(expr)
Definition: rad_assert.h:38
#define PROXY(fmt,...)
Definition: log.h:141
#define DEBUG(fmt,...)
Definition: log.h:175
#define DEBUG2(fmt,...)
Definition: log.h:176
4 methods index for checksimul section.
Definition: modules.h:45
#define RPROXY(fmt,...)
Definition: log.h:204
3 methods index for accounting section.
Definition: modules.h:44
Stores an attribute, a value and various bits of other data.
Definition: pair.h:112
static rlm_rcode_t mod_checksimul(void *instance, REQUEST *request)
Check if a given user is already logged in.
0 methods index for authenticate section.
Definition: modules.h:41
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition: log.h:272
A truth value.
Definition: radius.h:56
32 Bit unsigned integer.
Definition: radius.h:34
enum rlm_rcodes rlm_rcode_t
Return codes indicating the result of the module call.
static rs_t * conf
Definition: radsniff.c:46
#define DEBUG4(fmt,...)
Definition: log.h:178
#define RDEBUG2(fmt,...)
Definition: log.h:244
uint64_t magic
Used to validate module struct.
Definition: modules.h:135
#define REDEBUG3(fmt,...)
Definition: log.h:256
#define ACCT(fmt,...)
Definition: log.h:140
#define FR_CONF_OFFSET(_n, _t, _s, _f)
Definition: conffile.h:168
int paircompare_register_byname(char const *name, fr_dict_attr_t const *from, bool first_only, RAD_COMPARE_FUNC func, void *instance)
Register a function as compare function.
Definition: pair.c:351
#define RAUTH(fmt,...)
Definition: log.h:202
#define WARN(fmt,...)
Definition: log.h:144
#define REDEBUG(fmt,...)
Definition: log.h:254
2 methods index for preacct section.
Definition: modules.h:43
#define RINFO(fmt,...)
Definition: log.h:205
IPv4/6 prefix.
Definition: inet.h:41
fr_dict_attr_t const * da
Dictionary attribute defines the attribute.
Definition: pair.h:113
struct rlm_test_t rlm_test_t
fr_dict_attr_t const * fr_dict_attr_by_num(fr_dict_t *dict, unsigned int vendor, unsigned int attr)
Lookup a fr_dict_attr_t by its vendor and attribute numbers.
Definition: dict.c:3519
String of printable characters.
Definition: radius.h:33
#define RWDEBUG(fmt,...)
Definition: log.h:251
PW_TYPE type
Value type.
Definition: dict.h:80
1 methods index for authorize section.
Definition: modules.h:42
#define RCSID(id)
Definition: build.h:135
char const * string
Definition: rlm_test.c:41
static int mod_detach(UNUSED void *instance)
Definition: rlm_test.c:185
32 Bit IPv4 Address.
Definition: radius.h:35
#define RDEBUG(fmt,...)
Definition: log.h:243
#define ERROR(fmt,...)
Definition: log.h:145
static int mod_instantiate(UNUSED CONF_SECTION *conf, void *instance)
Definition: rlm_test.c:77
uint32_t value
Definition: rlm_test.c:40