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