source: trunk/gsdl/src/recpt/authenaction.cpp@ 1129

Last change on this file since 1129 was 1129, checked in by sjboddie, 24 years ago

tidied up status pages and end-user collection building

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/**********************************************************************
2 *
3 * authenaction.cpp -- authenticating users
4 * Copyright (C) 1999 DigiLib Systems Limited, New Zealand
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * $Id: authenaction.cpp 1129 2000-04-19 22:30:25Z sjboddie $
25 *
26 *********************************************************************/
27
28/*
29 $Log$
30 Revision 1.10 2000/04/19 22:30:23 sjboddie
31 tidied up status pages and end-user collection building
32
33 Revision 1.9 1999/11/01 21:11:35 sjboddie
34 changed arguments passed to many functions
35
36 Revision 1.8 1999/10/19 03:23:41 davidb
37 Collection building support through web pages
38 and internal and external link handling for collection documents
39
40 Revision 1.7 1999/09/07 23:04:29 rjmcnab
41 removed some compiler warnings
42
43 Revision 1.6 1999/09/07 04:56:52 sjboddie
44 added GPL notice
45
46 Revision 1.5 1999/09/02 00:23:24 rjmcnab
47 a couple of minor things
48
49 Revision 1.4 1999/07/30 02:24:43 sjboddie
50 added collectinfo argument to some functions
51
52 Revision 1.3 1999/07/13 23:23:26 rjmcnab
53 Put users in their own gdbm database. Moved a lot of functionality to usersdb
54
55 Revision 1.2 1999/07/11 10:47:32 rjmcnab
56 Got something basic working.
57
58 Revision 1.1 1999/07/10 22:19:29 rjmcnab
59 Initial revision.
60
61
62 */
63
64
65#include "authenaction.h"
66#include "fileutil.h"
67#include "cfgread.h"
68#include "cgiutils.h"
69#include "infodbclass.h"
70#include "gsdltimes.h"
71#include "userdb.h"
72
73
74///////////////
75// authenaction
76///////////////
77
78authenaction::authenaction () {
79 keydecay = 600; // 10 minutes
80 recpt = NULL;
81
82 // this action uses cgi variable "a"
83 cgiarginfo arg_ainfo;
84 arg_ainfo.shortname = "a";
85 arg_ainfo.longname = "action";
86 arg_ainfo.multiplechar = true;
87 arg_ainfo.defaultstatus = cgiarginfo::weak;
88 arg_ainfo.argdefault = "a";
89 arg_ainfo.savedarginfo = cgiarginfo::must;
90 argsinfo.addarginfo (NULL, arg_ainfo);
91
92 // "us"
93 arg_ainfo.shortname = "us";
94 arg_ainfo.longname = "user account status";
95 arg_ainfo.multiplechar = true;
96 arg_ainfo.defaultstatus = cgiarginfo::weak;
97 arg_ainfo.argdefault = "invalid";
98 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
99 argsinfo.addarginfo (NULL, arg_ainfo);
100
101 // "ug"
102 arg_ainfo.shortname = "ug";
103 arg_ainfo.longname = "user groups"; // comma seperated list
104 arg_ainfo.multiplechar = true;
105 arg_ainfo.defaultstatus = cgiarginfo::weak;
106 arg_ainfo.argdefault = "";
107 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
108 argsinfo.addarginfo (NULL, arg_ainfo);
109
110 // "un"
111 arg_ainfo.shortname = "un";
112 arg_ainfo.longname = "user name";
113 arg_ainfo.multiplechar = true;
114 arg_ainfo.defaultstatus = cgiarginfo::weak;
115 arg_ainfo.argdefault = "";
116 arg_ainfo.savedarginfo = cgiarginfo::must;
117 argsinfo.addarginfo (NULL, arg_ainfo);
118
119 // "pw"
120 arg_ainfo.shortname = "pw";
121 arg_ainfo.longname = "password";
122 arg_ainfo.multiplechar = true;
123 arg_ainfo.defaultstatus = cgiarginfo::weak;
124 arg_ainfo.argdefault = "";
125 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
126 argsinfo.addarginfo (NULL, arg_ainfo);
127
128 // "ky" - gives a specific user authentication for a
129 // limited amount of time
130 arg_ainfo.shortname = "ky";
131 arg_ainfo.longname = "user time key";
132 arg_ainfo.multiplechar = true;
133 arg_ainfo.defaultstatus = cgiarginfo::weak;
134 arg_ainfo.argdefault = "";
135 arg_ainfo.savedarginfo = cgiarginfo::must;
136 argsinfo.addarginfo (NULL, arg_ainfo);
137
138 // "ua" - ""=no, "1"=yes
139 arg_ainfo.shortname = "ua";
140 arg_ainfo.longname = "whether a user has been authenticated";
141 arg_ainfo.multiplechar = true;
142 arg_ainfo.defaultstatus = cgiarginfo::weak;
143 arg_ainfo.argdefault = "";
144 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
145 argsinfo.addarginfo (NULL, arg_ainfo);
146
147 // "er" - compressed arguments for the referer page
148 arg_ainfo.shortname = "er";
149 arg_ainfo.longname = "the compressed args of the refer page";
150 arg_ainfo.multiplechar = true;
151 arg_ainfo.defaultstatus = cgiarginfo::weak;
152 arg_ainfo.argdefault = "";
153 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
154 argsinfo.addarginfo (NULL, arg_ainfo);
155
156 // "uan" - whether user authentication is needed
157 arg_ainfo.shortname = "uan";
158 arg_ainfo.longname = "whether user authentication is needed";
159 arg_ainfo.multiplechar = true;
160 arg_ainfo.defaultstatus = cgiarginfo::weak;
161 arg_ainfo.argdefault = "";
162 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
163 argsinfo.addarginfo (NULL, arg_ainfo);
164}
165
166void authenaction::configure (const text_t &key, const text_tarray &cfgline) {
167 // get the password filename
168 if (cfgline.size() == 1) {
169 if (key == "usersfile") usersfile = cfgline[0];
170 else if (key == "keyfile") keyfile = cfgline[0];
171 else if (key == "keydecay") keydecay = cfgline[0].getint();
172 else if (key == "gsdlhome") {
173 if (usersfile.empty())
174 usersfile = filename_cat (cfgline[0], "etc", "users.db");
175 if (keyfile.empty())
176 keyfile = filename_cat (cfgline[0], "etc", "key.db");
177 }
178 }
179
180 action::configure (key, cfgline);
181}
182
183bool authenaction::init (ostream &logout) {
184 return action::init (logout);
185}
186
187bool authenaction::check_cgiargs (cgiargsinfoclass &/*argsinfo*/, cgiargsclass &/*args*/,
188 ostream &/*logout*/) {
189 return true;
190}
191
192// returns false if there is a major problem with the cgi arguments -- not
193// if authentication fails. If the authentication fails "un" will be empty
194bool authenaction::check_external_cgiargs (cgiargsinfoclass &argsinfo,
195 cgiargsclass &args,
196 outconvertclass &outconvert,
197 const text_t &saveconf,
198 ostream &logout) {
199 // failure means we have to redirect to this action to get authentication
200 // (if we are not already doing this)
201
202 userinfo_t thisuser;
203
204 text_t &args_uan = args["uan"]; text_t &args_un = args["un"];
205 text_t &args_pw = args["pw"]; text_t &args_us = args["us"];
206 text_t &args_ug = args["ug"]; text_t &args_ky = args["ky"];
207 text_t &args_ua = args["ua"]; text_t &args_a = args["a"];
208
209 // we must have a username and a password or a key to
210 // do any authentication
211 args_ua.clear(); // default = false;
212 if (args_un.empty() || args_pw.empty()) args_us = "invalid";
213 else args_us = "failed";
214
215 // make sure we have a username
216 if (!args_un.empty() && get_user_info (usersfile, args_un, thisuser)) {
217 if (!args_pw.empty()) {
218 // we are authenticating using a password
219 if (check_passwd (thisuser, args_pw)) args_ua = "1"; // succeeded
220
221 } else if (!args_ky.empty()) {
222 // we are authenticating using a key
223 if (check_key (keyfile, thisuser, args_ky, args_ug, keydecay)) args_ua = "1";
224 else args_us = "stalekey";
225 }
226 }
227
228 args_pw.clear(); // password goes no further
229 if (!args_ua.empty()) {
230 if (thisuser.enabled) {
231 bool haspermission = true;
232 // check to make sure the user is in the required group
233 if (!args_ug.empty()) {
234 haspermission = false;
235 text_t::const_iterator group_here = thisuser.groups.begin();
236 text_t::const_iterator group_end = thisuser.groups.end();
237 text_t thisgroup;
238 while (group_here != group_end) {
239 group_here = getdelimitstr (group_here, group_end, ',', thisgroup);
240 if (thisgroup == args_ug) {
241 haspermission = true;
242 break;
243 }
244 }
245 }
246
247 if (haspermission) {
248 // succeeded, get info about this user
249 // note: we don't need to set "ug" as it is already set to what it needs to be
250 args_us = "enabled";
251 args_ky = generate_key (keyfile, args_un); // new key
252
253 // delete old keys around every 50 accesses
254 if (rand()%50 == 1) remove_old_keys (keyfile, keydecay);
255
256 } else {
257 // succeeded, however, the user is not in the correct group
258 args_ua.clear();
259 args_us = "permissiondenied";
260 args_ky.clear();
261 }
262
263 } else {
264 // succeeded, however, the account is disabled
265 args_ua.clear();
266 args_us = "disabled";
267 args_ky.clear();
268 }
269
270 } else {
271 // failure, reset info about the user
272 args_ky.clear();
273 }
274
275 // we will have to redirect the user if authentication is needed,
276 // it failed, and we weren't on our way to be authenticated anyway
277 if ((!args_uan.empty()) && (args_ua.empty()) && (args_a != "a")) {
278 // need to save the current arguments in "er"
279 text_t &arg_er = args["er"];
280 if (!compress_save_args(argsinfo, saveconf, args, arg_er, outconvert, logout))
281 arg_er.clear();
282
283 // redirect to this action
284 args_a = "a";
285 }
286
287 return true;
288}
289
290void authenaction::get_cgihead_info (cgiargsclass &/*args*/, recptprotolistclass * /*protos*/,
291 response_t &response, text_t &response_data,
292 ostream &/*logout*/) {
293 response = content;
294 response_data = "text/html";
295}
296
297void authenaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
298 recptprotolistclass * /*protos*/, ostream &/*logout*/) {
299 // sets _authen:messageextra_ based on the value of args["us"]
300 // _authen:hiddenargs_ to contain all the arguments that were
301 // explicitly set
302 disp.setmacro ("messagestatus", "authen", ("_authen:message" + args["us"]
303 + "_"));
304 // change style of header and footer if page is a frame
305 if ((args["sp"].empty()) || (args["sp"] == "frameset")) {
306 disp.setmacro ("header", "authen", "_status:infoheader_(Log in)");
307 disp.setmacro ("header", "authenok", "_status:infoheader_(Log in)");
308 disp.setmacro ("footer", "authen", "_status:infofooter_(Log in)");
309 disp.setmacro ("footer", "authenok", "_status:infofooter_(Log in)");
310 }
311
312 // get a list of saved configuration arguments (if possible)
313 text_t saveconf;
314 text_tset saveconfset;
315 if (recpt != NULL) {
316 saveconf = recpt->get_configinfo().saveconf;
317 splitchar (saveconf.begin(), saveconf.end(), '-', saveconfset);
318 }
319
320 text_t hiddenargs;
321 cgiargsclass::const_iterator args_here = args.begin();
322 cgiargsclass::const_iterator args_end = args.end();
323 while (args_here != args_end) {
324 // set this as a hidden argument if it came from the cgi arguments,
325 // its not the compressed arguments, the query string, a user name or
326 // password, and if it is not in the compressed arguments
327 if ((*args_here).second.source == cgiarg_t::cgi_arg &&
328 (*args_here).first != "e" && (*args_here).first != "q" &&
329 (*args_here).first != "un" && (*args_here).first != "pw" &&
330 saveconfset.find((*args_here).first) == saveconfset.end()) {
331 hiddenargs += "<input type=hidden name=\"" + (*args_here).first +
332 "\" value=\"_cgiarg" + (*args_here).first + "_\">\n";
333 }
334
335 args_here++;
336 }
337
338 disp.setmacro ("hiddenargs", "authen", hiddenargs);
339}
340
341bool authenaction::do_action (cgiargsclass &args, recptprotolistclass * /*protos*/,
342 browsermapclass * /*browsers*/, displayclass &disp,
343 outconvertclass &outconvert, ostream &textout,
344 ostream &/*logout*/) {
345 if (args["us"] == "enabled") {
346 // have been authenticated
347 textout << outconvert << disp
348 << "_authenok:header_\n_authenok:content_\n_authenok:footer_\n";
349 return true;
350 }
351
352 // need to be authenticated
353 textout << outconvert << disp
354 << "_authen:header_\n_authen:content_\n_authen:footer_\n";
355
356 return true;
357}
Note: See TracBrowser for help on using the repository browser.