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