summaryrefslogtreecommitdiff
path: root/source3/passdb/mysqlsampass.c
blob: 8d3049ce0ac7c67c767345f2f7e04f4de02e23a4 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* 
 *  Unix SMB/Netbios implementation.
 *  Version 1.9.
 *  Samba MYSQL SAM Database, by Benjamin Kuit.
 *  Copyright (C) Benjamin Kuit                     1999,
 *  Copyright (C) Andrew Tridgell              1992-1999,
 *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef WITH_MYSQLSAM

#include "includes.h"
#include <mysql.h>

extern int DEBUGLEVEL;

extern pstring samlogon_user;
extern BOOL sam_logon_in_ssb;

typedef void *(*mysql_fill_func)( MYSQL_ROW * );
#define FILL_SAM mysql_fill_sam_passwd

void *mysql_startpwent(BOOL update);
void mysql_endpwent(void *vp);
SMB_BIG_UINT mysql_getpwpos(void *vp);
BOOL mysql_setpwpos(void *vp, SMB_BIG_UINT pos);
void *mysql_fill_smb_passwd( MYSQL_ROW *row );
MYSQL_ROW *mysql_getpwent(void *vp);
void *mysql_fetch_passwd( mysql_fill_func filler, char *where );
void *mysql_getpwuid( mysql_fill_func filler, uid_t uid );
void *mysql_getpwnam( mysql_fill_func filler, char *field, const char *name );
int mysql_db_lock_connect( MYSQL *handle );
BOOL mysql_add_smb( MYSQL *handle, struct smb_passwd *smb );
BOOL mysql_mod_smb( MYSQL *handle, struct smb_passwd *smb, BOOL override );
BOOL mysql_del_smb( MYSQL *handle, char *unix_name );

void *mysql_fill_sam_passwd( MYSQL_ROW *row ) {
	static struct sam_passwd *user;

	static pstring full_name;
	static pstring home_dir;
	static pstring home_drive;
	static pstring logon_script;
	static pstring profile_path;
	static pstring acct_desc;
	static pstring workstations;

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	user = pwdb_smb_to_sam((struct smb_passwd *)mysql_fill_smb_passwd(row));

	if ( user == NULL ) {
		return NULL;
	}

	/* 'Researched' from sampass.c =) */

	pstrcpy(samlogon_user, user->unix_name);

	if (samlogon_user[strlen(samlogon_user)-1] == '$' && 
	    user->group_rid != DOMAIN_GROUP_RID_USERS)
	{
		DEBUG(0,("trust account %s should be in DOMAIN_GROUP_RID_USERS\n", samlogon_user));
	}

	/* XXXX hack to get standard_sub_basic() to use sam logon username */
	/* possibly a better way would be to do a become_user() call */
	sam_logon_in_ssb = True;

	pstrcpy(full_name    , "");
	pstrcpy(logon_script , lp_logon_script  ());
	pstrcpy(profile_path , lp_logon_path    ());
	pstrcpy(home_drive   , lp_logon_drive   ());
	pstrcpy(home_dir     , lp_logon_home    ());
	pstrcpy(acct_desc    , "");
	pstrcpy(workstations , "");

	sam_logon_in_ssb = False;

	user->full_name    = full_name;
	user->home_dir     = home_dir;
	user->dir_drive    = home_drive;
	user->logon_script = logon_script;
	user->profile_path = profile_path;
	user->acct_desc    = acct_desc;
	user->workstations = workstations;

	user->unknown_str = NULL; /* don't know, yet! */
	user->munged_dial = NULL; /* "munged" dial-back telephone number */

	user->unknown_3 = 0xffffff; /* don't know */
	user->logon_divs = 168; /* hours per week */
	user->hours_len = 21; /* 21 times 8 bits = 168 */
	memset(user->hours, 0xff, user->hours_len); /* available at all hours */
	user->unknown_5 = 0x00020000; /* don't know */
	user->unknown_6 = 0x000004ec; /* don't know */

	return (void*)user;
}

struct sam_passwd *mysql_getsampwent(void *vp) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return (struct sam_passwd*)mysql_fill_sam_passwd( mysql_getpwent(vp) );
}

struct sam_passwd *mysql_getsampwrid(uint32 rid) {
	fstring where;

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	slprintf( where, sizeof(where), "user_rid=%lu", (long unsigned)rid);

	return (struct sam_passwd *)mysql_fetch_passwd( FILL_SAM, where );
}

struct sam_passwd *mysql_getsampwuid(uid_t uid) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return (struct sam_passwd *)mysql_getpwuid( FILL_SAM, uid );
}

struct sam_passwd *mysql_getsampwntnam(const char *nt_name) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return (struct sam_passwd *)mysql_getpwnam( FILL_SAM, "nt_name", nt_name);
}

struct sam_disp_info *mysql_getsamdispntnam(const char *nt_name) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return pwdb_sam_to_dispinfo(mysql_getsampwntnam(nt_name));
}

struct sam_disp_info *mysql_getsamdisprid(uint32 rid) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return pwdb_sam_to_dispinfo(mysql_getsampwrid(rid));
}

struct sam_disp_info *mysql_getsamdispent(void *vp) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return pwdb_sam_to_dispinfo(mysql_getsampwent(vp));
}

static BOOL mysql_mod_sam( MYSQL *handle, struct sam_passwd *sam, BOOL override ) {

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	return True;
}

BOOL mysql_add_sampwd_entry(struct sam_passwd *sam) {
	MYSQL handle;
	struct smb_passwd *smb;

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	smb = pwdb_sam_to_smb( sam );

	if ( smb == NULL ) {
		return False;
	}

	if ( mysql_db_lock_connect( &handle ) ) {
		return False;
	}

	if ( !mysql_add_smb( &handle, smb ) ) {
		mysql_close(&handle);
		return False;
	}

	if ( !mysql_mod_smb( &handle, smb, True ) ) {
		mysql_del_smb( &handle, smb->unix_name );
		mysql_close(&handle);
		return False;
	}

	if ( !mysql_mod_sam( &handle, sam, True ) ) {
		mysql_del_smb( &handle, smb->unix_name );
		mysql_close(&handle);
		return False;
	}

	mysql_close(&handle);
	return True;
}

BOOL mysql_mod_sampwd_entry(struct sam_passwd *sam, BOOL override) {
	MYSQL handle;
	struct smb_passwd *smb;

	DEBUG(5,("%s\n",FUNCTION_MACRO));

	smb = pwdb_sam_to_smb(sam);

	if ( smb == NULL ) {
		return False;
	}

	if ( mysql_db_lock_connect( &handle ) ) {
		return False;
	}

	if ( !mysql_mod_smb( &handle, smb, override ) ) {
		mysql_close(&handle);
		return False;
	}

	if ( !mysql_mod_sam( &handle, sam, override ) ) {
		mysql_close(&handle);
		return False;
	}

	mysql_close(&handle);
	return True;
}

static struct sam_passdb_ops sam_mysql_ops =
{
        mysql_startpwent,
        mysql_endpwent,
        mysql_getpwpos,
        mysql_setpwpos,
        mysql_getsampwntnam,
        mysql_getsampwuid,
        mysql_getsampwrid,
        mysql_getsampwent,
        mysql_add_sampwd_entry,
        mysql_mod_sampwd_entry,
        mysql_getsamdispntnam,
        mysql_getsamdisprid,
        mysql_getsamdispent
};

struct sam_passdb_ops *mysql_initialise_sam_password_db(void)
{
	return &sam_mysql_ops;
}

#else
	void mysql_dummy_sam_function(void) { }
#endif