e39bd6163353e8de533a5023c3f58c3fc761cb28
[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 #if defined(HAVE_MYSQL_H) && defined(WITH_MYSQLSAM)
25
26 #include "includes.h"
27
28 extern int DEBUGLEVEL;
29
30 extern pstring samlogon_user;
31 extern BOOL sam_logon_in_ssb;
32
33 void *mysql_fill_sam_passwd( MYSQL_ROW *row )
34 {
35         static struct sam_passwd *user;
36
37         static pstring full_name;
38         static pstring home_dir;
39         static pstring home_drive;
40         static pstring logon_script;
41         static pstring profile_path;
42         static pstring acct_desc;
43         static pstring workstations;
44
45         DEBUG(5,("%s\n",FUNCTION_MACRO));
46
47         user = pwdb_smb_to_sam((struct smb_passwd *)mysql_fill_smb_passwd(row));
48
49         if ( user == NULL ) {
50                 return NULL;
51         }
52
53         /* 'Researched' from sampass.c =) */
54
55         pstrcpy(samlogon_user, user->unix_name);
56
57         if (samlogon_user[strlen(samlogon_user)-1] == '$' && 
58             user->group_rid != DOMAIN_GROUP_RID_USERS)
59         {
60                 DEBUG(0,("trust account %s should be in DOMAIN_GROUP_RID_USERS\n", samlogon_user));
61         }
62
63         /* XXXX hack to get standard_sub_basic() to use sam logon username */
64         /* possibly a better way would be to do a become_user() call */
65         sam_logon_in_ssb = True;
66
67         pstrcpy(full_name    , "");
68         pstrcpy(logon_script , lp_logon_script  ());
69         pstrcpy(profile_path , lp_logon_path    ());
70         pstrcpy(home_drive   , lp_logon_drive   ());
71         pstrcpy(home_dir     , lp_logon_home    ());
72         pstrcpy(acct_desc    , "");
73         pstrcpy(workstations , "");
74
75         sam_logon_in_ssb = False;
76
77         user->full_name    = full_name;
78         user->home_dir     = home_dir;
79         user->dir_drive    = home_drive;
80         user->logon_script = logon_script;
81         user->profile_path = profile_path;
82         user->acct_desc    = acct_desc;
83         user->workstations = workstations;
84
85         user->unknown_str = NULL; /* don't know, yet! */
86         user->munged_dial = NULL; /* "munged" dial-back telephone number */
87
88         user->unknown_3 = 0xffffff; /* don't know */
89         user->logon_divs = 168; /* hours per week */
90         user->hours_len = 21; /* 21 times 8 bits = 168 */
91         memset(user->hours, 0xff, user->hours_len); /* available at all hours */
92         user->unknown_5 = 0x00020000; /* don't know */
93         user->unknown_6 = 0x000004ec; /* don't know */
94
95         return (void*)user;
96 }
97
98 struct sam_passwd *mysql_getsampwent(void *vp)
99 {
100
101         DEBUG(5,("%s\n",FUNCTION_MACRO));
102
103         return (struct sam_passwd*)mysql_fill_sam_passwd( mysql_getpwent(vp) );
104 }
105
106 struct sam_passwd *mysql_getsampwrid(uint32 rid)
107 {
108         fstring where;
109
110         DEBUG(5,("%s\n",FUNCTION_MACRO));
111
112         slprintf( where, sizeof(where), "user_rid=%lu", (long unsigned)rid);
113
114         return (struct sam_passwd *)mysql_fetch_passwd( mysql_fill_sam_passwd, where );
115 }
116
117 struct sam_passwd *mysql_getsampwuid(uid_t uid)
118 {
119
120         DEBUG(5,("%s\n",FUNCTION_MACRO));
121
122         return (struct sam_passwd *)mysql_getpwuid( mysql_fill_sam_passwd, uid );
123 }
124
125 struct sam_passwd *mysql_getsampwntnam(const char *nt_name)
126 {
127
128         DEBUG(5,("%s\n",FUNCTION_MACRO));
129
130         return (struct sam_passwd *)mysql_getpwnam( mysql_fill_sam_passwd, "nt_name", nt_name);
131 }
132
133 struct sam_disp_info *mysql_getsamdispntnam(const char *nt_name)
134 {
135
136         DEBUG(5,("%s\n",FUNCTION_MACRO));
137
138         return pwdb_sam_to_dispinfo(mysql_getsampwntnam(nt_name));
139 }
140
141 struct sam_disp_info *mysql_getsamdisprid(uint32 rid)
142 {
143
144         DEBUG(5,("%s\n",FUNCTION_MACRO));
145
146         return pwdb_sam_to_dispinfo(mysql_getsampwrid(rid));
147 }
148
149 struct sam_disp_info *mysql_getsamdispent(void *vp)
150 {
151
152         DEBUG(5,("%s\n",FUNCTION_MACRO));
153
154         return pwdb_sam_to_dispinfo(mysql_getsampwent(vp));
155 }
156
157 static BOOL mysql_mod_sam( MYSQL *handle, struct sam_passwd *sam, BOOL override )
158 {
159
160         DEBUG(5,("%s\n",FUNCTION_MACRO));
161
162         return True;
163 }
164
165 BOOL mysql_add_sampwd_entry(struct sam_passwd *sam)
166 {
167         MYSQL handle;
168         struct smb_passwd *smb;
169
170         DEBUG(5,("%s\n",FUNCTION_MACRO));
171
172         smb = pwdb_sam_to_smb( sam );
173
174         if ( smb == NULL ) {
175                 return False;
176         }
177
178         if ( mysql_db_lock_connect( &handle ) ) {
179                 return False;
180         }
181
182         if ( !mysql_add_smb( &handle, smb ) ) {
183                 mysql_close(&handle);
184                 return False;
185         }
186
187         if ( !mysql_mod_smb( &handle, smb, True ) ) {
188                 mysql_del_smb( &handle, smb->unix_name );
189                 mysql_close(&handle);
190                 return False;
191         }
192
193         if ( !mysql_mod_sam( &handle, sam, True ) ) {
194                 mysql_del_smb( &handle, smb->unix_name );
195                 mysql_close(&handle);
196                 return False;
197         }
198
199         mysql_close(&handle);
200         return True;
201 }
202
203 BOOL mysql_mod_sampwd_entry(struct sam_passwd *sam, BOOL override)
204 {
205         MYSQL handle;
206         struct smb_passwd *smb;
207
208         DEBUG(5,("%s\n",FUNCTION_MACRO));
209
210         smb = pwdb_sam_to_smb(sam);
211
212         if ( smb == NULL ) {
213                 return False;
214         }
215
216         if ( mysql_db_lock_connect( &handle ) ) {
217                 return False;
218         }
219
220         if ( !mysql_mod_smb( &handle, smb, override ) ) {
221                 mysql_close(&handle);
222                 return False;
223         }
224
225         if ( !mysql_mod_sam( &handle, sam, override ) ) {
226                 mysql_close(&handle);
227                 return False;
228         }
229
230         mysql_close(&handle);
231         return True;
232 }
233
234 static struct sam_passdb_ops sam_mysql_ops =
235 {
236         mysql_startpwent,
237         mysql_endpwent,
238         mysql_getpwpos,
239         mysql_setpwpos,
240         mysql_getsampwntnam,
241         mysql_getsampwuid,
242         mysql_getsampwrid,
243         mysql_getsampwent,
244         mysql_add_sampwd_entry,
245         mysql_mod_sampwd_entry,
246         mysql_getsamdispntnam,
247         mysql_getsamdisprid,
248         mysql_getsamdispent
249 };
250
251 struct sam_passdb_ops *mysql_initialise_sam_password_db(void)
252 {
253         return &sam_mysql_ops;
254 }
255
256 #else
257         void mysql_dummy_sam_function(void) { }
258 #endif