Пример #1
0
int main()
{
  struct in_addr  addr;
  smb_session   *session;
  smb_tid     tid;
  smb_fd      fd;

  session = smb_session_new();
  if (session == NULL)
    exit(1);

  inet_aton("127.0.0.1", &addr.sin_addr);

  if (smb_session_connect(session, "MYNAME", 
      addr.sin_addr.s_addr, SMB_TRANSPORT_TCP))
  {
    printf("Unable to connect to host\n");
    exit(2);
  }

  smb_session_set_creds(session, "MYNAME", "login", 
              "password");
  if (smb_session_login(session))
  {
    if (session->guest)
      printf("Logged in as GUEST \n");
    else
      printf("Successfully logged in\n");
  }
  else
  {
    printf("Auth failed\n");
    exit(3);
  }

  tid = smb_tree_connect(session, "MyShare");
  if (!tid)
  {
    printf("Unable to connect to share\n");
    exit(4);
  }

  fd = smb_fopen(session, tid, "\\My\\File");
  if (!fd)
  {
    printf("Unable to open file\n");
    exit(5);
  }

  char buffer[512];
  smb_fread(session, fd, buffer, 512);

  /* Use data */

  smb_fclose(session, fd);
  smb_tree_disconnect(session, tid);
  smb_session_destroy(session);

  return(0);
}
Пример #2
0
int DLLCALL getmail(scfg_t* cfg, int usernumber, BOOL sent)
{
    char    str[128];
    int     i=0;
    long    l;
    idxrec_t idx;
	smb_t	smb;

	ZERO_VAR(smb);
	sprintf(smb.file,"%smail",cfg->data_dir);
	smb.retry_time=cfg->smb_retry_time;
	sprintf(str,"%s.sid",smb.file);
	l=flength(str);
	if(l<(long)sizeof(idxrec_t))
		return(0);
	if(!usernumber) 
		return(l/sizeof(idxrec_t)); 	/* Total system e-mail */
	smb.subnum=INVALID_SUB;
	if(smb_open(&smb)!=0) 
		return(0); 
	while(!smb_feof(smb.sid_fp)) {
		if(smb_fread(&smb,&idx,sizeof(idx),smb.sid_fp) != sizeof(idx))
			break;
		if(idx.number==0)	/* invalid message number, ignore */
			continue;
		if(idx.attr&MSG_DELETE)
			continue;
		if((!sent && idx.to==usernumber)
		 || (sent && idx.from==usernumber))
			i++; 
	}
	smb_close(&smb);
	return(i);
}
Пример #3
0
void dump_hashes(void)
{
	char	tmp[128];
	int		retval;
	hash_t	hash;

	if((retval=smb_open_hash(&smb))!=SMB_SUCCESS) {
		fprintf(errfp,"\n%s!smb_open_hash returned %d: %s\n"
			,beep, retval, smb.last_error);
		return;
	}

	while(!smb_feof(smb.hash_fp)) {
		if(smb_fread(&smb,&hash,sizeof(hash),smb.hash_fp)!=sizeof(hash))
			break;
		printf("\n");
		printf("%-10s: %"PRIu32"\n","Number",	hash.number);
		printf("%-10s: %s\n",		"Source",	smb_hashsourcetype(hash.source));
		printf("%-10s: %"PRIu32"\n","Length",	hash.length);
		printf("%-10s: %s\n",		"Time",		my_timestr(hash.time));
		printf("%-10s: %02x\n",		"Flags",	hash.flags);
		if(hash.flags&SMB_HASH_CRC16)
			printf("%-10s: %04x\n",	"CRC-16",	hash.crc16);
		if(hash.flags&SMB_HASH_CRC32)
			printf("%-10s: %08"PRIx32"\n","CRC-32",	hash.crc32);
		if(hash.flags&SMB_HASH_MD5)
			printf("%-10s: %s\n",	"MD5",		MD5_hex((BYTE*)tmp,hash.md5));
	}

	smb_close_hash(&smb);
}
Пример #4
0
/*****************************************************************************
 * Read:
 *****************************************************************************/
