r5687: Fix for bug #2398 from Kevin Dalley <kevin@kelphead.org>.
[Samba.git] / source / passdb / pdb_sql.c
blobee9ece2baf03d409d05b6334d63806d0a527e3b1
1 /*
2 * Common PDB SQL backend functions
3 * Copyright (C) Jelmer Vernooij 2003-2004
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
20 #include "includes.h"
22 #define CONFIG_TABLE_DEFAULT "user"
23 #define CONFIG_LOGON_TIME_DEFAULT "logon_time"
24 #define CONFIG_LOGOFF_TIME_DEFAULT "logoff_time"
25 #define CONFIG_KICKOFF_TIME_DEFAULT "kickoff_time"
26 #define CONFIG_PASS_LAST_SET_TIME_DEFAULT "pass_last_set_time"
27 #define CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT "pass_can_change_time"
28 #define CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT "pass_must_change_time"
29 #define CONFIG_USERNAME_DEFAULT "username"
30 #define CONFIG_DOMAIN_DEFAULT "domain"
31 #define CONFIG_NT_USERNAME_DEFAULT "nt_username"
32 #define CONFIG_FULLNAME_DEFAULT "nt_fullname"
33 #define CONFIG_HOME_DIR_DEFAULT "home_dir"
34 #define CONFIG_DIR_DRIVE_DEFAULT "dir_drive"
35 #define CONFIG_LOGON_SCRIPT_DEFAULT "logon_script"
36 #define CONFIG_PROFILE_PATH_DEFAULT "profile_path"
37 #define CONFIG_ACCT_DESC_DEFAULT "acct_desc"
38 #define CONFIG_WORKSTATIONS_DEFAULT "workstations"
39 #define CONFIG_UNKNOWN_STR_DEFAULT "unknown_str"
40 #define CONFIG_MUNGED_DIAL_DEFAULT "munged_dial"
41 #define CONFIG_USER_SID_DEFAULT "user_sid"
42 #define CONFIG_GROUP_SID_DEFAULT "group_sid"
43 #define CONFIG_LM_PW_DEFAULT "lm_pw"
44 #define CONFIG_NT_PW_DEFAULT "nt_pw"
45 #define CONFIG_PLAIN_PW_DEFAULT "NULL"
46 #define CONFIG_ACCT_CTRL_DEFAULT "acct_ctrl"
47 #define CONFIG_LOGON_DIVS_DEFAULT "logon_divs"
48 #define CONFIG_HOURS_LEN_DEFAULT "hours_len"
49 #define CONFIG_BAD_PASSWORD_COUNT_DEFAULT "bad_password_count"
50 #define CONFIG_LOGON_COUNT_DEFAULT "logon_count"
51 #define CONFIG_UNKNOWN_6_DEFAULT "unknown_6"
53 /* Used to construct insert and update queries */
55 typedef struct pdb_sql_query {
56 char update;
57 TALLOC_CTX *mem_ctx;
58 char *part1;
59 char *part2;
60 } pdb_sql_query;
62 static void pdb_sql_int_field(struct pdb_sql_query *q, const char *name, int value)
64 if (!name || strchr(name, '\''))
65 return; /* This field shouldn't be set by us */
67 if (q->update) {
68 q->part1 =
69 talloc_asprintf_append(q->mem_ctx, q->part1,
70 "%s = %d,", name, value);
71 } else {
72 q->part1 =
73 talloc_asprintf_append(q->mem_ctx, q->part1, "%s,", name);
74 q->part2 =
75 talloc_asprintf_append(q->mem_ctx, q->part2, "%d,", value);
79 char *sql_escape_string(const char *unesc)
81 char *esc = SMB_MALLOC(strlen(unesc) * 2 + 3);
82 size_t pos_unesc = 0, pos_esc = 0;
84 for(pos_unesc = 0; unesc[pos_unesc]; pos_unesc++) {
85 switch(unesc[pos_unesc]) {
86 case '\\':
87 case '\'':
88 case '"':
89 esc[pos_esc] = '\\'; pos_esc++;
90 default:
91 esc[pos_esc] = unesc[pos_unesc]; pos_esc++;
92 break;
96 esc[pos_esc] = '\0';
98 return esc;
101 static NTSTATUS pdb_sql_string_field(struct pdb_sql_query *q,
102 const char *name, const char *value)
104 char *esc_value;
106 if (!name || !value || !strcmp(value, "") || strchr(name, '\''))
107 return NT_STATUS_INVALID_PARAMETER; /* This field shouldn't be set by module */
109 esc_value = sql_escape_string(value);
111 if (q->update) {
112 q->part1 =
113 talloc_asprintf_append(q->mem_ctx, q->part1,
114 "%s = '%s',", name, esc_value);
115 } else {
116 q->part1 =
117 talloc_asprintf_append(q->mem_ctx, q->part1, "%s,", name);
118 q->part2 =
119 talloc_asprintf_append(q->mem_ctx, q->part2, "'%s',",
120 esc_value);
123 SAFE_FREE(esc_value);
125 return NT_STATUS_OK;
128 #define config_value(data,name,default_value) \
129 lp_parm_const_string(GLOBAL_SECTION_SNUM, data, name, default_value)
131 static const char * config_value_write(const char *location, const char *name, const char *default_value)
133 char const *v = NULL;
134 char const *swrite = NULL;
136 v = lp_parm_const_string(GLOBAL_SECTION_SNUM, location, name, default_value);
138 if (!v)
139 return NULL;
141 swrite = strrchr(v, ':');
143 /* Default to the same field as read field */
144 if (!swrite) {
146 /* Updating NULL does not make much sense */
147 if (!strcmp(v, "NULL"))
148 return NULL;
150 return v;
153 swrite++;
155 /* If the field is 0 chars long, we shouldn't write to it */
156 if (!strlen(swrite) || !strcmp(swrite, "NULL"))
157 return NULL;
159 /* Otherwise, use the additionally specified */
160 return swrite;
163 static const char * config_value_read(const char *location, const char *name, const char *default_value)
165 char *v = NULL;
166 char *swrite;
168 v = lp_parm_talloc_string(GLOBAL_SECTION_SNUM, location, name, default_value);
170 if (!v)
171 return "NULL";
173 swrite = strrchr(v, ':');
175 /* If no write is specified, there are no problems */
176 if (!swrite) {
177 if (strlen(v) == 0)
178 return "NULL";
179 return (const char *)v;
182 /* Otherwise, we have to cut the ':write_part' */
183 *swrite = '\0';
184 if (strlen(v) == 0)
185 return "NULL";
187 return (const char *)v;
190 char *sql_account_query_select(const char *data, BOOL update, enum sql_search_field field, const char *value)
192 const char *field_string;
193 char *query;
195 switch(field) {
196 case SQL_SEARCH_NONE:
197 field_string = "'1'";
198 value = "1";
199 break;
201 case SQL_SEARCH_USER_SID:
202 field_string = config_value_read(data, "user sid column",
203 CONFIG_USER_SID_DEFAULT);
204 break;
206 case SQL_SEARCH_USER_NAME:
207 field_string = config_value_read(data, "username column",
208 CONFIG_USERNAME_DEFAULT);
209 break;
210 default:
211 field_string = "unknown";
212 break;
215 asprintf(&query,
216 "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 FROM %s WHERE %s = '%s'",
217 config_value_read(data, "logon time column",
218 CONFIG_LOGON_TIME_DEFAULT),
219 config_value_read(data, "logoff time column",
220 CONFIG_LOGOFF_TIME_DEFAULT),
221 config_value_read(data, "kickoff time column",
222 CONFIG_KICKOFF_TIME_DEFAULT),
223 config_value_read(data, "pass last set time column",
224 CONFIG_PASS_LAST_SET_TIME_DEFAULT),
225 config_value_read(data, "pass can change time column",
226 CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT),
227 config_value_read(data, "pass must change time column",
228 CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT),
229 config_value_read(data, "username column",
230 CONFIG_USERNAME_DEFAULT),
231 config_value_read(data, "domain column",
232 CONFIG_DOMAIN_DEFAULT),
233 config_value_read(data, "nt username column",
234 CONFIG_NT_USERNAME_DEFAULT),
235 config_value_read(data, "fullname column",
236 CONFIG_FULLNAME_DEFAULT),
237 config_value_read(data, "home dir column",
238 CONFIG_HOME_DIR_DEFAULT),
239 config_value_read(data, "dir drive column",
240 CONFIG_DIR_DRIVE_DEFAULT),
241 config_value_read(data, "logon script column",
242 CONFIG_LOGON_SCRIPT_DEFAULT),
243 config_value_read(data, "profile path column",
244 CONFIG_PROFILE_PATH_DEFAULT),
245 config_value_read(data, "acct desc column",
246 CONFIG_ACCT_DESC_DEFAULT),
247 config_value_read(data, "workstations column",
248 CONFIG_WORKSTATIONS_DEFAULT),
249 config_value_read(data, "unknown string column",
250 CONFIG_UNKNOWN_STR_DEFAULT),
251 config_value_read(data, "munged dial column",
252 CONFIG_MUNGED_DIAL_DEFAULT),
253 config_value_read(data, "user sid column",
254 CONFIG_USER_SID_DEFAULT),
255 config_value_read(data, "group sid column",
256 CONFIG_GROUP_SID_DEFAULT),
257 config_value_read(data, "lanman pass column",
258 CONFIG_LM_PW_DEFAULT),
259 config_value_read(data, "nt pass column",
260 CONFIG_NT_PW_DEFAULT),
261 config_value_read(data, "plain pass column",
262 CONFIG_PLAIN_PW_DEFAULT),
263 config_value_read(data, "acct ctrl column",
264 CONFIG_ACCT_CTRL_DEFAULT),
265 config_value_read(data, "logon divs column",
266 CONFIG_LOGON_DIVS_DEFAULT),
267 config_value_read(data, "hours len column",
268 CONFIG_HOURS_LEN_DEFAULT),
269 config_value_read(data, "bad password count column",
270 CONFIG_BAD_PASSWORD_COUNT_DEFAULT),
271 config_value_read(data, "logon count column",
272 CONFIG_LOGON_COUNT_DEFAULT),
273 config_value_read(data, "unknown 6 column",
274 CONFIG_UNKNOWN_6_DEFAULT),
275 config_value(data, "table", CONFIG_TABLE_DEFAULT),
276 field_string, value
278 return query;
281 char *sql_account_query_delete(const char *data, const char *esc)
283 char *query;
285 asprintf(&query, "DELETE FROM %s WHERE %s = '%s'",
286 config_value(data, "table", CONFIG_TABLE_DEFAULT),
287 config_value_read(data, "username column",
288 CONFIG_USERNAME_DEFAULT), esc);
289 return query;
292 char *sql_account_query_update(const char *location, const SAM_ACCOUNT *newpwd, char isupdate)
294 char *ret;
295 pstring temp;
296 pdb_sql_query query;
297 fstring sid_str;
299 query.update = isupdate;
301 /* I know this is somewhat overkill but only the talloc
302 * functions have asprint_append and the 'normal' asprintf
303 * is a GNU extension */
304 query.mem_ctx = talloc_init("sql_query_update");
305 query.part2 = talloc_asprintf(query.mem_ctx, "%s", "");
306 if (query.update) {
307 query.part1 =
308 talloc_asprintf(query.mem_ctx, "UPDATE %s SET ",
309 config_value(location, "table",
310 CONFIG_TABLE_DEFAULT));
311 } else {
312 query.part1 =
313 talloc_asprintf(query.mem_ctx, "INSERT INTO %s (",
314 config_value(location, "table",
315 CONFIG_TABLE_DEFAULT));
318 pdb_sql_int_field(&query,
319 config_value_write(location, "acct ctrl column",
320 CONFIG_ACCT_CTRL_DEFAULT),
321 pdb_get_acct_ctrl(newpwd));
323 if (pdb_get_init_flags(newpwd, PDB_LOGONTIME) != PDB_DEFAULT) {
324 pdb_sql_int_field(&query,
325 config_value_write(location,
326 "logon time column",
327 CONFIG_LOGON_TIME_DEFAULT),
328 pdb_get_logon_time(newpwd));
331 if (pdb_get_init_flags(newpwd, PDB_LOGOFFTIME) != PDB_DEFAULT) {
332 pdb_sql_int_field(&query,
333 config_value_write(location,
334 "logoff time column",
335 CONFIG_LOGOFF_TIME_DEFAULT),
336 pdb_get_logoff_time(newpwd));
339 if (pdb_get_init_flags(newpwd, PDB_KICKOFFTIME) != PDB_DEFAULT) {
340 pdb_sql_int_field(&query,
341 config_value_write(location,
342 "kickoff time column",
343 CONFIG_KICKOFF_TIME_DEFAULT),
344 pdb_get_kickoff_time(newpwd));
347 if (pdb_get_init_flags(newpwd, PDB_CANCHANGETIME) != PDB_DEFAULT) {
348 pdb_sql_int_field(&query,
349 config_value_write(location,
350 "pass can change time column",
351 CONFIG_PASS_CAN_CHANGE_TIME_DEFAULT),
352 pdb_get_pass_can_change_time(newpwd));
355 if (pdb_get_init_flags(newpwd, PDB_MUSTCHANGETIME) != PDB_DEFAULT) {
356 pdb_sql_int_field(&query,
357 config_value_write(location,
358 "pass must change time column",
359 CONFIG_PASS_MUST_CHANGE_TIME_DEFAULT),
360 pdb_get_pass_must_change_time(newpwd));
363 if (pdb_get_pass_last_set_time(newpwd)) {
364 pdb_sql_int_field(&query,
365 config_value_write(location,
366 "pass last set time column",
367 CONFIG_PASS_LAST_SET_TIME_DEFAULT),
368 pdb_get_pass_last_set_time(newpwd));
371 if (pdb_get_hours_len(newpwd)) {
372 pdb_sql_int_field(&query,
373 config_value_write(location,
374 "hours len column",
375 CONFIG_HOURS_LEN_DEFAULT),
376 pdb_get_hours_len(newpwd));
379 if (pdb_get_logon_divs(newpwd)) {
380 pdb_sql_int_field(&query,
381 config_value_write(location,
382 "logon divs column",
383 CONFIG_LOGON_DIVS_DEFAULT),
384 pdb_get_logon_divs(newpwd));
387 pdb_sql_string_field(&query,
388 config_value_write(location, "user sid column",
389 CONFIG_USER_SID_DEFAULT),
390 sid_to_string(sid_str,
391 pdb_get_user_sid(newpwd)));
393 pdb_sql_string_field(&query,
394 config_value_write(location, "group sid column",
395 CONFIG_GROUP_SID_DEFAULT),
396 sid_to_string(sid_str,
397 pdb_get_group_sid(newpwd)));
399 pdb_sql_string_field(&query,
400 config_value_write(location, "username column",
401 CONFIG_USERNAME_DEFAULT),
402 pdb_get_username(newpwd));
404 pdb_sql_string_field(&query,
405 config_value_write(location, "domain column",
406 CONFIG_DOMAIN_DEFAULT),
407 pdb_get_domain(newpwd));
409 pdb_sql_string_field(&query,
410 config_value_write(location,
411 "nt username column",
412 CONFIG_NT_USERNAME_DEFAULT),
413 pdb_get_nt_username(newpwd));
415 pdb_sql_string_field(&query,
416 config_value_write(location, "fullname column",
417 CONFIG_FULLNAME_DEFAULT),
418 pdb_get_fullname(newpwd));
420 pdb_sql_string_field(&query,
421 config_value_write(location,
422 "logon script column",
423 CONFIG_LOGON_SCRIPT_DEFAULT),
424 pdb_get_logon_script(newpwd));
426 pdb_sql_string_field(&query,
427 config_value_write(location,
428 "profile path column",
429 CONFIG_PROFILE_PATH_DEFAULT),
430 pdb_get_profile_path(newpwd));
432 pdb_sql_string_field(&query,
433 config_value_write(location, "dir drive column",
434 CONFIG_DIR_DRIVE_DEFAULT),
435 pdb_get_dir_drive(newpwd));
437 pdb_sql_string_field(&query,
438 config_value_write(location, "home dir column",
439 CONFIG_HOME_DIR_DEFAULT),
440 pdb_get_homedir(newpwd));
442 pdb_sql_string_field(&query,
443 config_value_write(location,
444 "workstations column",
445 CONFIG_WORKSTATIONS_DEFAULT),
446 pdb_get_workstations(newpwd));
448 pdb_sql_string_field(&query,
449 config_value_write(location,
450 "unknown string column",
451 CONFIG_UNKNOWN_STR_DEFAULT),
452 pdb_get_workstations(newpwd));
454 pdb_sethexpwd(temp, pdb_get_lanman_passwd(newpwd),
455 pdb_get_acct_ctrl(newpwd));
456 pdb_sql_string_field(&query,
457 config_value_write(location,
458 "lanman pass column",
459 CONFIG_LM_PW_DEFAULT), temp);
461 pdb_sethexpwd(temp, pdb_get_nt_passwd(newpwd),
462 pdb_get_acct_ctrl(newpwd));
463 pdb_sql_string_field(&query,
464 config_value_write(location, "nt pass column",
465 CONFIG_NT_PW_DEFAULT), temp);
467 if (query.update) {
468 query.part1[strlen(query.part1) - 1] = '\0';
469 query.part1 =
470 talloc_asprintf_append(query.mem_ctx, query.part1,
471 " WHERE %s = '%s'",
472 config_value_read(location,
473 "user sid column",
474 CONFIG_USER_SID_DEFAULT),
475 sid_to_string(sid_str, pdb_get_user_sid (newpwd)));
476 } else {
477 query.part2[strlen(query.part2) - 1] = ')';
478 query.part1[strlen(query.part1) - 1] = ')';
479 query.part1 =
480 talloc_asprintf_append(query.mem_ctx, query.part1,
481 " VALUES (%s", query.part2);
484 ret = SMB_STRDUP(query.part1);
485 talloc_destroy(query.mem_ctx);
486 return ret;
489 BOOL sql_account_config_valid(const char *data)
491 const char *sid_column, *username_column;
493 sid_column = config_value_read(data, "user sid column", CONFIG_USER_SID_DEFAULT);
494 username_column = config_value_read(data, "username column", CONFIG_USERNAME_DEFAULT);
496 if(!strcmp(sid_column,"NULL") || !strcmp(username_column, "NULL")) {
497 DEBUG(0,("Please specify both a valid 'user sid column' and a valid 'username column' in smb.conf\n"));
498 return False;
501 return True;