Complain about duplicate charsets at debug level 0 instead of 2
[tprouty/samba.git] / source3 / passdb / pdb_mysql.c
1
2 /*
3  * MySQL password backend for samba
4  * Copyright (C) Jelmer Vernooij 2002
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 <mysql/mysql.h>
23
24 #define CONFIG_TABLE_DEFAULT                            "user"
25 #define CONFIG_LOGON_TIME_DEFAULT                       "logon_time"
26 #define CONFIG_LOGOFF_TIME_DEFAULT                      "logoff_time"
27 #define CONFIG_KICKOFF_TIME_DEFAULT                     "kickoff_time"
28 #define CONFIG_PASS_LAST_SET_TIME_DEFAULT               "pass_last_set_time"
29 #define CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT             "pass_can_change_time"
30 #define CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT            "pass_must_change_time"
31 #define CONFIG_USERNAME_DEFAULT                         "username"
32 #define CONFIG_DOMAIN_DEFAULT                           "domain"
33 #define CONFIG_NT_USERNAME_DEFAULT                      "nt_username"
34 #define CONFIG_FULLNAME_DEFAULT                         "nt_fullname"
35 #define CONFIG_HOME_DIR_DEFAULT                         "home_dir"
36 #define CONFIG_DIR_DRIVE_DEFAULT                        "dir_drive"
37 #define CONFIG_LOGON_SCRIPT_DEFAULT                     "logon_script"
38 #define CONFIG_PROFILE_PATH_DEFAULT                     "profile_path"
39 #define CONFIG_ACCT_DESC_DEFAULT                        "acct_desc"
40 #define CONFIG_WORKSTATIONS_DEFAULT                     "workstations"
41 #define CONFIG_UNKNOWN_STR_DEFAULT                      "unknown_str"
42 #define CONFIG_MUNGED_DIAL_DEFAULT                      "munged_dial"
43 #define CONFIG_UID_DEFAULT                              "uid"
44 #define CONFIG_GID_DEFAULT                              "gid"
45 #define CONFIG_USER_SID_DEFAULT                         "user_sid"
46 #define CONFIG_GROUP_SID_DEFAULT                        "group_sid"
47 #define CONFIG_LM_PW_DEFAULT                            "lm_pw"
48 #define CONFIG_NT_PW_DEFAULT                            "nt_pw"
49 #define CONFIG_PLAIN_PW_DEFAULT                         "NULL"
50 #define CONFIG_ACCT_CTRL_DEFAULT                        "acct_ctrl"
51 #define CONFIG_UNKNOWN_3_DEFAULT                        "unknown_3"
52 #define CONFIG_LOGON_DIVS_DEFAULT                       "logon_divs"
53 #define CONFIG_HOURS_LEN_DEFAULT                        "hours_len"
54 #define CONFIG_UNKNOWN_5_DEFAULT                        "unknown_5"
55 #define CONFIG_UNKNOWN_6_DEFAULT                        "unknown_6"
56 #define CONFIG_HOST_DEFAULT                             "localhost"
57 #define CONFIG_USER_DEFAULT                             "samba"
58 #define CONFIG_PASS_DEFAULT                             ""
59 #define CONFIG_PORT_DEFAULT                             "3306"
60 #define CONFIG_DB_DEFAULT                               "samba"
61
62 static int mysqlsam_debug_level = DBGC_ALL;
63
64 #undef DBGC_CLASS
65 #define DBGC_CLASS mysqlsam_debug_level
66
67 typedef struct pdb_mysql_data {
68         MYSQL *handle;
69         MYSQL_RES *pwent;
70         const char *location;
71 } pdb_mysql_data;
72
73 /* Used to construct insert and update queries */
74
75 typedef struct pdb_mysql_query {
76         char update;
77         TALLOC_CTX *mem_ctx;
78         char *part1;
79         char *part2;
80 } pdb_mysql_query;
81 #define SET_DATA(data,methods) { \
82         if(!methods){ \
83                 DEBUG(0, ("invalid methods!\n")); \
84                         return NT_STATUS_INVALID_PARAMETER; \
85         } \
86         data = (struct pdb_mysql_data *)methods->private_data; \
87                 if(!data || !(data->handle)){ \
88                         DEBUG(0, ("invalid handle!\n")); \
89                                 return NT_STATUS_INVALID_HANDLE; \
90                 } \
91 }
92
93 static void pdb_mysql_int_field(struct pdb_methods *m,
94                                         struct pdb_mysql_query *q, const char *name, int value)
95 {
96         if (!name || strchr(name, '\''))
97                 return;                 /* This field shouldn't be set by us */
98
99         if (q->update) {
100                 q->part1 =
101                         talloc_asprintf_append(q->mem_ctx, q->part1,
102                                                                    "%s = %d,", name, value);
103         } else {
104                 q->part1 =
105                         talloc_asprintf_append(q->mem_ctx, q->part1, "%s,", name);
106                 q->part2 =
107                         talloc_asprintf_append(q->mem_ctx, q->part2, "%d,", value);
108         }
109 }
110
111 static NTSTATUS pdb_mysql_string_field(struct pdb_methods *methods,
112                                            struct pdb_mysql_query *q,
113                                            const char *name, const char *value)
114 {
115         char *esc_value;
116         struct pdb_mysql_data *data;
117         char *tmp_value;
118
119         SET_DATA(data, methods);
120
121         if (!name || !value || !strcmp(value, "") || strchr(name, '\''))
122                 return NT_STATUS_INVALID_PARAMETER;   /* This field shouldn't be set by module */
123
124         esc_value = malloc(strlen(value) * 2 + 1);
125
126         tmp_value = smb_xstrdup(value);
127         mysql_real_escape_string(data->handle, esc_value, tmp_value,
128                                                          strlen(tmp_value));
129         SAFE_FREE(tmp_value);
130
131         if (q->update) {
132                 q->part1 =
133                         talloc_asprintf_append(q->mem_ctx, q->part1,
134                                                                    "%s = '%s',", name, esc_value);
135         } else {
136                 q->part1 =
137                         talloc_asprintf_append(q->mem_ctx, q->part1, "%s,", name);
138                 q->part2 =
139                         talloc_asprintf_append(q->mem_ctx, q->part2, "'%s',",
140                                                                    esc_value);
141         }
142
143         SAFE_FREE(esc_value);
144
145         return NT_STATUS_OK;
146 }
147
148 #define config_value(data,name,default_value) \
149         lp_parm_const_string(GLOBAL_SECTION_SNUM, (data)->location, name, default_value)
150
151 static const char * config_value_write(pdb_mysql_data * data, const char *name, const char *default_value) {
152         char const *v = NULL;
153         char const *swrite = NULL;
154
155         v = lp_parm_const_string(GLOBAL_SECTION_SNUM, data->location, name, default_value);
156
157         if (!v)
158                 return NULL;
159
160         swrite = strchr(v, ':');
161
162         /* Default to the same field as read field */
163         if (!swrite)
164                 return v;
165
166         swrite++;
167
168         /* If the field is 0 chars long, we shouldn't write to it */
169         if (!strlen(swrite) || !strcmp(swrite, "NULL"))
170                 return NULL;
171
172         /* Otherwise, use the additionally specified */
173         return swrite;
174 }
175
176 static const char * config_value_read(pdb_mysql_data * data, const char *name, const char *default_value)
177 {
178         char *v = NULL;
179         char *swrite;
180
181         v = lp_parm_talloc_string(GLOBAL_SECTION_SNUM, data->location, name, default_value);
182
183         if (!v)
184                 return "NULL";
185
186         swrite = strchr(v, ':');
187
188         /* If no write is specified, there are no problems */
189         if (!swrite) {
190                 if (strlen(v) == 0)
191                         return "NULL";
192                 return (const char *)v;
193         }
194
195         /* Otherwise, we have to cut the ':write_part' */
196         *swrite = '\0';
197         if (strlen(v) == 0)
198                 return "NULL";
199
200         return (const char *)v;
201 }
202
203 /* Wrapper for atol that returns 0 if 'a' points to NULL */
204 static long xatol(const char *a)
205 {
206         long ret = 0;
207
208         if (a != NULL)
209                 ret = atol(a);
210
211         return ret;
212 }
213
214 static NTSTATUS row_to_sam_account(MYSQL_RES * r, SAM_ACCOUNT * u)
215 {
216         MYSQL_ROW row;
217         pstring temp;
218         unsigned int num_fields;
219         DOM_SID sid;
220
221         num_fields = mysql_num_fields(r);
222         row = mysql_fetch_row(r);
223         if (!row)
224                 return NT_STATUS_INVALID_PARAMETER;
225
226         pdb_set_logon_time(u, xatol(row[0]), PDB_SET);
227         pdb_set_logoff_time(u, xatol(row[1]), PDB_SET);
228         pdb_set_kickoff_time(u, xatol(row[2]), PDB_SET);
229         pdb_set_pass_last_set_time(u, xatol(row[3]), PDB_SET);
230         pdb_set_pass_can_change_time(u, xatol(row[4]), PDB_SET);
231         pdb_set_pass_must_change_time(u, xatol(row[5]), PDB_SET);
232         pdb_set_username(u, row[6], PDB_SET);
233         pdb_set_domain(u, row[7], PDB_SET);
234         pdb_set_nt_username(u, row[8], PDB_SET);
235         pdb_set_fullname(u, row[9], PDB_SET);
236         pdb_set_homedir(u, row[10], PDB_SET);
237         pdb_set_dir_drive(u, row[11], PDB_SET);
238         pdb_set_logon_script(u, row[12], PDB_SET);
239         pdb_set_profile_path(u, row[13], PDB_SET);
240         pdb_set_acct_desc(u, row[14], PDB_SET);
241         pdb_set_workstations(u, row[15], PDB_SET);
242         pdb_set_unknown_str(u, row[16], PDB_SET);
243         pdb_set_munged_dial(u, row[17], PDB_SET);
244
245         if (row[18])
246                 pdb_set_uid(u, xatol(row[18]), PDB_SET);
247         if (row[19])
248                 pdb_set_gid(u, xatol(row[19]), PDB_SET);
249
250         string_to_sid(&sid, row[20]);
251         pdb_set_user_sid(u, &sid, PDB_SET);
252         string_to_sid(&sid, row[21]);
253         pdb_set_group_sid(u, &sid, PDB_SET);
254
255         if (pdb_gethexpwd(row[22], temp), PDB_SET)
256                 pdb_set_lanman_passwd(u, temp, PDB_SET);
257         if (pdb_gethexpwd(row[23], temp), PDB_SET)
258                 pdb_set_nt_passwd(u, temp, PDB_SET);
259
260         /* Only use plaintext password storage when lanman and nt are
261          * NOT used */
262         if (!row[22] || !row[23])
263                 pdb_set_plaintext_passwd(u, row[24]);
264
265         pdb_set_acct_ctrl(u, xatol(row[25]), PDB_SET);
266         pdb_set_unknown_3(u, xatol(row[26]), PDB_SET);
267         pdb_set_logon_divs(u, xatol(row[27]), PDB_SET);
268         pdb_set_hours_len(u, xatol(row[28]), PDB_SET);
269         pdb_set_unknown_5(u, xatol(row[29]), PDB_SET);
270         pdb_set_unknown_6(u, xatol(row[30]), PDB_SET);
271
272         return NT_STATUS_OK;
273 }
274
275 static NTSTATUS mysqlsam_setsampwent(struct pdb_methods *methods, BOOL update)
276 {
277         struct pdb_mysql_data *data =
278                 (struct pdb_mysql_data *) methods->private_data;
279         char *query;
280         int ret;
281
282         if (!data || !(data->handle)) {
283                 DEBUG(0, ("invalid handle!\n"));
284                 return NT_STATUS_INVALID_HANDLE;
285         }
286
287         asprintf(&query,
288                          "SELECT %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s FROM %s",
289                          config_value_read(data, "logon time column",
290                                                            CONFIG_LOGON_TIME_DEFAULT),
291                          config_value_read(data, "logoff time column",
292                                                            CONFIG_LOGOFF_TIME_DEFAULT),
293                          config_value_read(data, "kickoff time column",
294                                                            CONFIG_KICKOFF_TIME_DEFAULT),
295                          config_value_read(data, "pass last set time column",
296                                                            CONFIG_PASS_LAST_SET_TIME_DEFAULT),
297                          config_value_read(data, "pass can change time column",
298                                                            CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT),
299                          config_value_read(data, "pass must change time column",
300                                                            CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT),
301                          config_value_read(data, "username column",
302                                                            CONFIG_USERNAME_DEFAULT),
303                          config_value_read(data, "domain column",
304                                                            CONFIG_DOMAIN_DEFAULT),
305                          config_value_read(data, "nt username column",
306                                                            CONFIG_NT_USERNAME_DEFAULT),
307                          config_value_read(data, "fullname column",
308                                                            CONFIG_FULLNAME_DEFAULT),
309                          config_value_read(data, "home dir column",
310                                                            CONFIG_HOME_DIR_DEFAULT),
311                          config_value_read(data, "dir drive column",
312                                                            CONFIG_DIR_DRIVE_DEFAULT),
313                          config_value_read(data, "logon script column",
314                                                            CONFIG_LOGON_SCRIPT_DEFAULT),
315                          config_value_read(data, "profile path column",
316                                                            CONFIG_PROFILE_PATH_DEFAULT),
317                          config_value_read(data, "acct desc column",
318                                                            CONFIG_ACCT_DESC_DEFAULT),
319                          config_value_read(data, "workstations column",
320                                                            CONFIG_WORKSTATIONS_DEFAULT),
321                          config_value_read(data, "unknown string column",
322                                                            CONFIG_UNKNOWN_STR_DEFAULT),
323                          config_value_read(data, "munged dial column",
324                                                            CONFIG_MUNGED_DIAL_DEFAULT),
325                          config_value_read(data, "uid column", CONFIG_UID_DEFAULT),
326                          config_value_read(data, "gid column", CONFIG_GID_DEFAULT),
327                          config_value_read(data, "user sid column",
328                                                            CONFIG_USER_SID_DEFAULT),
329                          config_value_read(data, "group sid column",
330                                                            CONFIG_GROUP_SID_DEFAULT),
331                          config_value_read(data, "lanman pass column",
332                                                            CONFIG_LM_PW_DEFAULT),
333                          config_value_read(data, "nt pass column",
334                                                            CONFIG_NT_PW_DEFAULT),
335                          config_value_read(data, "plain pass column",
336                                                            CONFIG_PLAIN_PW_DEFAULT),
337                          config_value_read(data, "acct ctrl column",
338                                                            CONFIG_ACCT_CTRL_DEFAULT),
339                          config_value_read(data, "unknown 3 column",
340                                                            CONFIG_UNKNOWN_3_DEFAULT),
341                          config_value_read(data, "logon divs column",
342                                                            CONFIG_LOGON_DIVS_DEFAULT),
343                          config_value_read(data, "hours len column",
344                                                            CONFIG_HOURS_LEN_DEFAULT),
345                          config_value_read(data, "unknown 5 column",
346                                                            CONFIG_UNKNOWN_5_DEFAULT),
347                          config_value_read(data, "unknown 6 column",
348                                                            CONFIG_UNKNOWN_6_DEFAULT),
349                          config_value(data, "table", CONFIG_TABLE_DEFAULT)
350                                  );
351         DEBUG(5, ("Executing query %s\n", query));
352         
353         ret = mysql_query(data->handle, query);
354         SAFE_FREE(query);
355
356         if (ret) {
357                 DEBUG(0,
358                            ("Error executing MySQL query %s\n", mysql_error(data->handle)));
359                 return NT_STATUS_UNSUCCESSFUL;
360         }
361
362         data->pwent = mysql_store_result(data->handle);
363
364         if (data->pwent == NULL) {
365                 DEBUG(0,
366                         ("Error storing results: %s\n", mysql_error(data->handle)));
367                 return NT_STATUS_UNSUCCESSFUL;
368         }
369         
370         DEBUG(5,
371                 ("mysqlsam_setsampwent succeeded(%llu results)!\n",
372                                 mysql_num_rows(data->pwent)));
373         
374         return NT_STATUS_OK;
375 }
376
377 /***************************************************************
378   End enumeration of the passwd list.
379  ****************************************************************/
380
381 static void mysqlsam_endsampwent(struct pdb_methods *methods)
382 {
383         struct pdb_mysql_data *data =
384                 (struct pdb_mysql_data *) methods->private_data;
385
386         if (data == NULL) {
387                 DEBUG(0, ("invalid handle!\n"));
388                 return;
389         }
390
391         if (data->pwent != NULL)
392                 mysql_free_result(data->pwent);
393
394         data->pwent = NULL;
395
396         DEBUG(5, ("mysql_endsampwent called\n"));
397 }
398
399 /*****************************************************************
400   Get one SAM_ACCOUNT from the list (next in line)
401  *****************************************************************/
402
403 static NTSTATUS mysqlsam_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT * user)
404 {
405         struct pdb_mysql_data *data;
406
407         SET_DATA(data, methods);
408
409         if (data->pwent == NULL) {
410                 DEBUG(0, ("invalid pwent\n"));
411                 return NT_STATUS_INVALID_PARAMETER;
412         }
413
414         return row_to_sam_account(data->pwent, user);
415 }
416
417 static NTSTATUS mysqlsam_select_by_field(struct pdb_methods * methods, SAM_ACCOUNT * user,
418                                                  const char *field, const char *sname)
419 {
420         char *esc_sname;
421         char *query;
422         NTSTATUS ret;
423         MYSQL_RES *res;
424         int mysql_ret;
425         struct pdb_mysql_data *data;
426         char *tmp_sname;
427
428         SET_DATA(data, methods);
429
430         esc_sname = malloc(strlen(sname) * 2 + 1);
431         if (!esc_sname) {
432                 return NT_STATUS_NO_MEMORY; 
433         }
434
435         DEBUG(5,
436                   ("mysqlsam_select_by_field: getting data where %s = %s(nonescaped)\n",
437                    field, sname));
438
439         tmp_sname = smb_xstrdup(sname);
440         
441         /* Escape sname */
442         mysql_real_escape_string(data->handle, esc_sname, tmp_sname,
443                                                          strlen(tmp_sname));
444
445         SAFE_FREE(tmp_sname);
446
447         if (user == NULL) {
448                 DEBUG(0, ("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
449                 SAFE_FREE(esc_sname);
450                 return NT_STATUS_INVALID_PARAMETER;
451         }
452
453         asprintf(&query,
454                          "SELECT %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s FROM %s WHERE %s = '%s'",
455                          config_value_read(data, "logon time column",
456                                                            CONFIG_LOGON_TIME_DEFAULT),
457                          config_value_read(data, "logoff time column",
458                                                            CONFIG_LOGOFF_TIME_DEFAULT),
459                          config_value_read(data, "kickoff time column",
460                                                            CONFIG_KICKOFF_TIME_DEFAULT),
461                          config_value_read(data, "pass last set time column",
462                                                            CONFIG_PASS_LAST_SET_TIME_DEFAULT),
463                          config_value_read(data, "pass can change time column",
464                                                            CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT),
465                          config_value_read(data, "pass must change time column",
466                                                            CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT),
467                          config_value_read(data, "username column",
468                                                            CONFIG_USERNAME_DEFAULT),
469                          config_value_read(data, "domain column",
470                                                            CONFIG_DOMAIN_DEFAULT),
471                          config_value_read(data, "nt username column",
472                                                            CONFIG_NT_USERNAME_DEFAULT),
473                          config_value_read(data, "fullname column",
474                                                            CONFIG_FULLNAME_DEFAULT),
475                          config_value_read(data, "home dir column",
476                                                            CONFIG_HOME_DIR_DEFAULT),
477                          config_value_read(data, "dir drive column",
478                                                            CONFIG_DIR_DRIVE_DEFAULT),
479                          config_value_read(data, "logon script column",
480                                                            CONFIG_LOGON_SCRIPT_DEFAULT),
481                          config_value_read(data, "profile path column",
482                                                            CONFIG_PROFILE_PATH_DEFAULT),
483                          config_value_read(data, "acct desc column",
484                                                            CONFIG_ACCT_DESC_DEFAULT),
485                          config_value_read(data, "workstations column",
486                                                            CONFIG_WORKSTATIONS_DEFAULT),
487                          config_value_read(data, "unknown string column",
488                                                            CONFIG_UNKNOWN_STR_DEFAULT),
489                          config_value_read(data, "munged dial column",
490                                                            CONFIG_MUNGED_DIAL_DEFAULT),
491                          config_value_read(data, "uid column", CONFIG_UID_DEFAULT),
492                          config_value_read(data, "gid column", CONFIG_GID_DEFAULT),
493                          config_value_read(data, "user sid column",
494                                                            CONFIG_USER_SID_DEFAULT),
495                          config_value_read(data, "group sid column",
496                                                            CONFIG_GROUP_SID_DEFAULT),
497                          config_value_read(data, "lanman pass column",
498                                                            CONFIG_LM_PW_DEFAULT),
499                          config_value_read(data, "nt pass column",
500                                                            CONFIG_NT_PW_DEFAULT),
501                          config_value_read(data, "plain pass column",
502                                                            CONFIG_PLAIN_PW_DEFAULT),
503                          config_value_read(data, "acct ctrl column",
504                                                            CONFIG_ACCT_CTRL_DEFAULT),
505                          config_value_read(data, "unknown 3 column",
506                                                            CONFIG_UNKNOWN_3_DEFAULT),
507                          config_value_read(data, "logon divs column",
508                                                            CONFIG_LOGON_DIVS_DEFAULT),
509                          config_value_read(data, "hours len column",
510                                                            CONFIG_HOURS_LEN_DEFAULT),
511                          config_value_read(data, "unknown 5 column",
512                                                            CONFIG_UNKNOWN_5_DEFAULT),
513                          config_value_read(data, "unknown 6 column",
514                                                            CONFIG_UNKNOWN_6_DEFAULT),
515                          config_value(data, "table", CONFIG_TABLE_DEFAULT), field,
516                          esc_sname);
517         
518         SAFE_FREE(esc_sname);
519
520         DEBUG(5, ("Executing query %s\n", query));
521         
522         mysql_ret = mysql_query(data->handle, query);
523         
524         SAFE_FREE(query);
525         
526         if (mysql_ret) {
527                 DEBUG(0,
528                         ("Error while executing MySQL query %s\n", 
529                                 mysql_error(data->handle)));
530                 return NT_STATUS_UNSUCCESSFUL;
531         }
532         
533         res = mysql_store_result(data->handle);
534         if (res == NULL) {
535                 DEBUG(0,
536                         ("Error storing results: %s\n", mysql_error(data->handle)));
537                 return NT_STATUS_UNSUCCESSFUL;
538         }
539         
540         ret = row_to_sam_account(res, user);
541         mysql_free_result(res);
542
543         return ret;
544 }
545
546 /******************************************************************
547   Lookup a name in the SAM database
548  ******************************************************************/
549
550 static NTSTATUS mysqlsam_getsampwnam(struct pdb_methods *methods, SAM_ACCOUNT * user,
551                                          const char *sname)
552 {
553         struct pdb_mysql_data *data;
554
555         SET_DATA(data, methods);
556
557         if (!sname) {
558                 DEBUG(0, ("invalid name specified"));
559                 return NT_STATUS_INVALID_PARAMETER;
560         }
561
562         return mysqlsam_select_by_field(methods, user,
563                         config_value_read(data, "username column",
564                                 CONFIG_USERNAME_DEFAULT), sname);
565 }
566
567
568 /***************************************************************************
569   Search by sid
570  **************************************************************************/
571
572 static NTSTATUS mysqlsam_getsampwsid(struct pdb_methods *methods, SAM_ACCOUNT * user,
573                                          const DOM_SID * sid)
574 {
575         struct pdb_mysql_data *data;
576         fstring sid_str;
577
578         SET_DATA(data, methods);
579
580         sid_to_string(sid_str, sid);
581
582         return mysqlsam_select_by_field(methods, user,
583                         config_value_read(data, "user sid column",
584                                 CONFIG_USER_SID_DEFAULT), sid_str);
585 }
586
587 /***************************************************************************
588   Delete a SAM_ACCOUNT
589  ****************************************************************************/
590
591 static NTSTATUS mysqlsam_delete_sam_account(struct pdb_methods *methods,
592                                                         SAM_ACCOUNT * sam_pass)
593 {
594         const char *sname = pdb_get_username(sam_pass);
595         char *esc;
596         char *query;
597         int ret;
598         struct pdb_mysql_data *data;
599         char *tmp_sname;
600
601         SET_DATA(data, methods);
602
603         if (!methods) {
604                 DEBUG(0, ("invalid methods!\n"));
605                 return NT_STATUS_INVALID_PARAMETER;
606         }
607
608         data = (struct pdb_mysql_data *) methods->private_data;
609         if (!data || !(data->handle)) {
610                 DEBUG(0, ("invalid handle!\n"));
611                 return NT_STATUS_INVALID_HANDLE;
612         }
613
614         if (!sname) {
615                 DEBUG(0, ("invalid name specified\n"));
616                 return NT_STATUS_INVALID_PARAMETER;
617         }
618
619         /* Escape sname */
620         esc = malloc(strlen(sname) * 2 + 1);
621         if (!esc) {
622                 DEBUG(0, ("Can't allocate memory to store escaped name\n"));
623                 return NT_STATUS_NO_MEMORY;
624         }
625         
626         tmp_sname = smb_xstrdup(sname);
627         
628         mysql_real_escape_string(data->handle, esc, tmp_sname,
629                                                          strlen(tmp_sname));
630
631         SAFE_FREE(tmp_sname);
632
633         asprintf(&query, "DELETE FROM %s WHERE %s = '%s'",
634                          config_value(data, "table", CONFIG_TABLE_DEFAULT),
635                          config_value_read(data, "username column",
636                                                            CONFIG_USERNAME_DEFAULT), esc);
637
638         SAFE_FREE(esc);
639
640         ret = mysql_query(data->handle, query);
641
642         SAFE_FREE(query);
643
644         if (ret) {
645                 DEBUG(0,
646                           ("Error while executing query: %s\n",
647                            mysql_error(data->handle)));
648                 return NT_STATUS_UNSUCCESSFUL;
649         }
650
651         DEBUG(5, ("User '%s' deleted\n", sname));
652         return NT_STATUS_OK;
653 }
654
655 static NTSTATUS mysqlsam_replace_sam_account(struct pdb_methods *methods,
656                                                          const SAM_ACCOUNT * newpwd, char isupdate)
657 {
658         pstring temp;
659         struct pdb_mysql_data *data;
660         pdb_mysql_query query;
661         fstring sid_str;
662
663         if (!methods) {
664                 DEBUG(0, ("invalid methods!\n"));
665                 return NT_STATUS_INVALID_PARAMETER;
666         }
667
668         data = (struct pdb_mysql_data *) methods->private_data;
669         if (data == NULL || data->handle == NULL) {
670                 DEBUG(0, ("invalid handle!\n"));
671                 return NT_STATUS_INVALID_HANDLE;
672         }
673         query.update = isupdate;
674
675         /* I know this is somewhat overkill but only the talloc 
676          * functions have asprint_append and the 'normal' asprintf 
677          * is a GNU extension */
678         query.mem_ctx = talloc_init("mysqlsam_replace_sam_account");
679         query.part2 = talloc_asprintf(query.mem_ctx, "%s", "");
680         if (query.update) {
681                 query.part1 =
682                         talloc_asprintf(query.mem_ctx, "UPDATE %s SET ",
683                                                         config_value(data, "table",
684                                                                                  CONFIG_TABLE_DEFAULT));
685         } else {
686                 query.part1 =
687                         talloc_asprintf(query.mem_ctx, "INSERT INTO %s (",
688                                                         config_value(data, "table",
689                                                                                  CONFIG_TABLE_DEFAULT));
690         }
691
692         pdb_mysql_int_field(methods, &query,
693                                                 config_value_write(data, "acct ctrl column",
694                                                                                    CONFIG_ACCT_CTRL_DEFAULT),
695                                                 pdb_get_acct_ctrl(newpwd));
696
697         if (pdb_get_init_flags(newpwd, PDB_LOGONTIME) != PDB_DEFAULT) {
698                 pdb_mysql_int_field(methods, &query,
699                                                         config_value_write(data,
700                                                                                            "logon time column",
701                                                                                            CONFIG_LOGON_TIME_DEFAULT),
702                                                         pdb_get_logon_time(newpwd));
703         }
704
705         if (pdb_get_init_flags(newpwd, PDB_LOGOFFTIME) != PDB_DEFAULT) {
706                 pdb_mysql_int_field(methods, &query,
707                                                         config_value_write(data,
708                                                                                            "logoff time column",
709                                                                                            CONFIG_LOGOFF_TIME_DEFAULT),
710                                                         pdb_get_logoff_time(newpwd));
711         }
712
713         if (pdb_get_init_flags(newpwd, PDB_KICKOFFTIME) != PDB_DEFAULT) {
714                 pdb_mysql_int_field(methods, &query,
715                                                         config_value_write(data,
716                                                                                            "kickoff time column",
717                                                                                            CONFIG_KICKOFF_TIME_DEFAULT),
718                                                         pdb_get_kickoff_time(newpwd));
719         }
720
721         if (pdb_get_init_flags(newpwd, PDB_CANCHANGETIME) != PDB_DEFAULT) {
722                 pdb_mysql_int_field(methods, &query,
723                                                         config_value_write(data,
724                                                                                            "pass can change time column",
725                                                                                            CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT),
726                                                         pdb_get_pass_can_change_time(newpwd));
727         }
728
729         if (pdb_get_init_flags(newpwd, PDB_MUSTCHANGETIME) != PDB_DEFAULT) {
730                 pdb_mysql_int_field(methods, &query,
731                                                         config_value_write(data,
732                                                                                            "pass must change time column",
733                                                                                            CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT),
734                                                         pdb_get_pass_must_change_time(newpwd));
735         }
736
737         if (pdb_get_pass_last_set_time(newpwd)) {
738                 pdb_mysql_int_field(methods, &query,
739                                                         config_value_write(data,
740                                                                                            "pass last set time column",
741                                                                                            CONFIG_PASS_LAST_SET_TIME_DEFAULT),
742                                                         pdb_get_pass_last_set_time(newpwd));
743         }
744
745         if (pdb_get_hours_len(newpwd)) {
746                 pdb_mysql_int_field(methods, &query,
747                                                         config_value_write(data,
748                                                                                            "hours len column",
749                                                                                            CONFIG_HOURS_LEN_DEFAULT),
750                                                         pdb_get_hours_len(newpwd));
751         }
752
753         if (pdb_get_logon_divs(newpwd)) {
754                 pdb_mysql_int_field(methods, &query,
755                                                         config_value_write(data,
756                                                                                            "logon divs column",
757                                                                                            CONFIG_LOGON_DIVS_DEFAULT),
758                                                         pdb_get_logon_divs(newpwd));
759         }
760
761         if (pdb_get_init_flags(newpwd, PDB_UID) != PDB_DEFAULT) {
762                 pdb_mysql_int_field(methods, &query,
763                                                         config_value_write(data, "uid column",
764                                                                                            CONFIG_UID_DEFAULT),
765                                                         pdb_get_uid(newpwd));
766         }
767
768         if (pdb_get_init_flags(newpwd, PDB_GID) != PDB_DEFAULT) {
769                 pdb_mysql_int_field(methods, &query,
770                                                         config_value_write(data, "gid column",
771                                                                                            CONFIG_GID_DEFAULT),
772                                                         pdb_get_gid(newpwd));
773         }
774
775         pdb_mysql_string_field(methods, &query,
776                                                    config_value_write(data, "user sid column",
777                                                                                           CONFIG_USER_SID_DEFAULT),
778                                                    sid_to_string(sid_str, 
779                                                                                  pdb_get_user_sid(newpwd)));
780
781         pdb_mysql_string_field(methods, &query,
782                                                    config_value_write(data, "group sid column",
783                                                                                           CONFIG_GROUP_SID_DEFAULT),
784                                                    sid_to_string(sid_str,
785                                                                                  pdb_get_group_sid(newpwd)));
786
787         pdb_mysql_string_field(methods, &query,
788                                                    config_value_write(data, "username column",
789                                                                                           CONFIG_USERNAME_DEFAULT),
790                                                    pdb_get_username(newpwd));
791
792         pdb_mysql_string_field(methods, &query,
793                                                    config_value_write(data, "domain column",
794                                                                                           CONFIG_DOMAIN_DEFAULT),
795                                                    pdb_get_domain(newpwd));
796
797         pdb_mysql_string_field(methods, &query,
798                                                    config_value_write(data,
799                                                                                           "nt username column",
800                                                                                           CONFIG_NT_USERNAME_DEFAULT),
801                                                    pdb_get_nt_username(newpwd));
802
803         pdb_mysql_string_field(methods, &query,
804                                                    config_value_write(data, "fullname column",
805                                                                                           CONFIG_FULLNAME_DEFAULT),
806                                                    pdb_get_fullname(newpwd));
807
808         pdb_mysql_string_field(methods, &query,
809                                                    config_value_write(data,
810                                                                                           "logon script column",
811                                                                                           CONFIG_LOGON_SCRIPT_DEFAULT),
812                                                    pdb_get_logon_script(newpwd));
813
814         pdb_mysql_string_field(methods, &query,
815                                                    config_value_write(data,
816                                                                                           "profile path column",
817                                                                                           CONFIG_PROFILE_PATH_DEFAULT),
818                                                    pdb_get_profile_path(newpwd));
819
820         pdb_mysql_string_field(methods, &query,
821                                                    config_value_write(data, "dir drive column",
822                                                                                           CONFIG_DIR_DRIVE_DEFAULT),
823                                                    pdb_get_dir_drive(newpwd));
824
825         pdb_mysql_string_field(methods, &query,
826                                                    config_value_write(data, "home dir column",
827                                                                                           CONFIG_HOME_DIR_DEFAULT),
828                                                    pdb_get_homedir(newpwd));
829
830         pdb_mysql_string_field(methods, &query,
831                                                    config_value_write(data,
832                                                                                           "workstations column",
833                                                                                           CONFIG_WORKSTATIONS_DEFAULT),
834                                                    pdb_get_workstations(newpwd));
835
836         pdb_mysql_string_field(methods, &query,
837                                                    config_value_write(data,
838                                                                                           "unknown string column",
839                                                                                           CONFIG_UNKNOWN_STR_DEFAULT),
840                                                    pdb_get_workstations(newpwd));
841
842         pdb_sethexpwd(temp, pdb_get_lanman_passwd(newpwd),
843                                   pdb_get_acct_ctrl(newpwd));
844         pdb_mysql_string_field(methods, &query,
845                                                    config_value_write(data,
846                                                                                           "lanman pass column",
847                                                                                           CONFIG_LM_PW_DEFAULT), temp);
848
849         pdb_sethexpwd(temp, pdb_get_nt_passwd(newpwd),
850                                   pdb_get_acct_ctrl(newpwd));
851         pdb_mysql_string_field(methods, &query,
852                                                    config_value_write(data, "nt pass column",
853                                                                                           CONFIG_NT_PW_DEFAULT), temp);
854
855         if (query.update) {
856                 query.part1[strlen(query.part1) - 1] = '\0';
857                 query.part1 =
858                         talloc_asprintf_append(query.mem_ctx, query.part1,
859                                                                    " WHERE %s = '%s'",
860                                                                    config_value_read(data,
861                                                                                                          "user sid column",
862                                                                                                          CONFIG_USER_SID_DEFAULT),
863                                                                    sid_to_string(sid_str, pdb_get_user_sid (newpwd)));
864         } else {
865                 query.part2[strlen(query.part2) - 1] = ')';
866                 query.part1[strlen(query.part1) - 1] = ')';
867                 query.part1 =
868                         talloc_asprintf_append(query.mem_ctx, query.part1,
869                                                                    " VALUES (%s", query.part2);
870         }
871
872         DEBUG(0, ("%s\n", query.part1));
873         /* Execute the query */
874         if (mysql_query(data->handle, query.part1)) {
875                 DEBUG(0,
876                           ("Error executing %s, %s\n", query.part1,
877                            mysql_error(data->handle)));
878                 return NT_STATUS_INVALID_PARAMETER;
879         }
880         talloc_destroy(query.mem_ctx);
881         return NT_STATUS_OK;
882 }
883
884 static NTSTATUS mysqlsam_add_sam_account(struct pdb_methods *methods, SAM_ACCOUNT * newpwd)
885 {
886         return mysqlsam_replace_sam_account(methods, newpwd, 0);
887 }
888
889 static NTSTATUS mysqlsam_update_sam_account(struct pdb_methods *methods,
890                                                         SAM_ACCOUNT * newpwd)
891 {
892         return mysqlsam_replace_sam_account(methods, newpwd, 1);
893 }
894
895 static NTSTATUS mysqlsam_init(struct pdb_context * pdb_context, struct pdb_methods ** pdb_method,
896                  const char *location)
897 {
898         NTSTATUS nt_status;
899         struct pdb_mysql_data *data;
900
901         mysqlsam_debug_level = debug_add_class("mysqlsam");
902         if (mysqlsam_debug_level == -1) {
903                 mysqlsam_debug_level = DBGC_ALL;
904                 DEBUG(0,
905                           ("mysqlsam: Couldn't register custom debugging class!\n"));
906         }
907
908         if (!pdb_context) {
909                 DEBUG(0, ("invalid pdb_methods specified\n"));
910                 return NT_STATUS_UNSUCCESSFUL;
911         }
912
913         if (!NT_STATUS_IS_OK
914                 (nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
915                 return nt_status;
916         }
917
918         (*pdb_method)->name = "mysqlsam";
919
920         (*pdb_method)->setsampwent = mysqlsam_setsampwent;
921         (*pdb_method)->endsampwent = mysqlsam_endsampwent;
922         (*pdb_method)->getsampwent = mysqlsam_getsampwent;
923         (*pdb_method)->getsampwnam = mysqlsam_getsampwnam;
924         (*pdb_method)->getsampwsid = mysqlsam_getsampwsid;
925         (*pdb_method)->add_sam_account = mysqlsam_add_sam_account;
926         (*pdb_method)->update_sam_account = mysqlsam_update_sam_account;
927         (*pdb_method)->delete_sam_account = mysqlsam_delete_sam_account;
928
929         data = talloc(pdb_context->mem_ctx, sizeof(struct pdb_mysql_data));
930         (*pdb_method)->private_data = data;
931         data->handle = NULL;
932         data->pwent = NULL;
933
934         if (!location) {
935                 DEBUG(0, ("No identifier specified. Check the Samba HOWTO Collection for details\n"));
936                 return NT_STATUS_INVALID_PARAMETER;
937         }
938
939         data->location = smb_xstrdup(location);
940
941         DEBUG(1,
942                   ("Connecting to database server, host: %s, user: %s, password: %s, database: %s, port: %ld\n",
943                    config_value(data, "mysql host", CONFIG_HOST_DEFAULT),
944                    config_value(data, "mysql user", CONFIG_USER_DEFAULT),
945                    config_value(data, "mysql password", CONFIG_PASS_DEFAULT),
946                    config_value(data, "mysql database", CONFIG_DB_DEFAULT),
947                    xatol(config_value(data, "mysql port", CONFIG_PORT_DEFAULT))));
948
949         /* Do the mysql initialization */
950         data->handle = mysql_init(NULL);
951         if (!data->handle) {
952                 DEBUG(0, ("Failed to connect to server\n"));
953                 return NT_STATUS_UNSUCCESSFUL;
954         }
955         /* Process correct entry in $HOME/.my.conf */
956         if (!mysql_real_connect(data->handle,
957                         config_value(data, "mysql host", CONFIG_HOST_DEFAULT),
958                         config_value(data, "mysql user", CONFIG_USER_DEFAULT),
959                         config_value(data, "mysql password", CONFIG_PASS_DEFAULT),
960                         config_value(data, "mysql database", CONFIG_DB_DEFAULT),
961                         xatol(config_value (data, "mysql port", CONFIG_PORT_DEFAULT)), 
962                         NULL, 0)) {
963                 DEBUG(0,
964                           ("Failed to connect to mysql database: error: %s\n",
965                            mysql_error(data->handle)));
966                 return NT_STATUS_UNSUCCESSFUL;
967         }
968         
969         DEBUG(5, ("Connected to mysql db\n"));
970
971         return NT_STATUS_OK;
972 }
973
974 int pdb_mysql_init(void) 
975 {
976         return smb_register_passdb("mysql", mysqlsam_init, PASSDB_INTERFACE_VERSION);
977 }