static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx, int argc, char **argv) { NTSTATUS result = NT_STATUS_UNSUCCESSFUL; unsigned char trust_passwd[16]; uint32 database_id, num_deltas, tmp; SAM_DELTA_HDR *hdr_deltas; SAM_DELTA_CTR *deltas; UINT64_S seqnum; if (argc != 3) { fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]); return NT_STATUS_OK; } database_id = atoi(argv[1]); tmp = atoi(argv[2]); seqnum.low = tmp & 0xffff; seqnum.high = 0; if (!secrets_init()) { fprintf(stderr, "Unable to initialise secrets database\n"); goto done; } /* Initialise session credentials */ if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, NULL)) { fprintf(stderr, "could not fetch trust account password\n"); goto done; } result = cli_nt_setup_creds(cli, trust_passwd); if (!NT_STATUS_IS_OK(result)) { fprintf(stderr, "Error initialising session creds\n"); goto done; } /* Synchronise sam database */ result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id, seqnum, &num_deltas, &hdr_deltas, &deltas); if (!NT_STATUS_IS_OK(result)) goto done; /* Display results */ display_sam_sync(num_deltas, hdr_deltas, deltas); done: return result; }
static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, int argc, char **argv) { NTSTATUS result = NT_STATUS_UNSUCCESSFUL; unsigned char trust_passwd[16]; uint32 database_id = 0, num_deltas; SAM_DELTA_HDR *hdr_deltas; SAM_DELTA_CTR *deltas; DOM_CRED ret_creds; if (argc > 2) { fprintf(stderr, "Usage: %s [database_id]\n", argv[0]); return NT_STATUS_OK; } if (argc == 2) database_id = atoi(argv[1]); if (!secrets_init()) { fprintf(stderr, "Unable to initialise secrets database\n"); return result; } /* Initialise session credentials */ if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, NULL)) { fprintf(stderr, "could not fetch trust account password\n"); goto done; } result = cli_nt_setup_creds(cli, trust_passwd); if (!NT_STATUS_IS_OK(result)) { fprintf(stderr, "Error initialising session creds\n"); goto done; } /* on first call the returnAuthenticator is empty */ memset(&ret_creds, 0, sizeof(ret_creds)); /* Synchronise sam database */ result = cli_netlogon_sam_sync(cli, mem_ctx, &ret_creds, database_id, &num_deltas, &hdr_deltas, &deltas); if (!NT_STATUS_IS_OK(result)) goto done; /* Display results */ display_sam_sync(num_deltas, hdr_deltas, deltas); done: return result; }
static NTSTATUS cmd_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx, int argc, char **argv) { unsigned char trust_passwd[16]; NTSTATUS result = NT_STATUS_UNSUCCESSFUL; int logon_type = NET_LOGON_TYPE; char *username, *password; /* Check arguments */ if (argc < 3 || argc > 4) { fprintf(stderr, "Usage: samlogon <username> <password> " "[logon_type]\n"); return NT_STATUS_OK; } username = argv[1]; password = argv[2]; if (argc == 4) sscanf(argv[3], "%i", &logon_type); /* Authenticate ourselves with the domain controller */ if (!secrets_init()) { fprintf(stderr, "Unable to initialise secrets database\n"); return result; } if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, NULL)) { fprintf(stderr, "could not fetch trust account password\n"); goto done; } result = cli_nt_setup_creds(cli, trust_passwd); if (!NT_STATUS_IS_OK(result)) { fprintf(stderr, "Error initialising session creds\n"); goto done; } /* Perform the sam logon */ result = cli_netlogon_sam_logon(cli, mem_ctx, username, password, logon_type); if (!NT_STATUS_IS_OK(result)) goto done; done: return result; }
NTSTATUS cli_nt_setup_netsec(struct cli_state *cli, int sec_chan, int auth_flags, const uchar trust_password[16]) { NTSTATUS result; uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS; cli->pipe_auth_flags = 0; if (lp_client_schannel() == False) { return NT_STATUS_OK; } if (!cli_nt_session_open(cli, PI_NETLOGON)) { DEBUG(0, ("Could not initialise %s\n", get_pipe_name_from_index(PI_NETLOGON))); return NT_STATUS_UNSUCCESSFUL; } if (lp_client_schannel() != False) neg_flags |= NETLOGON_NEG_SCHANNEL; neg_flags |= NETLOGON_NEG_SCHANNEL; result = cli_nt_setup_creds(cli, sec_chan, trust_password, &neg_flags, 2); if (!(neg_flags & NETLOGON_NEG_SCHANNEL) && lp_client_schannel() == True) { DEBUG(1, ("Could not negotiate SCHANNEL with the DC!\n")); result = NT_STATUS_UNSUCCESSFUL; } if (!NT_STATUS_IS_OK(result)) { ZERO_STRUCT(cli->auth_info.sess_key); ZERO_STRUCT(cli->sess_key); cli->pipe_auth_flags = 0; cli_nt_session_close(cli); return result; } memcpy(cli->auth_info.sess_key, cli->sess_key, sizeof(cli->auth_info.sess_key)); cli->saved_netlogon_pipe_fnum = cli->nt_pipe_fnum; cli->nt_pipe_fnum = 0; /* doing schannel, not per-user auth */ cli->pipe_auth_flags = auth_flags; return NT_STATUS_OK; }
NTSTATUS cli_nt_establish_netlogon(struct cli_state *cli, int sec_chan, const uchar trust_password[16]) { NTSTATUS result; uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS; int fnum; cli_nt_netlogon_netsec_session_close(cli); if (lp_client_schannel() != False) neg_flags |= NETLOGON_NEG_SCHANNEL; result = cli_nt_setup_creds(cli, sec_chan, trust_password, &neg_flags, 2); if (!NT_STATUS_IS_OK(result)) { cli_nt_session_close(cli); return result; } if ((lp_client_schannel() == True) && ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) { DEBUG(3, ("Server did not offer schannel\n")); cli_nt_session_close(cli); return NT_STATUS_UNSUCCESSFUL; } if ((lp_client_schannel() == False) || ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) { return NT_STATUS_OK; /* keep the existing connection to NETLOGON open */ } /* Server offered schannel, so try it. */ memcpy(cli->auth_info.sess_key, cli->sess_key, sizeof(cli->auth_info.sess_key)); cli->saved_netlogon_pipe_fnum = cli->nt_pipe_fnum; cli->pipe_auth_flags = AUTH_PIPE_NETSEC; cli->pipe_auth_flags |= AUTH_PIPE_SIGN; cli->pipe_auth_flags |= AUTH_PIPE_SEAL; if (cli->capabilities & CAP_NT_SMBS) { /* The secure channel connection must be opened on the same session (TCP connection) as the one the challenge was requested from. */ if ((fnum = cli_nt_create(cli, PIPE_NETLOGON_PLAIN, DESIRED_ACCESS_PIPE)) == -1) { DEBUG(0,("cli_nt_create failed to %s machine %s. " "Error was %s\n", PIPE_NETLOGON, cli->desthost, cli_errstr(cli))); return NT_STATUS_UNSUCCESSFUL; } cli->nt_pipe_fnum = (uint16)fnum; } else { if ((fnum = cli_open(cli, PIPE_NETLOGON, O_CREAT|O_RDWR, DENY_NONE)) == -1) { DEBUG(0,("cli_open failed on pipe %s to machine %s. " "Error was %s\n", PIPE_NETLOGON, cli->desthost, cli_errstr(cli))); return NT_STATUS_UNSUCCESSFUL; } cli->nt_pipe_fnum = (uint16)fnum; /**************** Set Named Pipe State ***************/ if (!rpc_pipe_set_hnd_state(cli, PIPE_NETLOGON, 0x4300)) { DEBUG(0,("Pipe hnd state failed. Error was %s\n", cli_errstr(cli))); cli_close(cli, cli->nt_pipe_fnum); return NT_STATUS_UNSUCCESSFUL; } } if (!rpc_pipe_bind(cli, PI_NETLOGON, global_myname())) { DEBUG(2,("rpc bind to %s failed\n", PIPE_NETLOGON)); cli_close(cli, cli->nt_pipe_fnum); return NT_STATUS_UNSUCCESSFUL; } return NT_STATUS_OK; }