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