Fixed HTTP-client query-string handling bug.
[doldaconnect.git] / daemon / auth.c
1 /*
2  *  Dolda Connect - Modular multiuser Direct Connect-style client
3  *  Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
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
30 struct authmech *mechs = NULL;
31
32 static int authless_inithandle(struct authhandle *auth, char *username)
33 {
34     return(0);
35 }
36
37 static void authless_release(struct authhandle *auth)
38 {
39 }
40
41 static int authless_authenticate(struct authhandle *auth, struct socket *sk, char *data)
42 {
43     return(AUTH_SUCCESS);
44 }
45
46 static int authless_succeed_1param(struct authhandle *auth)
47 {
48     return(AUTH_SUCCESS);
49 }
50
51 static 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
62 static 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
74 void authgethandle(struct authhandle *auth)
75 {
76     auth->refcount++;
77 }
78
79 void authputhandle(struct authhandle *auth)
80 {
81     if(--auth->refcount)
82         return;
83     if(auth->mechdata != NULL)
84         auth->mech->release(auth);
85     if(auth->text != NULL)
86         free(auth->text);
87     free(auth);
88 }
89
90 struct 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
115 int authenticate(struct authhandle *handle, struct socket *sk, char *data)
116 {
117     if(handle->mech == NULL)
118         return(AUTH_ERR);
119     return(handle->mech->authenticate(handle, sk, data));
120 }
121
122 int authavailable(struct authmech *mech, struct socket *sk)
123 {
124     if(mech->available == NULL)
125         return(1);
126     return(mech->available(sk));
127 }
128
129 int 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
136 int 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
143 int 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
150 void regmech(struct authmech *mech)
151 {
152     mech->next = mechs;
153     mechs = mech;
154 }
155
156 static void preinit(int hup)
157 {
158     if(hup)
159         return;
160     regmech(&authless);
161 }
162
163 static int init(int hup)
164 {
165     authless.enabled = confgetint("auth", "authless");
166     return(0);
167 }
168
169 static struct configvar myvars[] =
170 {
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. */
174     {CONF_VAR_BOOL, "authless", {.num = 0}},
175     {CONF_VAR_END}
176 };
177
178 static struct module me =
179 {
180     .name = "auth",
181     .conf =
182     {
183         .vars = myvars
184     },
185     .preinit = preinit,
186     .init = init
187 };
188
189 MODULE(me)