| 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 2 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, write to the Free Software | 
|---|
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | #include "includes.h" | 
|---|
| 24 | #include "utils/net.h" | 
|---|
| 25 |  | 
|---|
| 26 | /******************************************************** | 
|---|
| 27 | Connection cachine struct. Goes away when ctx destroyed. | 
|---|
| 28 | ********************************************************/ | 
|---|
| 29 |  | 
|---|
| 30 | struct con_struct { | 
|---|
| 31 | BOOL failed_connect; | 
|---|
| 32 | NTSTATUS err; | 
|---|
| 33 | struct cli_state *cli; | 
|---|
| 34 | struct rpc_pipe_client *lsapipe; | 
|---|
| 35 | POLICY_HND pol; | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | static struct con_struct *cs; | 
|---|
| 39 |  | 
|---|
| 40 | /******************************************************** | 
|---|
| 41 | Close connection on context destruction. | 
|---|
| 42 | ********************************************************/ | 
|---|
| 43 |  | 
|---|
| 44 | static int cs_destructor(struct con_struct *p) | 
|---|
| 45 | { | 
|---|
| 46 | if (cs->cli) { | 
|---|
| 47 | cli_shutdown(cs->cli); | 
|---|
| 48 | } | 
|---|
| 49 | cs = NULL; | 
|---|
| 50 | return 0; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | /******************************************************** | 
|---|
| 54 | Create the connection to localhost. | 
|---|
| 55 | ********************************************************/ | 
|---|
| 56 |  | 
|---|
| 57 | static struct con_struct *create_cs(TALLOC_CTX *ctx, NTSTATUS *perr) | 
|---|
| 58 | { | 
|---|
| 59 | NTSTATUS nt_status; | 
|---|
| 60 | struct in_addr loopback_ip = *interpret_addr2("127.0.0.1"); | 
|---|
| 61 |  | 
|---|
| 62 | *perr = NT_STATUS_OK; | 
|---|
| 63 |  | 
|---|
| 64 | if (cs) { | 
|---|
| 65 | if (cs->failed_connect) { | 
|---|
| 66 | *perr = cs->err; | 
|---|
| 67 | return NULL; | 
|---|
| 68 | } | 
|---|
| 69 | return cs; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | cs = TALLOC_P(ctx, struct con_struct); | 
|---|
| 73 | if (!cs) { | 
|---|
| 74 | *perr = NT_STATUS_NO_MEMORY; | 
|---|
| 75 | return NULL; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | ZERO_STRUCTP(cs); | 
|---|
| 79 | talloc_set_destructor(cs, cs_destructor); | 
|---|
| 80 |  | 
|---|
| 81 | /* Connect to localhost with given username/password. */ | 
|---|
| 82 | /* JRA. Pretty sure we can just do this anonymously.... */ | 
|---|
| 83 | #if 0 | 
|---|
| 84 | if (!opt_password && !opt_machine_pass) { | 
|---|
| 85 | char *pass = getpass("Password:"); | 
|---|
| 86 | if (pass) { | 
|---|
| 87 | opt_password = SMB_STRDUP(pass); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 | #endif | 
|---|
| 91 |  | 
|---|
| 92 | nt_status = cli_full_connection(&cs->cli, global_myname(), global_myname(), | 
|---|
| 93 | &loopback_ip, 0, | 
|---|
| 94 | "IPC$", "IPC", | 
|---|
| 95 | #if 0 | 
|---|
| 96 | opt_user_name, | 
|---|
| 97 | opt_workgroup, | 
|---|
| 98 | opt_password, | 
|---|
| 99 | #else | 
|---|
| 100 | "", | 
|---|
| 101 | opt_workgroup, | 
|---|
| 102 | "", | 
|---|
| 103 | #endif | 
|---|
| 104 | 0, | 
|---|
| 105 | Undefined, | 
|---|
| 106 | NULL); | 
|---|
| 107 |  | 
|---|
| 108 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 109 | DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status))); | 
|---|
| 110 | cs->failed_connect = True; | 
|---|
| 111 | cs->err = nt_status; | 
|---|
| 112 | *perr = nt_status; | 
|---|
| 113 | return NULL; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | cs->lsapipe = cli_rpc_pipe_open_noauth(cs->cli, | 
|---|
| 117 | PI_LSARPC, | 
|---|
| 118 | &nt_status); | 
|---|
| 119 |  | 
|---|
| 120 | if (cs->lsapipe == NULL) { | 
|---|
| 121 | DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status))); | 
|---|
| 122 | cs->failed_connect = True; | 
|---|
| 123 | cs->err = nt_status; | 
|---|
| 124 | *perr = nt_status; | 
|---|
| 125 | return NULL; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, True, | 
|---|
| 129 | SEC_RIGHTS_MAXIMUM_ALLOWED, | 
|---|
| 130 | &cs->pol); | 
|---|
| 131 |  | 
|---|
| 132 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 133 | DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status))); | 
|---|
| 134 | cs->failed_connect = True; | 
|---|
| 135 | cs->err = nt_status; | 
|---|
| 136 | *perr = nt_status; | 
|---|
| 137 | return NULL; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | return cs; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /******************************************************** | 
|---|
| 144 | Do a lookup_sids call to localhost. | 
|---|
| 145 | Check if the local machine is authoritative for this sid. We can't | 
|---|
| 146 | check if this is our SID as that's stored in the root-read-only | 
|---|
| 147 | secrets.tdb. | 
|---|
| 148 | The local smbd will also ask winbindd for us, so we don't have to. | 
|---|
| 149 | ********************************************************/ | 
|---|
| 150 |  | 
|---|
| 151 | NTSTATUS net_lookup_name_from_sid(TALLOC_CTX *ctx, | 
|---|
| 152 | DOM_SID *psid, | 
|---|
| 153 | const char **ppdomain, | 
|---|
| 154 | const char **ppname) | 
|---|
| 155 | { | 
|---|
| 156 | NTSTATUS nt_status; | 
|---|
| 157 | struct con_struct *csp = NULL; | 
|---|
| 158 | char **domains; | 
|---|
| 159 | char **names; | 
|---|
| 160 | enum lsa_SidType *types; | 
|---|
| 161 |  | 
|---|
| 162 | *ppdomain = NULL; | 
|---|
| 163 | *ppname = NULL; | 
|---|
| 164 |  | 
|---|
| 165 | csp = create_cs(ctx, &nt_status); | 
|---|
| 166 | if (csp == NULL) { | 
|---|
| 167 | return nt_status; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx, | 
|---|
| 171 | &csp->pol, | 
|---|
| 172 | 1, psid, | 
|---|
| 173 | &domains, | 
|---|
| 174 | &names, | 
|---|
| 175 | &types); | 
|---|
| 176 |  | 
|---|
| 177 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 178 | return nt_status; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | *ppdomain = domains[0]; | 
|---|
| 182 | *ppname = names[0]; | 
|---|
| 183 | /* Don't care about type here. */ | 
|---|
| 184 |  | 
|---|
| 185 | /* Converted OK */ | 
|---|
| 186 | return NT_STATUS_OK; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /******************************************************** | 
|---|
| 190 | Do a lookup_names call to localhost. | 
|---|
| 191 | ********************************************************/ | 
|---|
| 192 |  | 
|---|
| 193 | NTSTATUS net_lookup_sid_from_name(TALLOC_CTX *ctx, const char *full_name, DOM_SID *pret_sid) | 
|---|
| 194 | { | 
|---|
| 195 | NTSTATUS nt_status; | 
|---|
| 196 | struct con_struct *csp = NULL; | 
|---|
| 197 | DOM_SID *sids = NULL; | 
|---|
| 198 | enum lsa_SidType *types = NULL; | 
|---|
| 199 |  | 
|---|
| 200 | csp = create_cs(ctx, &nt_status); | 
|---|
| 201 | if (csp == NULL) { | 
|---|
| 202 | return nt_status; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx, | 
|---|
| 206 | &csp->pol, | 
|---|
| 207 | 1, | 
|---|
| 208 | &full_name, | 
|---|
| 209 | NULL, &sids, | 
|---|
| 210 | &types); | 
|---|
| 211 |  | 
|---|
| 212 | if (!NT_STATUS_IS_OK(nt_status)) { | 
|---|
| 213 | return nt_status; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | *pret_sid = sids[0]; | 
|---|
| 217 |  | 
|---|
| 218 | /* Converted OK */ | 
|---|
| 219 | return NT_STATUS_OK; | 
|---|
| 220 | } | 
|---|