• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src/router/samba-3.0.25b/examples/libsmbclient/
1/*
2 * Alternate testbrowse utility provided by Mikhail Kshevetskiy.
3 * This version tests use of multiple contexts.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <malloc.h>
9#include <string.h>
10#include <libsmbclient.h>
11
12int	debuglevel	= 0;
13char	*workgroup	= "NT";
14char	*username	= "guest";
15char	*password	= "";
16
17typedef struct smbitem smbitem;
18typedef int(*qsort_cmp)(const void *, const void *);
19
20struct smbitem{
21    smbitem	*next;
22    int		type;
23    char	name[1];
24};
25
26int smbitem_cmp(smbitem *elem1, smbitem *elem2){
27    return strcmp(elem1->name, elem2->name);
28}
29
30int smbitem_list_count(smbitem *list){
31    int count = 0;
32
33    while(list != NULL){
34	list = list->next;
35	count++;
36    }
37    return count;
38}
39
40void smbitem_list_delete(smbitem *list){
41    smbitem	*elem;
42
43    while(list != NULL){
44	elem = list;
45	list = list->next;
46	free(elem);
47    }
48}
49
50smbitem* smbitem_list_sort(smbitem *list){
51    smbitem	*item, **array;
52    int		count, i;
53
54    if ((count = smbitem_list_count(list)) == 0) return NULL;
55    if ((array = malloc(count * sizeof(smbitem*))) == NULL){
56	smbitem_list_delete(list);
57	return NULL;
58    }
59
60    for(i = 0; i < count; i++){
61	array[i] = list;
62	list = list->next;
63    }
64    qsort(array, count, sizeof(smbitem*), (qsort_cmp)smbitem_cmp);
65
66    for(i = 0; i < count - 1; i++) array[i]->next = array[i + 1];
67    array[count - 1]->next = NULL;
68
69    list = array[0];
70    free(array);
71    return list;
72}
73
74void smbc_auth_fn(
75                const char      *server,
76		const char      *share,
77		char            *wrkgrp, int wrkgrplen,
78		char            *user,   int userlen,
79		char            *passwd, int passwdlen){
80
81    (void) server;
82    (void) share;
83    (void) wrkgrp;
84    (void) wrkgrplen;
85
86    strncpy(wrkgrp, workgroup, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
87    strncpy(user, username, userlen - 1); user[userlen - 1] = 0;
88    strncpy(passwd, password, passwdlen - 1); passwd[passwdlen - 1] = 0;
89}
90
91SMBCCTX* create_smbctx(){
92    SMBCCTX	*ctx;
93
94    if ((ctx = smbc_new_context()) == NULL) return NULL;
95
96    ctx->debug = debuglevel;
97    ctx->callbacks.auth_fn = smbc_auth_fn;
98
99    if (smbc_init_context(ctx) == NULL){
100	smbc_free_context(ctx, 1);
101	return NULL;
102    }
103
104    return ctx;
105}
106
107void delete_smbctx(SMBCCTX* ctx){
108    ctx->callbacks.purge_cached_fn(ctx);
109    smbc_free_context(ctx, 1);
110}
111
112smbitem* get_smbitem_list(SMBCCTX *ctx, char *smb_path){
113    SMBCFILE		*fd;
114    struct smbc_dirent	*dirent;
115    smbitem		*list = NULL, *item;
116
117    if ((fd = ctx->opendir(ctx, smb_path)) == NULL) return NULL;
118    while((dirent = ctx->readdir(ctx, fd)) != NULL){
119	if (strcmp(dirent->name, "") == 0) continue;
120	if (strcmp(dirent->name, ".") == 0) continue;
121	if (strcmp(dirent->name, "..") == 0) continue;
122
123	if ((item = malloc(sizeof(smbitem) + strlen(dirent->name))) == NULL)
124	    continue;
125
126	item->next = list;
127	item->type = dirent->smbc_type;
128	strcpy(item->name, dirent->name);
129	list = item;
130    }
131    ctx->close_fn(ctx, fd);
132    return /* smbitem_list_sort */ (list);
133
134}
135
136void print_smb_path(char *group, char *path){
137    if ((strlen(group) == 0) && (strlen(path) == 0)) printf("/\n");
138    else if (strlen(path) == 0) printf("/%s\n", group);
139    else{
140	if (strlen(group) == 0) group = "(unknown_group)";
141	printf("/%s/%s\n", group, path);
142    }
143}
144
145void recurse(SMBCCTX *ctx, char *smb_group, char *smb_path, int maxlen){
146    int 	len;
147    smbitem	*list, *item;
148    SMBCCTX	*ctx1;
149
150    len = strlen(smb_path);
151
152    list = get_smbitem_list(ctx, smb_path);
153    while(list != NULL){
154	switch(list->type){
155	    case SMBC_WORKGROUP:
156	    case SMBC_SERVER:
157		if (list->type == SMBC_WORKGROUP){
158		    print_smb_path(list->name, "");
159		    smb_group = list->name;
160		}
161		else print_smb_path(smb_group, list->name);
162
163		if (maxlen < 7 + strlen(list->name)) break;
164		strcpy(smb_path + 6, list->name);
165		if ((ctx1 = create_smbctx()) != NULL){
166		    recurse(ctx1, smb_group, smb_path, maxlen);
167		    delete_smbctx(ctx1);
168		}else{
169		    recurse(ctx, smb_group, smb_path, maxlen);
170		    ctx->callbacks.purge_cached_fn(ctx);
171		}
172		break;
173	    case SMBC_FILE_SHARE:
174	    case SMBC_DIR:
175	    case SMBC_FILE:
176		if (maxlen < len + strlen(list->name) + 2) break;
177
178		smb_path[len] = '/';
179		strcpy(smb_path + len + 1, list->name);
180		print_smb_path(smb_group, smb_path + 6);
181		if (list->type != SMBC_FILE){
182		    recurse(ctx, smb_group, smb_path, maxlen);
183		    if (list->type == SMBC_FILE_SHARE)
184			ctx->callbacks.purge_cached_fn(ctx);
185		}
186		break;
187	}
188	item = list;
189	list = list->next;
190	free(item);
191    }
192    smb_path[len] = '\0';
193}
194
195int main(int argc, char *argv[]){
196    int		i;
197    SMBCCTX	*ctx;
198    char	smb_path[32768] = "smb://";
199
200    if ((ctx = create_smbctx()) == NULL){
201	perror("Cant create samba context.");
202	return 1;
203    }
204
205    if (argc == 1) recurse(ctx, "", smb_path, sizeof(smb_path));
206    else for(i = 1; i < argc; i++){
207	strncpy(smb_path + 6, argv[i], sizeof(smb_path) - 7);
208	smb_path[sizeof(smb_path) - 1] = '\0';
209	recurse(ctx, "", smb_path, sizeof(smb_path));
210    }
211
212    delete_smbctx(ctx);
213    return 0;
214}
215