00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "includes.h"
00024 #include "utils/net.h"
00025
00026
00027
00028
00029
00030 struct con_struct {
00031 BOOL failed_connect;
00032 NTSTATUS err;
00033 struct cli_state *cli;
00034 struct rpc_pipe_client *lsapipe;
00035 POLICY_HND pol;
00036 };
00037
00038 static struct con_struct *cs;
00039
00040
00041
00042
00043
00044 static int cs_destructor(struct con_struct *p)
00045 {
00046 if (cs->cli) {
00047 cli_shutdown(cs->cli);
00048 }
00049 cs = NULL;
00050 return 0;
00051 }
00052
00053
00054
00055
00056
00057 static struct con_struct *create_cs(TALLOC_CTX *ctx, NTSTATUS *perr)
00058 {
00059 NTSTATUS nt_status;
00060 struct in_addr loopback_ip = *interpret_addr2("127.0.0.1");
00061
00062 *perr = NT_STATUS_OK;
00063
00064 if (cs) {
00065 if (cs->failed_connect) {
00066 *perr = cs->err;
00067 return NULL;
00068 }
00069 return cs;
00070 }
00071
00072 cs = TALLOC_P(ctx, struct con_struct);
00073 if (!cs) {
00074 *perr = NT_STATUS_NO_MEMORY;
00075 return NULL;
00076 }
00077
00078 ZERO_STRUCTP(cs);
00079 talloc_set_destructor(cs, cs_destructor);
00080
00081
00082
00083 #if 0
00084 if (!opt_password && !opt_machine_pass) {
00085 char *pass = getpass("Password:");
00086 if (pass) {
00087 opt_password = SMB_STRDUP(pass);
00088 }
00089 }
00090 #endif
00091
00092 nt_status = cli_full_connection(&cs->cli, global_myname(), global_myname(),
00093 &loopback_ip, 0,
00094 "IPC$", "IPC",
00095 #if 0
00096 opt_user_name,
00097 opt_workgroup,
00098 opt_password,
00099 #else
00100 "",
00101 opt_workgroup,
00102 "",
00103 #endif
00104 0,
00105 Undefined,
00106 NULL);
00107
00108 if (!NT_STATUS_IS_OK(nt_status)) {
00109 DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status)));
00110 cs->failed_connect = True;
00111 cs->err = nt_status;
00112 *perr = nt_status;
00113 return NULL;
00114 }
00115
00116 cs->lsapipe = cli_rpc_pipe_open_noauth(cs->cli,
00117 PI_LSARPC,
00118 &nt_status);
00119
00120 if (cs->lsapipe == NULL) {
00121 DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status)));
00122 cs->failed_connect = True;
00123 cs->err = nt_status;
00124 *perr = nt_status;
00125 return NULL;
00126 }
00127
00128 nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, True,
00129 SEC_RIGHTS_MAXIMUM_ALLOWED,
00130 &cs->pol);
00131
00132 if (!NT_STATUS_IS_OK(nt_status)) {
00133 DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status)));
00134 cs->failed_connect = True;
00135 cs->err = nt_status;
00136 *perr = nt_status;
00137 return NULL;
00138 }
00139
00140 return cs;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 NTSTATUS net_lookup_name_from_sid(TALLOC_CTX *ctx,
00152 DOM_SID *psid,
00153 const char **ppdomain,
00154 const char **ppname)
00155 {
00156 NTSTATUS nt_status;
00157 struct con_struct *csp = NULL;
00158 char **domains;
00159 char **names;
00160 enum lsa_SidType *types;
00161
00162 *ppdomain = NULL;
00163 *ppname = NULL;
00164
00165 csp = create_cs(ctx, &nt_status);
00166 if (csp == NULL) {
00167 return nt_status;
00168 }
00169
00170 nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx,
00171 &csp->pol,
00172 1, psid,
00173 &domains,
00174 &names,
00175 &types);
00176
00177 if (!NT_STATUS_IS_OK(nt_status)) {
00178 return nt_status;
00179 }
00180
00181 *ppdomain = domains[0];
00182 *ppname = names[0];
00183
00184
00185
00186 return NT_STATUS_OK;
00187 }
00188
00189
00190
00191
00192
00193 NTSTATUS net_lookup_sid_from_name(TALLOC_CTX *ctx, const char *full_name, DOM_SID *pret_sid)
00194 {
00195 NTSTATUS nt_status;
00196 struct con_struct *csp = NULL;
00197 DOM_SID *sids = NULL;
00198 enum lsa_SidType *types = NULL;
00199
00200 csp = create_cs(ctx, &nt_status);
00201 if (csp == NULL) {
00202 return nt_status;
00203 }
00204
00205 nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx,
00206 &csp->pol,
00207 1,
00208 &full_name,
00209 NULL, &sids,
00210 &types);
00211
00212 if (!NT_STATUS_IS_OK(nt_status)) {
00213 return nt_status;
00214 }
00215
00216 *pret_sid = sids[0];
00217
00218
00219 return NT_STATUS_OK;
00220 }