File: SQLuserdb.pike

package info (click to toggle)
roxen 1.2beta2-3
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 16,920 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,784; cpp: 377
file content (309 lines) | stat: -rw-r--r-- 8,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* This code is (C) 1997 Francesco Chemolli <kinkie@comedia.it>
 * It can be freely distributed and copied under the terms of the
 * GNU General Public License.
 * This code comes with NO WARRANTY of any kind, either implicit or explicit.
 *
 * This module handles a SQL-based User Database. 
 * It uses the generic-SQL pike module, so it should run on any server
 * pike supports. This includes at least MiniSQL, MySql and Postgres (more
 * could be supported in the future)
 *
 * Documentation can be found at 
 * http://kame.usr.dsi.unimi.it:1111/sw/roxen/sqlauth/
 * or should have been shipped along with the module.
 */

string cvs_version="$Id: SQLuserdb.pike,v 1.3 1997/12/16 16:18:21 grubba Exp $";

//#define SQLAUTHDEBUG

#include <module.h>
inherit "roxenlib";
inherit "module";

#ifdef SQLAUTHDEBUG
#define DEBUGLOG(X) perror("SQLuserdb: "+X+"\n");
#else
#define DEBUGLOG(X)
#endif

int att=0, succ=0, nouser=0, db_accesses=0, last_db_access=0;
object db=0;

/*
 * Utilities
 */

/*
 * Object management and configuration variables definitions
 */
void create() 
{
	defvar ("sqlserver","localhost","SQL server: Location",
			TYPE_STRING, "This is the host running the SQL server with the "
			"authentication information"
			);
	defvar ("database","passwd","SQL server: Database name",
			TYPE_STRING, "This is the name of the authorizations database"
			);
	defvar ("dbuser","","SQL server: Database user's username",
			TYPE_STRING, "This username will be used to authenticate when "
			"connecting to the SQL server. Refer to your SQL server documentation, "
			"this could be irrelevant."
			);
	defvar ("dbpass","", "SQL server: Database user's password",TYPE_STRING,
			"This is the password used to authenticate the server when accessing "
			"the database. Refer to your SQL server documentation, this could be "
			"irrelevant"
			);
	defvar ("table","passwd","SQL server: Passwords table",TYPE_STRING,
			"This is the table containing the data. It is  advisable not "
			"to change it once the service has been started."
			);
	defvar ("disable_userlist",0,"Disable Userlist",TYPE_FLAG,
			"If this is turned on, the module will NOT honor userlist answers. "
			"Those are used if you have an user filesystem, and try to access "
			"its mountpoint. It is recommended to turn this on if you have huge "
			"users databases, since that feature would require much memory.");
	defvar ("usecache",1,"Cache entries", TYPE_FLAG,
			"This flag defines whether the module will cache the database "
			"entries. Makes accesses faster, but changes in the database will "
			"not show immediately. <B>Recommended</B>."
			);
	defvar ("closedb",1,"Close the database if not used",TYPE_FLAG,
			"Setting this will save one filedescriptor without a small "
			"performance loss."
			);
	defvar ("timer",60,"Database close timer", TYPE_INT,
			"The timer after which the database is closed",0,
			lambda(){return !QUERY(closedb);}
			);
	defvar ("defaultuid",geteuid(),"Defaults: User ID", TYPE_INT,
			"Some modules require an user ID to work correctly. This is the "
			"user ID which will be returned to such requests if the information "
			"is not supplied by the database."
			);
	defvar ("defaultgid", getegid(), "Defaults: Group ID", TYPE_INT,
			"Same as User ID, only it refers rather to the group."
			);
	defvar ("defaultgecos", "", "Defaults: Gecos", TYPE_STRING,
			"The default Gecos."
			);
	defvar ("defaulthome","/", "Defaults: Home Directory", TYPE_DIR, 
			"It is possible to specify an user's home "
			"directory in the passwords database. This is used if it's "
			"not provided."
			);
	defvar ("defaultshell", "/bin/sh", "Defaults: Login Shell", TYPE_FILE,
			"Same as the default home, only referring to the user's login shell."
			);
}

/*
 * DB management functions
 */
//this gets called only by call_outs, so we can avoid storing call_out_ids
//Also, I believe storing in a local variable the last time of an access
//to the database is more efficient than removing and resetting call_outs
//This leaves a degree of uncertainty on when the DB will be effectively
//closed, but it's below the value of the module variable "timer" for sure.
void close_db() {
	if (!QUERY(closedb))
		return;
	if( (time(1)-last_db_access) > QUERY(timer) ) {
		db=0;
		DEBUGLOG("closing the database");
		return;
	}
	call_out(close_db,QUERY(timer));
}

