Пример #1
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);
        fd = smbc_open(url, O_RDWR | O_CREAT, 0666);
        smbc_close(fd);
        fd = smbc_open(url, O_RDWR, 0666);

        err = errno;

    }

    return err;

}
Пример #2
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int msg_len = 0;
	char url[MAX_BUFF_SIZE];
	char* message;
	char* response;

	memset(g_workgroup, '\0', MAX_BUFF_SIZE);
	memset(url, '\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]));

		msg_len = strlen(argv[5])+1;
		message = malloc(msg_len);
		response = malloc(msg_len);
		message[msg_len - 1] = 0;	
		strncpy(message,argv[5],msg_len);

		smbc_init(auth_fn, 0);
		smbc_unlink(url);
		fd = smbc_open(url,O_RDWR | O_CREAT, 0666);
		smbc_close(fd);

		strncpy(g_username,"xxxxxxxx",8);
		strncpy(g_password,"xxxxxxxx",8);

		fd = smbc_open(url, O_RDWR, 0666);

		if (fd < 0)
			 
			err = errno;
		 
		else { 

			smbc_write(fd, message, msg_len);
			err = errno;

			smbc_close(fd);

		}

		free(message);
		free(response);
		
	}

	return err;

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

	bzero(g_workgroup,MAX_BUFF_SIZE);
	bzero(url,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]));

		msg_len = strlen(argv[5])+1;
		message = malloc(msg_len);
		response = malloc(msg_len);
		message[msg_len - 1] = 0;	
		strncpy(message,argv[5],msg_len);

		smbc_init(auth_fn, 0);
		smbc_unlink(url);
		fd = smbc_open(url,O_RDWR | O_CREAT, 0666);

		smbc_write(fd, message, msg_len);
		smbc_close(fd);

		fd = smbc_open(url,O_WRONLY, 0666);
		err = smbc_read(fd,response,msg_len);

		free(message);
		free(response);

		if ( err < 0 )
			err = 1;

		else if ( err != msg_len )
			err = 1;

		else
			err = 0;

	}

	return err;

}
Пример #4
0
//int CFile::OpenFile(const CURL &url, CStdString& strAuth)
int CFile::OpenFile()
{
  int fd = -1;
  smb.Init();

  //strAuth = GetAuthenticatedPath(url);
  //CStdString strPath = strAuth;
  CStdString strPath = m_fileName;

  {
    CLockObject lock(smb);
    fd = smbc_open(strPath.c_str(), O_RDONLY, 0);
  }

  // file open failed, try to open the directory to force authentication
  if (fd < 0 && errno == EACCES)
  {
#if 0
    CURL urlshare(url);

    /* just replace the filename with the sharename */
    urlshare.SetFileName(url.GetShareName());

    CSMBDirectory smbDir;
    // TODO: Currently we always allow prompting on files.  This may need to
    // change in the future as background scanners are more prolific.
    smbDir.SetFlags(DIR_FLAG_ALLOW_PROMPT);
    fd = smbDir.Open(urlshare);

    // directory open worked, try opening the file again
    if (fd >= 0)
    {
      CLockObject lock(smb);
      // close current directory filehandle
      // dont need to purge since its the same server and share
      smbc_closedir(fd);

      // set up new filehandle (as CFile::Open does)
      strPath = GetAuthenticatedPath(url);

      fd = smbc_open(strPath.c_str(), O_RDONLY, 0);
    }
#else
  XBMC->Log(LOG_ERROR, "%s: File open on %s failed\n", __FUNCTION__, strPath.c_str());
#endif
  }

//  if (fd >= 0)
//    strAuth = strPath;

  return fd;
}
Пример #5
0
static int smbcfs_open(const char* const path, struct fuse_file_info* const fi)
{

  const std::string smbcfs_path = smbcfs_root_path + path;

#ifdef MUTEX_LOCK
  pthread_mutex_lock(&mutex_lock);  
#endif

  const int ret_open = smbc_open(smbcfs_path.c_str(), fi->flags, 0);

#ifdef MUTEX_LOCK
  pthread_mutex_unlock(&mutex_lock);  
#endif

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

  if(ret_open<0) { return ret_open; }

  fi->fh = ret_open;
  return 0;

}
Пример #6
0
int main(int argc, char* argv[])
{
        int             fd;
        struct stat     st1;
        struct stat     st2;
        char *          pUrl = argv[1];

        if(argc != 2)
        {
                printf("usage: %s <file_url>\n", argv[0]);
                return 1;
        }

        
        smbc_init(get_auth_data_fn, 0);
        
        if (smbc_stat(pUrl, &st1) < 0)
        {
                perror("smbc_stat");
                return 1;
        }
        
        if ((fd = smbc_open(pUrl, O_RDONLY, 0)) < 0)
        {
                perror("smbc_open");
                return 1;
        }

        if (smbc_fstat(fd, &st2) < 0)
        {
                perror("smbc_fstat");
                return 1;
        }
        
        smbc_close(fd);

#define COMPARE(name, field)                                            \
        if (st1.field != st2.field)                                     \
        {                                                               \
                printf("Field " name " MISMATCH: st1=%lu, st2=%lu\n",   \
                       (unsigned long) st1.field,                       \
                       (unsigned long) st2.field);                      \
        }

        COMPARE("st_dev", st_dev);
        COMPARE("st_ino", st_ino);
        COMPARE("st_mode", st_mode);
        COMPARE("st_nlink", st_nlink);
        COMPARE("st_uid", st_uid);
        COMPARE("st_gid", st_gid);
        COMPARE("st_rdev", st_rdev);
        COMPARE("st_size", st_size);
        COMPARE("st_blksize", st_blksize);
        COMPARE("st_blocks", st_blocks);
        COMPARE("st_atime", st_atime);
        COMPARE("st_mtime", st_mtime);
        COMPARE("st_ctime", st_ctime);

        return 0;
}
Пример #7
0
void* OpenForWrite(VFSURL* url, bool bOverWrite)
{ 
  CSMB2::Get().Init();
  // we can't open files like smb://file.f or smb://server/file.f
  // if a file matches the if below return false, it can't exist on a samba share.
  if (!IsValidFile(url->filename))
    return NULL;

  std::string strFileName = GetAuthenticatedPath(url);
  PLATFORM::CLockObject lock(CSMB2::Get());

  SMBContext* result = new SMBContext;
  if (bOverWrite)
  {
    XBMC->Log(ADDON::LOG_INFO, "FileSmb::OpenForWrite() called with overwriting enabled! - %s", strFileName.c_str());
    result->fd = smbc_creat(strFileName.c_str(), 0);
  }
  else
  {
    result->fd = smbc_open(strFileName.c_str(), O_RDWR, 0);
  }

  if (result->fd == -1)
  {
    // write error to logfile
    XBMC->Log(ADDON::LOG_ERROR, "FileSmb->Open: Unable to open file : '%s'\nunix_err:'%x' error : '%s'", strFileName.c_str(), errno, strerror(errno));
    delete result;
    return NULL;
  }

  // We've successfully opened the file!
  return result;
}
Пример #8
0
static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
  struct stream_priv_s *p = (struct stream_priv_s*)opts;
  char *filename;
  mode_t m = 0;
  off_t len;
  int fd, err;

  filename = stream->url;

  if(mode == STREAM_READ)
    m = O_RDONLY;
  else if (mode == STREAM_WRITE) //who's gonna do that ?
    m = O_RDWR|O_CREAT|O_TRUNC;
  else {
    mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
    m_struct_free (&stream_opts, opts);
    return STREAM_UNSUPPORTED;
  }

  if(!filename) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  err = smbc_init(smb_auth_fn, 1);
  if (err < 0) {
    mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBInitError,err);
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  fd = smbc_open(filename, m,0644);
  if (fd < 0) {
    mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBFileNotFound, filename);
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  stream->flags = mode;
  len = 0;
  if(mode == STREAM_READ) {
    len = smbc_lseek(fd,0,SEEK_END);
    smbc_lseek (fd, 0, SEEK_SET);
  }
  if(len > 0 || mode == STREAM_WRITE) {
    stream->flags |= MP_STREAM_SEEK;
    stream->seek = seek;
    if(mode == STREAM_READ) stream->end_pos = len;
  }
  stream->type = STREAMTYPE_SMB;
  stream->fd = fd;
  stream->fill_buffer = fill_buffer;
  stream->write_buffer = write_buffer;
  stream->close = close_f;
  stream->control = control;

  m_struct_free(&stream_opts, opts);
  return STREAM_OK;
}
Пример #9
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd1 = 0;
	char url[MAX_BUFF_SIZE];

	memset(g_workgroup, '\0', MAX_BUFF_SIZE);
	memset(url, '\0', 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 );
		fd1 = smbc_open( url, O_RDWR | O_CREAT, 0666 );
		smbc_close( fd1 );

		err = smbc_rmdir( url );

		if ( err < 0 )
			err = 1;

		else
			err = 0;	


	}

	return err;

}
Пример #10
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 == 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);
		smbc_open(url,O_RDWR | O_CREAT,0666);
		err = smbc_rename( url, argv[5] );
	
		if ( err < 0 )
			err = 1;

	}

	return err;

}
Пример #11
0
bool CFile::OpenForWrite(const CURL& url, bool bOverWrite)
{
  m_fileSize = 0;

  Close();
  smb.Init();
  // we can't open files like smb://file.f or smb://server/file.f
  // if a file matches the if below return false, it can't exist on a samba share.
  if (!IsValidFile(url.GetFileName())) return false;

  CStdString strFileName = GetAuthenticatedPath(url);
  CLockObject lock(smb);

  if (bOverWrite)
  {
    XBMC->Log(LOG_WARNING, "FileSmb::OpenForWrite() called with overwriting enabled! - %s", strFileName.c_str());
    m_fd = smbc_creat(strFileName.c_str(), 0);
  }
  else
  {
    m_fd = smbc_open(strFileName.c_str(), O_RDWR, 0);
  }

  if (m_fd == -1)
  {
    // write error to logfile
    XBMC->Log(LOG_ERROR, "FileSmb->Open: Unable to open file : '%s'\nunix_err:'%x' error : '%s'", strFileName.c_str(), errno, strerror(errno));
    return false;
  }

  // We've successfully opened the file!
  return true;
}
Пример #12
0
int get_smb_server_fd(struct SMB_DS *ds, char* filename)
{
	int ret, fd, i;
	char buf[256+1];
	ret = smbc_init(smb_auth_fn, 0);NE_ERR(ret, 0);
	for(i=0;i<strlen(ds->path);i++)
	{
		if(ds->path[i]=='\\')
			ds->path[i] = '/';
	}
	sprintf(buf, "smb://%s/%s/%s", ds->server, ds->path, filename);
	buf[256]='\0';
	//fprintf(stderr, "%s\n", buf);
	if(ds->user[0]!='\0')
		strcpy(smb_username, ds->user);
	else
		strcpy(smb_username, "dummy");
	strcpy(smb_password, ds->passwd);
	
	//fprintf(stderr, "%s %s\n", smb_username, smb_password);

	fd = smbc_open(buf, O_RDWR | O_CREAT | O_TRUNC, 0666);EQ_ERR(fd, -1);
	return fd;
errout:
        if(fd != -1)
		smbc_close(fd);
	return -1;
}
Пример #13
0
/***************************************************** 
a wrapper for open()
*******************************************************/
int smbw_open(const char *fname, int flags, mode_t mode)
{
        int client_fd;
        int smbw_fd;
	char path[PATH_MAX];

	SMBW_INIT();

	if (!fname) {
		errno = EINVAL;
		return -1;
	}

	smbw_fd = (smbw_libc.open)(SMBW_DUMMY, O_WRONLY, 0200);
	if (smbw_fd == -1) {
		errno = EMFILE;
                return -1;
	}

        smbw_fix_path(fname, path);
        if (flags == creat_bits) {
                client_fd =  smbc_creat(path, mode);
        } else {
                client_fd = smbc_open(path, flags, mode);
        }

        if (client_fd < 0) {
                (* smbw_libc.close)(smbw_fd);
                return -1;
        }

        smbw_fd_map[smbw_fd] = client_fd;
        smbw_ref(client_fd, SMBW_RCT_Increment);
        return smbw_fd;
}
Пример #14
0
int FSSmb::OpenRead( FSPath& path, int flags, int* err, FSCInfo* info )
{
	FREPARE_SMB_OPER( lock, info, &_param );

	int n = smbc_open( pathBuffer1.SetPath( path ), O_RDONLY | OPENFLAG_LARGEFILE, 0 );
	SetError( err, errno );
	return n < 0 ? -1 : n;
}
Пример #15
0
int FSSmb::OpenCreate( FSPath& path, bool overwrite, int mode, int flags, int* err, FSCInfo* info )
{
	FREPARE_SMB_OPER( lock, info, &_param );

	int n = smbc_open( pathBuffer1.SetPath( path ),
	                   O_CREAT | O_WRONLY | O_TRUNC | OPENFLAG_LARGEFILE | ( overwrite ? 0 : O_EXCL ) , mode );
	SetError( err, errno );
	return n < 0 ? -1 : n;
}
Пример #16
0
static int open_f (stream_t *stream, int mode)
{
  char *filename;
  mode_t m = 0;
  int64_t len;
  int fd, err;

  struct priv *priv = talloc_zero(stream, struct priv);
  stream->priv = priv;

  filename = stream->url;

  if(mode == STREAM_READ)
    m = O_RDONLY;
  else if (mode == STREAM_WRITE) //who's gonna do that ?
    m = O_RDWR|O_CREAT|O_TRUNC;
  else {
    MP_ERR(stream, "[smb] Unknown open mode %d\n", mode);
    return STREAM_UNSUPPORTED;
  }

  if(!filename) {
    MP_ERR(stream, "[smb] Bad url\n");
    return STREAM_ERROR;
  }

  err = smbc_init(smb_auth_fn, 1);
  if (err < 0) {
    MP_ERR(stream, "Cannot init the libsmbclient library: %d\n",err);
    return STREAM_ERROR;
  }

  fd = smbc_open(filename, m,0644);
  if (fd < 0) {
    MP_ERR(stream, "Could not open from LAN: '%s'\n", filename);
    return STREAM_ERROR;
  }

  stream->flags = mode;
  len = 0;
  if(mode == STREAM_READ) {
    len = smbc_lseek(fd,0,SEEK_END);
    smbc_lseek (fd, 0, SEEK_SET);
  }
  if(len > 0 || mode == STREAM_WRITE) {
    stream->flags |= MP_STREAM_SEEK;
    stream->seek = seek;
    if(mode == STREAM_READ) stream->end_pos = len;
  }
  priv->fd = fd;
  stream->fill_buffer = fill_buffer;
  stream->write_buffer = write_buffer;
  stream->close = close_f;
  stream->control = control;

  return STREAM_OK;
}
Пример #17
0
int smbc_wrapper_open(connection* con, const char *furl, int flags, mode_t mode)
{
	if(con->mode== SMB_BASIC)
		return smbc_open(furl, flags, mode);
	else if(con->mode == SMB_NTLM)
		return smbc_cli_open(con->smb_info->cli, furl, flags);

	return -1;
}
Пример #18
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int msg_len = 0;
	char url[MAX_BUFF_SIZE];
	char* message;

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

	g_print_id = 0;
	g_print_priority = 0;
	g_print_size = 0;

	if ( argc == 7 )
	{
		
		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]));

		msg_len = strlen(argv[5])+1;
		message = malloc(msg_len);
		message[msg_len - 1] = 0;	
		strncpy(message,argv[5],msg_len);
		/* printf("Message: %s\n",message); */
		/* printf("Message len: %i\n",msg_len); */

		smbc_init(auth_fn, 0);
		smbc_unlink(url);
		fd = smbc_open(url,O_RDWR | O_CREAT, 0666);
		smbc_write(fd, message, msg_len);
		smbc_close(fd);

		free(message);
		smbc_print_file(url,argv[6]);
		smbc_list_print_jobs(argv[6],print_list_fn);

		g_print_id = -1;
		err = smbc_unlink_print_job(argv[6],g_print_id);

		if ( err < 0 )

			err = 1;

		else
			err = 0;

	}

	return err;

}
Пример #19
0
/* upload file */
int SMB_upload(char *localpath, int index)
{
        if(access(localpath, F_OK) != 0)
        {
                printf("Local has no %s\n", localpath);
                return LOCAL_FILE_LOST;
        }

        write_log(S_UPLOAD, "", localpath, index);

        SMB_init(index);

        int buflen;
        char buffer[4096] = {0};

        char *serverpath = localpath_to_serverpath(localpath, index);
        int smb_fd = smbc_open(serverpath, O_RDWR|O_CREAT, FILE_MODE);
        int cli_fd;
        if((cli_fd = open(localpath, O_RDONLY, FILE_MODE)) > 0)
        {//以只读的方式打开,文件
            DEBUG("open localpath sucess\n");
                unsigned long long cli_filesize = 0;
                unsigned long long smb_filesize = 0;
                cli_filesize = lseek(cli_fd, 0L, SEEK_END);
                lseek(cli_fd, 0L, SEEK_SET);//read or write 文件的偏移量
                //2014.11.19 by sherry 判断上传是否成功

                //buflen = read(cli_fd, buffer, sizeof(buffer));
                //DEBUG("buflen=%d\n",buflen);
                //while(buflen > 0 && exit_loop == 0)
                while((buflen = read(cli_fd, buffer, sizeof(buffer))) > 0 && exit_loop == 0)
                {
                        smb_filesize += buflen;
                        //2014.11.19 by sherry 判断上传是否成功
                        //smbc_write(smb_fd, buffer, buflen);//smb_fd为server端的文件
                        int res=0;
                        res=smbc_write(smb_fd, buffer, buflen);
                        if(res==-1)
                            return -1;

                        printf("\rUpload [%s] percent - %f ", localpath, (float)smb_filesize/(float)cli_filesize);
                }
                if(smb_filesize != cli_filesize && exit_loop != 0)
                {
                        DEBUG("smb_filesize != cli_filesize && exit_loop != 0");
                        FILE *f_stream = fopen(g_pSyncList[index]->up_item_file, "w");
                        fprintf(f_stream, "%s", localpath);
                        fclose(f_stream);
                }
                smbc_close(smb_fd);
                close(cli_fd);
                DEBUG("upload --end\n");
        }
        free(serverpath);
        return 0;
}
Пример #20
0
int main(int argc, char** argv)
{
	int err = -1;
	int fd = 0;
	int msg_len = 0;
	char url[MAX_BUFF_SIZE];
	char* message = NULL;

	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);

		msg_len = 10;
		fd = smbc_open(url, O_RDWR, 0666);
		err = smbc_write(fd, message, msg_len);
		smbc_close(fd);

		free(message);

		if ( err < 0 )
			err = 1;

		else
			err = 0;

	}

	return err;

}
Пример #21
0
/*
 * Member functions
 */
