⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nmblib.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*    Unix SMB/CIFS implementation.   NBT netbios library routines   Copyright (C) Andrew Tridgell 1994-1998      This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   */#include "includes.h"extern struct in_addr lastip;extern int lastport;int num_good_sends = 0;int num_good_receives = 0;static const struct opcode_names {	const char *nmb_opcode_name;	int opcode;} nmb_header_opcode_names[] = {	{"Query",           0 },	{"Registration",      5 },	{"Release",           6 },	{"WACK",              7 },	{"Refresh",           8 },	{"Refresh(altcode)",  9 },	{"Multi-homed Registration", 15 },	{0, -1 }};/**************************************************************************** Lookup a nmb opcode name.****************************************************************************/static const char *lookup_opcode_name( int opcode ){	const struct opcode_names *op_namep;	int i;	for(i = 0; nmb_header_opcode_names[i].nmb_opcode_name != 0; i++) {		op_namep = &nmb_header_opcode_names[i];		if(opcode == op_namep->opcode)			return op_namep->nmb_opcode_name;	}	return "<unknown opcode>";}/**************************************************************************** Print out a res_rec structure.****************************************************************************/static void debug_nmb_res_rec(struct res_rec *res, const char *hdr){	int i, j;	DEBUGADD( 4, ( "    %s: nmb_name=%s rr_type=%d rr_class=%d ttl=%d\n",		hdr,		nmb_namestr(&res->rr_name),		res->rr_type,		res->rr_class,		res->ttl ) );	if( res->rdlength == 0 || res->rdata == NULL )		return;	for (i = 0; i < res->rdlength; i+= MAX_NETBIOSNAME_LEN) {		DEBUGADD(4, ("    %s %3x char ", hdr, i));		for (j = 0; j < MAX_NETBIOSNAME_LEN; j++) {			unsigned char x = res->rdata[i+j];			if (x < 32 || x > 127)				x = '.';	  			if (i+j >= res->rdlength)				break;			DEBUGADD(4, ("%c", x));		}      		DEBUGADD(4, ("   hex "));		for (j = 0; j < MAX_NETBIOSNAME_LEN; j++) {			if (i+j >= res->rdlength)				break;			DEBUGADD(4, ("%02X", (unsigned char)res->rdata[i+j]));		}      		DEBUGADD(4, ("\n"));	}}/**************************************************************************** Process a nmb packet.****************************************************************************/void debug_nmb_packet(struct packet_struct *p){	struct nmb_packet *nmb = &p->packet.nmb;	if( DEBUGLVL( 4 ) ) {		dbgtext( "nmb packet from %s(%d) header: id=%d opcode=%s(%d) response=%s\n",			inet_ntoa(p->ip), p->port,			nmb->header.name_trn_id,			lookup_opcode_name(nmb->header.opcode),			nmb->header.opcode,			BOOLSTR(nmb->header.response) );		dbgtext( "    header: flags: bcast=%s rec_avail=%s rec_des=%s trunc=%s auth=%s\n",			BOOLSTR(nmb->header.nm_flags.bcast),			BOOLSTR(nmb->header.nm_flags.recursion_available),			BOOLSTR(nmb->header.nm_flags.recursion_desired),			BOOLSTR(nmb->header.nm_flags.trunc),			BOOLSTR(nmb->header.nm_flags.authoritative) );		dbgtext( "    header: rcode=%d qdcount=%d ancount=%d nscount=%d arcount=%d\n",			nmb->header.rcode,			nmb->header.qdcount,			nmb->header.ancount,			nmb->header.nscount,			nmb->header.arcount );	}	if (nmb->header.qdcount) {		DEBUGADD( 4, ( "    question: q_name=%s q_type=%d q_class=%d\n",			nmb_namestr(&nmb->question.question_name),			nmb->question.question_type,			nmb->question.question_class) );	}	if (nmb->answers && nmb->header.ancount) {		debug_nmb_res_rec(nmb->answers,"answers");	}	if (nmb->nsrecs && nmb->header.nscount) {		debug_nmb_res_rec(nmb->nsrecs,"nsrecs");	}	if (nmb->additional && nmb->header.arcount) {		debug_nmb_res_rec(nmb->additional,"additional");	}}/******************************************************************* Handle "compressed" name pointers.******************************************************************/static BOOL handle_name_ptrs(unsigned char *ubuf,int *offset,int length,			     BOOL *got_pointer,int *ret){	int loop_count=0;  	while ((ubuf[*offset] & 0xC0) == 0xC0) {		if (!*got_pointer)			(*ret) += 2;		(*got_pointer)=True;		(*offset) = ((ubuf[*offset] & ~0xC0)<<8) | ubuf[(*offset)+1];		if (loop_count++ == 10 || (*offset) < 0 || (*offset)>(length-2)) {			return(False);		}	}	return(True);}/******************************************************************* Parse a nmb name from "compressed" format to something readable return the space taken by the name, or 0 if the name is invalid******************************************************************/static int parse_nmb_name(char *inbuf,int ofs,int length, struct nmb_name *name){	int m,n=0;	unsigned char *ubuf = (unsigned char *)inbuf;	int ret = 0;	BOOL got_pointer=False;	int loop_count=0;	int offset = ofs;	if (length - offset < 2)		return(0);  	/* handle initial name pointers */	if (!handle_name_ptrs(ubuf,&offset,length,&got_pointer,&ret))		return(0);  	m = ubuf[offset];	if (!m)		return(0);	if ((m & 0xC0) || offset+m+2 > length)		return(0);	memset((char *)name,'\0',sizeof(*name));	/* the "compressed" part */	if (!got_pointer)		ret += m + 2;	offset++;	while (m > 0) {		unsigned char c1,c2;		c1 = ubuf[offset++]-'A';		c2 = ubuf[offset++]-'A';		if ((c1 & 0xF0) || (c2 & 0xF0) || (n > sizeof(name->name)-1))			return(0);		name->name[n++] = (c1<<4) | c2;		m -= 2;	}	name->name[n] = 0;	if (n==MAX_NETBIOSNAME_LEN) {		/* parse out the name type, its always in the 16th byte of the name */		name->name_type = ((unsigned char)name->name[15]) & 0xff;  		/* remove trailing spaces */		name->name[15] = 0;		n = 14;		while (n && name->name[n]==' ')			name->name[n--] = 0;  	}	/* now the domain parts (if any) */	n = 0;	while (ubuf[offset]) {		/* we can have pointers within the domain part as well */		if (!handle_name_ptrs(ubuf,&offset,length,&got_pointer,&ret))			return(0);		m = ubuf[offset];		/*		 * Don't allow null domain parts.		 */		if (!m)			return(0);		if (!got_pointer)			ret += m+1;		if (n)			name->scope[n++] = '.';		if (m+2+offset>length || n+m+1>sizeof(name->scope))			return(0);		offset++;		while (m--)			name->scope[n++] = (char)ubuf[offset++];		/*		 * Watch for malicious loops.		 */		if (loop_count++ == 10)			return 0;	}	name->scope[n++] = 0;  	return(ret);}/**************************************************************************** Put a netbios name, padding(s) and a name type into a 16 character buffer. name is already in DOS charset. [15 bytes name + padding][1 byte name type].****************************************************************************/static void put_name(char *dest, const char *name, int pad, unsigned int name_type){	size_t len = strlen(name);	memcpy(dest, name, (len < MAX_NETBIOSNAME_LEN) ? len : MAX_NETBIOSNAME_LEN - 1);	if (len < MAX_NETBIOSNAME_LEN - 1) {		memset(dest + len, pad, MAX_NETBIOSNAME_LEN - 1 - len);	}	dest[MAX_NETBIOSNAME_LEN - 1] = name_type;}/******************************************************************* Put a compressed nmb name into a buffer. Return the length of the compressed name. Compressed names are really weird. The "compression" doubles the size. The idea is that it also means that compressed names conform to the doman name system. See RFC1002.******************************************************************/static int put_nmb_name(char *buf,int offset,struct nmb_name *name){	int ret,m;	nstring buf1;	char *p;	if (strcmp(name->name,"*") == 0) {		/* special case for wildcard name */		put_name(buf1, "*", '\0', name->name_type);	} else {		put_name(buf1, name->name, ' ', name->name_type);	}	buf[offset] = 0x20;	ret = 34;	for (m=0;m<MAX_NETBIOSNAME_LEN;m++) {		buf[offset+1+2*m] = 'A' + ((buf1[m]>>4)&0xF);		buf[offset+2+2*m] = 'A' + (buf1[m]&0xF);	}	offset += 33;	buf[offset] = 0;	if (name->scope[0]) {		/* XXXX this scope handling needs testing */		ret += strlen(name->scope) + 1;		safe_strcpy(&buf[offset+1],name->scope,sizeof(name->scope));    		p = &buf[offset+1];		while ((p = strchr_m(p,'.'))) {			buf[offset] = PTR_DIFF(p,&buf[offset+1]);			offset += (buf[offset] + 1);			p = &buf[offset+1];		}		buf[offset] = strlen(&buf[offset+1]);	}	return(ret);}/******************************************************************* Useful for debugging messages.******************************************************************/char *nmb_namestr(const struct nmb_name *n){	static int i=0;	static fstring ret[4];	fstring name;	char *p = ret[i];	pull_ascii_fstring(name, n->name);	if (!n->scope[0])		slprintf(p,sizeof(fstring)-1, "%s<%02x>",name,n->name_type);	else		slprintf(p,sizeof(fstring)-1, "%s<%02x>.%s",name,n->name_type,n->scope);	i = (i+1)%4;	return(p);}/******************************************************************* Allocate and parse some resource records.******************************************************************/static BOOL parse_alloc_res_rec(char *inbuf,int *offset,int length,				struct res_rec **recs, int count){	int i;	*recs = SMB_MALLOC_ARRAY(struct res_rec, count);	if (!*recs)		return(False);	memset((char *)*recs,'\0',sizeof(**recs)*count);	for (i=0;i<count;i++) {		int l = parse_nmb_name(inbuf,*offset,length,&(*recs)[i].rr_name);		(*offset) += l;		if (!l || (*offset)+10 > length) {			SAFE_FREE(*recs);			return(False);		}		(*recs)[i].rr_type = RSVAL(inbuf,(*offset));		(*recs)[i].rr_class = RSVAL(inbuf,(*offset)+2);		(*recs)[i].ttl = RIVAL(inbuf,(*offset)+4);		(*recs)[i].rdlength = RSVAL(inbuf,(*offset)+8);		(*offset) += 10;		if ((*recs)[i].rdlength>sizeof((*recs)[i].rdata) || 				(*offset)+(*recs)[i].rdlength > length) {			SAFE_FREE(*recs);			return(False);		}		memcpy((*recs)[i].rdata,inbuf+(*offset),(*recs)[i].rdlength);		(*offset) += (*recs)[i].rdlength;    	}	return(True);}/******************************************************************* Put a resource record into a packet.******************************************************************/static int put_res_rec(char *buf,int offset,struct res_rec *recs,int count){	int ret=0;	int i;	for (i=0;i<count;i++) {		int l = put_nmb_name(buf,offset,&recs[i].rr_name);		offset += l;		ret += l;		RSSVAL(buf,offset,recs[i].rr_type);		RSSVAL(buf,offset+2,recs[i].rr_class);		RSIVAL(buf,offset+4,recs[i].ttl);		RSSVAL(buf,offset+8,recs[i].rdlength);		memcpy(buf+offset+10,recs[i].rdata,recs[i].rdlength);		offset += 10+recs[i].rdlength;		ret += 10+recs[i].rdlength;	}	return(ret);}/******************************************************************* Put a compressed name pointer record into a packet.******************************************************************/static int put_compressed_name_ptr(unsigned char *buf,int offset,struct res_rec *rec,int ptr_offset){  	int ret=0;	buf[offset] = (0xC0 | ((ptr_offset >> 8) & 0xFF));	buf[offset+1] = (ptr_offset & 0xFF);	offset += 2;	ret += 2;	RSSVAL(buf,offset,rec->rr_type);	RSSVAL(buf,offset+2,rec->rr_class);	RSIVAL(buf,offset+4,rec->ttl);	RSSVAL(buf,offset+8,rec->rdlength);	memcpy(buf+offset+10,rec->rdata,rec->rdlength);	offset += 10+rec->rdlength;	ret += 10+rec->rdlength;    	return(ret);}/******************************************************************* Parse a dgram packet. Return False if the packet can't be parsed  or is invalid for some reason, True otherwise. This is documented in section 4.4.1 of RFC1002.******************************************************************/static BOOL parse_dgram(char *inbuf,int length,struct dgram_packet *dgram){	int offset;	int flags;	memset((char *)dgram,'\0',sizeof(*dgram));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -