Пример #1
0
void search_smb(char *url)
{
        struct smbc_dirent* dirptr;
        int dh = 0;
        dh = smbc_opendir(url);
        while((dirptr = smbc_readdir(dh)) != NULL){
                if(!strcmp(dirptr->name, ".") || !strcmp(dirptr->name, ".."))
                        continue;

                char buf[MAX_BUFF_SIZE] = {0};
                sprintf(buf, "%s%s", url, dirptr->name);
                struct stat info;
                smbc_stat(buf, &info);

                if(dirptr->smbc_type == SMBC_FILE) {
                        printf("FILE-------%s%s\n", url, dirptr->name);
                        printf("-----------mtime:%lu\n", info.st_mtime);
                        printf("-----------size :%lld\n", info.st_size);
                        printf("-----------mode :%d S_IDDIR :%d\n", info.st_mode, S_ISDIR(info.st_mode));
                }
                else if(dirptr->smbc_type == SMBC_DIR) {
                        printf("FOLDER-----%s%s\n", url, dirptr->name);
                        printf("-----------mode :%d S_IDDIR :%d\n", info.st_mode, S_ISDIR(info.st_mode));
                        char new_url[255] = {0};
                        sprintf(new_url, "%s%s/", url, dirptr->name);
                        search_smb(new_url);
                }
        }
        smbc_closedir(dh);
}
Пример #2
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	char url[MAX_BUFF_SIZE];

	bzero(g_workgroup,MAX_BUFF_SIZE);
	bzero(url,MAX_BUFF_SIZE);

	if ( argc == 5 )
	{

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));
	
		smbc_init(auth_fn, 0);
		smbc_unlink(url);
		fd = smbc_open(url,O_RDWR|O_CREAT,0666);
		smbc_close(fd);

		smbc_opendir(url);

		err = errno;


	}

	return err;

}
Пример #3
0
int main(int argc, char** argv)
{
	int err = -1;
	int dh = 0; 
	char url[MAX_BUFF_SIZE];

	bzero(g_workgroup,MAX_BUFF_SIZE);
	bzero(url,MAX_BUFF_SIZE);

	if ( argc == 5 )
	{

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));

		smbc_init(auth_fn, 0);
		dh = smbc_opendir(url);
		/* printf("directory handle: %i\n",dh); */
		smbc_closedir(dh);

		err = errno;


	}

	return err;

}
Пример #4
0
static int smbcfs_opendir(const char *path, struct fuse_file_info *fi)
{

  const std::string smbcfs_path = smbcfs_root_path + path;

#ifdef MUTEX_LOCK
  pthread_mutex_lock(&mutex_lock);  
#endif

  const int ret_open = smbc_opendir(smbcfs_path.c_str());

#ifdef MUTEX_LOCK
  pthread_mutex_unlock(&mutex_lock);  
#endif

#ifdef SMBCFS_DEBUG
  fprintf(stderr, "[smbc_opendir] %s => %d\n",
	  smbcfs_path.c_str(), ret_open);
#endif

  if(ret_open<0) {
    return ret_open;
  }
  fi->fh = ret_open;

  return 0;
}
Пример #5
0
    SmbDir(const QUrl &url)
    {
        Q_UNUSED(url);
        m_error = SmbAuth::Unsupported;
#if HAVE_SAMBA
        m_url = url;
        m_dh = smbc_opendir(url.toString(QUrl::FullyEncoded).toLatin1());
        if (m_dh < 0) {
            switch (errno) {
            case EACCES:
                m_error = SmbAuth::NoPermission;
                break;
            case EINVAL:
                m_error = SmbAuth::InvalidUrl;
                break;
            case ENOENT:
                m_error = SmbAuth::NotExistingPath;
                break;
            case ENOMEM:
                m_error = SmbAuth::OutOfMemory;
                break;
            case EPERM:
            case ENODEV:
                m_error = SmbAuth::NotExistingShare;
                break;
            default: break;
            }
            return;
        }
        smbc_dirent *child = nullptr;
        while ((child = smbc_readdir(m_dh))) {
            qDebug() << QByteArray(child->name, child->namelen);
        }
#endif
    }
Пример #6
0
int main(int argc, char** argv)
{
	int err = -1;
	char url[MAX_BUFF_SIZE];

	bzero(g_workgroup,MAX_BUFF_SIZE);
	bzero(url,MAX_BUFF_SIZE);

	if ( argc == 5 )
	{

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));

		smbc_init(auth_fn, 0);
		smbc_opendir(url);

		err = errno;

	}

	return err;

}
Пример #7
0
void* GetDirectory(VFSURL* url, VFSDirEntry** items,
                   int* num_items, VFSCallbacks* callbacks)
{
  CSMB2::Get().AddActiveConnection();

  PLATFORM::CLockObject lock(CSMB2::Get());
  CSMB2::Get().Init();
  lock.Unlock();

  XBMC->AuthenticateURL(url);

  std::string strFileName = CSMB2::Get().URLEncode(url->domain, url->hostname,
                                                   url->filename,
                                                   url->username, url->password);
  // remove the / or \ at the end. the samba library does not strip them off
  // don't do this for smb:// !!
  std::string s = strFileName;
  int len = s.length();
  if (len > 1 && s.at(len - 2) != '/' &&
      (s.at(len - 1) == '/' || s.at(len - 1) == '\\'))
  {
    s.erase(len - 1, 1);
  }

  XBMC->Log(ADDON::LOG_DEBUG, "%s - Using authentication url %s", __FUNCTION__, url->redacted);
  lock.Lock();
  int fd = smbc_opendir(s.c_str());
  lock.Unlock();

  while (fd < 0) /* only to avoid goto in following code */
  {
    char cError[1024];
    if (errno == EACCES)
    {
      callbacks->RequireAuthentication(callbacks->ctx, url->url);
      break;
    }
    if (errno == ENODEV || errno == ENOENT)
    {
      char* str770 = XBMC->GetLocalizedString(770);
      sprintf(cError, str770, errno);
      XBMC->FreeString(str770);
    }
    else
      strcpy(cError,strerror(errno));

    char* str257 = XBMC->GetLocalizedString(257);
    callbacks->SetErrorDialog(callbacks->ctx, str257, cError, NULL, NULL);
    XBMC->FreeString(str257);
    break;
  }
  if (fd < 0)
  {
    XBMC->Log(ADDON::LOG_ERROR, "SMBDirectory->GetDirectory: Unable to open directory : '%s'\nunix_err:'%x' error : '%s'", url->redacted, errno, strerror(errno));
    return NULL;
  }

  return NULL;
}
Пример #8
0
/*
 * FUNCTION	:	static BOOL access_dir(char* name, char* user, char *passwd)
 * DESCRIPTION	:	check user can access directory or not
 * INPUT	:	char* name - server name
 * 			char* user - username
 * 			char *passwd - password
 * 			int id -
 * OUTPUT	:	N/A
 * RETURN	:	TRUE - can access
 * 			FALSE - can't access 
 */