static gboolean
xmms_samba_init (xmms_xform_t *xform)
{
	xmms_samba_data_t *data;
	const gchar *url;
	const gchar *metakey;
	struct stat st;
	gint fd, err;

	g_return_val_if_fail (xform, FALSE);

	url = xmms_xform_indata_get_str (xform, XMMS_STREAM_TYPE_URL);
	g_return_val_if_fail (url, FALSE);

	G_LOCK (mutex);
	err = smbc_stat (url, &st);
	G_UNLOCK (mutex);

	if (err < 0) {
		xmms_log_error ("%s", strerror (errno));
		return FALSE;
	}

	if (!S_ISREG (st.st_mode)) {
		xmms_log_error ("%s is not a regular file.", url);
		return FALSE;
	}

	G_LOCK (mutex);
	fd = smbc_open (url, O_RDONLY | O_NONBLOCK, 0);
	G_UNLOCK (mutex);

	if (fd == -1) {
		xmms_log_error ("%s", strerror (errno));
		return FALSE;
	}

	data = g_new0 (xmms_samba_data_t, 1);
	data->fd = fd;

	xmms_xform_private_data_set (xform, data);

	xmms_xform_outdata_type_add (xform, XMMS_STREAM_TYPE_MIMETYPE,
	                             "application/octet-stream",
	                             XMMS_STREAM_TYPE_END);

	metakey = XMMS_MEDIALIB_ENTRY_PROPERTY_SIZE;
	xmms_xform_metadata_set_int (xform, metakey, st.st_size);

	metakey = XMMS_MEDIALIB_ENTRY_PROPERTY_LMOD;
	xmms_xform_metadata_set_int (xform, metakey, st.st_mtime);

	return TRUE;
}
Пример #22
0
int main(int argc, char * argv[]) 
{ 
    int             fd;
    int             ret;
    int             debug = 0;
    int             savedErrno;
    char            buffer[2048]; 
    char            path[2048];
    char *          p;
    
    smbc_init(get_auth_data_fn, debug); 
    
    printf("CAUTION: This program will overwrite a file.  "
           "Press ENTER to continue.");
    fgets(buffer, sizeof(buffer), stdin);
           

    for (;;)
    {
        fprintf(stdout, "\nPath: ");
        *path = '\0';
        fgets(path, sizeof(path) - 1, stdin);
        if (strlen(path) == 0)
        {
            return 0;
        }

        p = path + strlen(path) - 1;
        if (*p == '\n')
        {
            *p = '\0';
        }
    
        if ((fd = smbc_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0)) < 0)
        {
            perror("smbc_open");
            continue;
        }

        snprintf(buffer, sizeof(buffer), "%s", "Hello world\n");

        ret = smbc_write(fd, buffer, strlen(buffer));
        savedErrno = errno;
        smbc_close(fd);

        if (ret < 0)
        {
            errno = savedErrno;
            perror("write");
        }
    }

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

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

    if ( argc == 7 )
    {
        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 );
        fd1 = smbc_open( url, O_RDWR | O_CREAT, 0666 );
        smbc_close( fd1 );

        smbc_mkdir( argv[5], 0666 );
        smbc_rename( url, argv[6] );

        fd1 = smbc_open( url, O_RDWR, 0666 );
        fd2 = smbc_open( argv[6], O_RDWR, 0666 );

        if ( fd1 == -1 && fd2 != -1 )
            err = 0;

        else
            err = 1;


    }

    return err;

}
Пример #24
0
FHANDLE FileOpen( const char *filename, int mode )
{
	FHANDLE handle;
	int		c;
	char	*ptr;

	memset( &handle, -1, sizeof(handle) );

	if(!filename)
		return handle;

	if( (ptr = strchr( filename, ':' )) == NULL ) {
#ifdef _DEBUG
		printf("FileOpen : Invalid Path (ptr = NULL)\n");
#endif
		return handle;
	}

	c = ptr - filename;

	// file is on cd/dvd
	if( !strncmp( filename, "cdfs:", c ) ) {
		CD_Init();
		CDVD_FlushCache();

		handle.fh = fioOpen( filename, mode );
		handle.dt = DT_CD;
	}
	else if( !strncmp( filename, "pfs", 3 ) ) {
		handle.fh = fileXioOpen( filename, mode, 0 );
		handle.dt = DT_HDD;
	}
	else if( !strncmp( filename, "mc0:", c ) || !strncmp( filename, "mc1:", c ) ) {
		handle.fh = fioOpen( filename, mode );
		handle.dt = DT_MC;
	}
	else if( !strncmp( filename, "mass:", c ) ) {
		handle.fh = fioOpen( filename, mode );
		handle.dt = DT_USB;
	}
	else if( !strncmp( filename, "host:", c ) ) {
		handle.fh = fioOpen( filename, mode );
		handle.dt = DT_HOST;
	}
	else if( !strncmp( filename, "smb:", c ) ) {
		handle.fh = smbc_open( filename, mode, 0666 );
		handle.dt = DT_SMB_SHARE;
	}

	return handle;
}
Пример #25
0
static csync_vio_method_handle_t *_open(const char *durl, int flags, mode_t mode) {
  smb_fhandle_t *handle = NULL;
  int fd = -1;

  if ((fd = smbc_open(durl, flags, mode)) < 0) {
    return NULL;
  }

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

  handle->fd = fd;
  return (csync_vio_method_handle_t *) handle;
}
Пример #26
0
int CFile::OpenFile()
{
  int fd = -1;
  smb.Init();

  CStdString strPath = m_fileName;

  {
    CLockObject lock(smb);
    fd = smbc_open(strPath.c_str(), O_RDONLY, 0);
  }

  // file open failed, try to open the directory to force authentication
  if (fd < 0 && errno == EACCES)
  {
    XBMC->Log(LOG_ERROR, "%s: File open on %s failed\n", __FUNCTION__, strPath.c_str());
  }

  return fd;
}
Пример #27
0
/* upload file */
int SMB_upload(char *localpath, int index)
{
        if(access(localpath, F_OK) != 0)
        {
                printf("Local has no %s\n", localpath);
                return LOCAL_FILE_LOST;
        }

        write_log(S_UPLOAD, "", localpath, index);

        SMB_init(index);

        int buflen;
        char buffer[4096] = {0};

        char *serverpath = localpath_to_serverpath(localpath, index);
        int smb_fd = smbc_open(serverpath, O_RDWR|O_CREAT, FILE_MODE);
        int cli_fd;
        if((cli_fd = open(localpath, O_RDONLY, FILE_MODE)) > 0){
                unsigned long long cli_filesize = 0;
                unsigned long long smb_filesize = 0;
                cli_filesize = lseek(cli_fd, 0L, SEEK_END);
                lseek(cli_fd, 0L, SEEK_SET);
                while((buflen = read(cli_fd, buffer, sizeof(buffer))) > 0 && exit_loop == 0){
                        smb_filesize += buflen;
                        smbc_write(smb_fd, buffer, buflen);
                        printf("\rUpload [%s] percent - %f ", localpath, (float)smb_filesize/(float)cli_filesize);
                }
                if(smb_filesize != cli_filesize && exit_loop != 0)
                {
                        FILE *f_stream = fopen(g_pSyncList[index]->up_item_file, "w");
                        fprintf(f_stream, "%s", localpath);
                        fclose(f_stream);
                }
                smbc_close(smb_fd);
                close(cli_fd);
        }
        free(serverpath);
        return 0;
}
Пример #28
0
void* Open(VFSURL* url)
{
  CSMB2::Get().Init();
  CSMB2::Get().AddActiveConnection();
  if (!IsValidFile(url->filename))
  {
    XBMC->Log(ADDON::LOG_INFO, "FileSmb->Open: Bad URL : '%s'",url->redacted);
    return NULL;
  }
  int fd = -1;
  std::string filename = GetAuthenticatedPath(url);
  PLATFORM::CLockObject lock(CSMB2::Get());
  fd = smbc_open(filename.c_str(), O_RDONLY, 0);
  if (fd == -1)
  {
    XBMC->Log(ADDON::LOG_INFO, "FileSmb->Open: Unable to open file : '%s'\nunix_err:'%x' error : '%s'", url->redacted, errno, strerror(errno));
    return NULL;
  }
  XBMC->Log(ADDON::LOG_DEBUG,"CSMB2File::Open - opened %s, fd=%d", url->filename, fd);
  struct stat tmpBuffer;
  if (smbc_stat(filename.c_str(), &tmpBuffer) < 0)
  {
    smbc_close(fd);
    return NULL;
  }
  int64_t ret = smbc_lseek(fd, 0, SEEK_SET);
  if (ret < 0)
  {
    smbc_close(fd);
    return NULL;
  }
  SMBContext* result = new SMBContext;
  result->fd = fd;
  result->size = tmpBuffer.st_size;
  return result;
}
Пример #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
handler_t stat_cache_get_entry(server *srv, connection *con, buffer *name, stat_cache_entry **ret_sce) {
#ifdef HAVE_FAM_H
	fam_dir_entry *fam_dir = NULL;
	int dir_ndx = -1;
	splay_tree *dir_node = NULL;
#endif
	stat_cache_entry *sce = NULL;
	stat_cache *sc;
	struct stat st;
	size_t k;
	int fd;
	struct stat lst;
#ifdef DEBUG_STAT_CACHE
	size_t i;
#endif

	int file_ndx;
	splay_tree *file_node = NULL;

	*ret_sce = NULL;

	/*
	 * check if the directory for this file has changed
	 */
	sc = srv->stat_cache;

	buffer_copy_string_buffer(sc->hash_key, name);
	buffer_append_long(sc->hash_key, con->conf.follow_symlink);

	file_ndx = hashme(sc->hash_key);
	sc->files = splaytree_splay(sc->files, file_ndx);

#ifdef DEBUG_STAT_CACHE
	for (i = 0; i < ctrl.used; i++) {
		if (ctrl.ptr[i] == file_ndx) break;
	}
#endif

	if (sc->files && (sc->files->key == file_ndx)) {
#ifdef DEBUG_STAT_CACHE
		/* it was in the cache */
		assert(i < ctrl.used);
#endif

		/* we have seen this file already and
		 * don't stat() it again in the same second */

		file_node = sc->files;

		sce = file_node->data;

		/* check if the name is the same, we might have a collision */

		if (buffer_is_equal(name, sce->name)) {
			if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_SIMPLE) {
				if (sce->stat_ts == srv->cur_ts) {
					*ret_sce = sce;
					return HANDLER_GO_ON;
				}
			}
		} else {
			/* oops, a collision,
			 *
			 * file_node is used by the FAM check below to see if we know this file
			 * and if we can save a stat().
			 *
			 * BUT, the sce is not reset here as the entry into the cache is ok, we
			 * it is just not pointing to our requested file.
			 *
			 *  */

			file_node = NULL;
		}
	} else {
#ifdef DEBUG_STAT_CACHE
		if (i != ctrl.used) {
			log_error_write(srv, __FILE__, __LINE__, "xSB",
				file_ndx, "was already inserted but not found in cache, ", name);
		}
		assert(i == ctrl.used);
#endif
	}

