r85: Update the winbind interface version, as I just extended the struct.
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_pgsql.c
blob1731c720a21b9c0a8678d16ec26168eacc9b2085
1 /*
2 * PostgresSQL password backend for samba
3 * Copyright (C) Hamish Friedlander 2003
4 * Copyright (C) Jelmer Vernooij 2004
5 *
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include <libpq-fe.h>
24 #define CONFIG_HOST_DEFAULT "localhost"
25 #define CONFIG_USER_DEFAULT "samba"
26 #define CONFIG_PASS_DEFAULT ""
27 #define CONFIG_PORT_DEFAULT "5432"
28 #define CONFIG_DB_DEFAULT "samba"
30 /* handles for doing db transactions */
31 typedef struct pdb_pgsql_data {
32 PGconn *handle ;
33 PGresult *pwent ;
34 long currow ;
36 const char *location ;
37 } pdb_pgsql_data ;
39 #define SET_DATA(data,methods) { \
40 if(!methods){ \
41 DEBUG(0, ("invalid methods!\n")); \
42 return NT_STATUS_INVALID_PARAMETER; \
43 } \
44 data = (struct pdb_pgsql_data *)methods->private_data; \
45 if(!data || !(data->handle)){ \
46 DEBUG(0, ("invalid handle!\n")); \
47 return NT_STATUS_INVALID_HANDLE; \
48 } \
51 #define SET_DATA_QUIET(data,methods) { \
52 if(!methods){ \
53 DEBUG(0, ("invalid methods!\n")); \
54 return ; \
55 } \
56 data = (struct pdb_pgsql_data *)methods->private_data; \
57 if(!data || !(data->handle)){ \
58 DEBUG(0, ("invalid handle!\n")); \
59 return ; \
60 } \
64 #define config_value( data, name, default_value ) \
65 lp_parm_const_string( GLOBAL_SECTION_SNUM, (data)->location, name, default_value )
67 static long PQgetlong( PGresult *r, long row, long col )
69 if ( PQgetisnull( r, row, col ) ) return 0 ;
71 return atol( PQgetvalue( r, row, col ) ) ;
74 static NTSTATUS row_to_sam_account ( PGresult *r, long row, SAM_ACCOUNT *u )
76 pstring temp ;
77 DOM_SID sid ;
79 if ( row >= PQntuples( r ) ) return NT_STATUS_INVALID_PARAMETER ;
81 pdb_set_logon_time ( u, PQgetlong ( r, row, 0 ), PDB_SET ) ;
82 pdb_set_logoff_time ( u, PQgetlong ( r, row, 1 ), PDB_SET ) ;
83 pdb_set_kickoff_time ( u, PQgetlong ( r, row, 2 ), PDB_SET ) ;
84 pdb_set_pass_last_set_time ( u, PQgetlong ( r, row, 3 ), PDB_SET ) ;
85 pdb_set_pass_can_change_time ( u, PQgetlong ( r, row, 4 ), PDB_SET ) ;
86 pdb_set_pass_must_change_time( u, PQgetlong ( r, row, 5 ), PDB_SET ) ;
87 pdb_set_username ( u, PQgetvalue( r, row, 6 ), PDB_SET ) ;
88 pdb_set_domain ( u, PQgetvalue( r, row, 7 ), PDB_SET ) ;
89 pdb_set_nt_username ( u, PQgetvalue( r, row, 8 ), PDB_SET ) ;
90 pdb_set_fullname ( u, PQgetvalue( r, row, 9 ), PDB_SET ) ;
91 pdb_set_homedir ( u, PQgetvalue( r, row, 10 ), PDB_SET ) ;
92 pdb_set_dir_drive ( u, PQgetvalue( r, row, 11 ), PDB_SET ) ;
93 pdb_set_logon_script ( u, PQgetvalue( r, row, 12 ), PDB_SET ) ;
94 pdb_set_profile_path ( u, PQgetvalue( r, row, 13 ), PDB_SET ) ;
95 pdb_set_acct_desc ( u, PQgetvalue( r, row, 14 ), PDB_SET ) ;
96 pdb_set_workstations ( u, PQgetvalue( r, row, 15 ), PDB_SET ) ;
97 pdb_set_unknown_str ( u, PQgetvalue( r, row, 16 ), PDB_SET ) ;
98 pdb_set_munged_dial ( u, PQgetvalue( r, row, 17 ), PDB_SET ) ;
100 pdb_set_acct_ctrl ( u, PQgetlong ( r, row, 23 ), PDB_SET ) ;
101 pdb_set_logon_divs ( u, PQgetlong ( r, row, 25 ), PDB_SET ) ;
102 pdb_set_hours_len ( u, PQgetlong ( r, row, 26 ), PDB_SET ) ;
103 pdb_set_logon_count ( u, PQgetlong ( r, row, 27 ), PDB_SET ) ;
104 pdb_set_unknown_6 ( u, PQgetlong ( r, row, 28 ), PDB_SET ) ;
106 if ( !PQgetisnull( r, row, 18 ) ) string_to_sid( &sid, PQgetvalue( r, row, 18 ) ) ;
107 pdb_set_user_sid ( u, &sid, PDB_SET ) ;
108 if ( !PQgetisnull( r, row, 19 ) ) string_to_sid( &sid, PQgetvalue( r, row, 19 ) ) ;
109 pdb_set_group_sid( u, &sid, PDB_SET ) ;
111 if ( pdb_gethexpwd( PQgetvalue( r, row, 20 ), temp ), PDB_SET ) pdb_set_lanman_passwd( u, temp, PDB_SET ) ;
112 if ( pdb_gethexpwd( PQgetvalue( r, row, 21 ), temp ), PDB_SET ) pdb_set_nt_passwd ( u, temp, PDB_SET ) ;
114 /* Only use plaintext password storage when lanman and nt are NOT used */
115 if ( PQgetisnull( r, row, 20 ) || PQgetisnull( r, row, 21 ) ) pdb_set_plaintext_passwd( u, PQgetvalue( r, row, 22 ) ) ;
117 return NT_STATUS_OK ;
120 static NTSTATUS pgsqlsam_setsampwent(struct pdb_methods *methods, BOOL update)
122 struct pdb_pgsql_data *data ;
123 char *query ;
124 NTSTATUS retval ;
126 SET_DATA( data, methods ) ;
128 query = sql_account_query_select(data->location, update, SQL_SEARCH_NONE, NULL);
130 /* Do it */
131 DEBUG( 5, ("Executing query %s\n", query) ) ;
132 data->pwent = PQexec( data->handle, query ) ;
133 data->currow = 0 ;
135 /* Result? */
136 if ( data->pwent == NULL )
138 DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
139 retval = NT_STATUS_UNSUCCESSFUL ;
141 else if ( PQresultStatus( data->pwent ) != PGRES_TUPLES_OK )
143 DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( data->pwent ) ) ) ;
144 retval = NT_STATUS_UNSUCCESSFUL ;
146 else
148 DEBUG( 5, ("pgsqlsam_setsampwent succeeded(%llu results)!\n", PQntuples(data->pwent)) ) ;
149 retval = NT_STATUS_OK ;
152 SAFE_FREE(query);
153 return retval ;
156 /***************************************************************
157 End enumeration of the passwd list.
158 ****************************************************************/
160 static void pgsqlsam_endsampwent(struct pdb_methods *methods)
162 struct pdb_pgsql_data *data ;
164 SET_DATA_QUIET( data, methods ) ;
166 if (data->pwent != NULL)
168 PQclear( data->pwent ) ;
171 data->pwent = NULL ;
172 data->currow = 0 ;
174 DEBUG( 5, ("pgsql_endsampwent called\n") ) ;
177 /*****************************************************************
178 Get one SAM_ACCOUNT from the list (next in line)
179 *****************************************************************/
181 static NTSTATUS pgsqlsam_getsampwent( struct pdb_methods *methods, SAM_ACCOUNT *user )
183 struct pdb_pgsql_data *data;
184 NTSTATUS retval ;
186 SET_DATA( data, methods ) ;
188 if ( data->pwent == NULL )
190 DEBUG( 0, ("invalid pwent\n") ) ;
191 return NT_STATUS_INVALID_PARAMETER ;
194 retval = row_to_sam_account( data->pwent, data->currow, user ) ;
195 data->currow++ ;
197 return retval ;
200 static NTSTATUS pgsqlsam_select_by_field ( struct pdb_methods *methods, SAM_ACCOUNT *user, enum sql_search_field field, const char *sname )
202 struct pdb_pgsql_data *data ;
204 char *esc ;
205 char *query ;
207 PGresult *result ;
208 NTSTATUS retval ;
210 SET_DATA(data, methods);
212 if ( user == NULL )
214 DEBUG( 0, ("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n") ) ;
215 return NT_STATUS_INVALID_PARAMETER;
218 DEBUG( 5, ("pgsqlsam_select_by_field: getting data where %d = %s(nonescaped)\n", field, sname) ) ;
220 /* Escape sname */
221 esc = malloc(strlen(sname) * 2 + 1);
222 if ( !esc )
224 DEBUG(0, ("Can't allocate memory to store escaped name\n"));
225 return NT_STATUS_NO_MEMORY;
228 //tmp_sname = smb_xstrdup(sname);
229 PQescapeString( esc, sname, strlen(sname) ) ;
231 query = sql_account_query_select(data->location, True, field, esc);
233 /* Do it */
234 DEBUG( 5, ("Executing query %s\n", query) ) ;
235 result = PQexec( data->handle, query ) ;
237 /* Result? */
238 if ( result == NULL )
240 DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
241 retval = NT_STATUS_UNSUCCESSFUL ;
243 else if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
245 DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( result ) ) ) ;
246 retval = NT_STATUS_UNSUCCESSFUL ;
248 else
250 retval = row_to_sam_account( result, 0, user ) ;
253 SAFE_FREE( esc ) ;
254 SAFE_FREE( query ) ;
256 PQclear( result ) ;
258 return retval ;
261 /******************************************************************
262 Lookup a name in the SAM database
263 ******************************************************************/
265 static NTSTATUS pgsqlsam_getsampwnam ( struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname )
267 struct pdb_pgsql_data *data;
269 SET_DATA(data, methods);
271 if ( !sname )
273 DEBUG( 0, ("invalid name specified") ) ;
274 return NT_STATUS_INVALID_PARAMETER;
277 return pgsqlsam_select_by_field( methods, user, SQL_SEARCH_USER_NAME, sname ) ;
281 /***************************************************************************
282 Search by sid
283 **************************************************************************/
285 static NTSTATUS pgsqlsam_getsampwsid ( struct pdb_methods *methods, SAM_ACCOUNT *user, const DOM_SID *sid )
287 struct pdb_pgsql_data *data;
288 fstring sid_str;
290 SET_DATA( data, methods ) ;
292 sid_to_string( sid_str, sid ) ;
294 return pgsqlsam_select_by_field( methods, user, SQL_SEARCH_USER_SID, sid_str ) ;
297 /***************************************************************************
298 Delete a SAM_ACCOUNT
299 ****************************************************************************/
301 static NTSTATUS pgsqlsam_delete_sam_account( struct pdb_methods *methods, SAM_ACCOUNT *sam_pass )
303 struct pdb_pgsql_data *data ;
305 const char *sname = pdb_get_username( sam_pass ) ;
306 char *esc ;
307 char *query ;
309 PGresult *result ;
310 NTSTATUS retval ;
312 SET_DATA(data, methods);
314 if ( !sname )
316 DEBUG( 0, ("invalid name specified\n") ) ;
317 return NT_STATUS_INVALID_PARAMETER ;
320 /* Escape sname */
321 esc = malloc(strlen(sname) * 2 + 1);
322 if ( !esc )
324 DEBUG(0, ("Can't allocate memory to store escaped name\n"));
325 return NT_STATUS_NO_MEMORY;
328 PQescapeString( esc, sname, strlen(sname) ) ;
330 query = sql_account_query_delete(data->location, esc);
332 /* Do it */
333 result = PQexec( data->handle, query ) ;
335 if ( result == NULL )
337 DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
338 retval = NT_STATUS_UNSUCCESSFUL ;
340 else if ( PQresultStatus( result ) != PGRES_COMMAND_OK )
342 DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( result ) ) ) ;
343 retval = NT_STATUS_UNSUCCESSFUL ;
345 else
347 DEBUG( 5, ("User '%s' deleted\n", sname) ) ;
348 retval = NT_STATUS_OK ;
351 SAFE_FREE( esc ) ;
352 SAFE_FREE( query ) ;
354 return retval ;
357 static NTSTATUS pgsqlsam_replace_sam_account( struct pdb_methods *methods, const SAM_ACCOUNT *newpwd, char isupdate )
359 struct pdb_pgsql_data *data ;
360 char *query;
361 PGresult *result ;
363 if ( !methods )
365 DEBUG( 0, ("invalid methods!\n") ) ;
366 return NT_STATUS_INVALID_PARAMETER ;
369 data = (struct pdb_pgsql_data *) methods->private_data ;
371 if ( data == NULL || data->handle == NULL )
373 DEBUG( 0, ("invalid handle!\n") ) ;
374 return NT_STATUS_INVALID_HANDLE ;
377 query = sql_account_query_update(data->location, newpwd, isupdate);
379 result = PQexec( data->handle, query ) ;
382 /* Execute the query */
383 if ( result == NULL )
385 DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
386 return NT_STATUS_INVALID_PARAMETER;
388 else if ( PQresultStatus( result ) != PGRES_COMMAND_OK )
390 DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( result ) ) ) ;
391 return NT_STATUS_INVALID_PARAMETER;
393 SAFE_FREE(query);
395 return NT_STATUS_OK;
398 static NTSTATUS pgsqlsam_add_sam_account ( struct pdb_methods *methods, SAM_ACCOUNT *newpwd )
400 return pgsqlsam_replace_sam_account( methods, newpwd, 0 ) ;
403 static NTSTATUS pgsqlsam_update_sam_account ( struct pdb_methods *methods, SAM_ACCOUNT *newpwd )
405 return pgsqlsam_replace_sam_account( methods, newpwd, 1 ) ;
408 static NTSTATUS pgsqlsam_init ( struct pdb_context *pdb_context, struct pdb_methods **pdb_method, const char *location )
410 NTSTATUS nt_status ;
411 struct pdb_pgsql_data *data ;
413 if ( !pdb_context )
415 DEBUG( 0, ("invalid pdb_methods specified\n") ) ;
416 return NT_STATUS_UNSUCCESSFUL;
419 if (!NT_STATUS_IS_OK
420 (nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
421 return nt_status;
424 (*pdb_method)->name = "pgsqlsam" ;
426 (*pdb_method)->setsampwent = pgsqlsam_setsampwent ;
427 (*pdb_method)->endsampwent = pgsqlsam_endsampwent ;
428 (*pdb_method)->getsampwent = pgsqlsam_getsampwent ;
429 (*pdb_method)->getsampwnam = pgsqlsam_getsampwnam ;
430 (*pdb_method)->getsampwsid = pgsqlsam_getsampwsid ;
431 (*pdb_method)->add_sam_account = pgsqlsam_add_sam_account ;
432 (*pdb_method)->update_sam_account = pgsqlsam_update_sam_account ;
433 (*pdb_method)->delete_sam_account = pgsqlsam_delete_sam_account ;
435 data = talloc( pdb_context->mem_ctx, sizeof( struct pdb_pgsql_data ) ) ;
436 (*pdb_method)->private_data = data ;
437 data->handle = NULL ;
438 data->pwent = NULL ;
440 if ( !location )
442 DEBUG( 0, ("No identifier specified. Check the Samba HOWTO Collection for details\n") ) ;
443 return NT_STATUS_INVALID_PARAMETER;
446 data->location = smb_xstrdup( location ) ;
448 if(!sql_account_config_valid(data->location)) {
449 return NT_STATUS_INVALID_PARAMETER;
452 DEBUG
456 "Connecting to database server, host: %s, user: %s, password: XXXXXX, database: %s, port: %s\n",
457 config_value( data, "pgsql host" , CONFIG_HOST_DEFAULT ),
458 config_value( data, "pgsql user" , CONFIG_USER_DEFAULT ),
459 config_value( data, "pgsql database", CONFIG_DB_DEFAULT ),
460 config_value( data, "pgsql port" , CONFIG_PORT_DEFAULT )
464 /* Do the pgsql initialization */
465 data->handle = PQsetdbLogin(
466 config_value( data, "pgsql host" , CONFIG_HOST_DEFAULT ),
467 config_value( data, "pgsql port" , CONFIG_PORT_DEFAULT ),
468 NULL,
469 NULL,
470 config_value( data, "pgsql database", CONFIG_DB_DEFAULT ),
471 config_value( data, "pgsql user" , CONFIG_USER_DEFAULT ),
472 config_value( data, "pgsql password", CONFIG_PASS_DEFAULT )
475 if ( PQstatus( data->handle ) != CONNECTION_OK )
477 DEBUG( 0, ("Failed to connect to pgsql database: error: %s\n", PQerrorMessage( data->handle )) ) ;
478 return NT_STATUS_UNSUCCESSFUL;
481 DEBUG( 5, ("Connected to pgsql database\n") ) ;
482 return NT_STATUS_OK;
485 NTSTATUS pdb_pgsql_init(void)
487 return smb_register_passdb( PASSDB_INTERFACE_VERSION, "pgsql", pgsqlsam_init ) ;