00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "includes.h"
00025
00026 static BOOL samu_correct(struct samu *s1, struct samu *s2)
00027 {
00028 BOOL ret = True;
00029 uint32 s1_len, s2_len;
00030 const char *s1_buf, *s2_buf;
00031 const uint8 *d1_buf, *d2_buf;
00032
00033
00034 s1_buf = pdb_get_username(s1);
00035 s2_buf = pdb_get_username(s2);
00036 if (s2_buf == NULL && s1_buf != NULL) {
00037 DEBUG(0, ("Username is not set\n"));
00038 ret = False;
00039 } else if (s1_buf == NULL) {
00040
00041 } else if (strcmp(s1_buf,s2_buf)) {
00042 DEBUG(0, ("Username not written correctly, want %s, got \"%s\"\n",
00043 pdb_get_username(s1),
00044 pdb_get_username(s2)));
00045 ret = False;
00046 }
00047
00048
00049 s1_buf = pdb_get_nt_username(s1);
00050 s2_buf = pdb_get_nt_username(s2);
00051 if (s2_buf == NULL && s1_buf != NULL) {
00052 DEBUG(0, ("NT Username is not set\n"));
00053 ret = False;
00054 } else if (s1_buf == NULL) {
00055
00056 } else if (strcmp(s1_buf, s2_buf)) {
00057 DEBUG(0, ("NT Username not written correctly, want \"%s\", got \"%s\"\n",
00058 pdb_get_nt_username(s1),
00059 pdb_get_nt_username(s2)));
00060 ret = False;
00061 }
00062
00063
00064 if (pdb_get_acct_ctrl(s1) != pdb_get_acct_ctrl(s2)) {
00065 DEBUG(0, ("Acct ctrl field not written correctly, want %d (0x%X), got %d (0x%X)\n",
00066 pdb_get_acct_ctrl(s1),
00067 pdb_get_acct_ctrl(s1),
00068 pdb_get_acct_ctrl(s2),
00069 pdb_get_acct_ctrl(s2)));
00070 ret = False;
00071 }
00072
00073
00074 d1_buf = pdb_get_nt_passwd(s1);
00075 d2_buf = pdb_get_nt_passwd(s2);
00076 if (d2_buf == NULL && d1_buf != NULL) {
00077 DEBUG(0, ("NT password is not set\n"));
00078 ret = False;
00079 } else if (d1_buf == NULL) {
00080
00081 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
00082 DEBUG(0, ("NT password not written correctly\n"));
00083 ret = False;
00084 }
00085
00086
00087 d1_buf = pdb_get_lanman_passwd(s1);
00088 d2_buf = pdb_get_lanman_passwd(s2);
00089 if (d2_buf == NULL && d1_buf != NULL) {
00090 DEBUG(0, ("Lanman password is not set\n"));
00091 } else if (d1_buf == NULL) {
00092
00093 } else if (memcmp(d1_buf, d2_buf, NT_HASH_LEN)) {
00094 DEBUG(0, ("Lanman password not written correctly\n"));
00095 ret = False;
00096 }
00097
00098
00099 d1_buf = pdb_get_pw_history(s1, &s1_len);
00100 d2_buf = pdb_get_pw_history(s2, &s2_len);
00101 if (d2_buf == NULL && d1_buf != NULL) {
00102 DEBUG(0, ("Password history is not set\n"));
00103 } else if (d1_buf == NULL) {
00104
00105 } else if (s1_len != s1_len) {
00106 DEBUG(0, ("Password history not written correctly, lengths differ, want %d, got %d\n",
00107 s1_len, s2_len));
00108 ret = False;
00109 } else if (strncmp(s1_buf, s2_buf, s1_len)) {
00110 DEBUG(0, ("Password history not written correctly\n"));
00111 ret = False;
00112 }
00113
00114
00115 if (pdb_get_logon_time(s1) != pdb_get_logon_time(s2)) {
00116 DEBUG(0, ("Logon time is not written correctly\n"));
00117 ret = False;
00118 }
00119
00120
00121 if (pdb_get_logoff_time(s1) != pdb_get_logoff_time(s2)) {
00122 DEBUG(0, ("Logoff time is not written correctly\n"));
00123 ret = False;
00124 }
00125
00126
00127 if (pdb_get_kickoff_time(s1) != pdb_get_logoff_time(s2)) {
00128 DEBUG(0, ("Kickoff time is not written correctly\n"));
00129 ret = False;
00130 }
00131
00132
00133 if (pdb_get_bad_password_time(s1) != pdb_get_bad_password_time(s2)) {
00134 DEBUG(0, ("Bad password time is not written correctly\n"));
00135 ret = False;
00136 }
00137
00138
00139 if (pdb_get_pass_last_set_time(s1) != pdb_get_pass_last_set_time(s2)) {
00140 DEBUG(0, ("Password last set time is not written correctly\n"));
00141 ret = False;
00142 }
00143
00144
00145 if (pdb_get_pass_can_change_time(s1) != pdb_get_pass_can_change_time(s2)) {
00146 DEBUG(0, ("Password can change time is not written correctly\n"));
00147 ret = False;
00148 }
00149
00150
00151 if (pdb_get_pass_must_change_time(s1) != pdb_get_pass_must_change_time(s2)) {
00152 DEBUG(0, ("Password must change time is not written correctly\n"));
00153 ret = False;
00154 }
00155
00156
00157 if (pdb_get_logon_divs(s1) != pdb_get_logon_divs(s2)) {
00158 DEBUG(0, ("Logon divs not written correctly\n"));
00159 ret = False;
00160 }
00161
00162
00163 if (pdb_get_hours_len(s1) != pdb_get_hours_len(s2)) {
00164 DEBUG(0, ("Logon hours length not written correctly\n"));
00165 ret = False;
00166 } else if (pdb_get_hours_len(s1) != 0) {
00167 d1_buf = pdb_get_hours(s1);
00168 d2_buf = pdb_get_hours(s2);
00169 if (d2_buf == NULL && d2_buf != NULL) {
00170 DEBUG(0, ("Logon hours is not set\n"));
00171 ret = False;
00172 } else if (d1_buf == NULL) {
00173
00174 } else if (memcmp(d1_buf, d2_buf, MAX_HOURS_LEN)) {
00175 DEBUG(0, ("Logon hours is not written correctly\n"));
00176 ret = False;
00177 }
00178 }
00179
00180
00181 s1_buf = pdb_get_profile_path(s1);
00182 s2_buf = pdb_get_profile_path(s2);
00183 if (s2_buf == NULL && s1_buf != NULL) {
00184 DEBUG(0, ("Profile path is not set\n"));
00185 ret = False;
00186 } else if (s1_buf == NULL) {
00187
00188 } else if (strcmp(s1_buf, s2_buf)) {
00189 DEBUG(0, ("Profile path is not written correctly\n"));
00190 ret = False;
00191 }
00192
00193
00194 s1_buf = pdb_get_homedir(s1);
00195 s2_buf = pdb_get_homedir(s2);
00196 if (s2_buf == NULL && s1_buf != NULL) {
00197 DEBUG(0, ("Home dir is not set\n"));
00198 ret = False;
00199 } else if (s1_buf == NULL) {
00200
00201 } else if (strcmp(s1_buf, s2_buf)) {
00202 DEBUG(0, ("Home dir is not written correctly\n"));
00203 ret = False;
00204 }
00205
00206
00207 s1_buf = pdb_get_logon_script(s1);
00208 s2_buf = pdb_get_logon_script(s2);
00209 if (s2_buf == NULL && s1_buf != NULL) {
00210 DEBUG(0, ("Logon script not set\n"));
00211 ret = False;
00212 } else if (s1_buf == NULL) {
00213
00214 } else if (strcmp(s1_buf, s2_buf)) {
00215 DEBUG(0, ("Logon script is not written correctly\n"));
00216 ret = False;
00217 }
00218
00219
00220
00221 return ret;
00222 }
00223
00224
00225 int main(int argc, char **argv)
00226 {
00227 TALLOC_CTX *ctx;
00228 struct samu *out = NULL;
00229 struct samu *in = NULL;
00230 NTSTATUS rv;
00231 int i;
00232 struct timeval tv;
00233 BOOL error = False;
00234 struct passwd *pwd;
00235 uint8 *buf;
00236 uint32 expire, min_age, history;
00237 struct pdb_methods *pdb;
00238 poptContext pc;
00239 static const char *backend = NULL;
00240 static const char *unix_user = "nobody";
00241 struct poptOption long_options[] = {
00242 {"username", 'u', POPT_ARG_STRING, &unix_user, 0, "Unix user to use for testing", "USERNAME" },
00243 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "Backend to use if not default", "BACKEND[:SETTINGS]" },
00244 POPT_AUTOHELP
00245 POPT_COMMON_SAMBA
00246 POPT_TABLEEND
00247 };
00248
00249 load_case_tables();
00250
00251 pc = poptGetContext("vfstest", argc, (const char **) argv,
00252 long_options, 0);
00253
00254 poptSetOtherOptionHelp(pc, "backend[:settings] username");
00255
00256 while(poptGetNextOpt(pc) != -1);
00257
00258 poptFreeContext(pc);
00259
00260
00261 lp_load(dyn_CONFIGFILE, False, False, True, True);
00262 setup_logging("pdbtest", True);
00263
00264 if (backend == NULL) {
00265 backend = lp_passdb_backend();
00266 }
00267
00268 rv = make_pdb_method_name(&pdb, backend);
00269 if (NT_STATUS_IS_ERR(rv)) {
00270 fprintf(stderr, "Error initializing '%s': %s\n", backend, get_friendly_nt_error_msg(rv));
00271 exit(1);
00272 }
00273
00274 ctx = talloc_init("PDBTEST");
00275
00276 if (!(out = samu_new(ctx))) {
00277 fprintf(stderr, "Can't create samu structure.\n");
00278 exit(1);
00279 }
00280
00281 if ((pwd = getpwnam_alloc(ctx, unix_user)) == NULL) {
00282 fprintf(stderr, "Error getting user information for %s\n", unix_user);
00283 exit(1);
00284 }
00285
00286 samu_set_unix(out, pwd);
00287
00288 pdb_set_profile_path(out, "\\\\torture\\profile", PDB_SET);
00289 pdb_set_homedir(out, "\\\\torture\\home", PDB_SET);
00290 pdb_set_logon_script(out, "torture_script.cmd", PDB_SET);
00291
00292 pdb_get_account_policy(AP_PASSWORD_HISTORY, &history);
00293 if (history * PW_HISTORY_ENTRY_LEN < NT_HASH_LEN) {
00294 buf = (uint8 *)TALLOC(ctx, NT_HASH_LEN);
00295 } else {
00296 buf = (uint8 *)TALLOC(ctx, history * PW_HISTORY_ENTRY_LEN);
00297 }
00298
00299
00300 GetTimeOfDay(&tv);
00301 srand(tv.tv_usec);
00302 for (i = 0; i < NT_HASH_LEN; i++) {
00303 buf[i] = (uint8) rand();
00304 }
00305 pdb_set_nt_passwd(out, buf, PDB_SET);
00306 for (i = 0; i < LM_HASH_LEN; i++) {
00307 buf[i] = (uint8) rand();
00308 }
00309 pdb_set_lanman_passwd(out, buf, PDB_SET);
00310 for (i = 0; i < history * PW_HISTORY_ENTRY_LEN; i++) {
00311 buf[i] = (uint8) rand();
00312 }
00313 pdb_set_pw_history(out, buf, history, PDB_SET);
00314
00315 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &expire);
00316 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &min_age);
00317 pdb_set_pass_last_set_time(out, time(NULL), PDB_SET);
00318
00319 if (expire == 0 || expire == (uint32)-1) {
00320 pdb_set_pass_must_change_time(out, get_time_t_max(), PDB_SET);
00321 } else {
00322 pdb_set_pass_must_change_time(out, time(NULL)+expire, PDB_SET);
00323 }
00324
00325 if (min_age == (uint32)-1) {
00326 pdb_set_pass_can_change_time(out, 0, PDB_SET);
00327 } else {
00328 pdb_set_pass_can_change_time(out, time(NULL)+min_age, PDB_SET);
00329 }
00330
00331
00332 if (!NT_STATUS_IS_OK(rv = pdb->add_sam_account(pdb, out))) {
00333 fprintf(stderr, "Error in add_sam_account: %s\n",
00334 get_friendly_nt_error_msg(rv));
00335 exit(1);
00336 }
00337
00338 if (!(in = samu_new(ctx))) {
00339 fprintf(stderr, "Can't create samu structure.\n");
00340 exit(1);
00341 }
00342
00343
00344 if (NT_STATUS_IS_ERR(pdb->getsampwnam(pdb, in, out->username))) {
00345 fprintf(stderr, "Error getting sampw of added user %s.\n",
00346 out->username);
00347 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
00348 fprintf(stderr, "Error in delete_sam_account %s\n",
00349 get_friendly_nt_error_msg(rv));
00350 }
00351 TALLOC_FREE(ctx);
00352 }
00353
00354
00355 if (samu_correct(out, in)) {
00356 printf("User info written correctly\n");
00357 } else {
00358 printf("User info NOT written correctly\n");
00359 error = True;
00360 }
00361
00362
00363 if (!NT_STATUS_IS_OK(rv = pdb->delete_sam_account(pdb, out))) {
00364 fprintf(stderr, "Error in delete_sam_account %s\n",
00365 get_friendly_nt_error_msg(rv));
00366 }
00367
00368 pdb->setsampwent(pdb, False, 0);
00369 while (NT_STATUS_IS_OK(pdb->getsampwent(pdb, out))) {
00370 if (pdb_get_username(out) == NULL) {
00371 fprintf(stderr, "Got bad username through getsampwent()\n");
00372 error = True;
00373 break;
00374 }
00375 if (NT_STATUS_IS_ERR(pdb->getsampwnam(pdb, in, pdb_get_username(out)))) {
00376 fprintf(stderr, "Error getting samu through getsampwnam() of an account we got through getsampwent!\n");
00377 error = True;
00378 continue;
00379 }
00380 if (!samu_correct(out, in)) {
00381 printf("Record gotten through getsampwnam() differs from same record through getsampwent()\n");
00382 }
00383 }
00384 pdb->endsampwent(pdb);
00385
00386 TALLOC_FREE(ctx);
00387
00388 if (error) {
00389 return 1;
00390 }
00391 return 0;
00392 }