#ifdef HAVE_FAM_H
	/* dir-check */
	if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM) {
		if (0 != buffer_copy_dirname(sc->dir_name, name)) {
			log_error_write(srv, __FILE__, __LINE__, "sb",
				"no '/' found in filename:", name);
			return HANDLER_ERROR;
		}

		buffer_copy_string_buffer(sc->hash_key, sc->dir_name);
		buffer_append_long(sc->hash_key, con->conf.follow_symlink);

		dir_ndx = hashme(sc->hash_key);

		sc->dirs = splaytree_splay(sc->dirs, dir_ndx);

		if (sc->dirs && (sc->dirs->key == dir_ndx)) {
			dir_node = sc->dirs;
		}

		if (dir_node && file_node) {
			/* we found a file */

			sce = file_node->data;
			fam_dir = dir_node->data;

			if (fam_dir->version == sce->dir_version) {
				/* the stat()-cache entry is still ok */
				*ret_sce = sce;
				return HANDLER_GO_ON;
			}
		}
	}
#endif

	/*
	 * *lol*
	 * - open() + fstat() on a named-pipe results in a (intended) hang.
	 * - stat() if regular file + open() to see if we can read from it is better
	 *
	 * */
	 int r;
	
#ifdef HAVE_LIBSMBCLIENT
	if(con->mode == DIRECT) 
		r =  stat(name->ptr, &st);
	else if(con->mode == SMB_NTLM||con->mode == SMB_BASIC)
		r = smbc_wrapper_stat(con, name->ptr, &st);