void *access_dir(void *value)
{
	int err, dh1;
	char url[MAX_NAME_LEN] = {0};
	struct smbserverinfo_t *psmbserverinfo = NULL;

	if(value == NULL)
		return NULL;

	psmbserverinfo = (struct smbserverinfo_t *)value;

	printf("%s[%d], name = [%s], ip = [%s], user = [%s], passwd = [%s], cmd_id = [%d]\n", __FILE__, __LINE__, psmbserverinfo->name, psmbserverinfo->ip, psmbserverinfo->user, psmbserverinfo->passwd, psmbserverinfo->cmd_id);

	memset(smb_username, 0, sizeof(smb_username));
	memset(smb_passwd, 0, sizeof(smb_passwd));
	if(psmbserverinfo->user != NULL) {
		strncpy(smb_username, psmbserverinfo->user, strlen(psmbserverinfo->user));
		smb_username[strlen(psmbserverinfo->user)] = '\0';
	}
	if(psmbserverinfo->passwd != NULL) {
		strncpy(smb_passwd, psmbserverinfo->passwd, strlen(psmbserverinfo->passwd));
		smb_passwd[strlen(psmbserverinfo->passwd)] = '\0';
	}
	printf("%s[%d], smb_username = [%s], smb_passwd = [%s]\n", __FILE__, __LINE__, smb_username, smb_passwd);

	snprintf(url, sizeof(url), "smb://%s:%s@%s", smb_username, smb_passwd, psmbserverinfo->name);
	err = smbc_init(get_static_auth_data_fn,  0); /* Initialize things */
	if (err < 0) {
		printf("%s[%d], Initializing the smbclient library ...: %s\n", __FILE__, __LINE__, strerror(errno));
		if(errno == EACCES) {
			printf("\033[1;33;41m%s[%d], errno = EACCES (%s)\033[0m\n", __FILE__, __LINE__, strerror(errno));
			SMBTREE_send_event_to_OSD(SMB_CMD_ACCESS_RESPONSE, SMB_ACCESS_LOGIN_FAILED, psmbserverinfo->cmd_id, NULL);
		} else {
			printf("\033[1;33;41m%s[%d], SMB_ACCESS_FAILED\033[0m\n", __FILE__, __LINE__);
			SMBTREE_send_event_to_OSD(SMB_CMD_ACCESS_RESPONSE, SMB_ACCESS_FAILED, psmbserverinfo->cmd_id, NULL);
		}
		SMBTREE_free_smbserverinfo(psmbserverinfo);
		return NULL;
	}
	if ((dh1 = smbc_opendir(url))<1) {
		printf("%s[%d], Could not open directory: %s: %s\n", __FILE__, __LINE__, url, strerror(errno));
		if(errno == EACCES) {
			printf("\033[1;33;41m%s[%d], errno = EACCES (%s)\033[0m\n", __FILE__, __LINE__, strerror(errno));
			SMBTREE_send_event_to_OSD(SMB_CMD_ACCESS_RESPONSE, SMB_ACCESS_LOGIN_FAILED, psmbserverinfo->cmd_id, NULL);
		} else {
			printf("\033[1;33;41m%s[%d], SMB_ACCESS_FAILED\033[0m\n", __FILE__, __LINE__);
			SMBTREE_send_event_to_OSD(SMB_CMD_ACCESS_RESPONSE, SMB_ACCESS_FAILED, psmbserverinfo->cmd_id, NULL);
		}
		SMBTREE_free_smbserverinfo(psmbserverinfo);
		return NULL;
	}

	smbc_closedir(dh1);

	SMBTREE_send_event_to_OSD(SMB_CMD_ACCESS_RESPONSE, SMB_ACCESS_OK, psmbserverinfo->cmd_id, NULL);
	SMBTREE_free_smbserverinfo(psmbserverinfo);
	return NULL;
}
Пример #9
0
int main(int argc, char **argv)
{
	if (argc <= 1)
	{
		usage();
	}

	smbc_init(samba_auth_data_fn, 0);

	if (argc==3 && argv[1][0]=='-' && argv[1][1]=='r')
	{
		struct in_addr ip;
		if (samba_resolve_name(argv[2], &ip, 0x20))
		{
			puts(START_OUTPUT);
			puts(inet_ntoa(ip));
			return 0;
		} else
			return E_NAMENOTFOUND;
	}

	int i;
	for (i = 1; i <= argc; i++)
	{
		if (argv[i][0]=='-' && argv[i][1]=='u')
		{
			i++;
			g_user = argv[i];
		} else
		if (argv[i][0]=='-' && argv[i][1]=='p')
		{
			i++;
			g_pass = argv[i];
		} else
		{
			g_path = argv[i];
			break;
		}
	}

	int dh = smbc_opendir(g_path);
	if (dh < 0)
		return errno == EACCES ? E_ACCESS : E_OPENDIRFAILED;

	puts(START_OUTPUT);
	struct smbc_dirent *pdirent;
	while ((pdirent = smbc_readdir(dh)) != NULL)
	{
		if (pdirent->name[strlen(pdirent->name)-1] != '$')
			puts(pdirent->name);
	}
	smbc_closedir(dh);

	return 0;
}
Пример #10
0
int smbc_wrapper_opendir(connection* con, const char *durl)
{
	if(con->mode== SMB_BASIC){		
		return smbc_opendir(durl);
	}
	else if(con->mode == SMB_NTLM){
		return smbc_cli_opendir(con->smb_info->cli, durl);
	}
	
	return -1;
}
Пример #11
0
int smbc_wrapper_opensharedir(connection* con, const char *durl)
{
	if(con->mode== SMB_BASIC){
		return smbc_opendir(durl);
	}
	else if(con->mode == SMB_NTLM){
		char turi[512];
		sprintf(turi, "%s\\IPC$", durl);
		return smbc_cli_open_share(con->smb_info->cli, turi);
	}
		
	return -1;
}
Пример #12
0
static gboolean
xmms_samba_browse (xmms_xform_t *xform,
                   const gchar *url,
                   xmms_error_t *error)
{
	struct smbc_dirent *dir;
	int handle;

	G_LOCK (mutex);
	handle = smbc_opendir (url);
	G_UNLOCK (mutex);

	if (handle < 0) {
		xmms_error_set (error, XMMS_ERROR_GENERIC, "Couldn't browse URL");
		xmms_log_error ("Couldn't open directory %s!", url);
		return FALSE;
	}

	while (42) {
		guint32 flags = 0;

		G_LOCK (mutex);
		dir = smbc_readdir (handle);
		if (!dir) {
			G_UNLOCK (mutex);
			break;
		}

		if (dir->name[0] == '.') {
			G_UNLOCK (mutex);
			continue;
		}

		if (dir->smbc_type == SMBC_DIR ||
		    dir->smbc_type == SMBC_WORKGROUP ||
		    dir->smbc_type == SMBC_SERVER ||
		    dir->smbc_type == SMBC_FILE_SHARE)
			flags |= XMMS_XFORM_BROWSE_FLAG_DIR;

		xmms_xform_browse_add_entry (xform, dir->name, flags);
		G_UNLOCK (mutex);
	}

	G_LOCK (mutex);
	smbc_closedir (handle);
	G_UNLOCK (mutex);

	return TRUE;
}
Пример #13
0
static csync_vio_method_handle_t *_opendir(const char *name) {
  smb_dhandle_t *handle = NULL;

  handle = c_malloc(sizeof(smb_dhandle_t));
  if (handle == NULL) {
    return NULL;
  }

  handle->dh = smbc_opendir(name);
  if (handle->dh < 0) {
    SAFE_FREE(handle);
    return NULL;
  }
  handle->path = c_strdup(name);

  return (csync_vio_method_handle_t *) handle;
}
Пример #14
0
static bool test_opendir(struct torture_context *tctx,
			 SMBCCTX *ctx,
			 const char *fname,
			 bool expect_success)
{
	int handle, ret;

	torture_comment(tctx, "Testing smbc_opendir(%s)\n", fname);

	handle = smbc_opendir(fname);
	if (!expect_success) {
		return true;
	}
	if (handle < 0) {
		torture_fail(tctx, talloc_asprintf(tctx, "failed to obain file handle for '%s'", fname));
	}

	ret = smbc_closedir(handle);
	torture_assert_int_equal(tctx, ret, 0,
		talloc_asprintf(tctx, "failed to close file handle for '%s'", fname));

	return true;
}
Пример #15
0
int SMB_del(char *url, int index)
{
        SMB_init(index);
        int res = 0;
        int dh = 0;
        dh = smbc_opendir(url);
        printf("SMB_del() - dh = %d\n", dh);
        if(dh < 0){
                res = smbc_unlink(url);
        }
        else{
                struct smbc_dirent* dirptr;
                if(url[strlen(url) - 1] != '/')
                        strcat(url, "/");
                while((dirptr = smbc_readdir(dh)) != NULL){
                        if(!strcmp(dirptr->name, ".") || !strcmp(dirptr->name, ".."))
                                continue;

                        char new_url[255] = {0};
                        sprintf(new_url, "%s%s", url, dirptr->name);

                        printf("-------%s\n", new_url);

                        if(dirptr->smbc_type == SMBC_DIR) {
                                if(new_url[strlen(new_url) - 1] != '/')
                                        strcat(new_url, "/");
                                SMB_del(new_url, index);
                        }
                        else if(dirptr->smbc_type == SMBC_FILE) {
                                res = smbc_unlink(new_url);
                        }
                }
                res = smbc_rmdir(url);//删除成功返回0,失败返回-1
        }
        smbc_closedir(dh);
        return res;
}
Пример #16
0
void *smb_getdir(void *value)
{
	int err, dh1, dsize, dirc, i, count;
	char dirbuf[2048] = {0};
	struct smbc_dirent *dirp;
	char url[MAX_NAME_LEN] = {0};
	char buf[MAX_NAME_LEN] = {0};
	char tmpdirname[MAX_NAME_LEN] = {0};
	char dir_path[MAX_NAME_LEN] = {0};
	int ret, mount_ok_num, mount_fail_num;
	char *p = NULL;
	struct smbserverinfo_t *psmbserverinfo = NULL;
	struct smbEventProxyCmd *pevent = NULL;

	if(value == NULL)
		return NULL;

	psmbserverinfo = (struct smbserverinfo_t *)value;

	printf("%s[%d], name = [%s], ip = [%s], user = [%s], passwd = [%s], cmd_id = [%d]\n", __FILE__, __LINE__, psmbserverinfo->name, psmbserverinfo->ip, psmbserverinfo->user, psmbserverinfo->passwd, psmbserverinfo->cmd_id);

	memset(smb_username, 0, sizeof(smb_username));
	memset(smb_passwd, 0, sizeof(smb_passwd));
	if(psmbserverinfo->user != NULL) {
		strncpy(smb_username, psmbserverinfo->user, strlen(psmbserverinfo->user));
		smb_username[strlen(psmbserverinfo->user)] = '\0';
	}
	if(psmbserverinfo->passwd != NULL) {
		strncpy(smb_passwd, psmbserverinfo->passwd, strlen(psmbserverinfo->passwd));
		smb_passwd[strlen(psmbserverinfo->passwd)] = '\0';
	}
	printf("%s[%d], smb_username = [%s], smb_passwd = [%s]\n", __FILE__, __LINE__, smb_username, smb_passwd);

	snprintf(url, sizeof(url), "smb://%s:%s@%s", smb_username, smb_passwd, psmbserverinfo->name);
	err = smbc_init(get_static_auth_data_fn,  0); /* Initialize things */
	if (err < 0) {
		printf("%s[%d], Initializing the smbclient library ...: %s\n", __FILE__, __LINE__, strerror(errno));
		SMBTREE_send_event_to_OSD(SMB_CMD_BROWSE_RESPONSE, SMB_MOUNT_FAILED, psmbserverinfo->cmd_id, NULL);
		SMBTREE_free_smbserverinfo(psmbserverinfo);
		return NULL;
	}
	if ((dh1 = smbc_opendir(url))<1) {
		printf("%s[%d], Could not open directory: %s: %s\n", __FILE__, __LINE__, url, strerror(errno));
		SMBTREE_send_event_to_OSD(SMB_CMD_BROWSE_RESPONSE, SMB_MOUNT_FAILED, psmbserverinfo->cmd_id, NULL);
		SMBTREE_free_smbserverinfo(psmbserverinfo);
		return NULL;
	}

	snprintf(dir_path, sizeof(dir_path), "%s/%s", CONF_SAMBA_MOUNT_POINT, psmbserverinfo->name);
	smb_umount(dir_path);

	/* Now, list those directories, but in funny ways ... */
	/* keep call smbc_getdents() until there is no share folder to fix Device only can browse less than 8 root folder.*/
	while(0 != (dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirbuf, sizeof(dirbuf)))) {
		if(dirc < 0) {
			fprintf(stderr, "Problems getting directory entries: %s\n", strerror(errno));
			smbc_closedir(dh1);
			SMBTREE_send_event_to_OSD(SMB_CMD_BROWSE_RESPONSE, SMB_MOUNT_FAILED, psmbserverinfo->cmd_id, NULL);
			SMBTREE_free_smbserverinfo(psmbserverinfo);
			return NULL;	
		}
		/* Now, process the list of names ... */
		fprintf(stdout, "Directory listing, size = %u\n", dirc);
		
		dirp = (struct smbc_dirent *)dirbuf;
		count = 0;
		mount_ok_num = 0;
		mount_fail_num = 0;

		while (dirc > 0) {
			dsize = dirp->dirlen;
			switch (dirp->smbc_type) {
				case SMBC_DIR:
				case SMBC_FILE:
				case SMBC_FILE_SHARE:
					snprintf(buf, sizeof(buf), "%s\\%s\\%s", psmbserverinfo->name, psmbserverinfo->ip, dirp->name);
					fprintf(stdout, "Shared Dir Type: %u, Name: %s\n", dirp->smbc_type, dirp->name); 
					/*if share-folder-name is end of $, system doesn't mount it*/
					if((p=strrchr(buf, '$')) != NULL && strlen(p) == 1) {
						printf("%s[%d], ##### %s not match ####\n", __FILE__, __LINE__, buf);
						break;
					}
					memset(tmpdirname, '\0', sizeof(tmpdirname));
					smb_replace_special_char(dirp->name, tmpdirname);
					ret = smb_mount(psmbserverinfo->user, psmbserverinfo->passwd, psmbserverinfo->name, psmbserverinfo->ip, tmpdirname);
					if (ret == 0) {
#ifdef ENABLE_PTHREAD
						pevent = malloc(sizeof(struct smbEventProxyCmd));
						pevent->cmd = SMB_CMD_BROWSE_RESPONSE;
						pevent->status = SMB_MOUNTING;
						pevent->id = psmbserverinfo->cmd_id;
						pevent->datalen = strlen(buf) + 1;
						strcpy((char *)pevent->data, buf);
						XLinkedList_Lock(sharefolderlist);
							XLinkedList_AddTail(sharefolderlist, (void *)pevent);
						XLinkedList_UnLock(sharefolderlist);
#else
						SMBTREE_send_event_to_OSD(SMB_CMD_BROWSE_RESPONSE, SMB_MOUNTING, psmbserverinfo->cmd_id, buf);
#endif
						mount_ok_num++;
					} else {
						mount_fail_num++;
					}
					break;
				default:
					break;
			}
			i = dsize % 4 == 0 ? 0 : (4 - dsize % 4);
			dsize += i;
			count += dsize;
			dirp = (struct smbc_dirent *)(dirbuf + count);
			dirc -= dsize;
		}
		memset(dirbuf, 0, sizeof(dirbuf));
	}
	smbc_closedir(dh1);
	if(mount_fail_num > 0 && mount_ok_num == 0) {
		SMBTREE_send_event_to_OSD(SMB_CMD_BROWSE_RESPONSE, SMB_MOUNT_FAILED, psmbserverinfo->cmd_id, NULL);
	} else {
		SMBTREE_send_event_to_OSD(SMB_CMD_BROWSE_RESPONSE, SMB_MOUNT_OK, psmbserverinfo->cmd_id, NULL);
	}
	SMBTREE_free_smbserverinfo(psmbserverinfo);
	return NULL;
}
Пример #17
0
vector<string> FileBrowser::GetFolderList()
{
	vector<string> retVal;
	if(m_CurrentFolder.size()==0)
	{
		//List Drives
		retVal.push_back("Game:");

		if (SETTINGS::getInstance().getSambaClientOn()) 
			retVal.push_back("smb:");

		std::vector<Drive* const> mountedDrives;
	
		DrivesManager::getInstance().getMountedDrives(&mountedDrives);
		for(unsigned int x=0;x<mountedDrives.size();x++)
		{

			retVal.push_back(mountedDrives[x]->GetCleanDriveName() + ":");
		}

		return retVal;
	}
	else
	{
		retVal.push_back("..");

		string path = GetCurrentPath();
		if (path.substr(0,4).compare("smb:") == 0) {
			map<string, unsigned int> m_SambaTypes;

			int dir;
			if (path.length() == 5) {
				if (SambaClient::getInstance().m_SambaTopLevelTypes.size() > 0) {
					for (map<string, unsigned int>::const_iterator it = SambaClient::getInstance().m_SambaTopLevelTypes.begin(); it != SambaClient::getInstance().m_SambaTopLevelTypes.end(); ++it)
					{
						retVal.push_back(it->first);
					}
					return retVal;
				}
			}
			path.append("/");
			if ((dir = smbc_opendir(path.c_str())) < 0)
			{
				DebugMsg("FileBrowser", "Could not open directory [%s] (%d:%s)\n", path.c_str(), errno, strerror(errno));
				if (errno == 13) {  // permission denied
					string user;
					string password;
					string share;
					string rawpath = GetRawSmbPath(user, password, share);

					XUIMessage xuiMsg;
					XuiMessage(&xuiMsg, XM_SMB_PERMISSION_DENIED);
					Credentials * cred = new Credentials();
					_XuiMessageExtra(&xuiMsg,(XUIMessageData*) cred, sizeof(*cred));
					cred->smbPath = rawpath;
					cred->user = user;
					cred->password = password;
					cred->share = share;

					XuiSendMessage( parentScene , &xuiMsg );
				}
				return retVal;
			}
			struct smbc_dirent *        dirent;
		    while ((dirent = smbc_readdir(dir)) != NULL)
			{
				if (dirent->smbc_type != SMBC_WORKGROUP &&
					dirent->smbc_type != SMBC_SERVER &&
					dirent->smbc_type != SMBC_FILE_SHARE &&
					dirent->smbc_type != SMBC_DIR)
					continue;
				if (strcmp(dirent->name, ".") == 0 || strcmp(dirent->name, "..") == 0)
					continue;
				if (hasEnding(dirent->name, "$"))
					continue;

				if (m_CurrentFolder.size() == 1) {
					SambaClient::getInstance().m_SambaTopLevelTypes[dirent->name] = dirent->smbc_type;
				}

				// filter ourselves out of the list of servers
				if (SETTINGS::getInstance().getSambaServerOn() && CFreestyleApp::getInstance().hasInternetConnection()) {
					if (m_CurrentFolder.size() == 2) {
						string wrkgroup = SETTINGS::getInstance().getSambaClientWorkgroup();
						if (stricmp(wrkgroup.c_str(), m_CurrentFolder[1].c_str()) == 0) {
							string hostname = SETTINGS::getInstance().getHostname();
							if (stricmp(hostname.c_str(), dirent->name) == 0)
								continue;
						}
					}
				}
				retVal.push_back(dirent->name);
			}
//			if (m_CurrentFolder.size() == 1) {
//				m_SambaTopLevelTypes = m_SambaTypes;
//				//m_CurrentFolderSambaTypes.push_back(m_SambaTypes);
//			}
			smbc_closedir(dir);
			return retVal;
		}
		WIN32_FIND_DATA findFileData;
		memset(&findFileData,0,sizeof(WIN32_FIND_DATA));
		string searchcmd = GetCurrentPath() + "\\*";
	
		HANDLE hFind = FindFirstFile(searchcmd.c_str(), &findFileData);
		if (hFind == INVALID_HANDLE_VALUE)
			return retVal;
		do {
			string s = findFileData.cFileName;
			if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				retVal.push_back(s);
			}
		} while (FindNextFile(hFind, &findFileData));
		FindClose(hFind);
		return retVal;
	}

}
Пример #18
0
int browse(int (*scan_found_share)(char share[]),char * path, int scan, int indent)
{
    char *                      p;
    char                        buf[1024];
    int                         dir;
    struct stat                 stat;
    struct smbc_dirent *        dirent;

    if (! scan)
    {
        bblog(INFO, "Opening (%s)...", path);
    }

    if ((dir = smbc_opendir(path)) < 0)
    {
        bblog(ERROR, "Could not open directory [%s] (%d:%s)", path, errno, strerror(errno));
        return 0;
    }

    while ((dirent = smbc_readdir(dir)) != NULL)
    {
        bblog(INFO, "%*.*s%-30s", indent, indent, "", dirent->name);

        switch(dirent->smbc_type)
        {
        case SMBC_WORKGROUP:
            bblog(INFO, "WORKGROUP");
            break;

        case SMBC_SERVER:
            bblog(INFO, "SERVER");
            break;

        case SMBC_FILE_SHARE:
            bblog(INFO, "FILE_SHARE");
            (*scan_found_share)(dirent->name);
            break;

        case SMBC_PRINTER_SHARE:
            bblog(INFO, "PRINTER_SHARE");
            break;

        case SMBC_COMMS_SHARE:
            bblog(INFO, "COMMS_SHARE");
            break;

        case SMBC_IPC_SHARE:
            bblog(INFO, "IPC_SHARE");
            break;

        case SMBC_DIR:
            bblog(INFO, "DIR");
            break;

        case SMBC_FILE:
            bblog(INFO, "FILE");

            p = path + strlen(path);
            strcat(p, "/");
            strcat(p+1, dirent->name);
            if (smbc_stat(path, &stat) < 0)
            {
                bblog(WARN, " unknown size (reason %d: %s)",
                      errno, strerror(errno));
            }
            else
            {
                bblog(INFO, " size %lu", (unsigned long) stat.st_size);
            }
            *p = '\0';

            break;

        case SMBC_LINK:
            bblog(INFO, "LINK");
            break;
        }

        if (scan &&
                (dirent->smbc_type == SMBC_WORKGROUP ||
                 dirent->smbc_type == SMBC_SERVER))
        {
            /*
             * don't append server name to workgroup; what we want is:
             *
             *   smb://workgroup_name
             * or
             *   smb://server_name
             *
             */
            snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
            browse(scan_found_share,buf, scan, indent + 2);
        }
    }

    smbc_closedir(dir);

    return 1;
}
Пример #19
0
int smb_download_dir(const char *base, const char *name, int resume)
{
	char path[SMB_MAXPATHLEN];
	int dirhandle;
	struct smbc_dirent *dirent;
	const char *relname = name;
	char *tmpname;
	struct stat remotestat;
	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);

	/* List files in directory and call smb_download_file on them */
	dirhandle = smbc_opendir(path);
	if(dirhandle < 1) {
		if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL);
		fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
		return 0;
	}

	while(*relname == '/')relname++;
	mkdir(relname, 0755);
	
	tmpname = strdup(name);

	while((dirent = smbc_readdir(dirhandle))) {
		char *newname;
		if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
		asprintf(&newname, "%s/%s", tmpname, dirent->name);
		switch(dirent->smbc_type) {
		case SMBC_DIR:
			smb_download_dir(base, newname, resume);
			break;

		case SMBC_WORKGROUP:
			smb_download_dir("smb://", dirent->name, resume);
			break;

		case SMBC_SERVER:
			smb_download_dir("smb://", dirent->name, resume);
			break;

		case SMBC_FILE:
			smb_download_file(base, newname, 1, resume, NULL);
			break;

		case SMBC_FILE_SHARE:
			smb_download_dir(base, newname, resume);
			break;

		case SMBC_PRINTER_SHARE:
			if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
			break;

		case SMBC_COMMS_SHARE:
			if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
			break;
			
		case SMBC_IPC_SHARE:
			if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
			break;

		default:
			fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
			break;
		}
		free(newname);
	}
	free(tmpname);

	if(keep_permissions) {
		if(smbc_fstat(dirhandle, &remotestat) < 0) {
			fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
			smbc_closedir(dirhandle);
			return 0;
		}
		
		if(chmod(relname, remotestat.st_mode) < 0) {
			fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode);
			smbc_closedir(dirhandle);
			return 0;
		}
	}

	smbc_closedir(dirhandle);
	return 1;
}
Пример #20
0
vector<string> FileBrowser::GetFileList()
{
	DebugMsg("FileBrowser", "In Get FileList");
	vector<string> retVal;
	if(m_CurrentFolder.size()>0)
	{
		string path = GetCurrentPath();
		if (path.substr(0,4).compare("smb:") == 0) {
			map<string, unsigned int> m_SambaTypes;

			if (path.length() == 5) 
				return retVal;   // there are no "FILES" at the top level

			path.append("/");
			int dir;
			DebugMsg("FileBrowser", "calling opendir on %s", path.c_str());
			if ((dir = smbc_opendir(path.c_str())) < 0)
			{
				DebugMsg("FileBrowser", "Could not open directory [%s] (%d:%s)\n", path.c_str(), errno, strerror(errno));
				if (errno == 13) {
					retVal.push_back("*Permission Denied*");
				}
				return retVal;
			}
			struct smbc_dirent *dirent;
			DebugMsg("FileBrowser", "calling smbc_readdir on %s", path.c_str());
		    while ((dirent = smbc_readdir(dir)) != NULL)
			{
				if (dirent->smbc_type != SMBC_FILE)
					continue;
				if (strcmp(dirent->name, ".") == 0 || strcmp(dirent->name, "..") == 0)
					continue;
//				DebugMsg("FileBrowser","Inserting %s as type %d", dirent->name, dirent->smbc_type);
//				m_SambaTypes[dirent->name]  = dirent->smbc_type;

				retVal.push_back(dirent->name);
			}
			smbc_closedir(dir);
//			if (m_CurrentFolderSambaTypes.size() < m_CurrentFolder.size()) {
//				m_CurrentFolderSambaTypes.push_back(m_SambaTypes);
//			}

			return retVal;
		}



		WIN32_FIND_DATA findFileData;
		memset(&findFileData,0,sizeof(WIN32_FIND_DATA));
		string searchcmd = GetCurrentPath() + "\\*";
	
		HANDLE hFind = FindFirstFile(searchcmd.c_str(), &findFileData);
		if (hFind == INVALID_HANDLE_VALUE)
			return retVal;
		do {
			string s = findFileData.cFileName;
			if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				
			}
			else
			{
				if(strcmp(s.c_str(),"..")!=0)
				{
				retVal.push_back(s);
				}
			}
		} while (FindNextFile(hFind, &findFileData));
		FindClose(hFind);
	}
	return retVal;
}
Пример #21
0
int main(int argc, char *argv[])
{
  int err, fd, dh1, dsize, dirc;
  const char *file = "smb://samba/public/testfile.txt";
  const char *file2 = "smb://samba/public/testfile2.txt";
  char buff[256];
  char dirbuf[512];
  char *dirp;
  struct stat st1, st2;

  err = smbc_init(get_auth_data_fn,  10); /* Initialize things */

  if (err < 0) {

    fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));

  }

  if (argc > 1) {

    if ((dh1 = smbc_opendir(argv[1]))<1) {

      fprintf(stderr, "Could not open directory: %s: %s\n",
	      argv[1], strerror(errno));

      exit(1);

    }

    fprintf(stdout, "Directory handle: %u\n", dh1);

    /* Now, list those directories, but in funny ways ... */

    dirp = (char *)dirbuf;

    if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp, 
			      sizeof(dirbuf))) < 0) {

      fprintf(stderr, "Problems getting directory entries: %s\n",
	      strerror(errno));

      exit(1);

    }

    /* Now, process the list of names ... */

    fprintf(stdout, "Directory listing, size = %u\n", dirc);

    while (dirc > 0) {

      dsize = ((struct smbc_dirent *)dirp)->dirlen;
      fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
	      ((struct smbc_dirent *)dirp)->smbc_type, 
	      ((struct smbc_dirent *)dirp)->name, 
	      ((struct smbc_dirent *)dirp)->comment);

      dirp += dsize;
      dirc -= dsize;

    }

    dirp = (char *)dirbuf;

    exit(1);

  }

  /* For now, open a file on a server that is hard coded ... later will
   * read from the command line ...
   */

  fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);

  if (fd < 0) {

    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Opened or created file: %s\n", file);

  /* Now, write some date to the file ... */

  memset(buff, '\0', sizeof(buff));
  snprintf(buff, sizeof(buff), "%s", "Some test data for the moment ...");

  err = smbc_write(fd, buff, sizeof(buff));

  if (err < 0) {
    
    fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Wrote %lu bytes to file: %s\n",
          (unsigned long) sizeof(buff), buff);

  /* Now, seek the file back to offset 0 */

  err = smbc_lseek(fd, SEEK_SET, 0);

  if (err < 0) {

    fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Completed lseek on file: %s\n", file);

  /* Now, read the file contents back ... */

  err = smbc_read(fd, buff, sizeof(buff));

  if (err < 0) {

    fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */

  fprintf(stdout, "Now fstat'ing file: %s\n", file);

  err = smbc_fstat(fd, &st1);

  if (err < 0) {

    fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }


  /* Now, close the file ... */

  err = smbc_close(fd);

  if (err < 0) {

    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));

  }

  /* Now, rename the file ... */

  err = smbc_rename(file, file2);

  if (err < 0) {

    fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));

  }

  fprintf(stdout, "Renamed file %s to %s\n", file, file2);

  /* Now, create a file and delete it ... */

  fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);

  fd = smbc_open(file, O_RDWR | O_CREAT, 0666);

  if (fd < 0) {

    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Opened or created file: %s\n", file);

  err = smbc_close(fd);

  if (err < 0) {

    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }
  
  /* Now, delete the file ... */

  fprintf(stdout, "File %s created, now deleting ...\n", file);

  err = smbc_unlink(file);

  if (err < 0) {

    fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  /* Now, stat the file, file 2 ... */

  fprintf(stdout, "Now stat'ing file: %s\n", file);

  err = smbc_stat(file2, &st2);

  if (err < 0) {

    fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Stat'ed file:   %s. Size = %d, mode = %04X\n", file2, 
	  (int)st2.st_size, (unsigned int)st2.st_mode);
  fprintf(stdout, "    time: %s\n", ctime(&st2.st_atime));
  fprintf(stdout, "Earlier stat:   %s, Size = %d, mode = %04X\n", file, 
	  (int)st1.st_size, (unsigned int)st1.st_mode);
  fprintf(stdout, "    time: %s\n", ctime(&st1.st_atime));

  /* Now, make a directory ... */

  fprintf(stdout, "Making directory smb://samba/public/make-dir\n");

  if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {

    fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n", 
	    strerror(errno));

    if (errno == EEXIST) { /* Try to delete the directory */

      fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");

      if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */

	fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));

	exit(0);

      }

      fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");

      if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {

	fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
		strerror(errno));

	fprintf(stderr, "I give up!\n");

	exit(1);

      }

    }

    exit(0);
    
  }

  fprintf(stdout, "Made dir: make-dir\n");
  return 0;
}
Пример #22
0
int main(int argc, char * argv[]) 
{ 
    int             i;
    int             fd;
    int             ret;
    int             debug = 0;
    char *          p;
    char            path[2048];
    struct stat     statbuf;
    struct statvfs  statvfsbuf;
    
    smbc_init(get_auth_data_fn, debug); 
    
    for (;;)
    {
        fprintf(stdout, "Path: ");
        *path = '\0';
        fgets(path, sizeof(path) - 1, stdin);
        if (strlen(path) == 0)
        {
            return 0;
        }

        p = path + strlen(path) - 1;
        if (*p == '\n')
        {
            *p = '\0';
        }
    
        /* Determine if it's a file or a folder */
        if (smbc_stat(path, &statbuf) < 0)
        {
            perror("smbc_stat");
            continue;
        }

        if (S_ISREG(statbuf.st_mode))
        {
            if ((fd = smbc_open(path, O_RDONLY, 0)) < 0)
            {
                perror("smbc_open");
                continue;
            }
        }
        else
        {
            if ((fd = smbc_opendir(path)) < 0)
            {
                perror("smbc_opendir");
                continue;
            }
        }

        ret = smbc_fstatvfs(fd, &statvfsbuf);

        smbc_close(fd);

        if (ret < 0)
        {
            perror("fstatvfs");
        }
        else
        {
            printf("\n");
            printf("Block Size: %lu\n", statvfsbuf.f_bsize);
            printf("Fragment Size: %lu\n", statvfsbuf.f_frsize);
            printf("Blocks: %llu\n",
                   (unsigned long long) statvfsbuf.f_blocks);
            printf("Free Blocks: %llu\n",
                   (unsigned long long) statvfsbuf.f_bfree);
            printf("Available Blocks: %llu\n",
                   (unsigned long long) statvfsbuf.f_bavail);
            printf("Files : %llu\n",
                   (unsigned long long) statvfsbuf.f_files);
            printf("Free Files: %llu\n",
                   (unsigned long long) statvfsbuf.f_ffree);
            printf("Available Files: %llu\n",
                   (unsigned long long) statvfsbuf.f_favail);
            printf("File System ID: %lu\n",
                   (unsigned long) statvfsbuf.f_fsid);
            printf("\n");

            printf("Flags: 0x%lx\n", statvfsbuf.f_flag);
            printf("Extended Features: ");

            if (statvfsbuf.f_flag & SMBC_VFS_FEATURE_NO_UNIXCIFS)
            {
                printf("NO_UNIXCIFS ");
            }
            else
            {
                printf("unixcifs ");
            }

            if (statvfsbuf.f_flag & SMBC_VFS_FEATURE_CASE_INSENSITIVE)
            {
                printf("CASE_INSENSITIVE ");
            }
            else
            {
                printf("case_sensitive ");
            }

            if (statvfsbuf.f_flag & SMBC_VFS_FEATURE_DFS)
            {
                printf("DFS ");
            }
            else
            {
                printf("no_dfs ");
            }

            printf("\n");
        }
    }

    return 0; 
}
Пример #23
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int dh = 0; 
	int entry_num = 0;
	int i = 0;
	int offset = 0;
	int dirsize = 0;
	char *file_name;
	char *tmp_file_ptr;

	struct smbc_dirent *dirptr;


	char buff[MAX_BUFF_SIZE];
	char url[MAX_BUFF_SIZE];
	char file_url[MAX_BUFF_SIZE];
	char dir_url[MAX_BUFF_SIZE];
	char dirbuff[MAX_BUFF_SIZE];

	memset(g_workgroup, '\0', MAX_BUFF_SIZE);
	memset(url, '\0', MAX_BUFF_SIZE);
	memset(file_url, '\0', MAX_BUFF_SIZE);
	memset(dir_url, '\0', MAX_BUFF_SIZE);
	memset(buff, '\0', MAX_BUFF_SIZE);

	if ( argc == 6 )
	{

		dirptr = (struct smbc_dirent *) dirbuff;

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));
		smbc_init(auth_fn, 0);

		strncpy(file_url,"tempfile-",9);
		tmp_file_ptr = file_url;
		tmp_file_ptr += 9;

		smbc_rmdir(url);
		smbc_mkdir(url,0666);

		entry_num = atoi(argv[5]);
		strcat(dir_url,url);
		strcat(dir_url,"/");

		file_name = dir_url;
		file_name += strlen(dir_url);

		for ( i = 0; i < entry_num; i++ )
		{
			sprintf(buff,"%d",i);
			memcpy(tmp_file_ptr,buff,strlen(buff)+4);
			strncat(tmp_file_ptr,".txt",4);
			strcpy(file_name,file_url);
			fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666);
			smbc_close(fd);

		}

		dh = smbc_opendir(url);
		err = 0;

		while ( 1 )
		{
			dirptr = smbc_readdir(dh);

			if ( dirptr == NULL )
			{
				break;
			}

			dirsize += dirptr->dirlen;

		}
		
		offset = smbc_telldir(dh);	

		if ( offset != dirsize )
		{
/*
                        printf("offset: %i dirsize: %i\n", offset, dirsize);
*/
			err = 1;
		}	

	}

	return err;

}
Пример #24
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int dh = 0; 
	int entry_num = 0;
	int i = 0;
	char *file_name;
	char *tmp_file_ptr;

	char buff[MAX_BUFF_SIZE];
	char url[MAX_BUFF_SIZE];
	char file_url[MAX_BUFF_SIZE];
	char dir_url[MAX_BUFF_SIZE];

	memset(g_workgroup, '\0', MAX_BUFF_SIZE);
	memset(url, '\0', MAX_BUFF_SIZE);
	memset(file_url, '\0', MAX_BUFF_SIZE);
	memset(dir_url, '\0', MAX_BUFF_SIZE);
	memset(buff, '\0', MAX_BUFF_SIZE);

	if ( argc == 6 )
	{

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));
		smbc_init(auth_fn, 0);

		strncpy(file_url,"tempfile-",9);
		tmp_file_ptr = file_url;
		tmp_file_ptr += 9;

		smbc_rmdir(url);
		smbc_mkdir(url,0666);

		entry_num = atoi(argv[5]);
		strcat(dir_url,url);
		strcat(dir_url,"/");

		file_name = dir_url;
		file_name += strlen(dir_url);

		for ( i = 0; i < entry_num; i++ )
		{
			sprintf(buff,"%d",i);
			memcpy(tmp_file_ptr,buff,strlen(buff)+4);	
			strncat(tmp_file_ptr,".txt",4);
			strcpy(file_name,file_url);
			fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666);
			smbc_close(fd);

		}

		dh = smbc_opendir(url);
		/* printf("directory handle: %i\n",dh); */
		err = smbc_lseekdir(dh,0);
		/* printf("err: %i\n",err); */

		if ( err < 0 )	

			err = 1;

		else
			err = 0;

	}

	return err;

}
Пример #25
0
Файл: smb.c Проект: tguillem/vlc
/****************************************************************************
 * Open: connect to smb server and ask for file
 ****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    stream_t     *p_access = (stream_t*)p_this;
    access_sys_t *p_sys;
    struct stat  filestat;
    vlc_url_t    url;
    vlc_credential credential;
    char         *psz_decoded_path = NULL, *psz_uri = NULL,
                 *psz_var_domain = NULL;
    int          i_ret;
    int          i_smb;
    uint64_t     i_size;
    bool         b_is_dir = false;

#ifndef _WIN32
    if( smbc_init( smb_auth, 0 ) )
        return VLC_EGENERIC;
#endif

/*
** some version of glibc defines open as a macro, causing havoc
** with other macros using 'open' under the hood, such as the
** following one:
*/
#if defined(smbc_open) && defined(open)
# undef open
#endif

    vlc_UrlParse( &url, p_access->psz_url );
    if( url.psz_path )
    {
        psz_decoded_path = vlc_uri_decode_duplicate( url.psz_path );
        if( !psz_decoded_path )
        {
            vlc_UrlClean( &url );
            return VLC_EGENERIC;
        }
    }

    vlc_credential_init( &credential, &url );
    psz_var_domain = var_InheritString( p_access, "smb-domain" );
    credential.psz_realm = psz_var_domain;
    vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
                        NULL, NULL );
    for (;;)
    {
        if( smb_get_uri( p_access, &psz_uri, credential.psz_realm,
                         credential.psz_username, credential.psz_password,
                         url.psz_host, psz_decoded_path, NULL ) == -1 )
        {
            vlc_credential_clean( &credential );
            free(psz_var_domain);
            free( psz_decoded_path );
            vlc_UrlClean( &url );
            return VLC_ENOMEM;
        }

        if( ( i_ret = smbc_stat( psz_uri, &filestat ) ) && errno == EACCES )
        {
            errno = 0;
            if( vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
                                    SMB_LOGIN_DIALOG_TITLE,
                                    SMB_LOGIN_DIALOG_TEXT, url.psz_host) )
                continue;
        }

        /* smbc_stat fails with servers or shares. Assume they are directory */
        if( i_ret || S_ISDIR( filestat.st_mode ) )
            b_is_dir = true;
        break;
    }

    vlc_credential_store( &credential, p_access );
    vlc_credential_clean( &credential );
    free(psz_var_domain);
    free( psz_decoded_path );

    /* Init p_access */
    p_sys =
    p_access->p_sys = vlc_calloc( p_this, 1, sizeof( access_sys_t ) );
    if( !p_sys )
    {
        free( psz_uri );
        vlc_UrlClean( &url );
        return VLC_ENOMEM;
    }

    if( b_is_dir )
    {
        p_sys->url = url;
        p_access->pf_readdir = DirRead;
        p_access->pf_control = access_vaDirectoryControlHelper;
        i_size = 0;
#ifndef _WIN32
        i_smb = smbc_opendir( psz_uri );
        if( i_smb < 0 )
            vlc_UrlClean( &p_sys->url );
#endif
    }
    else
    {
        ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
        i_smb = smbc_open( psz_uri, O_RDONLY, 0 );
        i_size = filestat.st_size;
        vlc_UrlClean( &url );
    }
    free( psz_uri );

