Merge branch 'master' of pc18:/srv/git/r/doldaconnect
[doldaconnect.git] / daemon / auth.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
302a2600 3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
d3372da9 4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19#include <wchar.h>
20#include <errno.h>
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25#include "auth.h"
26#include "utils.h"
27#include "module.h"
28#include "conf.h"
29
30struct authmech *mechs = NULL;
31
32static int authless_inithandle(struct authhandle *auth, char *username)
33{
34 return(0);
35}
36
37static void authless_release(struct authhandle *auth)
38{
39}
40
3616b334 41static int authless_authenticate(struct authhandle *auth, struct socket *sk, char *data)
d3372da9 42{
43 return(AUTH_SUCCESS);
44}
45
46static int authless_succeed_1param(struct authhandle *auth)
47{
48 return(AUTH_SUCCESS);
49}
50
51static struct authmech authless =
52{
53 .name = L"authless",
54 .inithandle = authless_inithandle,
55 .release = authless_release,
56 .authenticate = authless_authenticate,
57 .renewcred = authless_succeed_1param,
58 .opensess = authless_succeed_1param,
59 .closesess = authless_succeed_1param
60};
61
62static struct authhandle *newhandle(void)
63{
64 struct authhandle *auth;
65
66 auth = smalloc(sizeof(*auth));
67 auth->refcount = 1;
68 auth->mech = NULL;
69 auth->text = NULL;
70 auth->mechdata = NULL;
71 return(auth);
72}
73
74void authgethandle(struct authhandle *auth)
75{
76 auth->refcount++;
77}
78
79void authputhandle(struct authhandle *auth)
80{
81 if(--auth->refcount)
82 return;
83 if(auth->text != NULL)
84 free(auth->text);
85 if(auth->mechdata != NULL)
86 auth->mech->release(auth);
87 free(auth);
88}
89
90struct authhandle *initauth(wchar_t *mechname, char *username)
91{
92 struct authmech *mech;
93 struct authhandle *auth;
94
95 for(mech = mechs; mech != NULL; mech = mech->next)
96 {
97 if(mech->enabled && !wcscmp(mechname, mech->name))
98 break;
99 }
100 if(mech == NULL)
101 {
102 errno = ENOENT;
103 return(NULL);
104 }
105 auth = newhandle();
106 auth->mech = mech;
107 if(mech->inithandle(auth, username))
108 {
109 authputhandle(auth);
110 return(NULL);
111 }
112 return(auth);
113}
114
3616b334 115int authenticate(struct authhandle *handle, struct socket *sk, char *data)
d3372da9 116{
117 if(handle->mech == NULL)
118 return(AUTH_ERR);
3616b334 119 return(handle->mech->authenticate(handle, sk, data));
120}
121
122int authavailable(struct authmech *mech, struct socket *sk)
123{
124 if(mech->available == NULL)
125 return(1);
126 return(mech->available(sk));
d3372da9 127}
128
129int authrenewcred(struct authhandle *handle)
130{
131 if((handle->mech == NULL) || (handle->mech->renewcred == NULL))
132 return(AUTH_SUCCESS);
133 return(handle->mech->renewcred(handle));
134}
135
136int authopensess(struct authhandle *handle)
137{
138 if((handle->mech == NULL) || (handle->mech->opensess == NULL))
139 return(AUTH_SUCCESS);
140 return(handle->mech->opensess(handle));
141}
142
143int authclosesess(struct authhandle *handle)
144{
145 if((handle->mech == NULL) || (handle->mech->closesess == NULL))
146 return(AUTH_SUCCESS);
147 return(handle->mech->closesess(handle));
148}
149
150void regmech(struct authmech *mech)
151{
152 mech->next = mechs;
153 mechs = mech;
154}
155
156static void preinit(int hup)
157{
d3372da9 158 if(hup)
159 return;
160 regmech(&authless);
d3372da9 161}
162
163static int init(int hup)
164{
165 authless.enabled = confgetint("auth", "authless");
166 return(0);
167}
168
169static struct configvar myvars[] =
170{
d9f89ef5 171 /** Specifies whether insecure authentication is to be allowed. If
172 * you are not completely sure what you are doing, never turn this
173 * on without also turning on net.onlylocal. */
f5367124 174 {CONF_VAR_BOOL, "authless", {.num = 0}},
d3372da9 175 {CONF_VAR_END}
176};
177
178static struct module me =
179{
180 .name = "auth",
181 .conf =
182 {
183 .vars = myvars
184 },
185 .preinit = preinit,
186 .init = init
187};
188
189MODULE(me)