source: branches/samba-3.2.x/source/libsmb/libsmb_context.c

Last change on this file was 232, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 branch to 3.2.8

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