#ifndef _WIN32
    if( i_smb < 0 )
    {
        msg_Err( p_access, "open failed for '%s' (%s)",
                 p_access->psz_location, vlc_strerror_c(errno) );
        return VLC_EGENERIC;
    }
#endif

    p_sys->size = i_size;
    p_sys->i_smb = i_smb;

    return VLC_SUCCESS;
}
Пример #26
0
int FSSmb::ReadDir( FSList* list, FSPath& _path, int* err, FSCInfo* info )
{
	FREPARE_SMB_OPER( lock, info, &_param );

	if ( info && info->Stopped() ) { return -2; }

	list->Clear();

	FSPath path( _path );

	int d = smbc_opendir( pathBuffer1.SetPath( path ) );

	if ( d < 0 )
	{
		SetError( err, errno );
		return -1;
	}

	try
	{
		struct smbc_dirent* pEnt;

		int n = path.Count();

		while ( true )
		{
			if ( info && info->Stopped() )
			{
				smbc_closedir( d );
				return -2;
			}

			pEnt = smbc_readdir( d );

			if ( !pEnt )
			{
				//???
				break;
			}

			//skip . and ..
			if ( pEnt->name[0] == '.' && ( !pEnt->name[1] || ( pEnt->name[1] == '.' && !pEnt->name[2] ) ) )
			{
				continue;
			}


			if ( //ignore it
			   pEnt->smbc_type == SMBC_PRINTER_SHARE ||
			   pEnt->smbc_type == SMBC_IPC_SHARE
			) { continue; }


			clPtr<FSNode> pNode = new FSNode();
			path.SetItem( n, CS_UTF8, pEnt->name );

			switch ( pEnt->smbc_type )
			{
				case  SMBC_WORKGROUP:
					pNode->extType = FSNode::WORKGROUP;
					pNode->st.mode = S_IFDIR;
					break;

				case  SMBC_SERVER:
					pNode->extType = FSNode::SERVER;
					pNode->st.mode = S_IFDIR;
					break;

				case  SMBC_FILE_SHARE:
					pNode->extType = FSNode::FILESHARE;
					pNode->st.mode = S_IFDIR;
					break;

				case  SMBC_PRINTER_SHARE:
				case  SMBC_COMMS_SHARE:
				case  SMBC_IPC_SHARE:
				case  SMBC_DIR:
					pNode->st.mode = S_IFDIR;
					break;

				default:
					InternalStat( path, &pNode->st, info );
			}

			pNode->name.Set( sys_charset_id, pEnt->name );

			list->Append( pNode );
		};

		smbc_closedir( d );

		return 0;

err:
		SetError( err, errno );

		smbc_closedir( d );

		return -1;


	}
	catch ( ... )
	{
		smbc_closedir( d );
		throw;
	}
}
Пример #27
0
static void browse(char * path, int scan, int indent)
{
    char *                      p;
    char                        buf[1024];
    int                         dir;
    struct stat                 stat;
    struct smbc_dirent *        dirent;

    if (! scan)
    {
        printf("Opening (%s)...\n", path);
    }
        
    if ((dir = smbc_opendir(path)) < 0)
    {
        printf("Could not open directory [%s] (%d:%s)\n",
               path, errno, strerror(errno));
        return;
    }

    while ((dirent = smbc_readdir(dir)) != NULL)
    {
        printf("%*.*s%-30s", indent, indent, "", dirent->name);

        switch(dirent->smbc_type)
        {
        case SMBC_WORKGROUP:
            printf("WORKGROUP");
            break;
            
        case SMBC_SERVER:
            printf("SERVER");
            break;
            
        case SMBC_FILE_SHARE:
            printf("FILE_SHARE");
            break;
            
        case SMBC_PRINTER_SHARE:
            printf("PRINTER_SHARE");
            break;
            
        case SMBC_COMMS_SHARE:
            printf("COMMS_SHARE");
            break;
            
        case SMBC_IPC_SHARE:
            printf("IPC_SHARE");
            break;
            
        case SMBC_DIR:
            printf("DIR");
            break;
            
        case SMBC_FILE:
            printf("FILE");

            p = path + strlen(path);
            strcat(p, "/");
            strcat(p+1, dirent->name);
            if (smbc_stat(path, &stat) < 0)
            {
                printf(" unknown size (reason %d: %s)",
                       errno, strerror(errno));
            }
            else
            {
                printf(" size %lu", (unsigned int) stat.st_size);
            }
            *p = '\0';

            break;
            
        case SMBC_LINK:
            printf("LINK");
            break;
        }

        printf("\n");

        if (scan &&
            (dirent->smbc_type == SMBC_WORKGROUP ||
             dirent->smbc_type == SMBC_SERVER))
        {
            /*
             * don't append server name to workgroup; what we want is:
             *
             *   smb://workgroup_name
             * or
             *   smb://server_name
             *
             */
            snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
            browse(buf, scan, indent + 2);
        }
    }

    smbc_closedir(dir);
}
Пример #28
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int dh = 0; 
	int entry_num = 0;
	int i = 0;
	int j = 0;
	char *file_name;
	char *tmp_file_ptr;
	int direntsize = 0;
	int diramount = 0;

	struct smbc_dirent *dirptr;


	char buff[MAX_BUFF_SIZE];
	char url[MAX_BUFF_SIZE];
	char file_url[MAX_BUFF_SIZE];
	char dir_url[MAX_BUFF_SIZE];
	char dirbuff[MAX_BUFF_SIZE];

	bzero(g_workgroup,MAX_BUFF_SIZE);
	bzero(url,MAX_BUFF_SIZE);
	bzero(file_url,MAX_BUFF_SIZE);
	bzero(dir_url,MAX_BUFF_SIZE);
	bzero(buff,MAX_BUFF_SIZE);

	if ( argc == 6 )
	{

		dirptr = (struct smbc_dirent *) dirbuff;

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));
		smbc_init(auth_fn, 0);

		strncpy(file_url,"tempfile-",9);
		tmp_file_ptr = file_url;
		tmp_file_ptr += 9;

		smbc_rmdir(url);
		smbc_mkdir(url,0666);

		entry_num = atoi(argv[5]);
		strcat(dir_url,url);
		strcat(dir_url,"/");

		file_name = dir_url;
		file_name += strlen(dir_url);

		for ( i = 0; i < entry_num; i++ )
		{
			sprintf(buff,"%d",i);
			memcpy(tmp_file_ptr,buff,strlen(buff)+4);
			strncat(tmp_file_ptr,".txt",4);
			strcpy(file_name,file_url);
			fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666);
			smbc_close(fd);

		}

		dh = smbc_opendir(url);
		diramount = smbc_getdents( dh, dirptr, sizeof(dirbuff));

		err = 0;
		i = 0;
		bzero(buff,MAX_BUFF_SIZE);
		bzero(tmp_file_ptr,MAX_BUFF_SIZE-9);

		while ( diramount > 0 )
		{
			direntsize = dirptr->dirlen;
			/* printf("Name: %s\n",dirptr->name); */
			if ( j == 0 )
			{
				if ( !(( strncmp(dirptr->name,".",1) == 0 )) ) 
				{
					break;
					err = 1; 
				}

			} else if ( j == 1 ) {	

				if ( !(( strncmp(dirptr->name,"..",2) == 0 )) )
			       	{
					break;
					err = 1; 
				} 
			
			} else if ( j > 1 ) {

					sprintf(buff,"%d",i);
					memcpy(tmp_file_ptr,buff,strlen(buff)+4);
					strncat(tmp_file_ptr,".txt",4);

					if ( !((strcmp(dirptr->name,file_url) == 0 )) ) /* make sure entries match */
					{
						err = 1;
						break;
					}

					i++;

			}

			(char *)dirptr += direntsize;
			(char *)diramount -= direntsize;
			j++;

		}
	
		if ( ! err )
		{
			if ( (j - 2) != entry_num ) /* Make sure that all entries created are counted and returned - minus . and .. */
				err = 1;
		}	

	}

	return err;

}
Пример #29
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int dh = 0; 
	int entry_num = 0;
	int i = 0;
	char *file_name;
	char *tmp_file_ptr;

	char buff[MAX_BUFF_SIZE];
	char url[MAX_BUFF_SIZE];
	char file_url[MAX_BUFF_SIZE];
	char dir_url[MAX_BUFF_SIZE];

	bzero(g_workgroup,MAX_BUFF_SIZE);
	bzero(url,MAX_BUFF_SIZE);
	bzero(file_url,MAX_BUFF_SIZE);
	bzero(dir_url,MAX_BUFF_SIZE);
	bzero(buff,MAX_BUFF_SIZE);

	if ( argc == 6 )
	{

		strncpy(g_workgroup,argv[1],strlen(argv[1]));
		strncpy(g_username,argv[2],strlen(argv[2]));
		strncpy(g_password,argv[3],strlen(argv[3]));
		strncpy(url,argv[4],strlen(argv[4]));
		smbc_init(auth_fn, 0);

		strncpy(file_url,"tempfile-",9);
		tmp_file_ptr = file_url;
		tmp_file_ptr += 9;

		smbc_rmdir(url);
		smbc_mkdir(url,0666);

		entry_num = atoi(argv[5]);
		strcat(dir_url,url);
		strcat(dir_url,"/");

		file_name = dir_url;
		file_name += strlen(dir_url);

		for ( i = 0; i < entry_num; i++ )
		{
			sprintf(buff,"%d",i);
			memcpy(tmp_file_ptr,buff,strlen(buff)+4);	
			strncat(tmp_file_ptr,".txt",4);
			strcpy(file_name,file_url);
			fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666);
			smbc_close(fd);

		}

		dh = smbc_opendir(url);
		smbc_telldir(dh);
	
		err = errno;

	}

	return err;

}
Пример #30
0
static void
do_smb(struct connection *conn)
{
	struct uri *uri = conn->uri;
	struct auth_entry *auth = find_auth(uri);
	struct string string;
	unsigned char *url;
	int dir;

	if ((uri->userlen && uri->passwordlen) || !auth) {
		url = get_uri_string(uri, URI_BASE);
	} else {
		unsigned char *uri_string = get_uri_string(uri, URI_HOST | URI_PORT | URI_DATA);

		if (!uri_string || !init_string(&string)) {
			smb_error(connection_state(S_OUT_OF_MEM));
		}
		/* Must URI-encode the username and password to avoid
		 * ambiguity if they contain "/:@" characters.
		 * Libsmbclient then decodes them again, and the
		 * server gets them as they were in auth->user and
		 * auth->password, i.e. as the user typed them in the
		 * auth dialog.  This implies that, if the username or
		 * password contains some characters or bytes that the
		 * user cannot directly type, then she cannot enter
		 * them.  If that becomes an actual problem, it should
		 * be fixed in the auth dialog, e.g. by providing a
		 * hexadecimal input mode.  */
		add_to_string(&string, "smb://");
		encode_uri_string(&string, auth->user, -1, 1);
		add_char_to_string(&string, ':');
		encode_uri_string(&string, auth->password, -1, 1);
		add_char_to_string(&string, '@');
		add_to_string(&string, uri_string);
		url = string.source;
	}

	if (!url) {
		smb_error(connection_state(S_OUT_OF_MEM));
	}
	if (smbc_init(smb_auth, 0)) {
		smb_error(connection_state_for_errno(errno));
	};


	dir = smbc_opendir(url);
	if (dir >= 0) {
		struct string prefix;

		init_string(&prefix);
		add_to_string(&prefix, url);
		add_char_to_string(&prefix, '/');
		smb_directory(dir, &prefix, conn->uri);
		done_string(&prefix);
	} else {
		const int errno_from_opendir = errno;
		char buf[READ_SIZE];
		struct stat sb;
		int r, res, fdout;
		int file = smbc_open(url, O_RDONLY, 0);

		if (file < 0) {
			/* If we're opening the list of shares without
			 * proper authentication, then smbc_opendir
			 * fails with EACCES and smbc_open fails with
			 * ENOENT.  In this case, return the EACCES so
			 * that the parent ELinks process will prompt
			 * for credentials.  */
			if (errno == ENOENT && errno_from_opendir == EACCES)
				errno = errno_from_opendir;
			smb_error(connection_state_for_errno(errno));
		}

		res = smbc_fstat(file, &sb);
		if (res) {
			smb_error(connection_state_for_errno(res));
		}
		/* filesize */
		fprintf(header_out, "%" OFF_PRINT_FORMAT,
			(off_print_T) sb.st_size);
		fclose(header_out);

		fdout = fileno(data_out);
		while ((r = smbc_read(file, buf, READ_SIZE)) > 0) {
			if (safe_write(fdout, buf, r) <= 0)
					break;
		}
		smbc_close(file);
		exit(0);
	}
}