static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

    i_read = smb_fread( p_sys->p_session, p_sys->i_fd, p_buffer, i_len );
    if( i_read < 0 )
    {
        msg_Err( p_access, "read failed" );
        return -1;
    }

    return i_read;
}
Пример #5
0
/*****************************************************************************
 * Read:
 *****************************************************************************/
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

    if( p_access->info.b_eof ) return 0;

    i_read = smb_fread( p_sys->p_session, p_sys->i_fd, p_buffer, i_len );
    if( i_read < 0 )
    {
        msg_Err( p_access, "read failed" );
        return -1;
    }

    if( i_read == 0 ) p_access->info.b_eof = true;

    return i_read;
}
Пример #6
0
mail_t* DLLCALL loadmail(smb_t* smb, long* msgs, uint usernumber
			   ,int which, long mode)
{
	ulong		l=0;
    idxrec_t    idx;
	mail_t*		mail=NULL;

	if(msgs==NULL)
		return(NULL);

	*msgs=0;

	if(smb==NULL)
		return(NULL);

	if(smb_locksmbhdr(smb)!=0)  				/* Be sure noone deletes or */
		return(NULL);							/* adds while we're reading */

	smb_rewind(smb->sid_fp);
	while(!smb_feof(smb->sid_fp)) {
		if(smb_fread(smb,&idx,sizeof(idx),smb->sid_fp) != sizeof(idx))
			break;
		if(idx.number==0)	/* invalid message number, ignore */
			continue;
		if((which==MAIL_SENT && idx.from!=usernumber)
			|| (which==MAIL_YOUR && idx.to!=usernumber)
			|| (which==MAIL_ANY && idx.from!=usernumber && idx.to!=usernumber))
			continue;
		if(idx.attr&MSG_DELETE && !(mode&LM_INCDEL))	/* Don't included deleted msgs */
			continue;					
		if(mode&LM_UNREAD && idx.attr&MSG_READ)
			continue;
		if((mail=(mail_t *)realloc(mail,sizeof(mail_t)*(l+1)))
			==NULL) {
			smb_unlocksmbhdr(smb);
			return(NULL); 
		}
		mail[l]=idx;
		l++; 
	}
	smb_unlocksmbhdr(smb);
	*msgs=l;
	return(mail);
}
Пример #7
0
/* If return value is SMB_ERROR_NOT_FOUND, hash file is left open */
int SMBCALL smb_findhash(smb_t* smb, hash_t** compare, hash_t* found_hash, 
						 long source_mask, BOOL mark)
{
	int		retval;
	BOOL	found=FALSE;
	size_t	c,count;
	hash_t	hash;

	if(found_hash!=NULL)
		memset(found_hash,0,sizeof(hash_t));

	if((retval=smb_open_hash(smb))!=SMB_SUCCESS)
		return(retval);

	COUNT_LIST_ITEMS(compare, count);

	if(count) {

		rewind(smb->hash_fp);
		while(!feof(smb->hash_fp)) {
			if(smb_fread(smb,&hash,sizeof(hash),smb->hash_fp)!=sizeof(hash))
				break;

			if(hash.flags==0)
				continue;		/* invalid hash record (!?) */

			if((source_mask&(1<<hash.source))==0)	/* not checking this source type */
				continue;

			for(c=0;compare[c]!=NULL;c++) {

				if(compare[c]->source!=hash.source)
					continue;	/* wrong source */
				if(compare[c]->length!=hash.length)
					continue;	/* wrong source length */
				if(compare[c]->flags&SMB_HASH_MARKED)
					continue;	/* already marked */
				if((compare[c]->flags&SMB_HASH_PROC_MASK)!=(hash.flags&SMB_HASH_PROC_MASK))
					continue;	/* wrong pre-process flags */
				if((compare[c]->flags&hash.flags&SMB_HASH_MASK)==0)	
					continue;	/* no matching hashes */
				if(compare[c]->flags&hash.flags&SMB_HASH_CRC16 
					&& compare[c]->crc16!=hash.crc16)
					continue;	/* wrong crc-16 */
				if(compare[c]->flags&hash.flags&SMB_HASH_CRC32
					&& compare[c]->crc32!=hash.crc32)
					continue;	/* wrong crc-32 */
				if(compare[c]->flags&hash.flags&SMB_HASH_MD5 
					&& memcmp(compare[c]->md5,hash.md5,sizeof(hash.md5)))
					continue;	/* wrong crc-16 */
				
				/* successful match! */
				break;	/* can't match more than one, so stop comparing */
			}

			if(compare[c]==NULL)
				continue;	/* no match */

			found=TRUE;

			if(found_hash!=NULL)
				memcpy(found_hash,&hash,sizeof(hash));

			if(!mark)
				break;

			compare[c]->flags|=SMB_HASH_MARKED;
		}
		if(found) {
			smb_close_hash(smb);
			return(SMB_SUCCESS);
		}
	}

	/* hash file left open */
	return(SMB_ERR_NOT_FOUND);
}
Пример #8
0
int sbbs_t::delmail(uint usernumber, int which)
{
	ulong	i,l;
	time_t	now;
	idxrec_t *idxbuf;
	smbmsg_t msg;

	now=time(NULL);
	if((i=smb_getstatus(&smb))!=0) {
		errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
		return(2); 
	}
	if(!smb.status.total_msgs)
		return(0);
	if((idxbuf=(idxrec_t *)malloc(smb.status.total_msgs*sizeof(idxrec_t)))==NULL) {
		errormsg(WHERE,ERR_ALLOC,smb.file,smb.status.total_msgs*sizeof(idxrec_t));
		return(-1); 
	}
	if((i=smb_open_da(&smb))!=0) {
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
		free(idxbuf);
		return(i); 
	}
	if((i=smb_open_ha(&smb))!=0) {
		smb_close_da(&smb);
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
		free(idxbuf);
		return(i); 
	}
	smb_rewind(smb.sid_fp);
	for(l=0;l<smb.status.total_msgs;) {
		if(smb_fread(&smb,&msg.idx,sizeof(idxrec_t),smb.sid_fp)!=sizeof(idxrec_t))
			break;
		if(!(msg.idx.attr&MSG_PERMANENT)
			&& ((which==MAIL_SENT && usernumber==msg.idx.from)
				|| (which==MAIL_YOUR && usernumber==msg.idx.to)
				|| (which==MAIL_ANY	&& (usernumber==msg.idx.to || usernumber==msg.idx.from))
				|| which==MAIL_ALL)) {
			if(smb.status.max_age && (now<0?0:(uintmax_t)now)>msg.idx.time
				&& (now-msg.idx.time)/(24L*60L*60L)>smb.status.max_age)
				msg.idx.attr|=MSG_DELETE;
			else if(msg.idx.attr&MSG_KILLREAD && msg.idx.attr&MSG_READ)
				msg.idx.attr|=MSG_DELETE;
			if(msg.idx.attr&MSG_DELETE) {
				/* Don't need to lock message because base is locked */
				if((i=smb_getmsghdr(&smb,&msg))!=0)
					errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
				else {
					if(msg.hdr.attr!=msg.idx.attr) {
						msg.hdr.attr=msg.idx.attr;
						if((i=smb_putmsghdr(&smb,&msg))!=0)
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error); 
					}
					if((i=smb_freemsg(&smb,&msg))!=0)
						errormsg(WHERE,ERR_REMOVE,smb.file,i,smb.last_error);
					if(msg.hdr.auxattr&MSG_FILEATTACH)
						delfattach(&cfg,&msg);
					smb_freemsgmem(&msg); 
				}
				continue; 
			}
		}
		idxbuf[l]=msg.idx;
		l++; 
	}
	smb_rewind(smb.sid_fp);
	smb_fsetlength(smb.sid_fp,0);
	for(i=0;i<l;i++)
		smb_fwrite(&smb,&idxbuf[i],sizeof(idxrec_t),smb.sid_fp);
	free(idxbuf);
	smb.status.total_msgs=l;
	smb_putstatus(&smb);
	smb_fflush(smb.sid_fp);
	smb_close_ha(&smb);
	smb_close_da(&smb);
	return(0);
}
Пример #9
0
char* SMBCALL smb_getmsgtxt(smb_t* smb, smbmsg_t* msg, ulong mode)
{
	char*	buf;
	char*	lzhbuf;
	char*	p;
	char*	str;
	ushort	xlat;
	uint 	i;
	int		lzh;	/* BOOL */
	long	l=0,lzhlen,length;

	if((buf=(char*)malloc(sizeof(char)))==NULL) {
		sprintf(smb->last_error
			,"malloc failure of %u bytes for buffer"
			,sizeof(char));
		return(NULL);
	}
	*buf=0;

	if(!(mode&GETMSGTXT_NO_HFIELDS)) {
		for(i=0;i<(uint)msg->total_hfields;i++) {			/* comment headers are part of text */
			if(msg->hfield[i].type!=SMB_COMMENT && msg->hfield[i].type!=SMTPSYSMSG)
				continue;
			str=(char*)msg->hfield_dat[i];
			length=strlen(str)+2;	/* +2 for crlf */
			if((p=(char*)realloc(buf,l+length+1))==NULL) {
				sprintf(smb->last_error
					,"realloc failure of %ld bytes for comment buffer"
					,l+length+1);
				return(buf);
			}
			buf=p;
			l+=sprintf(buf+l,"%s\r\n",str);
		}
	}

	for(i=0;i<(uint)msg->hdr.total_dfields;i++) {
		if(msg->dfield[i].length<=sizeof(xlat))
			continue;
		switch(msg->dfield[i].type) {
			case TEXT_BODY:
				if(mode&GETMSGTXT_NO_BODY)
					continue;
				break;
			case TEXT_TAIL:
				if(!(mode&GETMSGTXT_TAILS))
					continue;
				break;
			default:	/* ignore other data types */
				continue;
		}
		fseek(smb->sdt_fp,msg->hdr.offset+msg->dfield[i].offset
			,SEEK_SET);
		fread(&xlat,sizeof(xlat),1,smb->sdt_fp);
		lzh=0;
		if(xlat==XLAT_LZH) {
			lzh=1;
			fread(&xlat,sizeof(xlat),1,smb->sdt_fp); 
		}
		if(xlat!=XLAT_NONE) 	/* no other translations currently supported */
			continue;

		length=msg->dfield[i].length-sizeof(xlat);
		if(lzh) {
			length-=sizeof(xlat);
			if(length<1)
				continue;
			if((lzhbuf=(char*)malloc(length))==NULL) {
				sprintf(smb->last_error
					,"malloc failure of %ld bytes for LZH buffer"
					,length);
				return(buf);
			}
			smb_fread(smb,lzhbuf,length,smb->sdt_fp);
			lzhlen=*(long*)lzhbuf;
			if((p=(char*)realloc(buf,l+lzhlen+3L))==NULL) {
				sprintf(smb->last_error
					,"realloc failure of %ld bytes for text buffer"
					,l+lzhlen+3L);
				free(lzhbuf);
				return(buf); 
			}
			buf=p;
			lzh_decode((char*)lzhbuf,length,(char*)buf+l);
			free(lzhbuf);
			l+=lzhlen; 
		}
		else {
			if((p=(char*)realloc(buf,l+length+3L))==NULL) {
				sprintf(smb->last_error
					,"realloc failure of %ld bytes for text buffer"
					,l+length+3L);
				return(buf);
			}
			buf=p;
			p=buf+l;
			l+=fread(p,1,length,smb->sdt_fp);
		}
		if(!l)
			continue;
		l--;
		while(l && buf[l]==0) l--;
		l++;
		*(buf+l)='\r';	/* CR */
		l++;
		*(buf+l)='\n';	/* LF */
		l++;
		*(buf+l)=0; 
	}

	return(buf);
}