3615228b63e112656bf429b5d46fd3eb8af5fe01
[freeradius.git] / src / modules / rlm_smb / rlm_smb.c
1 /*
2  * rlm_smb.c
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2002,2006  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/autoconf.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <freeradius-devel/radiusd.h>
33 #include <freeradius-devel/modules.h>
34 #include <freeradius-devel/conffile.h>
35
36 #include "valid.h"
37
38 /*
39  *      Define a structure for our module configuration.
40  *
41  *      These variables do not need to be in a structure, but it's
42  *      a lot cleaner to do so, and a pointer to the structure can
43  *      be used as the instance handle.
44  */
45 typedef struct rlm_smb_t {
46         char            *server;
47         char            *backup;
48         char            *domain;
49 } rlm_smb_t;
50
51 /*
52  *      A mapping of configuration file names to internal variables.
53  *
54  *      Note that the string is dynamically allocated, so it MUST
55  *      be freed.  When the configuration file parse re-reads the string,
56  *      it free's the old one, and strdup's the new one, placing the pointer
57  *      to the strdup'd string into 'config.string'.  This gets around
58  *      buffer over-flows.
59  */
60 static const CONF_PARSER module_config[] = {
61   { "server",  PW_TYPE_STRING_PTR, offsetof(rlm_smb_t,server), NULL,  NULL},
62   { "backup",  PW_TYPE_STRING_PTR, offsetof(rlm_smb_t,backup), NULL,  NULL},
63   { "domain",  PW_TYPE_STRING_PTR, offsetof(rlm_smb_t,domain), NULL,  NULL},
64
65   { NULL, -1, 0, NULL, NULL }           /* end the list */
66 };
67
68 /*
69  *      Do any per-module initialization that is separate to each
70  *      configured instance of the module.  e.g. set up connections
71  *      to external databases, read configuration files, set up
72  *      dictionary entries, etc.
73  *
74  *      If configuration information is given in the config section
75  *      that must be referenced in later calls, store a handle to it
76  *      in *instance otherwise put a null pointer there.
77  */
78 static int smb_instantiate(CONF_SECTION *conf, void **instance)
79 {
80         rlm_smb_t *data;
81
82         /*
83          *      Set up a storage area for instance data
84          */
85         data = rad_malloc(sizeof(*data));
86         if (!data) {
87                 return -1;
88         }
89         memset(data, 0, sizeof(*data));
90
91         /*
92          *      If the configuration parameters can't be parsed, then
93          *      fail.
94          */
95         if (cf_section_parse(conf, data, module_config) < 0) {
96                 free(data);
97                 return -1;
98         }
99
100         *instance = data;
101
102         return 0;
103 }
104
105 /*
106  *      Authenticate the user with the given password.
107  */
108 static int smb_authenticate(void *instance, REQUEST *request)
109 {
110         rlm_smb_t *data = (rlm_smb_t *) instance;
111         int rcode;
112
113         /*
114          *      We can only authenticate user requests which HAVE
115          *      a User-Name attribute.
116          */
117         if (!request->username) {
118                 radlog(L_AUTH, "rlm_smb: Attribute \"User-Name\" is required for authentication.");
119                 return RLM_MODULE_INVALID;
120         }
121
122         /*
123          *      We can only authenticate user requests which HAVE
124          *      a User-Password attribute.
125          */
126         if (!request->password) {
127                 radlog(L_AUTH, "rlm_smb: Attribute \"User-Password\" is required for authentication.");
128                 return RLM_MODULE_INVALID;
129         }
130
131         /*
132          *  Ensure that we're being passed a plain-text password,
133          *  and not anything else.
134          */
135         if (request->password->attribute != PW_USER_PASSWORD) {
136                 radlog(L_AUTH, "rlm_smb: Attribute \"User-Password\" is required for authentication.  Cannot use \"%s\".", request->password->name);
137                 return RLM_MODULE_INVALID;
138         }
139
140         /*
141          *  Call the SMB magic to do the work.
142          */
143         rcode = Valid_User(request->username->vp_strvalue,
144                            request->password->vp_strvalue,
145                            data->server, data->backup, data->domain);
146
147         switch (rcode) {
148         case 0:                 /* success */
149           return RLM_MODULE_OK;
150           break;
151
152         case 1:                 /* network failure */
153         case 2:                 /* protocol failure */
154           return RLM_MODULE_FAIL;
155           break;
156
157         case 3:                 /* invalid user name or password */
158           return RLM_MODULE_REJECT;
159         }
160
161         /*
162          *  Something weird happened.  Give up.
163          */
164         return RLM_MODULE_INVALID;
165 }
166
167 static int smb_detach(void *instance)
168 {
169         rlm_smb_t *data = (rlm_smb_t *) instance;
170
171         if (data->server) free(data->server);
172         if (data->backup) free(data->backup);
173         if (data->domain) free(data->domain);
174
175         free(instance);
176         return 0;
177 }
178
179 /*
180  *      The module name should be the only globally exported symbol.
181  *      That is, everything else should be 'static'.
182  *
183  *      If the module needs to temporarily modify it's instantiation
184  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
185  *      The server will then take care of ensuring that the module
186  *      is single-threaded.
187  */
188 module_t rlm_smb = {
189         RLM_MODULE_INIT,
190         "SMB",
191         RLM_TYPE_THREAD_UNSAFE,         /* type */
192         smb_instantiate,                /* instantiation */
193         smb_detach,                     /* detach */
194         {
195                 smb_authenticate,       /* authentication */
196                 NULL,                   /* authorization */
197                 NULL,                   /* preaccounting */
198                 NULL,                   /* accounting */
199                 NULL,                   /* checksimul */
200                 NULL,                   /* pre-proxy */
201                 NULL,                   /* post-proxy */
202                 NULL                    /* post-auth */
203         },
204 };