00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <malloc.h>
00009 #include <string.h>
00010 #include <libsmbclient.h>
00011
00012 int debuglevel = 0;
00013 char *workgroup = "NT";
00014 char *username = "guest";
00015 char *password = "";
00016
00017 typedef struct smbitem smbitem;
00018 typedef int(*qsort_cmp)(const void *, const void *);
00019
00020 struct smbitem{
00021 smbitem *next;
00022 int type;
00023 char name[1];
00024 };
00025
00026 int smbitem_cmp(smbitem *elem1, smbitem *elem2){
00027 return strcmp(elem1->name, elem2->name);
00028 }
00029
00030 int smbitem_list_count(smbitem *list){
00031 int count = 0;
00032
00033 while(list != NULL){
00034 list = list->next;
00035 count++;
00036 }
00037 return count;
00038 }
00039
00040 void smbitem_list_delete(smbitem *list){
00041 smbitem *elem;
00042
00043 while(list != NULL){
00044 elem = list;
00045 list = list->next;
00046 free(elem);
00047 }
00048 }
00049
00050 smbitem* smbitem_list_sort(smbitem *list){
00051 smbitem *item, **array;
00052 int count, i;
00053
00054 if ((count = smbitem_list_count(list)) == 0) return NULL;
00055 if ((array = malloc(count * sizeof(smbitem*))) == NULL){
00056 smbitem_list_delete(list);
00057 return NULL;
00058 }
00059
00060 for(i = 0; i < count; i++){
00061 array[i] = list;
00062 list = list->next;
00063 }
00064 qsort(array, count, sizeof(smbitem*), (qsort_cmp)smbitem_cmp);
00065
00066 for(i = 0; i < count - 1; i++) array[i]->next = array[i + 1];
00067 array[count - 1]->next = NULL;
00068
00069 list = array[0];
00070 free(array);
00071 return list;
00072 }
00073
00074 void smbc_auth_fn(
00075 const char *server,
00076 const char *share,
00077 char *wrkgrp, int wrkgrplen,
00078 char *user, int userlen,
00079 char *passwd, int passwdlen){
00080
00081 (void) server;
00082 (void) share;
00083 (void) wrkgrp;
00084 (void) wrkgrplen;
00085
00086 strncpy(wrkgrp, workgroup, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
00087 strncpy(user, username, userlen - 1); user[userlen - 1] = 0;
00088 strncpy(passwd, password, passwdlen - 1); passwd[passwdlen - 1] = 0;
00089 }
00090
00091 SMBCCTX* create_smbctx(){
00092 SMBCCTX *ctx;
00093
00094 if ((ctx = smbc_new_context()) == NULL) return NULL;
00095
00096 ctx->debug = debuglevel;
00097 ctx->callbacks.auth_fn = smbc_auth_fn;
00098
00099 if (smbc_init_context(ctx) == NULL){
00100 smbc_free_context(ctx, 1);
00101 return NULL;
00102 }
00103
00104 return ctx;
00105 }
00106
00107 void delete_smbctx(SMBCCTX* ctx){
00108 ctx->callbacks.purge_cached_fn(ctx);
00109 smbc_free_context(ctx, 1);
00110 }
00111
00112 smbitem* get_smbitem_list(SMBCCTX *ctx, char *smb_path){
00113 SMBCFILE *fd;
00114 struct smbc_dirent *dirent;
00115 smbitem *list = NULL, *item;
00116
00117 if ((fd = ctx->opendir(ctx, smb_path)) == NULL) return NULL;
00118 while((dirent = ctx->readdir(ctx, fd)) != NULL){
00119 if (strcmp(dirent->name, "") == 0) continue;
00120 if (strcmp(dirent->name, ".") == 0) continue;
00121 if (strcmp(dirent->name, "..") == 0) continue;
00122
00123 if ((item = malloc(sizeof(smbitem) + strlen(dirent->name))) == NULL)
00124 continue;
00125
00126 item->next = list;
00127 item->type = dirent->smbc_type;
00128 strcpy(item->name, dirent->name);
00129 list = item;
00130 }
00131 ctx->close_fn(ctx, fd);
00132 return (list);
00133
00134 }
00135
00136 void print_smb_path(char *group, char *path){
00137 if ((strlen(group) == 0) && (strlen(path) == 0)) printf("/\n");
00138 else if (strlen(path) == 0) printf("/%s\n", group);
00139 else{
00140 if (strlen(group) == 0) group = "(unknown_group)";
00141 printf("/%s/%s\n", group, path);
00142 }
00143 }
00144
00145 void recurse(SMBCCTX *ctx, char *smb_group, char *smb_path, int maxlen){
00146 int len;
00147 smbitem *list, *item;
00148 SMBCCTX *ctx1;
00149
00150 len = strlen(smb_path);
00151
00152 list = get_smbitem_list(ctx, smb_path);
00153 while(list != NULL){
00154 switch(list->type){
00155 case SMBC_WORKGROUP:
00156 case SMBC_SERVER:
00157 if (list->type == SMBC_WORKGROUP){
00158 print_smb_path(list->name, "");
00159 smb_group = list->name;
00160 }
00161 else print_smb_path(smb_group, list->name);
00162
00163 if (maxlen < 7 + strlen(list->name)) break;
00164 strcpy(smb_path + 6, list->name);
00165 if ((ctx1 = create_smbctx()) != NULL){
00166 recurse(ctx1, smb_group, smb_path, maxlen);
00167 delete_smbctx(ctx1);
00168 }else{
00169 recurse(ctx, smb_group, smb_path, maxlen);
00170 ctx->callbacks.purge_cached_fn(ctx);
00171 }
00172 break;
00173 case SMBC_FILE_SHARE:
00174 case SMBC_DIR:
00175 case SMBC_FILE:
00176 if (maxlen < len + strlen(list->name) + 2) break;
00177
00178 smb_path[len] = '/';
00179 strcpy(smb_path + len + 1, list->name);
00180 print_smb_path(smb_group, smb_path + 6);
00181 if (list->type != SMBC_FILE){
00182 recurse(ctx, smb_group, smb_path, maxlen);
00183 if (list->type == SMBC_FILE_SHARE)
00184 ctx->callbacks.purge_cached_fn(ctx);
00185 }
00186 break;
00187 }
00188 item = list;
00189 list = list->next;
00190 free(item);
00191 }
00192 smb_path[len] = '\0';
00193 }
00194
00195 int main(int argc, char *argv[]){
00196 int i;
00197 SMBCCTX *ctx;
00198 char smb_path[32768] = "smb://";
00199
00200 if ((ctx = create_smbctx()) == NULL){
00201 perror("Cant create samba context.");
00202 return 1;
00203 }
00204
00205 if (argc == 1) recurse(ctx, "", smb_path, sizeof(smb_path));
00206 else for(i = 1; i < argc; i++){
00207 strncpy(smb_path + 6, argv[i], sizeof(smb_path) - 7);
00208 smb_path[sizeof(smb_path) - 1] = '\0';
00209 recurse(ctx, "", smb_path, sizeof(smb_path));
00210 }
00211
00212 delete_smbctx(ctx);
00213 return 0;
00214 }