void open_db() {
	mixed err;
	last_db_access=time(1);
	db_accesses++; //I count DB accesses here, since this is called before each
	if(objectp(db)) //already open
		return;
	err=catch{
		db=Sql.sql(QUERY(sqlserver),QUERY(database),QUERY(dbuser),QUERY(dbpass));
	};
	if (err) {
		perror ("SQLauth: Couldn't open authentication database!\n");
		if (db)
			perror("SQLauth: database interface replies: "+db->error()+"\n");
		else
			perror("SQLauth: unknown reason\n");
		perror ("SQLauth: check the values in the configuration interface, and "
				"that the user\n\trunning the server has adequate permissions to the "
				"server\n");
		db=0;
		return;
	}
	DEBUGLOG("database successfully opened");
	if(QUERY(closedb))
		call_out(close_db,QUERY(timer));
}

/*
 * Module Callbacks
 */
string *userinfo (string u) {
	string *dbinfo;
	array sql_results;
	mixed err,tmp;
	DEBUGLOG ("userinfo ("+u+")");

	if (QUERY(usecache))
		dbinfo=cache_lookup("sqlauthentries",u);
	if (dbinfo)
		return dbinfo;

	open_db();

	if (!db) {
		perror ("SQLauth: Returning 'user unknown'.\n");
		return 0;
	}
	sql_results=db->query("select username,passwd,uid,gid,homedir,shell "
			"from "+QUERY(table)+" where username='"+u+"'");
	if (!sql_results||!sizeof(sql_results)) {
		DEBUGLOG ("no entry in database, returning unknown")
		return 0;
	}
	tmp=sql_results[0];
//	DEBUGLOG(sprintf("userinfo: got %O",tmp));
	dbinfo= ({
			u,
			tmp->passwd,
			tmp->uid||QUERY(defaultuid),
			tmp->gid||QUERY(defaultgid),
			QUERY(defaultgecos),
			tmp->homedir||QUERY(defaulthome),
			tmp->shell||QUERY(defaultshell)
			});
	if (QUERY(usecache))
		cache_set("sqlauthentries",u,dbinfo);
	DEBUGLOG(sprintf("Result: %O",dbinfo)-"\n");
	return dbinfo;
	return 0;
}

string *userlist() {
	if (QUERY(disable_userlist))
		return ({});
	mixed err,tmp;
	array data;

	DEBUGLOG ("userlist()");
	open_db();
	if (!db) {
		perror ("SQLauth: returning empty user index!\n");
		return ({});
	}
	data=db->query("select username from "+QUERY(table));
	foreach(data,tmp)
		data=tmp->username;
	return data;
}

string user_from_uid (int u) 
{
	array data;
	if(!u)
		return 0;
	open_db(); //it's not easy to cache in this case.
	if (!db) {
		perror("SQLauth: returning no_such_user\n");
		return 0;
	}
	data=db->query("select username from "+QUERY(table)+" where uid=u");
	if(sizeof(data)!=1) //either there's noone with that uid or there's many
		return 0;
	return data[0]->username;
}

array|int auth (string *auth, object id)
{
	string u,p,*dbinfo;
	mixed err;

	att++;
	DEBUGLOG (sprintf("auth(%O)",auth)-"\n");

	sscanf (auth[1],"%s:%s",u,p);

	if (!p||!strlen(p)) {
		DEBUGLOG ("no password supplied by the user");
		return ({0, auth[1], -1});
	}

	if (QUERY(usecache))
		dbinfo=cache_lookup("sqlauthentries",u);

	if (!dbinfo) {
		open_db();

		if(!db) {
			DEBUGLOG ("Error in opening the database");
			return ({0, auth[1], -1});
		}
		dbinfo=userinfo(u); //cache is already set by userinfo
	}

	// I suppose that the user's password is at least 1 character long
	if (!dbinfo) {
		DEBUGLOG ("no such user");
		nouser++;
		return ({0,u,p});
	}

	if(!crypt (p,dbinfo[1])) {
		DEBUGLOG ("password check ("+dbinfo[1]+","+p+") failed");
		return ({0,u,p});
	}

	DEBUGLOG (u+" positively recognized");
	succ++;
	return ({1,u,0});
}

/*
 * Support Callbacks
 */
string status() {
	return "<H2>Security info</H2>"
			"Attempted authentications: "+att+"<BR>\n"
			"Failed: "+(att-succ+nouser)+" ("+nouser+" because of wrong username)"
			"<BR>\n"+
			db_accesses +" accesses to the database were required.<BR>\n"
			;
}

string|void check_variable (string name, mixed newvalue)
{
	switch (name) {
		case "timer":
			if (((int)newvalue)<=0) {
				set("timer",QUERY(timer));
				return "What? Have you lost your mind? How can I close the database"
					" before using it?";
			}
			return 0;
		default:
			return 0;
	}
	return 0; //should never reach here...
}

array register_module() {
	return ({
	MODULE_AUTH,
	"SQL user database",
	"This module implements user authentication via a SQL server.<p>\n "
	"For setup instruction, see the comments at the beginning of the module "
	"code.<P>"
	"&copy; 1997 Francesco Chemolli, distributed freely under GPL license.",
	0,
	1
	});
};