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 "libsmb/libsmb.h"
|
---|
27 | #include "libsmbclient.h"
|
---|
28 | #include "libsmb_internal.h"
|
---|
29 | #include "secrets.h"
|
---|
30 | #include "../libcli/smb/smbXcli_base.h"
|
---|
31 |
|
---|
32 | /*
|
---|
33 | * Is the logging working / configfile read ?
|
---|
34 | */
|
---|
35 | static bool SMBC_initialized = false;
|
---|
36 | static unsigned int initialized_ctx_count = 0;
|
---|
37 | static void *initialized_ctx_count_mutex = NULL;
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Do some module- and library-wide intializations
|
---|
41 | */
|
---|
42 | static void
|
---|
43 | SMBC_module_init(void * punused)
|
---|
44 | {
|
---|
45 | bool conf_loaded = False;
|
---|
46 | char *home = NULL;
|
---|
47 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
48 |
|
---|
49 | setup_logging("libsmbclient", DEBUG_STDOUT);
|
---|
50 |
|
---|
51 | /* Here we would open the smb.conf file if needed ... */
|
---|
52 |
|
---|
53 | home = getenv("HOME");
|
---|
54 | if (home) {
|
---|
55 | char *conf = NULL;
|
---|
56 | if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
|
---|
57 | if (lp_load_client(conf)) {
|
---|
58 | conf_loaded = True;
|
---|
59 | } else {
|
---|
60 | DEBUG(5, ("Could not load config file: %s\n",
|
---|
61 | conf));
|
---|
62 | }
|
---|
63 | SAFE_FREE(conf);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (!conf_loaded) {
|
---|
68 | /*
|
---|
69 | * Well, if that failed, try the get_dyn_CONFIGFILE
|
---|
70 | * Which points to the standard locn, and if that
|
---|
71 | * fails, silently ignore it and use the internal
|
---|
72 | * defaults ...
|
---|
73 | */
|
---|
74 |
|
---|
75 | if (!lp_load_client(get_dyn_CONFIGFILE())) {
|
---|
76 | DEBUG(5, ("Could not load config file: %s\n",
|
---|
77 | get_dyn_CONFIGFILE()));
|
---|
78 | } else if (home) {
|
---|
79 | char *conf;
|
---|
80 | /*
|
---|
81 | * We loaded the global config file. Now lets
|
---|
82 | * load user-specific modifications to the
|
---|
83 | * global config.
|
---|
84 | */
|
---|
85 | if (asprintf(&conf,
|
---|
86 | "%s/.smb/smb.conf.append",
|
---|
87 | home) > 0) {
|
---|
88 | if (!lp_load_client_no_reinit(conf)) {
|
---|
89 | DEBUG(10,
|
---|
90 | ("Could not append config file: "
|
---|
91 | "%s\n",
|
---|
92 | conf));
|
---|
93 | }
|
---|
94 | SAFE_FREE(conf);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | load_interfaces(); /* Load the list of interfaces ... */
|
---|
100 |
|
---|
101 | reopen_logs(); /* Get logging working ... */
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Block SIGPIPE (from lib/util_sock.c: write())
|
---|
105 | * It is not needed and should not stop execution
|
---|
106 | */
|
---|
107 | BlockSignals(True, SIGPIPE);
|
---|
108 |
|
---|
109 | /* Create the mutex we'll use to protect initialized_ctx_count */
|
---|
110 | if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
|
---|
111 | initialized_ctx_count_mutex) != 0) {
|
---|
112 | smb_panic("SMBC_module_init: "
|
---|
113 | "failed to create 'initialized_ctx_count' mutex");
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | TALLOC_FREE(frame);
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | static void
|
---|
122 | SMBC_module_terminate(void)
|
---|
123 | {
|
---|
124 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
125 | secrets_shutdown();
|
---|
126 | gfree_all();
|
---|
127 | SMBC_initialized = false;
|
---|
128 | TALLOC_FREE(frame);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Get a new empty handle to fill in with your own info
|
---|
134 | */
|
---|
135 | SMBCCTX *
|
---|
136 | smbc_new_context(void)
|
---|
137 | {
|
---|
138 | SMBCCTX *context;
|
---|
139 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
140 |
|
---|
141 | /* The first call to this function should initialize the module */
|
---|
142 | SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * All newly added context fields should be placed in
|
---|
146 | * SMBC_internal_data, not directly in SMBCCTX.
|
---|
147 | */
|
---|
148 | context = SMB_MALLOC_P(SMBCCTX);
|
---|
149 | if (!context) {
|
---|
150 | TALLOC_FREE(frame);
|
---|
151 | errno = ENOMEM;
|
---|
152 | return NULL;
|
---|
153 | }
|
---|
154 |
|
---|
155 | ZERO_STRUCTP(context);
|
---|
156 |
|
---|
157 | context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
|
---|
158 | if (!context->internal) {
|
---|
159 | TALLOC_FREE(frame);
|
---|
160 | SAFE_FREE(context);
|
---|
161 | errno = ENOMEM;
|
---|
162 | return NULL;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Initialize the context and establish reasonable defaults */
|
---|
166 | ZERO_STRUCTP(context->internal);
|
---|
167 |
|
---|
168 | smbc_setDebug(context, 0);
|
---|
169 | smbc_setTimeout(context, 20000);
|
---|
170 | smbc_setPort(context, 0);
|
---|
171 |
|
---|
172 | smbc_setOptionFullTimeNames(context, False);
|
---|
173 | smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
|
---|
174 | smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
|
---|
175 | smbc_setOptionUseCCache(context, True);
|
---|
176 | smbc_setOptionCaseSensitive(context, False);
|
---|
177 | smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
|
---|
178 | smbc_setOptionUrlEncodeReaddirEntries(context, False);
|
---|
179 | smbc_setOptionOneSharePerServer(context, False);
|
---|
180 | if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL) {
|
---|
181 | smbc_setOptionUseCCache(context, true);
|
---|
182 | }
|
---|
183 |
|
---|
184 | smbc_setFunctionAuthData(context, SMBC_get_auth_data);
|
---|
185 | smbc_setFunctionCheckServer(context, SMBC_check_server);
|
---|
186 | smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
|
---|
187 |
|
---|
188 | smbc_setOptionUserData(context, NULL);
|
---|
189 | smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
|
---|
190 | smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
|
---|
191 | smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
|
---|
192 | smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
|
---|
193 |
|
---|
194 | smbc_setFunctionOpen(context, SMBC_open_ctx);
|
---|
195 | smbc_setFunctionCreat(context, SMBC_creat_ctx);
|
---|
196 | smbc_setFunctionRead(context, SMBC_read_ctx);
|
---|
197 | smbc_setFunctionSplice(context, SMBC_splice_ctx);
|
---|
198 | smbc_setFunctionWrite(context, SMBC_write_ctx);
|
---|
199 | smbc_setFunctionClose(context, SMBC_close_ctx);
|
---|
200 | smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
|
---|
201 | smbc_setFunctionRename(context, SMBC_rename_ctx);
|
---|
202 | smbc_setFunctionLseek(context, SMBC_lseek_ctx);
|
---|
203 | smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
|
---|
204 | smbc_setFunctionStat(context, SMBC_stat_ctx);
|
---|
205 | smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
|
---|
206 | smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
|
---|
207 | smbc_setFunctionFstat(context, SMBC_fstat_ctx);
|
---|
208 | smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
|
---|
209 | smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
|
---|
210 | smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
|
---|
211 | smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
|
---|
212 | smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
|
---|
213 | smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
|
---|
214 | smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
|
---|
215 | smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
|
---|
216 | smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
|
---|
217 | smbc_setFunctionNotify(context, SMBC_notify_ctx);
|
---|
218 | smbc_setFunctionChmod(context, SMBC_chmod_ctx);
|
---|
219 | smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
|
---|
220 | smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
|
---|
221 | smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
|
---|
222 | smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
|
---|
223 | smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
|
---|
224 |
|
---|
225 | smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
|
---|
226 | smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
|
---|
227 | smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
|
---|
228 | smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
|
---|
229 |
|
---|
230 | TALLOC_FREE(frame);
|
---|
231 | return context;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Free a context
|
---|
236 | *
|
---|
237 | * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
|
---|
238 | * and thus you'll be leaking memory if not handled properly.
|
---|
239 | *
|
---|
240 | */
|
---|
241 | int
|
---|
242 | smbc_free_context(SMBCCTX *context,
|
---|
243 | int shutdown_ctx)
|
---|
244 | {
|
---|
245 | TALLOC_CTX *frame;
|
---|
246 | if (!context) {
|
---|
247 | errno = EBADF;
|
---|
248 | return 1;
|
---|
249 | }
|
---|
250 |
|
---|
251 | frame = talloc_stackframe();
|
---|
252 |
|
---|
253 | if (shutdown_ctx) {
|
---|
254 | SMBCFILE * f;
|
---|
255 | DEBUG(1,("Performing aggressive shutdown.\n"));
|
---|
256 |
|
---|
257 | f = context->internal->files;
|
---|
258 | while (f) {
|
---|
259 | smbc_getFunctionClose(context)(context, f);
|
---|
260 | f = f->next;
|
---|
261 | }
|
---|
262 | context->internal->files = NULL;
|
---|
263 |
|
---|
264 | /* First try to remove the servers the nice way. */
|
---|
265 | if (smbc_getFunctionPurgeCachedServers(context)(context)) {
|
---|
266 | SMBCSRV * s;
|
---|
267 | SMBCSRV * next;
|
---|
268 | DEBUG(1, ("Could not purge all servers, "
|
---|
269 | "Nice way shutdown failed.\n"));
|
---|
270 | s = context->internal->servers;
|
---|
271 | while (s) {
|
---|
272 | DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
|
---|
273 | s, s->cli));
|
---|
274 | cli_shutdown(s->cli);
|
---|
275 | smbc_getFunctionRemoveCachedServer(context)(context,
|
---|
276 | s);
|
---|
277 | next = s->next;
|
---|
278 | DLIST_REMOVE(context->internal->servers, s);
|
---|
279 | SAFE_FREE(s);
|
---|
280 | s = next;
|
---|
281 | }
|
---|
282 | context->internal->servers = NULL;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | else {
|
---|
286 | /* This is the polite way */
|
---|
287 | if (smbc_getFunctionPurgeCachedServers(context)(context)) {
|
---|
288 | DEBUG(1, ("Could not purge all servers, "
|
---|
289 | "free_context failed.\n"));
|
---|
290 | errno = EBUSY;
|
---|
291 | TALLOC_FREE(frame);
|
---|
292 | return 1;
|
---|
293 | }
|
---|
294 | if (context->internal->servers) {
|
---|
295 | DEBUG(1, ("Active servers in context, "
|
---|
296 | "free_context failed.\n"));
|
---|
297 | errno = EBUSY;
|
---|
298 | TALLOC_FREE(frame);
|
---|
299 | return 1;
|
---|
300 | }
|
---|
301 | if (context->internal->files) {
|
---|
302 | DEBUG(1, ("Active files in context, "
|
---|
303 | "free_context failed.\n"));
|
---|
304 | errno = EBUSY;
|
---|
305 | TALLOC_FREE(frame);
|
---|
306 | return 1;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* Things we have to clean up */
|
---|
311 | smbc_setWorkgroup(context, NULL);
|
---|
312 | smbc_setNetbiosName(context, NULL);
|
---|
313 | smbc_setUser(context, NULL);
|
---|
314 |
|
---|
315 | DEBUG(3, ("Context %p successfully freed\n", context));
|
---|
316 |
|
---|
317 | /* Free any DFS auth context. */
|
---|
318 | TALLOC_FREE(context->internal->auth_info);
|
---|
319 |
|
---|
320 | SAFE_FREE(context->internal);
|
---|
321 | SAFE_FREE(context);
|
---|
322 |
|
---|
323 | /* Protect access to the count of contexts in use */
|
---|
324 | if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
|
---|
325 | smb_panic("error locking 'initialized_ctx_count'");
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (initialized_ctx_count) {
|
---|
329 | initialized_ctx_count--;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (initialized_ctx_count == 0) {
|
---|
333 | SMBC_module_terminate();
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* Unlock the mutex */
|
---|
337 | if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
|
---|
338 | smb_panic("error unlocking 'initialized_ctx_count'");
|
---|
339 | }
|
---|
340 |
|
---|
341 | TALLOC_FREE(frame);
|
---|
342 | return 0;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Deprecated interface. Do not use. Instead, use the various
|
---|
348 | * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
|
---|
349 | */
|
---|
350 | void
|
---|
351 | smbc_option_set(SMBCCTX *context,
|
---|
352 | char *option_name,
|
---|
353 | ... /* option_value */)
|
---|
354 | {
|
---|
355 | va_list ap;
|
---|
356 | union {
|
---|
357 | int i;
|
---|
358 | bool b;
|
---|
359 | smbc_get_auth_data_with_context_fn auth_fn;
|
---|
360 | void *v;
|
---|
361 | const char *s;
|
---|
362 | } option_value;
|
---|
363 |
|
---|
364 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
365 |
|
---|
366 | va_start(ap, option_name);
|
---|
367 |
|
---|
368 | if (strcmp(option_name, "debug_to_stderr") == 0) {
|
---|
369 | option_value.b = (bool) va_arg(ap, int);
|
---|
370 | smbc_setOptionDebugToStderr(context, option_value.b);
|
---|
371 |
|
---|
372 | } else if (strcmp(option_name, "full_time_names") == 0) {
|
---|
373 | option_value.b = (bool) va_arg(ap, int);
|
---|
374 | smbc_setOptionFullTimeNames(context, option_value.b);
|
---|
375 |
|
---|
376 | } else if (strcmp(option_name, "open_share_mode") == 0) {
|
---|
377 | option_value.i = va_arg(ap, int);
|
---|
378 | smbc_setOptionOpenShareMode(context, option_value.i);
|
---|
379 |
|
---|
380 | } else if (strcmp(option_name, "auth_function") == 0) {
|
---|
381 | option_value.auth_fn =
|
---|
382 | va_arg(ap, smbc_get_auth_data_with_context_fn);
|
---|
383 | smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
|
---|
384 |
|
---|
385 | } else if (strcmp(option_name, "user_data") == 0) {
|
---|
386 | option_value.v = va_arg(ap, void *);
|
---|
387 | smbc_setOptionUserData(context, option_value.v);
|
---|
388 |
|
---|
389 | } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
|
---|
390 | option_value.s = va_arg(ap, const char *);
|
---|
391 | if (strcmp(option_value.s, "none") == 0) {
|
---|
392 | smbc_setOptionSmbEncryptionLevel(context,
|
---|
393 | SMBC_ENCRYPTLEVEL_NONE);
|
---|
394 | } else if (strcmp(option_value.s, "request") == 0) {
|
---|
395 | smbc_setOptionSmbEncryptionLevel(context,
|
---|
396 | SMBC_ENCRYPTLEVEL_REQUEST);
|
---|
397 | } else if (strcmp(option_value.s, "require") == 0) {
|
---|
398 | smbc_setOptionSmbEncryptionLevel(context,
|
---|
399 | SMBC_ENCRYPTLEVEL_REQUIRE);
|
---|
400 | }
|
---|
401 |
|
---|
402 | } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
|
---|
403 | option_value.i = va_arg(ap, int);
|
---|
404 | smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
|
---|
405 |
|
---|
406 | } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
|
---|
407 | option_value.b = (bool) va_arg(ap, int);
|
---|
408 | smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
|
---|
409 |
|
---|
410 | } else if (strcmp(option_name, "one_share_per_server") == 0) {
|
---|
411 | option_value.b = (bool) va_arg(ap, int);
|
---|
412 | smbc_setOptionOneSharePerServer(context, option_value.b);
|
---|
413 |
|
---|
414 | } else if (strcmp(option_name, "use_kerberos") == 0) {
|
---|
415 | option_value.b = (bool) va_arg(ap, int);
|
---|
416 | smbc_setOptionUseKerberos(context, option_value.b);
|
---|
417 |
|
---|
418 | } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
|
---|
419 | option_value.b = (bool) va_arg(ap, int);
|
---|
420 | smbc_setOptionFallbackAfterKerberos(context, option_value.b);
|
---|
421 |
|
---|
422 | } else if (strcmp(option_name, "use_ccache") == 0) {
|
---|
423 | option_value.b = (bool) va_arg(ap, int);
|
---|
424 | smbc_setOptionUseCCache(context, option_value.b);
|
---|
425 |
|
---|
426 | } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
|
---|
427 | option_value.b = (bool) va_arg(ap, int);
|
---|
428 | smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
|
---|
429 | }
|
---|
430 |
|
---|
431 | va_end(ap);
|
---|
432 | TALLOC_FREE(frame);
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Deprecated interface. Do not use. Instead, use the various
|
---|
438 | * smbc_getOption*() functions.
|
---|
439 | */
|
---|
440 | void *
|
---|
441 | smbc_option_get(SMBCCTX *context,
|
---|
442 | char *option_name)
|
---|
443 | {
|
---|
444 | if (strcmp(option_name, "debug_stderr") == 0) {
|
---|
445 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
446 | return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
|
---|
447 | #else
|
---|
448 | return (void *) smbc_getOptionDebugToStderr(context);
|
---|
449 | #endif
|
---|
450 |
|
---|
451 | } else if (strcmp(option_name, "full_time_names") == 0) {
|
---|
452 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
453 | return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
|
---|
454 | #else
|
---|
455 | return (void *) smbc_getOptionFullTimeNames(context);
|
---|
456 | #endif
|
---|
457 |
|
---|
458 | } else if (strcmp(option_name, "open_share_mode") == 0) {
|
---|
459 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
460 | return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
|
---|
461 | #else
|
---|
462 | return (void *) smbc_getOptionOpenShareMode(context);
|
---|
463 | #endif
|
---|
464 |
|
---|
465 | } else if (strcmp(option_name, "auth_function") == 0) {
|
---|
466 | return (void *) smbc_getFunctionAuthDataWithContext(context);
|
---|
467 |
|
---|
468 | } else if (strcmp(option_name, "user_data") == 0) {
|
---|
469 | return smbc_getOptionUserData(context);
|
---|
470 |
|
---|
471 | } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
|
---|
472 | switch(smbc_getOptionSmbEncryptionLevel(context))
|
---|
473 | {
|
---|
474 | case 0:
|
---|
475 | return discard_const_p(void, "none");
|
---|
476 | case 1:
|
---|
477 | return discard_const_p(void, "request");
|
---|
478 | case 2:
|
---|
479 | return discard_const_p(void, "require");
|
---|
480 | }
|
---|
481 |
|
---|
482 | } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
|
---|
483 | SMBCSRV *s;
|
---|
484 | unsigned int num_servers = 0;
|
---|
485 |
|
---|
486 | for (s = context->internal->servers; s; s = s->next) {
|
---|
487 | num_servers++;
|
---|
488 | if (!smb1cli_conn_encryption_on(s->cli->conn)) {
|
---|
489 | return (void *)false;
|
---|
490 | }
|
---|
491 | }
|
---|
492 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
493 | return (void *) (intptr_t) (bool) (num_servers > 0);
|
---|
494 | #else
|
---|
495 | return (void *) (bool) (num_servers > 0);
|
---|
496 | #endif
|
---|
497 |
|
---|
498 | } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
|
---|
499 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
500 | return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
|
---|
501 | #else
|
---|
502 | return (void *) smbc_getOptionBrowseMaxLmbCount(context);
|
---|
503 | #endif
|
---|
504 |
|
---|
505 | } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
|
---|
506 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
507 | return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
|
---|
508 | #else
|
---|
509 | return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
|
---|
510 | #endif
|
---|
511 |
|
---|
512 | } else if (strcmp(option_name, "one_share_per_server") == 0) {
|
---|
513 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
514 | return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
|
---|
515 | #else
|
---|
516 | return (void *) (bool) smbc_getOptionOneSharePerServer(context);
|
---|
517 | #endif
|
---|
518 |
|
---|
519 | } else if (strcmp(option_name, "use_kerberos") == 0) {
|
---|
520 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
521 | return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
|
---|
522 | #else
|
---|
523 | return (void *) (bool) smbc_getOptionUseKerberos(context);
|
---|
524 | #endif
|
---|
525 |
|
---|
526 | } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
|
---|
527 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
528 | return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
|
---|
529 | #else
|
---|
530 | return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
|
---|
531 | #endif
|
---|
532 |
|
---|
533 | } else if (strcmp(option_name, "use_ccache") == 0) {
|
---|
534 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
535 | return (void *) (intptr_t) smbc_getOptionUseCCache(context);
|
---|
536 | #else
|
---|
537 | return (void *) (bool) smbc_getOptionUseCCache(context);
|
---|
538 | #endif
|
---|
539 |
|
---|
540 | } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
|
---|
541 | #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
|
---|
542 | return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
|
---|
543 | #else
|
---|
544 | return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
|
---|
545 | #endif
|
---|
546 | }
|
---|
547 |
|
---|
548 | return NULL;
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * Initialize the library, etc.
|
---|
554 | *
|
---|
555 | * We accept a struct containing handle information.
|
---|
556 | * valid values for info->debug from 0 to 100,
|
---|
557 | * and insist that info->fn must be non-null.
|
---|
558 | */
|
---|
559 | SMBCCTX *
|
---|
560 | smbc_init_context(SMBCCTX *context)
|
---|
561 | {
|
---|
562 | int pid;
|
---|
563 | TALLOC_CTX *frame;
|
---|
564 |
|
---|
565 | if (!context) {
|
---|
566 | errno = EBADF;
|
---|
567 | return NULL;
|
---|
568 | }
|
---|
569 |
|
---|
570 | /* Do not initialise the same client twice */
|
---|
571 | if (context->internal->initialized) {
|
---|
572 | return NULL;
|
---|
573 | }
|
---|
574 |
|
---|
575 | frame = talloc_stackframe();
|
---|
576 |
|
---|
577 | if ((!smbc_getFunctionAuthData(context) &&
|
---|
578 | !smbc_getFunctionAuthDataWithContext(context)) ||
|
---|
579 | smbc_getDebug(context) < 0 ||
|
---|
580 | smbc_getDebug(context) > 100) {
|
---|
581 |
|
---|
582 | TALLOC_FREE(frame);
|
---|
583 | errno = EINVAL;
|
---|
584 | return NULL;
|
---|
585 |
|
---|
586 | }
|
---|
587 |
|
---|
588 | if (!smbc_getUser(context)) {
|
---|
589 | /*
|
---|
590 | * FIXME: Is this the best way to get the user info?
|
---|
591 | */
|
---|
592 | char *user = getenv("USER");
|
---|
593 | /* walk around as "guest" if no username can be found */
|
---|
594 | if (!user) {
|
---|
595 | user = SMB_STRDUP("guest");
|
---|
596 | } else {
|
---|
597 | user = SMB_STRDUP(user);
|
---|
598 | }
|
---|
599 |
|
---|
600 | if (!user) {
|
---|
601 | TALLOC_FREE(frame);
|
---|
602 | errno = ENOMEM;
|
---|
603 | return NULL;
|
---|
604 | }
|
---|
605 |
|
---|
606 | smbc_setUser(context, user);
|
---|
607 | SAFE_FREE(user);
|
---|
608 |
|
---|
609 | if (!smbc_getUser(context)) {
|
---|
610 | TALLOC_FREE(frame);
|
---|
611 | errno = ENOMEM;
|
---|
612 | return NULL;
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | if (!smbc_getNetbiosName(context)) {
|
---|
617 | /*
|
---|
618 | * We try to get our netbios name from the config. If that
|
---|
619 | * fails we fall back on constructing our netbios name from
|
---|
620 | * our hostname etc
|
---|
621 | */
|
---|
622 | char *netbios_name;
|
---|
623 | if (lp_netbios_name()) {
|
---|
624 | netbios_name = SMB_STRDUP(lp_netbios_name());
|
---|
625 | } else {
|
---|
626 | /*
|
---|
627 | * Hmmm, I want to get hostname as well, but I am too
|
---|
628 | * lazy for the moment
|
---|
629 | */
|
---|
630 | pid = getpid();
|
---|
631 | netbios_name = (char *)SMB_MALLOC(17);
|
---|
632 | if (!netbios_name) {
|
---|
633 | TALLOC_FREE(frame);
|
---|
634 | errno = ENOMEM;
|
---|
635 | return NULL;
|
---|
636 | }
|
---|
637 | slprintf(netbios_name, 16,
|
---|
638 | "smbc%s%d", smbc_getUser(context), pid);
|
---|
639 | }
|
---|
640 |
|
---|
641 | if (!netbios_name) {
|
---|
642 | TALLOC_FREE(frame);
|
---|
643 | errno = ENOMEM;
|
---|
644 | return NULL;
|
---|
645 | }
|
---|
646 |
|
---|
647 | smbc_setNetbiosName(context, netbios_name);
|
---|
648 | SAFE_FREE(netbios_name);
|
---|
649 |
|
---|
650 | if (!smbc_getNetbiosName(context)) {
|
---|
651 | TALLOC_FREE(frame);
|
---|
652 | errno = ENOMEM;
|
---|
653 | return NULL;
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
|
---|
658 |
|
---|
659 | if (!smbc_getWorkgroup(context)) {
|
---|
660 | char *workgroup;
|
---|
661 |
|
---|
662 | if (lp_workgroup()) {
|
---|
663 | workgroup = SMB_STRDUP(lp_workgroup());
|
---|
664 | }
|
---|
665 | else {
|
---|
666 | /* TODO: Think about a decent default workgroup */
|
---|
667 | workgroup = SMB_STRDUP("samba");
|
---|
668 | }
|
---|
669 |
|
---|
670 | if (!workgroup) {
|
---|
671 | TALLOC_FREE(frame);
|
---|
672 | errno = ENOMEM;
|
---|
673 | return NULL;
|
---|
674 | }
|
---|
675 |
|
---|
676 | smbc_setWorkgroup(context, workgroup);
|
---|
677 | SAFE_FREE(workgroup);
|
---|
678 |
|
---|
679 | if (!smbc_getWorkgroup(context)) {
|
---|
680 | TALLOC_FREE(frame);
|
---|
681 | errno = ENOMEM;
|
---|
682 | return NULL;
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
|
---|
687 |
|
---|
688 | /* shortest timeout is 1 second */
|
---|
689 | if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
|
---|
690 | smbc_setTimeout(context, 1000);
|
---|
691 |
|
---|
692 | context->internal->initialized = True;
|
---|
693 |
|
---|
694 | /* Protect access to the count of contexts in use */
|
---|
695 | if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
|
---|
696 | smb_panic("error locking 'initialized_ctx_count'");
|
---|
697 | }
|
---|
698 |
|
---|
699 | initialized_ctx_count++;
|
---|
700 |
|
---|
701 | /* Unlock the mutex */
|
---|
702 | if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
|
---|
703 | smb_panic("error unlocking 'initialized_ctx_count'");
|
---|
704 | }
|
---|
705 |
|
---|
706 | TALLOC_FREE(frame);
|
---|
707 | return context;
|
---|
708 | }
|
---|
709 |
|
---|
710 |
|
---|
711 | /* Return the verion of samba, and thus libsmbclient */
|
---|
712 | const char *
|
---|
713 | smbc_version(void)
|
---|
714 | {
|
---|
715 | return samba_version_string();
|
---|
716 | }
|
---|
717 |
|
---|
718 | /*
|
---|
719 | * Set the credentials so DFS will work when following referrals.
|
---|
720 | * This function is broken and must be removed. No SMBCCTX arg...
|
---|
721 | * JRA.
|
---|
722 | */
|
---|
723 |
|
---|
724 | void
|
---|
725 | smbc_set_credentials(const char *workgroup,
|
---|
726 | const char *user,
|
---|
727 | const char *password,
|
---|
728 | smbc_bool use_kerberos,
|
---|
729 | const char *signing_state)
|
---|
730 | {
|
---|
731 | d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
|
---|
732 | }
|
---|
733 |
|
---|
734 | void smbc_set_credentials_with_fallback(SMBCCTX *context,
|
---|
735 | const char *workgroup,
|
---|
736 | const char *user,
|
---|
737 | const char *password)
|
---|
738 | {
|
---|
739 | smbc_bool use_kerberos = false;
|
---|
740 | const char *signing_state = "off";
|
---|
741 | struct user_auth_info *auth_info = NULL;
|
---|
742 | TALLOC_CTX *frame;
|
---|
743 |
|
---|
744 | if (! context) {
|
---|
745 |
|
---|
746 | return;
|
---|
747 | }
|
---|
748 |
|
---|
749 | frame = talloc_stackframe();
|
---|
750 |
|
---|
751 | if (! workgroup || ! *workgroup) {
|
---|
752 | workgroup = smbc_getWorkgroup(context);
|
---|
753 | }
|
---|
754 |
|
---|
755 | if (! user) {
|
---|
756 | user = smbc_getUser(context);
|
---|
757 | }
|
---|
758 |
|
---|
759 | if (! password) {
|
---|
760 | password = "";
|
---|
761 | }
|
---|
762 |
|
---|
763 | auth_info = user_auth_info_init(NULL);
|
---|
764 |
|
---|
765 | if (! auth_info) {
|
---|
766 | DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
|
---|
767 | TALLOC_FREE(frame);
|
---|
768 | return;
|
---|
769 | }
|
---|
770 |
|
---|
771 | if (smbc_getOptionUseKerberos(context)) {
|
---|
772 | use_kerberos = True;
|
---|
773 | }
|
---|
774 |
|
---|
775 | if (lp_client_signing() != SMB_SIGNING_OFF) {
|
---|
776 | signing_state = "if_required";
|
---|
777 | }
|
---|
778 |
|
---|
779 | if (lp_client_signing() == SMB_SIGNING_REQUIRED) {
|
---|
780 | signing_state = "required";
|
---|
781 | }
|
---|
782 |
|
---|
783 | set_cmdline_auth_info_username(auth_info, user);
|
---|
784 | set_cmdline_auth_info_domain(auth_info, workgroup);
|
---|
785 | set_cmdline_auth_info_password(auth_info, password);
|
---|
786 | set_cmdline_auth_info_use_kerberos(auth_info, use_kerberos);
|
---|
787 | set_cmdline_auth_info_signing_state(auth_info, signing_state);
|
---|
788 | set_cmdline_auth_info_fallback_after_kerberos(auth_info,
|
---|
789 | smbc_getOptionFallbackAfterKerberos(context));
|
---|
790 | set_cmdline_auth_info_use_ccache(
|
---|
791 | auth_info, smbc_getOptionUseCCache(context));
|
---|
792 |
|
---|
793 | TALLOC_FREE(context->internal->auth_info);
|
---|
794 |
|
---|
795 | context->internal->auth_info = auth_info;
|
---|
796 | TALLOC_FREE(frame);
|
---|
797 | }
|
---|