Make cli_negprot return NTSTATUS instead of bool
[samba.git] / source3 / libsmb / libsmb_server.c
index 978a843d30c72b73e2985768449367c911e6c0ac..f4714346d1d98c0027fc87f8ca7599877e6dc223 100644 (file)
@@ -7,17 +7,17 @@
    Copyright (C) Tom Jansen (Ninja ISD) 2002 
    Copyright (C) Derrell Lipman 2003-2008
    Copyright (C) Jeremy Allison 2007, 2008
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@@ -58,12 +58,12 @@ SMBC_remove_unused_server(SMBCCTX * context,
        SMBCFILE * file;
 
        /* are we being fooled ? */
-       if (!context || !context->initialized || !srv) {
+       if (!context || !context->internal->initialized || !srv) {
                 return 1;
         }
-       
+
        /* Check all open files/directories for a relation with this server */
-       for (file = context->files; file; file = file->next) {
+       for (file = context->internal->files; file; file = file->next) {
                if (file->srv == srv) {
                        /* Still used */
                        DEBUG(3, ("smbc_remove_usused_server: "
@@ -73,14 +73,14 @@ SMBC_remove_unused_server(SMBCCTX * context,
                }
        }
 
-       DLIST_REMOVE(context->servers, srv);
+       DLIST_REMOVE(context->internal->servers, srv);
 
        cli_shutdown(srv->cli);
        srv->cli = NULL;
 
        DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
 
-       (context->cache.remove_cached_server_fn)(context, srv);
+       smbc_getFunctionRemoveCachedServer(context)(context, srv);
 
         SAFE_FREE(srv);
        return 0;
@@ -101,16 +101,29 @@ SMBC_call_auth_fn(TALLOC_CTX *ctx,
        fstring workgroup;
        fstring username;
        fstring password;
+        smbc_get_auth_data_with_context_fn auth_with_context_fn;
 
        strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
        strlcpy(username, *pp_username, sizeof(username));
        strlcpy(password, *pp_password, sizeof(password));
 
-        (context->server.get_auth_data_fn)(
-                server, share,
-                workgroup, sizeof(workgroup),
-                username, sizeof(username),
-                password, sizeof(password));
+        /* See if there's an authentication with context function provided */
+        auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
+        if (auth_with_context_fn)
+        {
+            (* auth_with_context_fn)(context,
+                                     server, share,
+                                     workgroup, sizeof(workgroup),
+                                     username, sizeof(username),
+                                     password, sizeof(password));
+        }
+        else
+        {
+            smbc_getFunctionAuthData(context)(server, share,
+                                              workgroup, sizeof(workgroup),
+                                              username, sizeof(username),
+                                              password, sizeof(password));
+        }
 
        TALLOC_FREE(*pp_workgroup);
        TALLOC_FREE(*pp_username);
@@ -145,21 +158,21 @@ SMBC_find_server(TALLOC_CTX *ctx,
         SMBCSRV *srv;
         int auth_called = 0;
 
- check_server_cache:
+        if (!pp_workgroup || !pp_username || !pp_password) {
+                return NULL;
+        }
+
+check_server_cache:
 
-       srv = (context->cache.get_cached_server_fn)(context,
-                                                    server, share,
-                                                    *pp_workgroup,
-                                                    *pp_username);
+       srv = smbc_getFunctionGetCachedServer(context)(context,
+                                                       server, share,
+                                                       *pp_workgroup,
+                                                       *pp_username);
 
        if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
-                               !*pp_password || !(*pp_password)[0])) {
+                                     !*pp_password || !(*pp_password)[0])) {
                SMBC_call_auth_fn(ctx, context, server, share,
-                               pp_workgroup, pp_username, pp_password);
-
-               if (!pp_workgroup || !pp_username || !pp_password) {
-                       return NULL;
-               }
+                                  pp_workgroup, pp_username, pp_password);
 
                /*
                  * However, smbc_auth_fn may have picked up info relating to
@@ -172,22 +185,22 @@ SMBC_find_server(TALLOC_CTX *ctx,
        }
 
        if (srv) {
-               if ((context->server.check_server_fn)(context, srv)) {
+               if (smbc_getFunctionCheckServer(context)(context, srv)) {
                        /*
                          * This server is no good anymore
                          * Try to remove it and check for more possible
                          * servers in the cache
                          */
-                       if ((context->server.remove_unused_server_fn)(context,
-                                                                      srv)) { 
+                       if (smbc_getFunctionRemoveUnusedServer(context)(context,
+                                                                        srv)) { 
                                 /*
                                  * We could not remove the server completely,
                                  * remove it from the cache so we will not get
                                  * it again. It will be removed when the last
                                  * file/dir is closed.
                                  */
-                               (context->cache.remove_cached_server_fn)(context,
-                                                                         srv);
+                               smbc_getFunctionRemoveCachedServer(context)(context,
+                                                                            srv);
                        }
 
                        /*
@@ -235,7 +248,7 @@ SMBC_server(TALLOC_CTX *ctx,
         const char *username_used;
        NTSTATUS status;
 
-       zero_addr(&ss);
+       zero_sockaddr(&ss);
        ZERO_STRUCT(c);
 
        if (server[0] == 0) {
@@ -245,18 +258,20 @@ SMBC_server(TALLOC_CTX *ctx,
 
         /* Look for a cached connection */
         srv = SMBC_find_server(ctx, context, server, share,
-                          pp_workgroup, pp_username, pp_password);
+                               pp_workgroup, pp_username, pp_password);
 
         /*
          * If we found a connection and we're only allowed one share per
          * server...
          */
-        if (srv && *share != '\0' && context->one_share_per_server) {
+        if (srv &&
+            *share != '\0' &&
+            smbc_getOptionOneSharePerServer(context)) {
 
                 /*
                  * ... then if there's no current connection to the share,
                  * connect to it.  SMBC_find_server(), or rather the function
-                 * pointed to by context->cache.get_cached_srv_fn which
+                 * pointed to by context->get_cached_srv_fn which
                  * was called by SMBC_find_server(), will have issued a tree
                  * disconnect if the requested share is not the same as the
                  * one that was already connected.
@@ -264,14 +279,16 @@ SMBC_server(TALLOC_CTX *ctx,
                 if (srv->cli->cnum == (uint16) -1) {
                         /* Ensure we have accurate auth info */
                        SMBC_call_auth_fn(ctx, context, server, share,
-                               pp_workgroup, pp_username, pp_password);
+                                          pp_workgroup,
+                                          pp_username,
+                                          pp_password);
 
                        if (!*pp_workgroup || !*pp_username || !*pp_password) {
                                errno = ENOMEM;
                                cli_shutdown(srv->cli);
                                srv->cli = NULL;
-                               (context->cache.remove_cached_server_fn)(context,
-                                                                         srv);
+                               smbc_getFunctionRemoveCachedServer(context)(context,
+                                                                            srv);
                                return NULL;
                        }
 
@@ -282,14 +299,14 @@ SMBC_server(TALLOC_CTX *ctx,
                         */
 
                        if (!cli_send_tconX(srv->cli, share, "?????",
-                                               *pp_password,
-                                               strlen(*pp_password)+1)) {
+                                            *pp_password,
+                                            strlen(*pp_password)+1)) {
 
                                 errno = SMBC_errno(context, srv->cli);
                                 cli_shutdown(srv->cli);
                                srv->cli = NULL;
-                                (context->cache.remove_cached_server_fn)(context,
-                                                                         srv);
+                                smbc_getFunctionRemoveCachedServer(context)(context,
+                                                                            srv);
                                 srv = NULL;
                         }
 
@@ -322,16 +339,16 @@ SMBC_server(TALLOC_CTX *ctx,
                return NULL;
        }
 
-       make_nmb_name(&calling, context->netbios_name, 0x0);
+       make_nmb_name(&calling, smbc_getNetbiosName(context), 0x0);
        make_nmb_name(&called , server, 0x20);
 
        DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
 
        DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
 
- again:
+again:
 
-       zero_addr(&ss);
+       zero_sockaddr(&ss);
 
        /* have to open a new connection */
        if ((c = cli_initialise()) == NULL) {
@@ -339,14 +356,15 @@ SMBC_server(TALLOC_CTX *ctx,
                return NULL;
        }
 
-       if (context->use_kerberos) {
+        if (smbc_getOptionUseKerberos(context)) {
                c->use_kerberos = True;
        }
-       if (context->fallback_after_kerberos) {
+
+        if (smbc_getOptionFallbackAfterKerberos(context)) {
                c->fallback_after_kerberos = True;
        }
 
-       c->timeout = context->timeout;
+       c->timeout = smbc_getTimeout(context);
 
         /*
          * Force use of port 139 for first try if share is $IPC, empty, or
@@ -390,18 +408,21 @@ SMBC_server(TALLOC_CTX *ctx,
                                struct sockaddr_storage rem_ss;
 
                                if (!interpret_string_addr(&rem_ss, server,
-                                                       NI_NUMERICHOST)) {
+                                                           NI_NUMERICHOST)) {
                                        DEBUG(4, ("Could not convert IP address "
-                                               "%s to struct sockaddr_storage\n",
-                                               server));
+                                                  "%s to struct sockaddr_storage\n",
+                                                  server));
                                        errno = ETIMEDOUT;
                                        return NULL;
                                }
 
                                tried_reverse++; /* Yuck */
 
-                               if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
-                                       make_nmb_name(&called, remote_name, 0x20);
+                               if (name_status_find("*", 0, 0,
+                                                     &rem_ss, remote_name)) {
+                                       make_nmb_name(&called,
+                                                      remote_name,
+                                                      0x20);
                                        goto again;
                                }
                        }
@@ -412,7 +433,9 @@ SMBC_server(TALLOC_CTX *ctx,
 
        DEBUG(4,(" session request ok\n"));
 
-       if (!cli_negprot(c)) {
+       status = cli_negprot(c);
+
+       if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(c);
                errno = ETIMEDOUT;
                return NULL;
@@ -421,14 +444,16 @@ SMBC_server(TALLOC_CTX *ctx,
         username_used = *pp_username;
 
        if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
-                                              *pp_password, strlen(*pp_password),
-                                              *pp_password, strlen(*pp_password),
+                                              *pp_password,
+                                               strlen(*pp_password),
+                                              *pp_password,
+                                               strlen(*pp_password),
                                               *pp_workgroup))) {
 
                 /* Failed.  Try an anonymous login, if allowed by flags. */
                 username_used = "";
 
-                if (context->no_auto_anonymous_login ||
+                if (smbc_getOptionNoAutoAnonymousLogin(context) ||
                     !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
                                                        *pp_password, 1,
                                                        *pp_password, 0,
@@ -451,12 +476,12 @@ SMBC_server(TALLOC_CTX *ctx,
 
        DEBUG(4,(" tconx ok\n"));
 
-       if (context->smb_encryption_level) {
+       if (context->internal->smb_encryption_level) {
                /* Attempt UNIX smb encryption. */
                if (!NT_STATUS_IS_OK(cli_force_encryption(c,
-                                               username_used,
-                                               *pp_password,
-                                               *pp_workgroup))) {
+                                                          username_used,
+                                                          *pp_password,
+                                                          *pp_workgroup))) {
 
                        /*
                         * context->smb_encryption_level == 1
@@ -466,7 +491,7 @@ SMBC_server(TALLOC_CTX *ctx,
 
                        DEBUG(4,(" SMB encrypt failed\n"));
 
-                       if (context->smb_encryption_level == 2) {
+                       if (context->internal->smb_encryption_level == 2) {
                                cli_shutdown(c);
                                errno = EPERM;
                                return NULL;
@@ -496,10 +521,10 @@ SMBC_server(TALLOC_CTX *ctx,
        /* now add it to the cache (internal or external)  */
        /* Let the cache function set errno if it wants to */
        errno = 0;
-       if ((context->cache.add_cached_server_fn)(context, srv,
-                                                  server, share,
-                                                  *pp_workgroup,
-                                                  *pp_username)) {
+       if (smbc_getFunctionAddCachedServer(context)(context, srv,
+                                                     server, share,
+                                                     *pp_workgroup,
+                                                     *pp_username)) {
                int saved_errno = errno;
                DEBUG(3, (" Failed to add server to cache\n"));
                errno = saved_errno;
@@ -512,10 +537,10 @@ SMBC_server(TALLOC_CTX *ctx,
        DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
                  server, share, srv));
 
-       DLIST_ADD(context->servers, srv);
+       DLIST_ADD(context->internal->servers, srv);
        return srv;
 
- failed:
+failed:
        cli_shutdown(c);
        if (!srv) {
                return NULL;
@@ -551,14 +576,16 @@ SMBC_attr_server(TALLOC_CTX *ctx,
          * name due to the leading asterisk.
          */
         ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
-                              pp_workgroup, pp_username, pp_password);
+                                   pp_workgroup, pp_username, pp_password);
         if (!ipc_srv) {
 
                 /* We didn't find a cached connection.  Get the password */
                if (!*pp_password || (*pp_password)[0] == '\0') {
                         /* ... then retrieve it now. */
                        SMBC_call_auth_fn(ctx, context, server, share,
-                               pp_workgroup, pp_username, pp_password);
+                                          pp_workgroup,
+                                          pp_username,
+                                          pp_password);
                        if (!*pp_workgroup || !*pp_username || !*pp_password) {
                                errno = ENOMEM;
                                return NULL;
@@ -566,11 +593,11 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 }
 
                 flags = 0;
-                if (context->use_kerberos) {
+                if (smbc_getOptionUseKerberos(context)) {
                         flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
                 }
 
-                zero_addr(&ss);
+                zero_sockaddr(&ss);
                 nt_status = cli_full_connection(&ipc_cli,
                                                global_myname(), server,
                                                &ss, 0, "IPC$", "?????",
@@ -586,12 +613,12 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                         return NULL;
                 }
 
-               if (context->smb_encryption_level) {
+               if (context->internal->smb_encryption_level) {
                        /* Attempt UNIX smb encryption. */
                        if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
-                                               *pp_username,
-                                               *pp_password,
-                                               *pp_workgroup))) {
+                                                                  *pp_username,
+                                                                  *pp_password,
+                                                                  *pp_workgroup))) {
 
                                /*
                                 * context->smb_encryption_level ==
@@ -602,7 +629,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
 
                                DEBUG(4,(" SMB encrypt failed on IPC$\n"));
 
-                               if (context->smb_encryption_level == 2) {
+                               if (context->internal->smb_encryption_level == 2) {
                                        cli_shutdown(ipc_cli);
                                        errno = EPERM;
                                        return NULL;
@@ -621,15 +648,14 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 ZERO_STRUCTP(ipc_srv);
                 ipc_srv->cli = ipc_cli;
 
-                pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
-                                                    PI_LSARPC,
-                                                    &nt_status);
-                if (!pipe_hnd) {
-                    DEBUG(1, ("cli_nt_session_open fail!\n"));
-                    errno = ENOTSUP;
-                    cli_shutdown(ipc_srv->cli);
-                    free(ipc_srv);
-                    return NULL;
+                nt_status = cli_rpc_pipe_open_noauth(
+                       ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
+                if (!NT_STATUS_IS_OK(nt_status)) {
+                        DEBUG(1, ("cli_nt_session_open fail!\n"));
+                        errno = ENOTSUP;
+                        cli_shutdown(ipc_srv->cli);
+                        free(ipc_srv);
+                        return NULL;
                 }
 
                 /*
@@ -639,26 +665,26 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                  */
 
                 nt_status = rpccli_lsa_open_policy(
-                    pipe_hnd,
-                    talloc_tos(),
-                    True,
-                    GENERIC_EXECUTE_ACCESS,
-                    &ipc_srv->pol);
+                        pipe_hnd,
+                        talloc_tos(),
+                        True,
+                        GENERIC_EXECUTE_ACCESS,
+                        &ipc_srv->pol);
 
                 if (!NT_STATUS_IS_OK(nt_status)) {
-                    errno = SMBC_errno(context, ipc_srv->cli);
-                    cli_shutdown(ipc_srv->cli);
-                    return NULL;
+                        errno = SMBC_errno(context, ipc_srv->cli);
+                        cli_shutdown(ipc_srv->cli);
+                        return NULL;
                 }
 
                 /* now add it to the cache (internal or external) */
 
                 errno = 0;      /* let cache function set errno if it likes */
-                if ((context->cache.add_cached_server_fn)(context, ipc_srv,
-                                                          server,
-                                                          "*IPC$",
-                                                          *pp_workgroup,
-                                                          *pp_username)) {
+                if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
+                                                             server,
+                                                             "*IPC$",
+                                                             *pp_workgroup,
+                                                             *pp_username)) {
                         DEBUG(3, (" Failed to add server to cache\n"));
                         if (errno == 0) {
                                 errno = ENOMEM;
@@ -668,7 +694,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                         return NULL;
                 }
 
-                DLIST_ADD(context->servers, ipc_srv);
+                DLIST_ADD(context->internal->servers, ipc_srv);
         }
 
         return ipc_srv;