8d3049ce0ac7c67c767345f2f7e04f4de02e23a4
[samba.git] / source3 / passdb / mysqlsampass.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  Samba MYSQL SAM Database, by Benjamin Kuit.
5  *  Copyright (C) Benjamin Kuit                     1999,
6  *  Copyright (C) Andrew Tridgell              1992-1999,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #ifdef WITH_MYSQLSAM
25
26 #include "includes.h"
27 #include <mysql.h>
28
29 extern int DEBUGLEVEL;
30
31 extern pstring samlogon_user;
32 extern BOOL sam_logon_in_ssb;
33
34 typedef void *(*mysql_fill_func)( MYSQL_ROW * );
35 #define FILL_SAM mysql_fill_sam_passwd
36
37 void *mysql_startpwent(BOOL update);
38 void mysql_endpwent(void *vp);
39 SMB_BIG_UINT mysql_getpwpos(void *vp);
40 BOOL mysql_setpwpos(void *vp, SMB_BIG_UINT pos);
41 void *mysql_fill_smb_passwd( MYSQL_ROW *row );
42 MYSQL_ROW *mysql_getpwent(void *vp);
43 void *mysql_fetch_passwd( mysql_fill_func filler, char *where );
44 void *mysql_getpwuid( mysql_fill_func filler, uid_t uid );
45 void *mysql_getpwnam( mysql_fill_func filler, char *field, const char *name );
46 int mysql_db_lock_connect( MYSQL *handle );
47 BOOL mysql_add_smb( MYSQL *handle, struct smb_passwd *smb );
48 BOOL mysql_mod_smb( MYSQL *handle, struct smb_passwd *smb, BOOL override );
49 BOOL mysql_del_smb( MYSQL *handle, char *unix_name );
50
51 void *mysql_fill_sam_passwd( MYSQL_ROW *row ) {
52         static struct sam_passwd *user;
53
54         static pstring full_name;
55         static pstring home_dir;
56         static pstring home_drive;
57         static pstring logon_script;
58         static pstring profile_path;
59         static pstring acct_desc;
60         static pstring workstations;
61
62         DEBUG(5,("%s\n",FUNCTION_MACRO));
63
64         user = pwdb_smb_to_sam((struct smb_passwd *)mysql_fill_smb_passwd(row));
65
66         if ( user == NULL ) {
67                 return NULL;
68         }
69
70         /* 'Researched' from sampass.c =) */
71
72         pstrcpy(samlogon_user, user->unix_name);
73
74         if (samlogon_user[strlen(samlogon_user)-1] == '$' && 
75             user->group_rid != DOMAIN_GROUP_RID_USERS)
76         {
77                 DEBUG(0,("trust account %s should be in DOMAIN_GROUP_RID_USERS\n", samlogon_user));
78         }
79
80         /* XXXX hack to get standard_sub_basic() to use sam logon username */
81         /* possibly a better way would be to do a become_user() call */
82         sam_logon_in_ssb = True;
83
84         pstrcpy(full_name    , "");
85         pstrcpy(logon_script , lp_logon_script  ());
86         pstrcpy(profile_path , lp_logon_path    ());
87         pstrcpy(home_drive   , lp_logon_drive   ());
88         pstrcpy(home_dir     , lp_logon_home    ());
89         pstrcpy(acct_desc    , "");
90         pstrcpy(workstations , "");
91
92         sam_logon_in_ssb = False;
93
94         user->full_name    = full_name;
95         user->home_dir     = home_dir;
96         user->dir_drive    = home_drive;
97         user->logon_script = logon_script;
98         user->profile_path = profile_path;
99         user->acct_desc    = acct_desc;
100         user->workstations = workstations;
101
102         user->unknown_str = NULL; /* don't know, yet! */
103         user->munged_dial = NULL; /* "munged" dial-back telephone number */
104
105         user->unknown_3 = 0xffffff; /* don't know */
106         user->logon_divs = 168; /* hours per week */
107         user->hours_len = 21; /* 21 times 8 bits = 168 */
108         memset(user->hours, 0xff, user->hours_len); /* available at all hours */
109         user->unknown_5 = 0x00020000; /* don't know */
110         user->unknown_6 = 0x000004ec; /* don't know */
111
112         return (void*)user;
113 }
114
115 struct sam_passwd *mysql_getsampwent(void *vp) {
116
117         DEBUG(5,("%s\n",FUNCTION_MACRO));
118
119         return (struct sam_passwd*)mysql_fill_sam_passwd( mysql_getpwent(vp) );
120 }
121
122 struct sam_passwd *mysql_getsampwrid(uint32 rid) {
123         fstring where;
124
125         DEBUG(5,("%s\n",FUNCTION_MACRO));
126
127         slprintf( where, sizeof(where), "user_rid=%lu", (long unsigned)rid);
128
129         return (struct sam_passwd *)mysql_fetch_passwd( FILL_SAM, where );
130 }
131
132 struct sam_passwd *mysql_getsampwuid(uid_t uid) {
133
134         DEBUG(5,("%s\n",FUNCTION_MACRO));
135
136         return (struct sam_passwd *)mysql_getpwuid( FILL_SAM, uid );
137 }
138
139 struct sam_passwd *mysql_getsampwntnam(const char *nt_name) {
140
141         DEBUG(5,("%s\n",FUNCTION_MACRO));
142
143         return (struct sam_passwd *)mysql_getpwnam( FILL_SAM, "nt_name", nt_name);
144 }
145
146 struct sam_disp_info *mysql_getsamdispntnam(const char *nt_name) {
147
148         DEBUG(5,("%s\n",FUNCTION_MACRO));
149
150         return pwdb_sam_to_dispinfo(mysql_getsampwntnam(nt_name));
151 }
152
153 struct sam_disp_info *mysql_getsamdisprid(uint32 rid) {
154
155         DEBUG(5,("%s\n",FUNCTION_MACRO));
156
157         return pwdb_sam_to_dispinfo(mysql_getsampwrid(rid));
158 }
159
160 struct sam_disp_info *mysql_getsamdispent(void *vp) {
161
162         DEBUG(5,("%s\n",FUNCTION_MACRO));
163
164         return pwdb_sam_to_dispinfo(mysql_getsampwent(vp));
165 }
166
167 static BOOL mysql_mod_sam( MYSQL *handle, struct sam_passwd *sam, BOOL override ) {
168
169         DEBUG(5,("%s\n",FUNCTION_MACRO));
170
171         return True;
172 }
173
174 BOOL mysql_add_sampwd_entry(struct sam_passwd *sam) {
175         MYSQL handle;
176         struct smb_passwd *smb;
177
178         DEBUG(5,("%s\n",FUNCTION_MACRO));
179
180         smb = pwdb_sam_to_smb( sam );
181
182         if ( smb == NULL ) {
183                 return False;
184         }
185
186         if ( mysql_db_lock_connect( &handle ) ) {
187                 return False;
188         }
189
190         if ( !mysql_add_smb( &handle, smb ) ) {
191                 mysql_close(&handle);
192                 return False;
193         }
194
195         if ( !mysql_mod_smb( &handle, smb, True ) ) {
196                 mysql_del_smb( &handle, smb->unix_name );
197                 mysql_close(&handle);
198                 return False;
199         }
200
201         if ( !mysql_mod_sam( &handle, sam, True ) ) {
202                 mysql_del_smb( &handle, smb->unix_name );
203                 mysql_close(&handle);
204                 return False;
205         }
206
207         mysql_close(&handle);
208         return True;
209 }
210
211 BOOL mysql_mod_sampwd_entry(struct sam_passwd *sam, BOOL override) {
212         MYSQL handle;
213         struct smb_passwd *smb;
214
215         DEBUG(5,("%s\n",FUNCTION_MACRO));
216
217         smb = pwdb_sam_to_smb(sam);
218
219         if ( smb == NULL ) {
220                 return False;
221         }
222
223         if ( mysql_db_lock_connect( &handle ) ) {
224                 return False;
225         }
226
227         if ( !mysql_mod_smb( &handle, smb, override ) ) {
228                 mysql_close(&handle);
229                 return False;
230         }
231
232         if ( !mysql_mod_sam( &handle, sam, override ) ) {
233                 mysql_close(&handle);
234                 return False;
235         }
236
237         mysql_close(&handle);
238         return True;
239 }
240
241 static struct sam_passdb_ops sam_mysql_ops =
242 {
243         mysql_startpwent,
244         mysql_endpwent,
245         mysql_getpwpos,
246         mysql_setpwpos,
247         mysql_getsampwntnam,
248         mysql_getsampwuid,
249         mysql_getsampwrid,
250         mysql_getsampwent,
251         mysql_add_sampwd_entry,
252         mysql_mod_sampwd_entry,
253         mysql_getsamdispntnam,
254         mysql_getsamdisprid,
255         mysql_getsamdispent
256 };
257
258 struct sam_passdb_ops *mysql_initialise_sam_password_db(void)
259 {
260         return &sam_mysql_ops;
261 }
262
263 #else
264         void mysql_dummy_sam_function(void) { }
265 #endif