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