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