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
  
//<locale-token project="mod_userdb_sql">_</locale-token> 
#define _(X,Y)        _DEF_LOCALE("mod_userdb_sql",X,Y) 
 
inherit UserDB; 
constant name = "sql"; 
 
inherit "module"; 
#include <module.h> 
 
int inited; 
 
constant cvs_version = 
  "$Id: userdb_sql.pike,v 1.12 2009/12/05 01:04:00 jonasw Exp $"; 
 
LocaleString module_name = _(1,"Authentication: SQL user database"); 
LocaleString module_doc  = _(2,"This module implements a user database via " 
                             "a SQL server.\n"); 
 
class SqlUser 
{ 
  inherit User; 
  protected mapping ent; 
 
  string name()             { return ent->name; } 
  string crypted_password() { return ent->password; } 
  int    uid()              { return (int)ent->uid; } 
  int    gid()              { return (int)ent->gid; } 
  string gecos()            { return ent->gecos || ent->name; } 
  string real_name()        { return (gecos()/",")[0]; } 
  string homedir()          { return ent->homedir; } 
  string shell()            { return ent->shell; } 
 
  array(string) groups() 
  { 
    return get_user_groups( uid() ); 
  } 
 
  int password_authenticate(string password) 
  // Return 1 if the password is correct, 0 otherwise. 
  { 
    switch(query("passwd_type")) { 
      case "password": 
        return (int)sql_query("SELECT PASSWORD(%s) = %s as pswmatch", 
                              password, crypted_password())[0]->pswmatch; 
      case "old-password": 
        return (int)sql_query("SELECT OLD_PASSWORD(%s) = %s as pswmatch", 
                              password, crypted_password())[0]->pswmatch; 
      case "crypt": 
        return (crypt(password, crypted_password())); 
      case "clear text": 
        return (password == crypted_password()); 
      case "md5 crypt": 
        catch {return Crypto.verify_crypt_md5 (password, crypted_password());}; 
        return 0; 
    } 
  } 
 
  protected void create( UserDB p, mapping e ) 
  { 
    ::create( p ); 
    ent = e; 
  } 
} 
 
class SqlGroup 
{ 
  inherit Group; 
  protected mapping ent; 
 
  int gid() { return (int)ent->gid; } 
  string name() { return ent->name; } 
  array(string) members() 
  { 
    return get_group_users( gid() ); 
  } 
 
  protected void create( UserDB p, mapping e ) 
  { 
    ::create( p ); 
    ent = e; 
  } 
} 
 
constant db_defs = 
([ 
  "group_members":({ 
    "gid int unsigned not null", 
    "uid int unsigned not null", 
    "INDEX g (gid)", 
    "INDEX u (uid)", 
  }), 
  "groups":({ 
    "gid  int unsigned not null primary key auto_increment", 
    "name varchar(255) not null", 
    "INDEX n (name)", 
  }), 
  "users":({ 
    "uid      int unsigned not null primary key auto_increment", 
    "gid      int unsigned not null default 0", /*Hm. Not _really_ nessesary.*/ 
    "name     varchar(255) not null default ''", 
    "password varchar(255) not null default '*'", 
    "gecos    varchar(255) not null default ''", 
    "homedir  varchar(255) not null default '/'", 
    "shell    varchar(255) not null default '/bin/sh'", 
    "INDEX n (name)", 
  }), 
]); 
   
array(string) get_user_groups( int user ) 
{ 
  if(!inited) return ({}); 
  return  
    sql_query( "SELECT " 
               "  groups.name as name FROM groups,group_members " 
               "WHERE " 
               "  group_members.uid=%d AND groups.gid=group_members.gid " 
               "GROUP BY " 
               "  groups.name", user ) 
    ->name; 
} 
 
array(string) get_group_users( int group ) 
{ 
  if(!inited) return ({}); 
  return  
    sql_query( "SELECT " 
               "  users.name as name FROM users,group_members " 
               "WHERE " 
               "  group_members.gid=%d AND users.uid=group_members.uid " 
               "GROUP BY " 
               "  users.name", group ) 
    ->name; 
} 
 
User find_user( string s ) 
{ 
  if(!inited) return 0; 
  array r = sql_query( "SELECT * FROM users WHERE name=%s", s ); 
  if( sizeof( r ) ) 
    return SqlUser( this_object(), r[0] ); 
} 
 
User find_user_from_uid( int id ) 
{ 
  if(!inited) return 0; 
  array r = sql_query( "SELECT * FROM users WHERE uid=%d", id ); 
  if( sizeof( r ) ) 
    return SqlUser( this_object(), r[0] ); 
} 
 
array(string) list_users( ) 
{ 
  if(!inited) return ({}); 
  return sql_query( "SELECT name FROM users" )->name; 
} 
 
Group find_group( string s ) 
{ 
  if(!inited) return 0; 
  array r = sql_query( "SELECT * FROM groups WHERE name=%s", s ); 
  if( sizeof( r ) ) 
    return SqlGroup( this_object(), r[0] ); 
} 
 
Group find_group_from_gid( int i ) 
{ 
  if(!inited) return 0; 
  array r = sql_query( "SELECT * FROM groups WHERE gid=%d", i ); 
  if( sizeof( r ) ) 
    return SqlGroup( this_object(), r[0] ); 
} 
 
array(string) list_groups( ) 
{ 
  if(!inited) return ({}); 
  return sql_query( "SELECT name FROM groups" )->name; 
} 
 
 
void start() 
{ 
  if( !DBManager.get( query("db"), my_configuration() ) ) 
  { 
    if( DBManager.get( query("db") ) ) 
    { 
      inited = 0; 
      report_error( query("db")+ 
                    " exists, but cannot be written to from this module" ); 
      return; 
    } 
    DBManager.create_db( query("db"), 0, 1 ); 
    DBManager.is_module_db( this_module(), query("db"), 
                            "This datbase contains the user database" ); 
    DBManager.set_permission( query("db"), my_configuration(), DBManager.WRITE ); 
  } 
  set_my_db( query("db") ); 
  create_sql_tables( db_defs, "User database table", 1 ); 
  inited = 1; 
} 
 
class DatabaseVar 
{ 
  inherit Variable.StringChoice; 
  array get_choice_list( ) 
  { 
    return sort(DBManager.list( my_configuration() )); 
  } 
} 
 
void create() 
{ 
  defvar( "db", 
          DatabaseVar( "sql_users",({}),0, 
                       _(3,"Database"), 
                       _(4,"This is the database that this module will " 
                              "store it's users in.") ) ); 
 
  defvar( "passwd_type", 
          Variable.StringChoice("password", 
                                ([ 
                                  "password":_(5,"MySQL Password"), 
                                  "old-password":_(11,"MySQL OLD_PASSWORD() " 
                                                   "(4.0 Compat Mode)"), 
                                  "crypt":_(6,"Unix crypt"), 
                                  "clear text":_(7,"Clear text"), 
                                  "md5 crypt":_(8,"MD5 crypt"), 
                                ]), 0, 
                                _(9,"Password type"), 
                                _(10,"Password hashing method. " 
                                  "By changing this variable you can " 
                                  "select the meaning of password field. " 
                                  "By default the passwords are supposed " 
                                  "to be hashed by internal MySQL PASSWORD() " 
                                  "function.") 
                                )); 
 
}