1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-25 23:21:54 +03:00
samba-mirror/examples/libsmbclient/testbrowse2.c

167 lines
4.2 KiB
C
Raw Normal View History

/*
* Alternate testbrowse utility provided by Mikhail Kshevetskiy.
* This version tests use of multiple contexts.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libsmbclient.h>
int debuglevel = 0;
const char *workgroup = "NT";
const char *username = "guest";
const char *password = "";
typedef struct smbitem smbitem;
typedef int(*qsort_cmp)(const void *, const void *);
struct smbitem{
smbitem *next;
int type;
char name[1];
};
static void smbc_auth_fn(
const char *server,
const char *share,
char *wrkgrp, int wrkgrplen,
char *user, int userlen,
char *passwd, int passwdlen){
(void) server;
(void) share;
(void) wrkgrp;
(void) wrkgrplen;
strncpy(wrkgrp, workgroup, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
strncpy(user, username, userlen - 1); user[userlen - 1] = 0;
strncpy(passwd, password, passwdlen - 1); passwd[passwdlen - 1] = 0;
}
static SMBCCTX* create_smbctx(void){
SMBCCTX *ctx;
if ((ctx = smbc_new_context()) == NULL) return NULL;
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
smbc_setDebug(ctx, debuglevel);
smbc_setFunctionAuthData(ctx, smbc_auth_fn);
if (smbc_init_context(ctx) == NULL){
smbc_free_context(ctx, 1);
return NULL;
}
return ctx;
}
static void delete_smbctx(SMBCCTX* ctx){
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
smbc_getFunctionPurgeCachedServers(ctx)(ctx);
smbc_free_context(ctx, 1);
}
static smbitem* get_smbitem_list(SMBCCTX *ctx, char *smb_path){
SMBCFILE *fd;
struct smbc_dirent *dirent;
smbitem *list = NULL, *item;
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
if ((fd = smbc_getFunctionOpendir(ctx)(ctx, smb_path)) == NULL)
return NULL;
while((dirent = smbc_getFunctionReaddir(ctx)(ctx, fd)) != NULL){
if (strcmp(dirent->name, "") == 0) continue;
if (strcmp(dirent->name, ".") == 0) continue;
if (strcmp(dirent->name, "..") == 0) continue;
if ((item = malloc(sizeof(smbitem) + strlen(dirent->name))) == NULL)
continue;
item->next = list;
item->type = dirent->smbc_type;
strcpy(item->name, dirent->name);
list = item;
}
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
smbc_getFunctionClose(ctx)(ctx, fd);
return /* smbitem_list_sort */ (list);
}
static void print_smb_path(const char *group, const char *path){
if ((strlen(group) == 0) && (strlen(path) == 0)) printf("/\n");
else if (strlen(path) == 0) printf("/%s\n", group);
else{
if (strlen(group) == 0) group = "(unknown_group)";
printf("/%s/%s\n", group, path);
}
}
static void recurse(SMBCCTX *ctx, const char *smb_group, char *smb_path, int maxlen){
int len;
smbitem *list, *item;
SMBCCTX *ctx1;
len = strlen(smb_path);
list = get_smbitem_list(ctx, smb_path);
while(list != NULL){
switch(list->type){
case SMBC_WORKGROUP:
case SMBC_SERVER:
if (list->type == SMBC_WORKGROUP){
print_smb_path(list->name, "");
smb_group = list->name;
}
else print_smb_path(smb_group, list->name);
if (maxlen < 7 + strlen(list->name)) break;
strcpy(smb_path + 6, list->name);
if ((ctx1 = create_smbctx()) != NULL){
recurse(ctx1, smb_group, smb_path, maxlen);
delete_smbctx(ctx1);
}else{
recurse(ctx, smb_group, smb_path, maxlen);
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
smbc_getFunctionPurgeCachedServers(ctx)(ctx);
}
break;
case SMBC_FILE_SHARE:
case SMBC_DIR:
case SMBC_FILE:
if (maxlen < len + strlen(list->name) + 2) break;
smb_path[len] = '/';
strcpy(smb_path + len + 1, list->name);
print_smb_path(smb_group, smb_path + 6);
if (list->type != SMBC_FILE){
recurse(ctx, smb_group, smb_path, maxlen);
if (list->type == SMBC_FILE_SHARE)
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
smbc_getFunctionPurgeCachedServers(ctx)(ctx);
}
break;
}
item = list;
list = list->next;
free(item);
}
smb_path[len] = '\0';
}
int main(int argc, char *argv[]){
int i;
SMBCCTX *ctx;
char smb_path[32768] = "smb://";
if ((ctx = create_smbctx()) == NULL){
perror("Cant create samba context.");
return 1;
}
if (argc == 1) recurse(ctx, "", smb_path, sizeof(smb_path));
else for(i = 1; i < argc; i++){
strncpy(smb_path + 6, argv[i], sizeof(smb_path) - 7);
smb_path[sizeof(smb_path) - 1] = '\0';
recurse(ctx, "", smb_path, sizeof(smb_path));
}
delete_smbctx(ctx);
return 0;
}