r2619: Only issue the ldap extended password change operation if the ldap server
[ira/wip.git] / source / passdb / pdb_pgsql.c
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.
10  * 
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.
15  * 
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.
19  */
20
21 #include "includes.h"
22 #include <libpq-fe.h>
23
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"
29
30 /* handles for doing db transactions */
31 typedef struct pdb_pgsql_data {
32   PGconn     *handle ;
33   PGresult   *pwent  ;
34   long        currow ;
35   
36   const char *location ;
37 } pdb_pgsql_data ;
38
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                 } \
49 }
50
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                 } \
61 }
62
63
64 #define config_value( data, name, default_value ) \
65   lp_parm_const_string( GLOBAL_SECTION_SNUM, (data)->location, name, default_value )
66
67 static long PQgetlong( PGresult *r, long row, long col )
68 {
69   if ( PQgetisnull( r, row, col ) ) return 0 ;
70   
71   return atol( PQgetvalue( r, row,  col ) ) ;
72 }
73
74 static NTSTATUS row_to_sam_account ( PGresult *r, long row, SAM_ACCOUNT *u )
75 {
76   pstring temp ;
77   DOM_SID sid ;
78   
79   if ( row >= PQntuples( r ) ) return NT_STATUS_INVALID_PARAMETER ;
80
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 ) ;
99   
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 ) ;
105   
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 ) ;
110   
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 ) ;
113   
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 ) ) ;
116   
117   return NT_STATUS_OK ;
118 }
119
120 static NTSTATUS pgsqlsam_setsampwent(struct pdb_methods *methods, BOOL update)
121 {
122   struct pdb_pgsql_data *data ;
123   char *query ;
124   NTSTATUS retval ;
125   
126   SET_DATA( data, methods ) ;
127   
128   query = sql_account_query_select(data->location, update, SQL_SEARCH_NONE, NULL);
129   
130   /* Do it */
131   DEBUG( 5, ("Executing query %s\n", query) ) ;
132   data->pwent  = PQexec( data->handle, query ) ;
133   data->currow = 0 ;
134   
135   /* Result? */
136   if ( data->pwent == NULL )
137   {
138     DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
139     retval = NT_STATUS_UNSUCCESSFUL ;
140   }
141   else if ( PQresultStatus( data->pwent ) != PGRES_TUPLES_OK )
142   {
143     DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( data->pwent ) ) ) ;
144     retval = NT_STATUS_UNSUCCESSFUL ;
145   }
146   else
147   {
148     DEBUG( 5, ("pgsqlsam_setsampwent succeeded(%llu results)!\n", PQntuples(data->pwent)) ) ;
149     retval = NT_STATUS_OK ;
150   }
151   
152   SAFE_FREE(query);
153   return retval ;
154 }
155
156 /***************************************************************
157   End enumeration of the passwd list.
158  ****************************************************************/
159
160 static void pgsqlsam_endsampwent(struct pdb_methods *methods)
161 {
162   struct pdb_pgsql_data *data ; 
163     
164   SET_DATA_QUIET( data, methods ) ;
165   
166   if (data->pwent != NULL)
167   {
168     PQclear( data->pwent ) ;
169   }
170   
171   data->pwent  = NULL ;
172   data->currow = 0 ;
173   
174   DEBUG( 5, ("pgsql_endsampwent called\n") ) ;
175 }
176
177 /*****************************************************************
178   Get one SAM_ACCOUNT from the list (next in line)
179  *****************************************************************/
180
181 static NTSTATUS pgsqlsam_getsampwent( struct pdb_methods *methods, SAM_ACCOUNT *user )
182 {
183   struct pdb_pgsql_data *data;
184   NTSTATUS retval ;
185   
186   SET_DATA( data, methods ) ;
187   
188   if ( data->pwent == NULL )
189   {
190     DEBUG( 0, ("invalid pwent\n") ) ;
191     return NT_STATUS_INVALID_PARAMETER ;
192   }
193   
194   retval = row_to_sam_account( data->pwent, data->currow, user ) ;
195   data->currow++ ;
196   
197   return retval ;
198 }
199
200 static NTSTATUS pgsqlsam_select_by_field ( struct pdb_methods *methods, SAM_ACCOUNT *user, enum sql_search_field field, const char *sname )
201 {
202   struct pdb_pgsql_data *data ;
203   
204   char *esc ;
205   char *query ;
206   
207   PGresult *result ;
208   NTSTATUS retval ;
209
210   SET_DATA(data, methods);
211
212   if ( user == NULL )
213   {
214     DEBUG( 0, ("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n") ) ;
215     return NT_STATUS_INVALID_PARAMETER;
216   }
217   
218   DEBUG( 5, ("pgsqlsam_select_by_field: getting data where %d = %s(nonescaped)\n", field, sname) ) ;
219   
220   /* Escape sname */
221   esc = malloc(strlen(sname) * 2 + 1);
222   if ( !esc )
223   {
224     DEBUG(0, ("Can't allocate memory to store escaped name\n"));
225     return NT_STATUS_NO_MEMORY; 
226   }
227   
228   //tmp_sname = smb_xstrdup(sname);
229   PQescapeString( esc, sname, strlen(sname) ) ;
230   
231   query = sql_account_query_select(data->location, True, field, esc);
232   
233   /* Do it */
234   DEBUG( 5, ("Executing query %s\n", query) ) ;
235   result = PQexec( data->handle, query ) ;
236   
237   /* Result? */
238   if ( result == NULL )
239   {
240     DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
241     retval = NT_STATUS_UNSUCCESSFUL ;
242   }
243   else if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
244   {
245     DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( result ) ) ) ;
246     retval = NT_STATUS_UNSUCCESSFUL ;
247   }
248   else
249   {
250     retval = row_to_sam_account( result, 0, user ) ;
251   }
252   
253   SAFE_FREE( esc   ) ;
254   SAFE_FREE( query ) ;
255   
256   PQclear( result ) ;
257   
258   return retval ;
259 }
260
261 /******************************************************************
262   Lookup a name in the SAM database
263  ******************************************************************/
264
265 static NTSTATUS pgsqlsam_getsampwnam ( struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname )
266 {
267   struct pdb_pgsql_data *data;
268   
269   SET_DATA(data, methods);
270   
271   if ( !sname )
272   {
273     DEBUG( 0, ("invalid name specified") ) ;
274     return NT_STATUS_INVALID_PARAMETER;
275   }
276   
277   return pgsqlsam_select_by_field( methods, user, SQL_SEARCH_USER_NAME, sname ) ;
278 }
279
280
281 /***************************************************************************
282   Search by sid
283  **************************************************************************/
284
285 static NTSTATUS pgsqlsam_getsampwsid ( struct pdb_methods *methods, SAM_ACCOUNT *user, const DOM_SID *sid )
286 {
287   struct pdb_pgsql_data *data;
288   fstring sid_str;
289   
290   SET_DATA( data, methods ) ;
291   
292   sid_to_string( sid_str, sid ) ;
293   
294   return pgsqlsam_select_by_field( methods, user, SQL_SEARCH_USER_SID, sid_str ) ;
295 }
296
297 /***************************************************************************
298   Delete a SAM_ACCOUNT
299  ****************************************************************************/
300
301 static NTSTATUS pgsqlsam_delete_sam_account( struct pdb_methods *methods, SAM_ACCOUNT *sam_pass )
302 {
303   struct pdb_pgsql_data *data ;
304   
305   const char *sname = pdb_get_username( sam_pass ) ;
306   char *esc ;
307   char *query ;
308   
309   PGresult *result ;
310   NTSTATUS retval ;
311   
312   SET_DATA(data, methods);
313   
314   if ( !sname )
315   {
316     DEBUG( 0, ("invalid name specified\n") ) ;
317     return NT_STATUS_INVALID_PARAMETER ;
318   }
319   
320   /* Escape sname */
321   esc = malloc(strlen(sname) * 2 + 1);
322   if ( !esc )
323   {
324     DEBUG(0, ("Can't allocate memory to store escaped name\n"));
325     return NT_STATUS_NO_MEMORY;
326   }
327   
328   PQescapeString( esc, sname, strlen(sname) ) ;
329   
330   query = sql_account_query_delete(data->location, esc);
331   
332   /* Do it */
333   result = PQexec( data->handle, query ) ;
334   
335   if ( result == NULL )
336   {
337     DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
338     retval = NT_STATUS_UNSUCCESSFUL ;
339   }
340   else if ( PQresultStatus( result ) != PGRES_COMMAND_OK )
341   {
342     DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( result ) ) ) ;
343     retval = NT_STATUS_UNSUCCESSFUL ;
344   }
345   else
346   {
347     DEBUG( 5, ("User '%s' deleted\n", sname) ) ;
348     retval = NT_STATUS_OK ;
349   }
350   
351   SAFE_FREE( esc ) ;
352   SAFE_FREE( query ) ;
353   
354   return retval ;
355 }
356
357 static NTSTATUS pgsqlsam_replace_sam_account( struct pdb_methods *methods, const SAM_ACCOUNT *newpwd, char isupdate )
358 {
359   struct pdb_pgsql_data *data ;
360   char *query;
361   PGresult *result ;
362   
363   if ( !methods )
364   {
365     DEBUG( 0, ("invalid methods!\n") ) ;
366     return NT_STATUS_INVALID_PARAMETER ;
367   }
368   
369   data = (struct pdb_pgsql_data *) methods->private_data ;
370   
371   if ( data == NULL || data->handle == NULL )
372   {
373     DEBUG( 0, ("invalid handle!\n") ) ;
374     return NT_STATUS_INVALID_HANDLE ;
375   }
376
377   query = sql_account_query_update(data->location, newpwd, isupdate);
378
379   result = PQexec( data->handle, query ) ;
380
381   
382   /* Execute the query */
383   if ( result == NULL )
384   {
385     DEBUG( 0, ("Error executing %s, %s\n", query, PQerrorMessage( data->handle ) ) ) ;
386     return NT_STATUS_INVALID_PARAMETER;
387   }
388   else if ( PQresultStatus( result ) != PGRES_COMMAND_OK )
389   {
390     DEBUG( 0, ("Error executing %s, %s\n", query, PQresultErrorMessage( result ) ) ) ;
391     return NT_STATUS_INVALID_PARAMETER;
392   }
393   SAFE_FREE(query);
394   
395   return NT_STATUS_OK;
396 }
397
398 static NTSTATUS pgsqlsam_add_sam_account ( struct pdb_methods *methods, SAM_ACCOUNT *newpwd )
399 {
400   return pgsqlsam_replace_sam_account( methods, newpwd, 0 ) ;
401 }
402
403 static NTSTATUS pgsqlsam_update_sam_account ( struct pdb_methods *methods, SAM_ACCOUNT *newpwd )
404 {
405   return pgsqlsam_replace_sam_account( methods, newpwd, 1 ) ;
406 }
407
408 static NTSTATUS pgsqlsam_init ( struct pdb_context *pdb_context, struct pdb_methods **pdb_method, const char *location )
409 {
410   NTSTATUS nt_status ;
411   struct pdb_pgsql_data *data ;
412   
413   if ( !pdb_context )
414   {
415     DEBUG( 0, ("invalid pdb_methods specified\n") ) ;
416     return NT_STATUS_UNSUCCESSFUL;
417   }
418   
419   if (!NT_STATUS_IS_OK
420     (nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
421     return nt_status;
422   }
423   
424   (*pdb_method)->name               = "pgsqlsam" ;
425   
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 ;
434   
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 ;
439
440   if ( !location )
441   {
442     DEBUG( 0, ("No identifier specified. Check the Samba HOWTO Collection for details\n") ) ;
443     return NT_STATUS_INVALID_PARAMETER;
444   }
445   
446   data->location = smb_xstrdup( location ) ;
447
448   if(!sql_account_config_valid(data->location)) {
449           return NT_STATUS_INVALID_PARAMETER;
450   }
451   
452   DEBUG
453   (
454     1, 
455     (
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 )
461     )
462   ) ;
463   
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 )
473                              ) ;
474   
475   if ( PQstatus( data->handle ) != CONNECTION_OK )
476   {
477     DEBUG( 0, ("Failed to connect to pgsql database: error: %s\n", PQerrorMessage( data->handle )) ) ;
478     return NT_STATUS_UNSUCCESSFUL;
479   }
480   
481   DEBUG( 5, ("Connected to pgsql database\n") ) ;
482   return NT_STATUS_OK;
483 }
484
485 NTSTATUS pdb_pgsql_init(void) 
486 {
487   return smb_register_passdb( PASSDB_INTERFACE_VERSION, "pgsql", pgsqlsam_init ) ;
488 }