Massively cleaned up #include's, so they're in a consistent
[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/radiusd.h>
28 #include <freeradius-devel/modules.h>
29
30 #include "valid.h"
31
32 /*
33  *      Define a structure for our module configuration.
34  *
35  *      These variables do not need to be in a structure, but it's
36  *      a lot cleaner to do so, and a pointer to the structure can
37  *      be used as the instance handle.
38  */
39 typedef struct rlm_smb_t {
40         char            *server;
41         char            *backup;
42         char            *domain;
43 } rlm_smb_t;
44
45 /*
46  *      A mapping of configuration file names to internal variables.
47  *
48  *      Note that the string is dynamically allocated, so it MUST
49  *      be freed.  When the configuration file parse re-reads the string,
50  *      it free's the old one, and strdup's the new one, placing the pointer
51  *      to the strdup'd string into 'config.string'.  This gets around
52  *      buffer over-flows.
53  */
54 static const CONF_PARSER module_config[] = {
55   { "server",  PW_TYPE_STRING_PTR, offsetof(rlm_smb_t,server), NULL,  NULL},
56   { "backup",  PW_TYPE_STRING_PTR, offsetof(rlm_smb_t,backup), NULL,  NULL},
57   { "domain",  PW_TYPE_STRING_PTR, offsetof(rlm_smb_t,domain), NULL,  NULL},
58
59   { NULL, -1, 0, NULL, NULL }           /* end the list */
60 };
61
62 /*
63  *      Do any per-module initialization that is separate to each
64  *      configured instance of the module.  e.g. set up connections
65  *      to external databases, read configuration files, set up
66  *      dictionary entries, etc.
67  *
68  *      If configuration information is given in the config section
69  *      that must be referenced in later calls, store a handle to it
70  *      in *instance otherwise put a null pointer there.
71  */
72 static int smb_instantiate(CONF_SECTION *conf, void **instance)
73 {
74         rlm_smb_t *data;
75
76         /*
77          *      Set up a storage area for instance data
78          */
79         data = rad_malloc(sizeof(*data));
80         if (!data) {
81                 return -1;
82         }
83         memset(data, 0, sizeof(*data));
84
85         /*
86          *      If the configuration parameters can't be parsed, then
87          *      fail.
88          */
89         if (cf_section_parse(conf, data, module_config) < 0) {
90                 free(data);
91                 return -1;
92         }
93
94         *instance = data;
95
96         return 0;
97 }
98
99 /*
100  *      Authenticate the user with the given password.
101  */
102 static int smb_authenticate(void *instance, REQUEST *request)
103 {
104         rlm_smb_t *data = (rlm_smb_t *) instance;
105         int rcode;
106
107         /*
108          *      We can only authenticate user requests which HAVE
109          *      a User-Name attribute.
110          */
111         if (!request->username) {
112                 radlog(L_AUTH, "rlm_smb: Attribute \"User-Name\" is required for authentication.");
113                 return RLM_MODULE_INVALID;
114         }
115
116         /*
117          *      We can only authenticate user requests which HAVE
118          *      a User-Password attribute.
119          */
120         if (!request->password) {
121                 radlog(L_AUTH, "rlm_smb: Attribute \"User-Password\" is required for authentication.");
122                 return RLM_MODULE_INVALID;
123         }
124
125         /*
126          *  Ensure that we're being passed a plain-text password,
127          *  and not anything else.
128          */
129         if (request->password->attribute != PW_USER_PASSWORD) {
130                 radlog(L_AUTH, "rlm_smb: Attribute \"User-Password\" is required for authentication.  Cannot use \"%s\".", request->password->name);
131                 return RLM_MODULE_INVALID;
132         }
133
134         /*
135          *  Call the SMB magic to do the work.
136          */
137         rcode = Valid_User(request->username->vp_strvalue,
138                            request->password->vp_strvalue,
139                            data->server, data->backup, data->domain);
140
141         switch (rcode) {
142         case 0:                 /* success */
143           return RLM_MODULE_OK;
144           break;
145
146         case 1:                 /* network failure */
147         case 2:                 /* protocol failure */
148           return RLM_MODULE_FAIL;
149           break;
150
151         case 3:                 /* invalid user name or password */
152           return RLM_MODULE_REJECT;
153         }
154
155         /*
156          *  Something weird happened.  Give up.
157          */
158         return RLM_MODULE_INVALID;
159 }
160
161 static int smb_detach(void *instance)
162 {
163         rlm_smb_t *data = (rlm_smb_t *) instance;
164
165
166         free(instance);
167         return 0;
168 }
169
170 /*
171  *      The module name should be the only globally exported symbol.
172  *      That is, everything else should be 'static'.
173  *
174  *      If the module needs to temporarily modify it's instantiation
175  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
176  *      The server will then take care of ensuring that the module
177  *      is single-threaded.
178  */
179 module_t rlm_smb = {
180         RLM_MODULE_INIT,
181         "SMB",
182         RLM_TYPE_THREAD_UNSAFE,         /* type */
183         smb_instantiate,                /* instantiation */
184         smb_detach,                     /* detach */
185         {
186                 smb_authenticate,       /* authentication */
187                 NULL,                   /* authorization */
188                 NULL,                   /* preaccounting */
189                 NULL,                   /* accounting */
190                 NULL,                   /* checksimul */
191                 NULL,                   /* pre-proxy */
192                 NULL,                   /* post-proxy */
193                 NULL                    /* post-auth */
194         },
195 };