2 * Unix SMB/Netbios implementation.
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,
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.
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.
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.
29 extern int DEBUGLEVEL;
31 extern pstring samlogon_user;
32 extern BOOL sam_logon_in_ssb;
34 typedef void *(*mysql_fill_func)( MYSQL_ROW * );
35 #define FILL_SAM mysql_fill_sam_passwd
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 );
51 void *mysql_fill_sam_passwd( MYSQL_ROW *row ) {
52 static struct sam_passwd *user;
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;
62 DEBUG(5,("%s\n",FUNCTION_MACRO));
64 user = pwdb_smb_to_sam((struct smb_passwd *)mysql_fill_smb_passwd(row));
70 /* 'Researched' from sampass.c =) */
72 pstrcpy(samlogon_user, user->unix_name);
74 if (samlogon_user[strlen(samlogon_user)-1] == '$' &&
75 user->group_rid != DOMAIN_GROUP_RID_USERS)
77 DEBUG(0,("trust account %s should be in DOMAIN_GROUP_RID_USERS\n", samlogon_user));
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;
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 , "");
92 sam_logon_in_ssb = False;
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;
102 user->unknown_str = NULL; /* don't know, yet! */
103 user->munged_dial = NULL; /* "munged" dial-back telephone number */
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 */
115 struct sam_passwd *mysql_getsampwent(void *vp) {
117 DEBUG(5,("%s\n",FUNCTION_MACRO));
119 return (struct sam_passwd*)mysql_fill_sam_passwd( mysql_getpwent(vp) );
122 struct sam_passwd *mysql_getsampwrid(uint32 rid) {
125 DEBUG(5,("%s\n",FUNCTION_MACRO));
127 slprintf( where, sizeof(where), "user_rid=%lu", (long unsigned)rid);
129 return (struct sam_passwd *)mysql_fetch_passwd( FILL_SAM, where );
132 struct sam_passwd *mysql_getsampwuid(uid_t uid) {
134 DEBUG(5,("%s\n",FUNCTION_MACRO));
136 return (struct sam_passwd *)mysql_getpwuid( FILL_SAM, uid );
139 struct sam_passwd *mysql_getsampwntnam(const char *nt_name) {
141 DEBUG(5,("%s\n",FUNCTION_MACRO));
143 return (struct sam_passwd *)mysql_getpwnam( FILL_SAM, "nt_name", nt_name);
146 struct sam_disp_info *mysql_getsamdispntnam(const char *nt_name) {
148 DEBUG(5,("%s\n",FUNCTION_MACRO));
150 return pwdb_sam_to_dispinfo(mysql_getsampwntnam(nt_name));
153 struct sam_disp_info *mysql_getsamdisprid(uint32 rid) {
155 DEBUG(5,("%s\n",FUNCTION_MACRO));
157 return pwdb_sam_to_dispinfo(mysql_getsampwrid(rid));
160 struct sam_disp_info *mysql_getsamdispent(void *vp) {
162 DEBUG(5,("%s\n",FUNCTION_MACRO));
164 return pwdb_sam_to_dispinfo(mysql_getsampwent(vp));
167 static BOOL mysql_mod_sam( MYSQL *handle, struct sam_passwd *sam, BOOL override ) {
169 DEBUG(5,("%s\n",FUNCTION_MACRO));
174 BOOL mysql_add_sampwd_entry(struct sam_passwd *sam) {
176 struct smb_passwd *smb;
178 DEBUG(5,("%s\n",FUNCTION_MACRO));
180 smb = pwdb_sam_to_smb( sam );
186 if ( mysql_db_lock_connect( &handle ) ) {
190 if ( !mysql_add_smb( &handle, smb ) ) {
191 mysql_close(&handle);
195 if ( !mysql_mod_smb( &handle, smb, True ) ) {
196 mysql_del_smb( &handle, smb->unix_name );
197 mysql_close(&handle);
201 if ( !mysql_mod_sam( &handle, sam, True ) ) {
202 mysql_del_smb( &handle, smb->unix_name );
203 mysql_close(&handle);
207 mysql_close(&handle);
211 BOOL mysql_mod_sampwd_entry(struct sam_passwd *sam, BOOL override) {
213 struct smb_passwd *smb;
215 DEBUG(5,("%s\n",FUNCTION_MACRO));
217 smb = pwdb_sam_to_smb(sam);
223 if ( mysql_db_lock_connect( &handle ) ) {
227 if ( !mysql_mod_smb( &handle, smb, override ) ) {
228 mysql_close(&handle);
232 if ( !mysql_mod_sam( &handle, sam, override ) ) {
233 mysql_close(&handle);
237 mysql_close(&handle);
241 static struct sam_passdb_ops sam_mysql_ops =
251 mysql_add_sampwd_entry,
252 mysql_mod_sampwd_entry,
253 mysql_getsamdispntnam,
258 struct sam_passdb_ops *mysql_initialise_sam_password_db(void)
260 return &sam_mysql_ops;
264 void mysql_dummy_sam_function(void) { }