| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 |  | 
|---|
| 4 | Name lookup. | 
|---|
| 5 |  | 
|---|
| 6 | Copyright (C) Jeremy Allison 2005 | 
|---|
| 7 |  | 
|---|
| 8 | This program is free software; you can redistribute it and/or modify | 
|---|
| 9 | it under the terms of the GNU General Public License as published by | 
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 11 | (at your option) any later version. | 
|---|
| 12 |  | 
|---|
| 13 | This program is distributed in the hope that it will be useful, | 
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | GNU General Public License for more details. | 
|---|
| 17 |  | 
|---|
| 18 | You should have received a copy of the GNU General Public License | 
|---|
| 19 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #include "includes.h" | 
|---|
| 23 | #include "utils/net.h" | 
|---|
| 24 | #include "rpc_client/cli_pipe.h" | 
|---|
| 25 | #include "../librpc/gen_ndr/ndr_lsa.h" | 
|---|
| 26 | #include "rpc_client/cli_lsarpc.h" | 
|---|
| 27 | #include "libsmb/libsmb.h" | 
|---|
| 28 |  | 
|---|
| 29 | /******************************************************** | 
|---|
| 30 | Connection cachine struct. Goes away when ctx destroyed. | 
|---|
| 31 | ********************************************************/ | 
|---|
| 32 |  | 
|---|
| 33 | struct con_struct { | 
|---|
| 34 | bool failed_connect; | 
|---|
| 35 | NTSTATUS err; | 
|---|
| 36 | struct cli_state *cli; | 
|---|
| 37 | struct rpc_pipe_client *lsapipe; | 
|---|
| 38 | struct policy_handle pol; | 
|---|
| 39 | }; | 
|---|
| 40 |  | 
|---|
| 41 | static struct con_struct *cs; | 
|---|
| 42 |  | 
|---|
| 43 | /******************************************************** | 
|---|
| 44 | Close connection on context destruction. | 
|---|
| 45 | ********************************************************/ | 
|---|
| 46 |  | 
|---|
| 47 | static int cs_destructor(struct con_struct *p) | 
|---|
| 48 | { | 
|---|
| 49 | if (cs->cli) { | 
|---|
| 50 | cli_shutdown(cs->cli); | 
|---|
| 51 | } | 
|---|
| 52 | cs = NULL; | 
|---|
| 53 | return 0; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | /******************************************************** | 
|---|
| 57 | Create the connection to localhost. | 
|---|
| 58 | ********************************************************/ | 
|---|
| 59 |  | 
|---|
| 60 | static struct con_struct *create_cs(struct net_context *c, | 
|---|
| 61 | TALLOC_CTX *ctx, NTSTATUS *perr) | 
|---|
| 62 | { | 
|---|
| 63 | NTSTATUS nt_status; | 
|---|
| 64 | struct sockaddr_storage loopback_ss; | 
|---|
| 65 |  | 
|---|
| 66 | *perr = NT_STATUS_OK; | 
|---|
| 67 |  | 
|---|
| 68 | if (!interpret_string_addr(&loopback_ss, "127.0.0.1", AI_NUMERICHOST)) { | 
|---|
| 69 | *perr = NT_STATUS_INVALID_PARAMETER; | 
|---|
| 70 | return NULL; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | if (cs) { | 
|---|
| 74 | if (cs->failed_connect) { | 
|---|
| 75 | *perr = cs->err; | 
|---|
| 76 | return NULL; | 
|---|
| 77 | } | 
|---|
| 78 | return cs; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | cs = TALLOC_P(ctx, struct con_struct); | 
|---|
| 82 | if (!cs) { | 
|---|
| 83 | *perr = NT_STATUS_NO_MEMORY; | 
|---|
| 84 | return NULL; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | ZERO_STRUCTP(cs); | 
|---|
| 88 | talloc_set_destructor(cs, cs_destructor); | 
|---|
| 89 |  | 
|---|
| 90 | /* Connect to localhost with given username/password. */ | 
|---|
| 91 | /* JRA. Pretty sure we can just do this anonymously.... */ | 
|---|
| 92 | #if 0 | 
|---|
| 93 | if (!opt_password && !opt_machine_pass) { | 
|---|
| 94 | char *pass = getpass("Password:"); | 
|---|
| 95 | if (pass) { | 
|---|
| 96 | opt_password = SMB_STRDUP(pass); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 | #endif | 
|---|
| 100 |  | 
|---|
| 101 | nt_status = cli_full_connection(&cs->cli, global_myname(), global_myname(), | 
|---|
| 102 | &loopback_ss, 0, | 
|---|
| 103 | "IPC$", "IPC", | 
|---|
| 104 | #if 0 | 
|---|
| 105 | c->opt_user_name, | 
|---|
| 106 | c->opt_workgroup, | 
|---|
| 107 | c->opt_password, | 
|---|
| 108 | #else | 
|---|
| 109 | "", | 
|---|
| 110 | c->opt_workgroup, | 
|---|
| 111 | "", | 
|---|
| 112 | #endif | 
|---|
| 113 | 0, | 
|---|
| 114 | Undefined); | 
|---|
| 115 |  | 
|---|
| 116 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 117 | DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status))); | 
|---|
| 118 | cs->failed_connect = true; | 
|---|
| 119 | cs->err = nt_status; | 
|---|
| 120 | *perr = nt_status; | 
|---|
| 121 | return NULL; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | nt_status = cli_rpc_pipe_open_noauth(cs->cli, | 
|---|
| 125 | &ndr_table_lsarpc.syntax_id, | 
|---|
| 126 | &cs->lsapipe); | 
|---|
| 127 |  | 
|---|
| 128 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 129 | DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status))); | 
|---|
| 130 | cs->failed_connect = true; | 
|---|
| 131 | cs->err = nt_status; | 
|---|
| 132 | *perr = nt_status; | 
|---|
| 133 | return NULL; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, true, | 
|---|
| 137 | SEC_FLAG_MAXIMUM_ALLOWED, | 
|---|
| 138 | &cs->pol); | 
|---|
| 139 |  | 
|---|
| 140 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 141 | DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status))); | 
|---|
| 142 | cs->failed_connect = true; | 
|---|
| 143 | cs->err = nt_status; | 
|---|
| 144 | *perr = nt_status; | 
|---|
| 145 | return NULL; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | return cs; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /******************************************************** | 
|---|
| 152 | Do a lookup_sids call to localhost. | 
|---|
| 153 | Check if the local machine is authoritative for this sid. We can't | 
|---|
| 154 | check if this is our SID as that's stored in the root-read-only | 
|---|
| 155 | secrets.tdb. | 
|---|
| 156 | The local smbd will also ask winbindd for us, so we don't have to. | 
|---|
| 157 | ********************************************************/ | 
|---|
| 158 |  | 
|---|
| 159 | NTSTATUS net_lookup_name_from_sid(struct net_context *c, | 
|---|
| 160 | TALLOC_CTX *ctx, | 
|---|
| 161 | struct dom_sid *psid, | 
|---|
| 162 | const char **ppdomain, | 
|---|
| 163 | const char **ppname) | 
|---|
| 164 | { | 
|---|
| 165 | NTSTATUS nt_status; | 
|---|
| 166 | struct con_struct *csp = NULL; | 
|---|
| 167 | char **domains; | 
|---|
| 168 | char **names; | 
|---|
| 169 | enum lsa_SidType *types; | 
|---|
| 170 |  | 
|---|
| 171 | *ppdomain = NULL; | 
|---|
| 172 | *ppname = NULL; | 
|---|
| 173 |  | 
|---|
| 174 | csp = create_cs(c, ctx, &nt_status); | 
|---|
| 175 | if (csp == NULL) { | 
|---|
| 176 | return nt_status; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx, | 
|---|
| 180 | &csp->pol, | 
|---|
| 181 | 1, psid, | 
|---|
| 182 | &domains, | 
|---|
| 183 | &names, | 
|---|
| 184 | &types); | 
|---|
| 185 |  | 
|---|
| 186 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 187 | return nt_status; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | *ppdomain = domains[0]; | 
|---|
| 191 | *ppname = names[0]; | 
|---|
| 192 | /* Don't care about type here. */ | 
|---|
| 193 |  | 
|---|
| 194 | /* Converted OK */ | 
|---|
| 195 | return NT_STATUS_OK; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | /******************************************************** | 
|---|
| 199 | Do a lookup_names call to localhost. | 
|---|
| 200 | ********************************************************/ | 
|---|
| 201 |  | 
|---|
| 202 | NTSTATUS net_lookup_sid_from_name(struct net_context *c, TALLOC_CTX *ctx, | 
|---|
| 203 | const char *full_name, struct dom_sid *pret_sid) | 
|---|
| 204 | { | 
|---|
| 205 | NTSTATUS nt_status; | 
|---|
| 206 | struct con_struct *csp = NULL; | 
|---|
| 207 | struct dom_sid *sids = NULL; | 
|---|
| 208 | enum lsa_SidType *types = NULL; | 
|---|
| 209 |  | 
|---|
| 210 | csp = create_cs(c, ctx, &nt_status); | 
|---|
| 211 | if (csp == NULL) { | 
|---|
| 212 | return nt_status; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx, | 
|---|
| 216 | &csp->pol, | 
|---|
| 217 | 1, | 
|---|
| 218 | &full_name, | 
|---|
| 219 | NULL, 1, | 
|---|
| 220 | &sids, &types); | 
|---|
| 221 |  | 
|---|
| 222 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 223 | return nt_status; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | *pret_sid = sids[0]; | 
|---|
| 227 |  | 
|---|
| 228 | /* Converted OK */ | 
|---|
| 229 | return NT_STATUS_OK; | 
|---|
| 230 | } | 
|---|