#else	
	r =  stat(name->ptr, &st);
#endif

	if (r == -1) {
		return HANDLER_ERROR;
	}


	if (S_ISREG(st.st_mode)) {
		/* fix broken stat/open for symlinks to reg files with appended slash on freebsd,osx */
		if (name->ptr[name->used-2] == '/') {
			errno = ENOTDIR;
			return HANDLER_ERROR;
		}

		/* try to open the file to check if we can read it */
#ifdef HAVE_LIBSMBCLIENT
		if(con->mode == DIRECT) {
			fd = open(name->ptr, O_RDONLY);
			if(fd == -1) {
				return HANDLER_ERROR;
			}
			close(fd);
		}
		else if(con->mode == SMB_NTLM) {
			smb_file_t *fp;
			fp = smbc_cli_ntcreate(con->smb_info->cli, name->ptr, 
				FILE_READ_DATA | FILE_WRITE_DATA, FILE_OPEN, 0);
			if(fp == NULL) {
				return HANDLER_ERROR;
			}
			smbc_cli_close(con->smb_info->cli, fp);
		}
		else {
			//Cdbg(DBE, "call smbc_open..%s", con->smb_info->url.path->ptr);			
			//Cdbg(DBE, "call smbc_open..%s", name->ptr);			

			//fd = smbc_open(con->smb_info->url.path->ptr, O_RDONLY, 0);
			fd = smbc_open(name->ptr, O_RDONLY, 0);
			
			if(fd == -1) {
				Cdbg(DBE, "fail to smbc_open");
				return HANDLER_ERROR;
			}
			//Cdbg(DBE, "call smbc_close");			
			smbc_close(fd);
		}
#else
		if (-1 == (fd = open(name->ptr, O_RDONLY))) {
			return HANDLER_ERROR;
		}
		close(fd);
#endif		
	}

	if (NULL == sce) {
#ifdef DEBUG_STAT_CACHE
		int osize = splaytree_size(sc->files);
#endif

		sce = stat_cache_entry_init();
		buffer_copy_string_buffer(sce->name, name);

		sc->files = splaytree_insert(sc->files, file_ndx, sce);
#ifdef DEBUG_STAT_CACHE
		if (ctrl.size == 0) {
			ctrl.size = 16;
			ctrl.used = 0;
			ctrl.ptr = malloc(ctrl.size * sizeof(*ctrl.ptr));
		} else if (ctrl.size == ctrl.used) {
			ctrl.size += 16;
			ctrl.ptr = realloc(ctrl.ptr, ctrl.size * sizeof(*ctrl.ptr));
		}

		ctrl.ptr[ctrl.used++] = file_ndx;

		assert(sc->files);
		assert(sc->files->data == sce);
		assert(osize + 1 == splaytree_size(sc->files));
#endif
	}

	sce->st = st;
	sce->stat_ts = srv->cur_ts;

	/* catch the obvious symlinks
	 *
	 * this is not a secure check as we still have a race-condition between
	 * the stat() and the open. We can only solve this by
	 * 1. open() the file
	 * 2. fstat() the fd
	 *
	 * and keeping the file open for the rest of the time. But this can
	 * only be done at network level.
	 *
	 * per default it is not a symlink
	 * */
