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
SMBC_module_init(void * punused)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
SMBC_module_terminate(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 *
smbc_new_context(void)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, false);
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_setFunctionReaddirPlus(context, SMBC_readdirplus_ctx);
212 smbc_setFunctionReaddirPlus2(context, SMBC_readdirplus2_ctx);
213 smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
214 smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
215 smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
216 smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
217 smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
218 smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
219 smbc_setFunctionNotify(context, SMBC_notify_ctx);
220 smbc_setFunctionChmod(context, SMBC_chmod_ctx);
221 smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
222 smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
223 smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
224 smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
225 smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
226
227 smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
228 smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
229 smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
230 smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
231
232 TALLOC_FREE(frame);
233 return context;
234 }
235
236 /*
237 * Free a context
238 *
239 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
240 * and thus you'll be leaking memory if not handled properly.
241 *
242 */
243 int
smbc_free_context(SMBCCTX * context,int shutdown_ctx)244 smbc_free_context(SMBCCTX *context,
245 int shutdown_ctx)
246 {
247 TALLOC_CTX *frame;
248 if (!context) {
249 errno = EBADF;
250 return 1;
251 }
252
253 frame = talloc_stackframe();
254
255 if (shutdown_ctx) {
256 SMBCFILE * f;
257 DEBUG(1,("Performing aggressive shutdown.\n"));
258
259 f = context->internal->files;
260 while (f) {
261 SMBCFILE *next = f->next;
262 smbc_getFunctionClose(context)(context, f);
263 f = next;
264 }
265 context->internal->files = NULL;
266
267 /* First try to remove the servers the nice way. */
268 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
269 SMBCSRV * s;
270 SMBCSRV * next;
271 DEBUG(1, ("Could not purge all servers, "
272 "Nice way shutdown failed.\n"));
273 s = context->internal->servers;
274 while (s) {
275 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
276 s, s->cli));
277 cli_shutdown(s->cli);
278 smbc_getFunctionRemoveCachedServer(context)(context,
279 s);
280 next = s->next;
281 DLIST_REMOVE(context->internal->servers, s);
282 SAFE_FREE(s);
283 s = next;
284 }
285 context->internal->servers = NULL;
286 }
287 }
288 else {
289 /* This is the polite way */
290 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
291 DEBUG(1, ("Could not purge all servers, "
292 "free_context failed.\n"));
293 errno = EBUSY;
294 TALLOC_FREE(frame);
295 return 1;
296 }
297 if (context->internal->servers) {
298 DEBUG(1, ("Active servers in context, "
299 "free_context failed.\n"));
300 errno = EBUSY;
301 TALLOC_FREE(frame);
302 return 1;
303 }
304 if (context->internal->files) {
305 DEBUG(1, ("Active files in context, "
306 "free_context failed.\n"));
307 errno = EBUSY;
308 TALLOC_FREE(frame);
309 return 1;
310 }
311 }
312
313 /* Things we have to clean up */
314 smbc_setWorkgroup(context, NULL);
315 smbc_setNetbiosName(context, NULL);
316 smbc_setUser(context, NULL);
317
318 DEBUG(3, ("Context %p successfully freed\n", context));
319
320 /* Free any DFS auth context. */
321 TALLOC_FREE(context->internal->auth_info);
322
323 SAFE_FREE(context->internal);
324 SAFE_FREE(context);
325
326 /* Protect access to the count of contexts in use */
327 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
328 smb_panic("error locking 'initialized_ctx_count'");
329 }
330
331 if (initialized_ctx_count) {
332 initialized_ctx_count--;
333 }
334
335 if (initialized_ctx_count == 0) {
336 SMBC_module_terminate();
337 }
338
339 /* Unlock the mutex */
340 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
341 smb_panic("error unlocking 'initialized_ctx_count'");
342 }
343
344 TALLOC_FREE(frame);
345 return 0;
346 }
347
348
349 /**
350 * Deprecated interface. Do not use. Instead, use the various
351 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
352 */
353 void
smbc_option_set(SMBCCTX * context,char * option_name,...)354 smbc_option_set(SMBCCTX *context,
355 char *option_name,
356 ... /* option_value */)
357 {
358 va_list ap;
359 union {
360 int i;
361 bool b;
362 smbc_get_auth_data_with_context_fn auth_fn;
363 void *v;
364 const char *s;
365 } option_value;
366
367 TALLOC_CTX *frame = talloc_stackframe();
368
369 va_start(ap, option_name);
370
371 if (strcmp(option_name, "debug_to_stderr") == 0) {
372 option_value.b = (bool) va_arg(ap, int);
373 smbc_setOptionDebugToStderr(context, option_value.b);
374
375 } else if (strcmp(option_name, "full_time_names") == 0) {
376 option_value.b = (bool) va_arg(ap, int);
377 smbc_setOptionFullTimeNames(context, option_value.b);
378
379 } else if (strcmp(option_name, "open_share_mode") == 0) {
380 option_value.i = va_arg(ap, int);
381 smbc_setOptionOpenShareMode(context, option_value.i);
382
383 } else if (strcmp(option_name, "auth_function") == 0) {
384 option_value.auth_fn =
385 va_arg(ap, smbc_get_auth_data_with_context_fn);
386 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
387
388 } else if (strcmp(option_name, "user_data") == 0) {
389 option_value.v = va_arg(ap, void *);
390 smbc_setOptionUserData(context, option_value.v);
391
392 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
393 option_value.s = va_arg(ap, const char *);
394 if (strcmp(option_value.s, "none") == 0) {
395 smbc_setOptionSmbEncryptionLevel(context,
396 SMBC_ENCRYPTLEVEL_NONE);
397 } else if (strcmp(option_value.s, "request") == 0) {
398 smbc_setOptionSmbEncryptionLevel(context,
399 SMBC_ENCRYPTLEVEL_REQUEST);
400 } else if (strcmp(option_value.s, "require") == 0) {
401 smbc_setOptionSmbEncryptionLevel(context,
402 SMBC_ENCRYPTLEVEL_REQUIRE);
403 }
404
405 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
406 option_value.i = va_arg(ap, int);
407 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
408
409 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
410 option_value.b = (bool) va_arg(ap, int);
411 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
412
413 } else if (strcmp(option_name, "one_share_per_server") == 0) {
414 option_value.b = (bool) va_arg(ap, int);
415 smbc_setOptionOneSharePerServer(context, option_value.b);
416
417 } else if (strcmp(option_name, "use_kerberos") == 0) {
418 option_value.b = (bool) va_arg(ap, int);
419 smbc_setOptionUseKerberos(context, option_value.b);
420
421 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
422 option_value.b = (bool) va_arg(ap, int);
423 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
424
425 } else if (strcmp(option_name, "use_ccache") == 0) {
426 option_value.b = (bool) va_arg(ap, int);
427 smbc_setOptionUseCCache(context, option_value.b);
428
429 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
430 option_value.b = (bool) va_arg(ap, int);
431 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
432 }
433
434 va_end(ap);
435 TALLOC_FREE(frame);
436 }
437
438
439 /*
440 * Deprecated interface. Do not use. Instead, use the various
441 * smbc_getOption*() functions.
442 */
443 void *
smbc_option_get(SMBCCTX * context,char * option_name)444 smbc_option_get(SMBCCTX *context,
445 char *option_name)
446 {
447 if (strcmp(option_name, "debug_stderr") == 0) {
448 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
449 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
450 #else
451 return (void *) smbc_getOptionDebugToStderr(context);
452 #endif
453
454 } else if (strcmp(option_name, "full_time_names") == 0) {
455 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
456 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
457 #else
458 return (void *) smbc_getOptionFullTimeNames(context);
459 #endif
460
461 } else if (strcmp(option_name, "open_share_mode") == 0) {
462 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
463 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
464 #else
465 return (void *) smbc_getOptionOpenShareMode(context);
466 #endif
467
468 } else if (strcmp(option_name, "auth_function") == 0) {
469 return (void *) smbc_getFunctionAuthDataWithContext(context);
470
471 } else if (strcmp(option_name, "user_data") == 0) {
472 return smbc_getOptionUserData(context);
473
474 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
475 switch(smbc_getOptionSmbEncryptionLevel(context))
476 {
477 case 0:
478 return discard_const_p(void, "none");
479 case 1:
480 return discard_const_p(void, "request");
481 case 2:
482 return discard_const_p(void, "require");
483 }
484
485 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
486 SMBCSRV *s;
487 unsigned int num_servers = 0;
488
489 for (s = context->internal->servers; s; s = s->next) {
490 num_servers++;
491 if (!cli_state_is_encryption_on(s->cli)) {
492 return (void *)false;
493 }
494 }
495 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
496 return (void *) (intptr_t) (bool) (num_servers > 0);
497 #else
498 return (void *) (bool) (num_servers > 0);
499 #endif
500
501 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
502 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
503 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
504 #else
505 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
506 #endif
507
508 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
509 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
510 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
511 #else
512 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
513 #endif
514
515 } else if (strcmp(option_name, "one_share_per_server") == 0) {
516 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
517 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
518 #else
519 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
520 #endif
521
522 } else if (strcmp(option_name, "use_kerberos") == 0) {
523 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
524 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
525 #else
526 return (void *) (bool) smbc_getOptionUseKerberos(context);
527 #endif
528
529 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
530 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
531 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
532 #else
533 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
534 #endif
535
536 } else if (strcmp(option_name, "use_ccache") == 0) {
537 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
538 return (void *) (intptr_t) smbc_getOptionUseCCache(context);
539 #else
540 return (void *) (bool) smbc_getOptionUseCCache(context);
541 #endif
542
543 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
544 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
545 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
546 #else
547 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
548 #endif
549 }
550
551 return NULL;
552 }
553
554
555 /*
556 * Initialize the library, etc.
557 *
558 * We accept a struct containing handle information.
559 * valid values for info->debug from 0 to 100,
560 * and insist that info->fn must be non-null.
561 */
562 SMBCCTX *
smbc_init_context(SMBCCTX * context)563 smbc_init_context(SMBCCTX *context)
564 {
565 int pid;
566 TALLOC_CTX *frame;
567
568 if (!context) {
569 errno = EBADF;
570 return NULL;
571 }
572
573 /* Do not initialise the same client twice */
574 if (context->internal->initialized) {
575 return NULL;
576 }
577
578 frame = talloc_stackframe();
579
580 if ((!smbc_getFunctionAuthData(context) &&
581 !smbc_getFunctionAuthDataWithContext(context)) ||
582 smbc_getDebug(context) < 0 ||
583 smbc_getDebug(context) > 100) {
584
585 TALLOC_FREE(frame);
586 errno = EINVAL;
587 return NULL;
588
589 }
590
591 if (!smbc_getUser(context)) {
592 /*
593 * FIXME: Is this the best way to get the user info?
594 */
595 char *user = getenv("USER");
596 /* walk around as "guest" if no username can be found */
597 if (!user) {
598 user = SMB_STRDUP("guest");
599 } else {
600 user = SMB_STRDUP(user);
601 }
602
603 if (!user) {
604 TALLOC_FREE(frame);
605 errno = ENOMEM;
606 return NULL;
607 }
608
609 smbc_setUser(context, user);
610 SAFE_FREE(user);
611
612 if (!smbc_getUser(context)) {
613 TALLOC_FREE(frame);
614 errno = ENOMEM;
615 return NULL;
616 }
617 }
618
619 if (!smbc_getNetbiosName(context)) {
620 /*
621 * We try to get our netbios name from the config. If that
622 * fails we fall back on constructing our netbios name from
623 * our hostname etc
624 */
625 char *netbios_name;
626 if (lp_netbios_name()) {
627 netbios_name = SMB_STRDUP(lp_netbios_name());
628 } else {
629 /*
630 * Hmmm, I want to get hostname as well, but I am too
631 * lazy for the moment
632 */
633 pid = getpid();
634 netbios_name = (char *)SMB_MALLOC(17);
635 if (!netbios_name) {
636 TALLOC_FREE(frame);
637 errno = ENOMEM;
638 return NULL;
639 }
640 slprintf(netbios_name, 16,
641 "smbc%s%d", smbc_getUser(context), pid);
642 }
643
644 if (!netbios_name) {
645 TALLOC_FREE(frame);
646 errno = ENOMEM;
647 return NULL;
648 }
649
650 smbc_setNetbiosName(context, netbios_name);
651 SAFE_FREE(netbios_name);
652
653 if (!smbc_getNetbiosName(context)) {
654 TALLOC_FREE(frame);
655 errno = ENOMEM;
656 return NULL;
657 }
658 }
659
660 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
661
662 if (!smbc_getWorkgroup(context)) {
663 const char *workgroup;
664
665 if (lp_workgroup()) {
666 workgroup = lp_workgroup();
667 } else {
668 /* TODO: Think about a decent default workgroup */
669 workgroup = "samba";
670 }
671
672 smbc_setWorkgroup(context, workgroup);
673
674 if (!smbc_getWorkgroup(context)) {
675 TALLOC_FREE(frame);
676 errno = ENOMEM;
677 return NULL;
678 }
679 }
680
681 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
682
683 /* shortest timeout is 1 second */
684 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
685 smbc_setTimeout(context, 1000);
686
687 context->internal->initialized = True;
688
689 /* Protect access to the count of contexts in use */
690 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
691 smb_panic("error locking 'initialized_ctx_count'");
692 }
693
694 initialized_ctx_count++;
695
696 /* Unlock the mutex */
697 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
698 smb_panic("error unlocking 'initialized_ctx_count'");
699 }
700
701 TALLOC_FREE(frame);
702 return context;
703 }
704
705
706 /* Return the verion of samba, and thus libsmbclient */
707 const char *
smbc_version(void)708 smbc_version(void)
709 {
710 return samba_version_string();
711 }
712
713 /*
714 * Set the credentials so DFS will work when following referrals.
715 * This function is broken and must be removed. No SMBCCTX arg...
716 * JRA.
717 */
718
719 void
smbc_set_credentials(const char * workgroup,const char * user,const char * password,smbc_bool use_kerberos,const char * signing_state)720 smbc_set_credentials(const char *workgroup,
721 const char *user,
722 const char *password,
723 smbc_bool use_kerberos,
724 const char *signing_state)
725 {
726 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
727 }
728
smbc_set_credentials_with_fallback(SMBCCTX * context,const char * workgroup,const char * user,const char * password)729 void smbc_set_credentials_with_fallback(SMBCCTX *context,
730 const char *workgroup,
731 const char *user,
732 const char *password)
733 {
734 smbc_bool use_kerberos = false;
735 const char *signing_state = "off";
736 struct user_auth_info *auth_info = NULL;
737 TALLOC_CTX *frame;
738
739 if (! context) {
740
741 return;
742 }
743
744 frame = talloc_stackframe();
745
746 if (! workgroup || ! *workgroup) {
747 workgroup = smbc_getWorkgroup(context);
748 }
749
750 if (! user) {
751 user = smbc_getUser(context);
752 }
753
754 if (! password) {
755 password = "";
756 }
757
758 auth_info = user_auth_info_init(NULL);
759
760 if (! auth_info) {
761 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
762 TALLOC_FREE(frame);
763 return;
764 }
765
766 if (smbc_getOptionUseKerberos(context)) {
767 use_kerberos = True;
768 }
769
770 if (lp_client_signing() != SMB_SIGNING_OFF) {
771 signing_state = "if_required";
772 }
773
774 if (lp_client_signing() == SMB_SIGNING_REQUIRED) {
775 signing_state = "required";
776 }
777
778 set_cmdline_auth_info_username(auth_info, user);
779 set_cmdline_auth_info_domain(auth_info, workgroup);
780 set_cmdline_auth_info_password(auth_info, password);
781 set_cmdline_auth_info_use_kerberos(auth_info, use_kerberos);
782 set_cmdline_auth_info_signing_state(auth_info, signing_state);
783 set_cmdline_auth_info_fallback_after_kerberos(auth_info,
784 smbc_getOptionFallbackAfterKerberos(context));
785 set_cmdline_auth_info_use_ccache(
786 auth_info, smbc_getOptionUseCCache(context));
787
788 TALLOC_FREE(context->internal->auth_info);
789
790 context->internal->auth_info = auth_info;
791 TALLOC_FREE(frame);
792 }
793