snmpusm.c

来自「eCos操作系统源码」· C语言 代码 · 共 2,486 行 · 第 1/5 页

C
2,486
字号
//==========================================================================////      ./lib/current/src/snmpusm.c//////==========================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.//// eCos 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 or (at your option) any later version.//// eCos 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 eCos; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.//// As a special exception, if other files instantiate templates or use macros// or inline functions from this file, or you compile this file and link it// with other works to produce a work based on this file, this file does not// by itself cause the resulting work to be covered by the GNU General Public// License. However the source code for this file must still be made available// in accordance with section (3) of the GNU General Public License.//// This exception does not invalidate any other reasons why a work based on// this file might be covered by the GNU General Public License.//// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.// at http://sources.redhat.com/ecos/ecos-license/// -------------------------------------------//####ECOSGPLCOPYRIGHTEND####//####UCDSNMPCOPYRIGHTBEGIN####//// -------------------------------------------//// Portions of this software may have been derived from the UCD-SNMP// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of// California at Davis, which was originally based on the Carnegie Mellon// University SNMP implementation.  Portions of this software are therefore// covered by the appropriate copyright disclaimers included herein.//// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"// -------------------------------------------////####UCDSNMPCOPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s):    hmt// Contributors: hmt// Date:         2000-05-30// Purpose:      Port of UCD-SNMP distribution to eCos.// Description:  //              ////####DESCRIPTIONEND####////==========================================================================/********************************************************************       Copyright 1989, 1991, 1992 by Carnegie Mellon University			  Derivative Work -Copyright 1996, 1998, 1999, 2000 The Regents of the University of California			 All Rights ReservedPermission to use, copy, modify and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appears in all copies andthat both that copyright notice and this permission notice appear insupporting documentation, and that the name of CMU and The Regents ofthe University of California not be used in advertising or publicitypertaining to distribution of the software without specific writtenpermission.CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALLWARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU ORTHE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTINGFROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OFCONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR INCONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*********************************************************************//* * snmpusm.c * * Routines to manipulate a information about a "user" as * defined by the SNMP-USER-BASED-SM-MIB MIB. * * All functions usm_set_usmStateReference_*() return 0 on success, -1 * otherwise. * * !! Tab stops set to 4 in some parts of this file. !! *    (Designated on a per function.) */#include <config.h>#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT#include <sys/types.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#include <stdio.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32#  include <sys/timeb.h># else#  include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#ifdef HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "asn1.h"#include "snmp_api.h"#include "snmp_debug.h"#include "callback.h"#include "tools.h"#include "keytools.h"#include "snmp.h"#include "read_config.h"#include "snmpv3.h"#include "snmp-tc.h"#include "lcd_time.h"#include "scapi.h"#include "callback.h"#include "default_store.h"#include "snmpusm.h"#include "transform_oids.h"static u_int    dummy_etime, dummy_eboot;	/* For ISENGINEKNOWN(). *//* * Globals. */static u_int salt_integer;	/* 1/2 of seed for the salt.   Cf. RFC2274, Sect 8.1.1.1.	 */int reportErrorOnUnknownID = 0;	/* Should be determined based on msg type.	 */static struct usmUser *initialUser = NULL;static struct usmUser *noNameUser = NULL;/* * Prototypes */intusm_check_secLevel_vs_protocols(int level,                                oid *authProtocol, u_int authProtocolLen,                                oid *privProtocol, u_int privProtocolLen);  /*  * Set a given field of the secStateRef. * * Allocate <len> bytes for type <type> pointed to by ref-><field>. * Then copy in <item> and record its length in ref-><field_len>. * * Return 0 on success, -1 otherwise. */#define MAKE_ENTRY( type, item, len, field, field_len )			\{									\	if (ref == NULL)						\		return -1;						\	if (ref->field != NULL)	{					\		SNMP_ZERO(ref->field, ref->field_len);			\		SNMP_FREE(ref->field);					\	}								\	ref->field_len = 0;						\        if (len == 0 || item == NULL) {					\		return 0;						\	}					 			\	if ((ref->field = (type*) malloc (len * sizeof(type))) == NULL)	\	{								\		return -1;						\	}								\									\	memcpy (ref->field, item, len * sizeof(type));			\	ref->field_len = len;						\									\	return 0;							\}voidusm_set_reportErrorOnUnknownID (int value){	reportErrorOnUnknownID = value;}struct usmStateReference *usm_malloc_usmStateReference(void){	struct usmStateReference *retval = (struct usmStateReference *)		calloc(1,sizeof(struct usmStateReference));	return retval;}  /* end usm_malloc_usmStateReference() */voidusm_free_usmStateReference (void *old){	struct usmStateReference *old_ref = (struct usmStateReference *)old;    if (old_ref) {	SNMP_FREE(old_ref->usr_name);	SNMP_FREE(old_ref->usr_engine_id);	SNMP_FREE(old_ref->usr_auth_protocol);	SNMP_FREE(old_ref->usr_priv_protocol);	if (old_ref->usr_auth_key) {		SNMP_ZERO(old_ref->usr_auth_key, old_ref->usr_auth_key_length);		SNMP_FREE(old_ref->usr_auth_key);	}	if (old_ref->usr_priv_key) {		SNMP_ZERO(old_ref->usr_priv_key, old_ref->usr_priv_key_length);		SNMP_FREE(old_ref->usr_priv_key);	}	SNMP_ZERO(old_ref, sizeof(*old_ref));	SNMP_FREE(old_ref);   }}  /* end usm_free_usmStateReference() */intusm_set_usmStateReference_name (	struct usmStateReference *ref,	char *name,	size_t name_len){	MAKE_ENTRY (char,name,name_len,usr_name,usr_name_length);}intusm_set_usmStateReference_engine_id (	struct usmStateReference *ref,	u_char *engine_id,	size_t engine_id_len){	MAKE_ENTRY (u_char,engine_id,engine_id_len,		usr_engine_id,usr_engine_id_length);}intusm_set_usmStateReference_auth_protocol (	struct usmStateReference *ref,	oid *auth_protocol,	size_t auth_protocol_len){	MAKE_ENTRY (oid ,auth_protocol,auth_protocol_len,		usr_auth_protocol,usr_auth_protocol_length);}intusm_set_usmStateReference_auth_key (	struct usmStateReference *ref,	u_char *auth_key,	size_t auth_key_len){	MAKE_ENTRY (u_char,auth_key,auth_key_len,		usr_auth_key,usr_auth_key_length);}intusm_set_usmStateReference_priv_protocol (	struct usmStateReference *ref,	oid *priv_protocol,	size_t priv_protocol_len){	MAKE_ENTRY (oid,priv_protocol,priv_protocol_len,		usr_priv_protocol,usr_priv_protocol_length);}intusm_set_usmStateReference_priv_key (	struct usmStateReference *ref,	u_char *priv_key,	size_t priv_key_len){	MAKE_ENTRY (u_char,priv_key,priv_key_len,		usr_priv_key,usr_priv_key_length);}intusm_set_usmStateReference_sec_level (	struct usmStateReference *ref,	int sec_level){	if (ref == NULL) return -1;	ref->usr_sec_level = sec_level;	return 0;}#ifdef SNMP_TESTING_CODE/*******************************************************************-o-****** * emergency_print * * Parameters: *	*field *	 length *       *	This is a print routine that is solely included so that it can be *	used in gdb.  Don't use it as a function, it will be pulled before *	a real release of the code. * *	tab stop 4 * *	XXX fflush() only works on FreeBSD; core dumps on Sun OS's */voidemergency_print (u_char *field, u_int length){	int iindex;	int start=0;	int stop=25;	while (start < stop)	{		for (iindex = start; iindex < stop; iindex++)			printf ("%02X ", field[iindex]);		printf ("\n");		start = stop;		stop = stop+25<length?stop+25:length;	}	fflush (0);}  /* end emergency_print() */#endif /* SNMP_TESTING_CODE *//*******************************************************************-o-****** * asn_predict_int_length * * Parameters: *	type	(UNUSED) *	number *	len *       * Returns: *	Number of bytes necessary to store the ASN.1 encoded value of 'number'. * * *	This gives the number of bytes that the ASN.1 encoder (in asn1.c) will *	use to encode a particular integer value. * *	Returns the length of the integer -- NOT THE HEADER! * *	Do this the same way as asn_build_int()... */intasn_predict_int_length (int type, long number, size_t len){	register u_long mask;	if (len != sizeof (long)) return -1;	mask = ((u_long) 0x1FF) << ((8 * (sizeof(long) - 1)) - 1);	/* mask is 0xFF800000 on a big-endian machine */	while((((number & mask) == 0) || ((number & mask) == mask)) && len > 1)	{		len--;		number <<= 8;	}	return len;}  /* end asn_predict_length() *//*******************************************************************-o-****** * asn_predict_length * * Parameters: *	 type *	*ptr *	 u_char_len *       * Returns: *	Length in bytes:	1 + <n> + <u_char_len>, where * *		1		For the ASN.1 type. *		<n>		# of bytes to store length of data. *		<u_char_len>	Length of data associated with ASN.1 type. * *	This gives the number of bytes that the ASN.1 encoder (in asn1.c) will *	use to encode a particular integer value.  This is as broken as the *	currently used encoder. * * XXX	How is <n> chosen, exactly?? */intasn_predict_length (int type, u_char *ptr, size_t u_char_len){	if (type & ASN_SEQUENCE) return 1+3+u_char_len;	if (type &  ASN_INTEGER)	{		u_long value;		memcpy (&value, ptr, u_char_len);		u_char_len = asn_predict_int_length (type, value, u_char_len);	}	if (u_char_len < 0x80)		return 1+1+u_char_len;	else if (u_char_len < 0xFF)		return 1+2+u_char_len;	else		return 1+3+u_char_len;}  /* end asn_predict_length() *//*******************************************************************-o-****** * usm_calc_offsets * * Parameters: *	(See list below...) *       * Returns: *	0	On success, *	-1	Otherwise. * * *	This routine calculates the offsets into an outgoing message buffer *	for the necessary values.  The outgoing buffer will generically *	look like this: * *	SNMPv3 Message *	SEQ len[11] *		INT len version *	Header *		SEQ len *			INT len MsgID *			INT len msgMaxSize *			OST len msgFlags (OST = OCTET STRING) *			INT len msgSecurityModel *	MsgSecurityParameters *		[1] OST len[2] *			SEQ len[3] *				OST len msgAuthoritativeEngineID *				INT len msgAuthoritativeEngineBoots *				INT len msgAuthoritativeEngineTime *				OST len msgUserName *				OST len[4] [5] msgAuthenticationParameters *				OST len[6] [7] msgPrivacyParameters *	MsgData *		[8] OST len[9] [10] encryptedPDU *		or *		[8,10] SEQUENCE len[9] scopedPDU

⌨️ 快捷键说明

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