#ifdef HAVE_LSTAT
	sce->is_symlink = 0;

	/* we want to only check for symlinks if we should block symlinks.
	 */
	if (!con->conf.follow_symlink) {
		if (stat_cache_lstat(srv, name, &lst)  == 0) {
#ifdef DEBUG_STAT_CACHE
				log_error_write(srv, __FILE__, __LINE__, "sb",
						"found symlink", name);
#endif
				sce->is_symlink = 1;
		}

		/*
		 * we assume "/" can not be symlink, so
		 * skip the symlink stuff if our path is /
		 **/
		else if ((name->used > 2)) {
			buffer *dname;
			char *s_cur;

			dname = buffer_init();
			buffer_copy_string_buffer(dname, name);

			while ((s_cur = strrchr(dname->ptr,'/'))) {
				*s_cur = '\0';
				dname->used = s_cur - dname->ptr + 1;
				if (dname->ptr == s_cur) {
#ifdef DEBUG_STAT_CACHE
					log_error_write(srv, __FILE__, __LINE__, "s", "reached /");
#endif
					break;
				}
#ifdef DEBUG_STAT_CACHE
				log_error_write(srv, __FILE__, __LINE__, "sbs",
						"checking if", dname, "is a symlink");
#endif
				if (stat_cache_lstat(srv, dname, &lst)  == 0) {
					sce->is_symlink = 1;
#ifdef DEBUG_STAT_CACHE
					log_error_write(srv, __FILE__, __LINE__, "sb",
							"found symlink", dname);
#endif
					break;
				};
			};
			buffer_free(dname);
		};
	};
