| 1 | /*
|
|---|
| 2 | Unix SMB/Netbios implementation.
|
|---|
| 3 | SMB client library implementation
|
|---|
| 4 | Copyright (C) Andrew Tridgell 1998
|
|---|
| 5 | Copyright (C) Richard Sharpe 2000, 2002
|
|---|
| 6 | Copyright (C) John Terpstra 2000
|
|---|
| 7 | Copyright (C) Tom Jansen (Ninja ISD) 2002
|
|---|
| 8 | Copyright (C) Derrell Lipman 2003-2008
|
|---|
| 9 | Copyright (C) Jeremy Allison 2007, 2008
|
|---|
| 10 |
|
|---|
| 11 | This program is free software; you can redistribute it and/or modify
|
|---|
| 12 | it under the terms of the GNU General Public License as published by
|
|---|
| 13 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 14 | (at your option) any later version.
|
|---|
| 15 |
|
|---|
| 16 | This program is distributed in the hope that it will be useful,
|
|---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | GNU General Public License for more details.
|
|---|
| 20 |
|
|---|
| 21 | You should have received a copy of the GNU General Public License
|
|---|
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include "includes.h"
|
|---|
| 26 | #include "libsmbclient.h"
|
|---|
| 27 | #include "libsmb_internal.h"
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | /*
|
|---|
| 31 | * Check a server for being alive and well.
|
|---|
| 32 | * returns 0 if the server is in shape. Returns 1 on error
|
|---|
| 33 | *
|
|---|
| 34 | * Also useable outside libsmbclient to enable external cache
|
|---|
| 35 | * to do some checks too.
|
|---|
| 36 | */
|
|---|
| 37 | int
|
|---|
| 38 | SMBC_check_server(SMBCCTX * context,
|
|---|
| 39 | SMBCSRV * server)
|
|---|
| 40 | {
|
|---|
| 41 | socklen_t size;
|
|---|
| 42 | struct sockaddr addr;
|
|---|
| 43 |
|
|---|
| 44 | size = sizeof(addr);
|
|---|
| 45 | return (getpeername(server->cli->fd, &addr, &size) == -1);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /*
|
|---|
| 49 | * Remove a server from the cached server list it's unused.
|
|---|
| 50 | * On success, 0 is returned. 1 is returned if the server could not be removed.
|
|---|
| 51 | *
|
|---|
| 52 | * Also useable outside libsmbclient
|
|---|
| 53 | */
|
|---|
| 54 | int
|
|---|
| 55 | SMBC_remove_unused_server(SMBCCTX * context,
|
|---|
| 56 | SMBCSRV * srv)
|
|---|
| 57 | {
|
|---|
| 58 | SMBCFILE * file;
|
|---|
| 59 |
|
|---|
| 60 | /* are we being fooled ? */
|
|---|
| 61 | if (!context || !context->internal->initialized || !srv) {
|
|---|
| 62 | return 1;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* Check all open files/directories for a relation with this server */
|
|---|
| 66 | for (file = context->internal->files; file; file = file->next) {
|
|---|
| 67 | if (file->srv == srv) {
|
|---|
| 68 | /* Still used */
|
|---|
| 69 | DEBUG(3, ("smbc_remove_usused_server: "
|
|---|
| 70 | "%p still used by %p.\n",
|
|---|
| 71 | srv, file));
|
|---|
| 72 | return 1;
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | DLIST_REMOVE(context->internal->servers, srv);
|
|---|
| 77 |
|
|---|
| 78 | cli_shutdown(srv->cli);
|
|---|
| 79 | srv->cli = NULL;
|
|---|
| 80 |
|
|---|
| 81 | DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
|
|---|
| 82 |
|
|---|
| 83 | smbc_getFunctionRemoveCachedServer(context)(context, srv);
|
|---|
| 84 |
|
|---|
| 85 | SAFE_FREE(srv);
|
|---|
| 86 | return 0;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /****************************************************************
|
|---|
| 90 | * Call the auth_fn with fixed size (fstring) buffers.
|
|---|
| 91 | ***************************************************************/
|
|---|
| 92 | void
|
|---|
| 93 | SMBC_call_auth_fn(TALLOC_CTX *ctx,
|
|---|
| 94 | SMBCCTX *context,
|
|---|
| 95 | const char *server,
|
|---|
| 96 | const char *share,
|
|---|
| 97 | char **pp_workgroup,
|
|---|
| 98 | char **pp_username,
|
|---|
| 99 | char **pp_password)
|
|---|
| 100 | {
|
|---|
| 101 | fstring workgroup;
|
|---|
| 102 | fstring username;
|
|---|
| 103 | fstring password;
|
|---|
| 104 | smbc_get_auth_data_with_context_fn auth_with_context_fn;
|
|---|
| 105 |
|
|---|
| 106 | strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
|
|---|
| 107 | strlcpy(username, *pp_username, sizeof(username));
|
|---|
| 108 | strlcpy(password, *pp_password, sizeof(password));
|
|---|
| 109 |
|
|---|
| 110 | /* See if there's an authentication with context function provided */
|
|---|
| 111 | auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
|
|---|
| 112 | if (auth_with_context_fn)
|
|---|
| 113 | {
|
|---|
| 114 | (* auth_with_context_fn)(context,
|
|---|
| 115 | server, share,
|
|---|
| 116 | workgroup, sizeof(workgroup),
|
|---|
| 117 | username, sizeof(username),
|
|---|
| 118 | password, sizeof(password));
|
|---|
| 119 | }
|
|---|
| 120 | else
|
|---|
| 121 | {
|
|---|
| 122 | smbc_getFunctionAuthData(context)(server, share,
|
|---|
| 123 | workgroup, sizeof(workgroup),
|
|---|
| 124 | username, sizeof(username),
|
|---|
| 125 | password, sizeof(password));
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | TALLOC_FREE(*pp_workgroup);
|
|---|
| 129 | TALLOC_FREE(*pp_username);
|
|---|
| 130 | TALLOC_FREE(*pp_password);
|
|---|
| 131 |
|
|---|
| 132 | *pp_workgroup = talloc_strdup(ctx, workgroup);
|
|---|
| 133 | *pp_username = talloc_strdup(ctx, username);
|
|---|
| 134 | *pp_password = talloc_strdup(ctx, password);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | void
|
|---|
| 139 | SMBC_get_auth_data(const char *server, const char *share,
|
|---|
| 140 | char *workgroup_buf, int workgroup_buf_len,
|
|---|
| 141 | char *username_buf, int username_buf_len,
|
|---|
| 142 | char *password_buf, int password_buf_len)
|
|---|
| 143 | {
|
|---|
| 144 | /* Default function just uses provided data. Nothing to do. */
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | SMBCSRV *
|
|---|
| 150 | SMBC_find_server(TALLOC_CTX *ctx,
|
|---|
| 151 | SMBCCTX *context,
|
|---|
| 152 | const char *server,
|
|---|
| 153 | const char *share,
|
|---|
| 154 | char **pp_workgroup,
|
|---|
| 155 | char **pp_username,
|
|---|
| 156 | char **pp_password)
|
|---|
| 157 | {
|
|---|
| 158 | SMBCSRV *srv;
|
|---|
| 159 | int auth_called = 0;
|
|---|
| 160 |
|
|---|
| 161 | if (!pp_workgroup || !pp_username || !pp_password) {
|
|---|
| 162 | return NULL;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | check_server_cache:
|
|---|
| 166 |
|
|---|
| 167 | srv = smbc_getFunctionGetCachedServer(context)(context,
|
|---|
| 168 | server, share,
|
|---|
| 169 | *pp_workgroup,
|
|---|
| 170 | *pp_username);
|
|---|
| 171 |
|
|---|
|
|---|