utils/netlookup.c

説明を見る。
00001 /*
00002    Unix SMB/CIFS implementation.
00003 
00004    Name lookup.
00005 
00006    Copyright (C) Jeremy Allison 2005
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 #include "includes.h"
00024 #include "utils/net.h"
00025 
00026 /********************************************************
00027  Connection cachine struct. Goes away when ctx destroyed.
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  Close connection on context destruction.
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  Create the connection to localhost.
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         /* Connect to localhost with given username/password. */
00082         /* JRA. Pretty sure we can just do this anonymously.... */
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  Do a lookup_sids call to localhost.
00145  Check if the local machine is authoritative for this sid. We can't
00146  check if this is our SID as that's stored in the root-read-only
00147  secrets.tdb.
00148  The local smbd will also ask winbindd for us, so we don't have to.
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         /* Don't care about type here. */
00184 
00185         /* Converted OK */
00186         return NT_STATUS_OK;
00187 }
00188 
00189 /********************************************************
00190  Do a lookup_names call to localhost.
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         /* Converted OK */
00219         return NT_STATUS_OK;
00220 }

Sambaに対してSat Aug 29 21:23:29 2009に生成されました。  doxygen 1.4.7