#endif

	if (S_ISREG(st.st_mode)) {
		/* determine mimetype */
		buffer_reset(sce->content_type);
#ifdef HAVE_XATTR
		if (con->conf.use_xattr) {
			stat_cache_attr_get(sce->content_type, name->ptr);
		}
#endif
		/* xattr did not set a content-type. ask the config */
		if (buffer_is_empty(sce->content_type)) {
			for (k = 0; k < con->conf.mimetypes->used; k++) {
				data_string *ds = (data_string *)con->conf.mimetypes->data[k];
				buffer *type = ds->key;

				if (type->used == 0) continue;

				/* check if the right side is the same */
				if (type->used > name->used) continue;

				if (0 == strncasecmp(name->ptr + name->used - type->used, type->ptr, type->used - 1)) {
					buffer_copy_string_buffer(sce->content_type, ds->value);
					break;
				}
			}
		}
		etag_create(sce->etag, &(sce->st), con->etag_flags);
	} else if (S_ISDIR(st.st_mode)) {
		etag_create(sce->etag, &(sce->st), con->etag_flags);
	}

#ifdef HAVE_FAM_H
	if (sc->fam &&
	    (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM)) {
		/* is this directory already registered ? */
		if (!dir_node) {
			fam_dir = fam_dir_entry_init();
			fam_dir->fc = sc->fam;

			buffer_copy_string_buffer(fam_dir->name, sc->dir_name);

			fam_dir->version = 1;

			fam_dir->req = calloc(1, sizeof(FAMRequest));

			if (0 != FAMMonitorDirectory(sc->fam, fam_dir->name->ptr,
						     fam_dir->req, fam_dir)) {

				log_error_write(srv, __FILE__, __LINE__, "sbsbs",
						"monitoring dir failed:",
						fam_dir->name, 
						"file:", name,
						FamErrlist[FAMErrno]);

				fam_dir_entry_free(fam_dir);
			} else {
				int osize = 0;

			       	if (sc->dirs) {
					osize = sc->dirs->size;
				}

				sc->dirs = splaytree_insert(sc->dirs, dir_ndx, fam_dir);
				assert(sc->dirs);
				assert(sc->dirs->data == fam_dir);
				assert(osize == (sc->dirs->size - 1));
			}
		} else {
			fam_dir = dir_node->data;
		}

		/* bind the fam_fc to the stat() cache entry */

		if (fam_dir) {
			sce->dir_version = fam_dir->version;
			sce->dir_ndx     = dir_ndx;
		}
	}
#endif

	*ret_sce = sce;

	return HANDLER_GO_ON;
}