OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [snmp/] [lib/] [current/] [src/] [snmpv3.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      ./lib/current/src/snmpv3.c
4
//
5
//
6
//==========================================================================
7
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
8
// -------------------------------------------                              
9
// This file is part of eCos, the Embedded Configurable Operating System.   
10
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
11
//
12
// eCos is free software; you can redistribute it and/or modify it under    
13
// the terms of the GNU General Public License as published by the Free     
14
// Software Foundation; either version 2 or (at your option) any later      
15
// version.                                                                 
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT      
18
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
20
// for more details.                                                        
21
//
22
// You should have received a copy of the GNU General Public License        
23
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
24
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
25
//
26
// As a special exception, if other files instantiate templates or use      
27
// macros or inline functions from this file, or you compile this file      
28
// and link it with other works to produce a work based on this file,       
29
// this file does not by itself cause the resulting work to be covered by   
30
// the GNU General Public License. However the source code for this file    
31
// must still be made available in accordance with section (3) of the GNU   
32
// General Public License v2.                                               
33
//
34
// This exception does not invalidate any other reasons why a work based    
35
// on this file might be covered by the GNU General Public License.         
36
// -------------------------------------------                              
37
// ####ECOSGPLCOPYRIGHTEND####                                              
38
//####UCDSNMPCOPYRIGHTBEGIN####
39
//
40
// -------------------------------------------
41
//
42
// Portions of this software may have been derived from the UCD-SNMP
43
// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
44
// California at Davis, which was originally based on the Carnegie Mellon
45
// University SNMP implementation.  Portions of this software are therefore
46
// covered by the appropriate copyright disclaimers included herein.
47
//
48
// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
49
// -------------------------------------------
50
//
51
//####UCDSNMPCOPYRIGHTEND####
52
//==========================================================================
53
//#####DESCRIPTIONBEGIN####
54
//
55
// Author(s):    hmt
56
// Contributors: hmt
57
// Date:         2000-05-30
58
// Purpose:      Port of UCD-SNMP distribution to eCos.
59
// Description:  
60
//              
61
//
62
//####DESCRIPTIONEND####
63
//
64
//==========================================================================
65
/********************************************************************
66
       Copyright 1989, 1991, 1992 by Carnegie Mellon University
67
 
68
                          Derivative Work -
69
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
70
 
71
                         All Rights Reserved
72
 
73
Permission to use, copy, modify and distribute this software and its
74
documentation for any purpose and without fee is hereby granted,
75
provided that the above copyright notice appears in all copies and
76
that both that copyright notice and this permission notice appear in
77
supporting documentation, and that the name of CMU and The Regents of
78
the University of California not be used in advertising or publicity
79
pertaining to distribution of the software without specific written
80
permission.
81
 
82
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
83
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
84
WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
85
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
86
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
87
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
88
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
89
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
90
*********************************************************************/
91
/*
92
 * snmpv3.c
93
 */
94
 
95
#include <config.h>
96
 
97
#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT
98
#include <stdio.h>
99
#include <sys/types.h>
100
#if TIME_WITH_SYS_TIME
101
# ifdef WIN32
102
#  include <sys/timeb.h>
103
# else
104
#  include <sys/time.h>
105
# endif
106
# include <time.h>
107
#else
108
# if HAVE_SYS_TIME_H
109
#  include <sys/time.h>
110
# else
111
#  include <time.h>
112
# endif
113
#endif
114
#if HAVE_STRING_H
115
#include <string.h>
116
#else
117
#include <strings.h>
118
#endif
119
#if HAVE_NETINET_IN_H
120
#include <netinet/in.h>
121
#endif
122
#if HAVE_UNISTD_H
123
#include <unistd.h>
124
#endif
125
#if HAVE_WINSOCK_H
126
#include <winsock.h>
127
#endif
128
#if HAVE_SYS_SOCKET_H
129
#include <sys/socket.h>
130
#endif
131
#if HAVE_NETDB_H
132
#include <netdb.h>
133
#endif
134
#if HAVE_STDLIB_H
135
#       include <stdlib.h>
136
#endif
137
 
138
#if HAVE_DMALLOC_H
139
#include <dmalloc.h>
140
#endif
141
 
142
#include "system.h"
143
#include "asn1.h"
144
#include "snmpv3.h"
145
#include "callback.h"
146
#include "snmpusm.h"
147
#include "snmp.h"
148
#include "snmp_api.h"
149
#include "snmp_impl.h"
150
#include "read_config.h"
151
#include "lcd_time.h"
152
#include "scapi.h"
153
#include "tools.h"
154
#include "keytools.h"
155
#include "lcd_time.h"
156
#include "snmp_debug.h"
157
#include "snmp_logging.h"
158
#include "default_store.h"
159
 
160
#include "transform_oids.h"
161
 
162
static u_long            engineBoots       = 1;
163
static unsigned char    *engineID          = NULL;
164
static size_t            engineIDLength    = 0;
165
static unsigned char    *oldEngineID       = NULL;
166
static size_t            oldEngineIDLength = 0;
167
static struct timeval    snmpv3starttime;
168
 
169
/*
170
 * Set up default snmpv3 parameter value storage.
171
 */
172
static oid      *defaultAuthType        = NULL;
173
static size_t    defaultAuthTypeLen     = 0;
174
static oid      *defaultPrivType        = NULL;
175
static size_t    defaultPrivTypeLen     = 0;
176
 
177
void
178
snmpv3_authtype_conf(const char *word, char *cptr)
179
{
180
  if (strcasecmp(cptr,"MD5") == 0)
181
    defaultAuthType = usmHMACMD5AuthProtocol;
182
  else if (strcasecmp(cptr,"SHA") == 0)
183
    defaultAuthType = usmHMACMD5AuthProtocol;
184
  else
185
    config_perror("Unknown authentication type");
186
  defaultAuthTypeLen = USM_LENGTH_OID_TRANSFORM;
187
  DEBUGMSGTL(("snmpv3","set default authentication type: %s\n", cptr));
188
}
189
 
190
oid *
191
get_default_authtype(size_t *len)
192
{
193
  if (defaultAuthType == NULL) {
194
    defaultAuthType = SNMP_DEFAULT_AUTH_PROTO;
195
    defaultAuthTypeLen = SNMP_DEFAULT_AUTH_PROTOLEN;
196
  }
197
  if (len)
198
    *len = defaultAuthTypeLen;
199
  return defaultAuthType;
200
}
201
 
202
void
203
snmpv3_privtype_conf(const char *word, char *cptr)
204
{
205
  if (strcasecmp(cptr,"DES") == 0)
206
    defaultPrivType = SNMP_DEFAULT_PRIV_PROTO;
207
  else
208
    config_perror("Unknown privacy type");
209
  defaultPrivTypeLen = SNMP_DEFAULT_PRIV_PROTOLEN;
210
  DEBUGMSGTL(("snmpv3","set default privacy type: %s\n", cptr));
211
}
212
 
213
oid *
214
get_default_privtype(size_t *len)
215
{
216
  if (defaultAuthType == NULL) {
217
    defaultAuthType = usmDESPrivProtocol;
218
    defaultPrivTypeLen = USM_LENGTH_OID_TRANSFORM;
219
  }
220
  if (len)
221
    *len = defaultPrivTypeLen;
222
  return defaultPrivType;
223
}
224
 
225
/*******************************************************************-o-******
226
 * snmpv3_secLevel_conf
227
 *
228
 * Parameters:
229
 *      *word
230
 *      *cptr
231
 *
232
 * Line syntax:
233
 *      defSecurityLevel "noAuthNoPriv" | "authNoPriv" | "authPriv"
234
 */
235
void
236
snmpv3_secLevel_conf(const char *word, char *cptr)
237
{
238
  char buf[1024];
239
 
240
  if (strcasecmp(cptr,"noAuthNoPriv") == 0 || strcmp(cptr, "1") == 0
241
        || strcasecmp(cptr, "nanp") == 0)
242
    ds_set_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL, SNMP_SEC_LEVEL_NOAUTH);
243
  else if (strcasecmp(cptr,"authNoPriv") == 0 || strcmp(cptr, "2") == 0
244
        || strcasecmp(cptr, "anp") == 0)
245
    ds_set_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL, SNMP_SEC_LEVEL_AUTHNOPRIV);
246
  else if (strcasecmp(cptr,"authPriv") == 0 || strcmp(cptr, "3") == 0
247
        || strcasecmp(cptr, "ap") == 0)
248
    ds_set_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL, SNMP_SEC_LEVEL_AUTHPRIV);
249
  else {
250
    sprintf(buf,"Unknown security level: %s", cptr);
251
    config_perror(buf);
252
  }
253
  DEBUGMSGTL(("snmpv3","default secLevel set to: %s = %d\n", cptr,
254
              ds_get_int(DS_LIBRARY_ID, DS_LIB_SECLEVEL)));
255
}
256
 
257
/*******************************************************************-o-******
258
 * setup_engineID
259
 *
260
 * Parameters:
261
 *      **eidp
262
 *       *text  Printable (?) text to be plugged into the snmpEngineID.
263
 *
264
 * Return:
265
 *      Length of allocated engineID string in bytes,  -OR-
266
 *      -1 on error.
267
 *
268
 *
269
 * Create an snmpEngineID using text and the local IP address.  If eidp
270
 * is defined, use it to return a pointer to the newly allocated data.
271
 * Otherwise, use the result to define engineID defined in this module.
272
 *
273
 * Line syntax:
274
 *      engineID <text> | NULL
275
 *
276
 * XXX  What if a node has multiple interfaces?
277
 * XXX  What if multiple engines all choose the same address?
278
 *      (answer:  You're screwed, because you might need a kul database
279
 *       which is dependant on the current engineID.  Enumeration and other
280
 *       tricks won't work).
281
 */
282
int
283
setup_engineID(u_char **eidp, const char *text)
284
{
285
  int             enterpriseid  = htonl(ENTERPRISE_NUMBER),
286
                  localsetup    = (eidp) ? 0 : 1;
287
                        /* Use local engineID if *eidp == NULL.  */
288
#ifdef HAVE_GETHOSTNAME
289
  u_char          buf[SNMP_MAXBUF_SMALL];
290
  struct hostent *hent;
291
#endif
292
  u_char     *bufp = NULL;
293
  size_t          len;
294
 
295
 
296
  /*
297
   * Determine length of the engineID string.
298
   */
299
  if (text) {
300
    len = 5+strlen(text);       /* 5 leading bytes+text. */
301
 
302
  } else {
303
    len = 5 + 4;                /* 5 leading bytes + four byte IPv4 address */
304
#ifdef HAVE_GETHOSTNAME
305
    gethostname((char *)buf, sizeof(buf));
306
    hent = gethostbyname((char *)buf);
307
#ifdef AF_INET6
308
    if (hent && hent->h_addrtype == AF_INET6)
309
      len += 12;                /* 16 bytes total for IPv6 address. */
310
#endif
311
#endif /* HAVE_GETHOSTNAME */
312
  }  /* endif -- text (1) */
313
 
314
 
315
  /*
316
   * Allocate memory and store enterprise ID.
317
   */
318
  if ((bufp = (u_char *) malloc(len)) == NULL) {
319
    snmp_log_perror("setup_engineID malloc");
320
    return -1;
321
  }
322
 
323
  memcpy(bufp, &enterpriseid, sizeof(enterpriseid)); /* XXX Must be 4 bytes! */
324
  bufp[0] |= 0x80;
325
 
326
 
327
  /*
328
   * Store the given text  -OR-   the first found IP address.
329
   */
330
  if (text) {
331
    bufp[4] = 4;
332
    memcpy((char *)bufp+5, text, strlen(text));
333
 
334
  } else {
335
    bufp[4] = 1;
336
#ifdef HAVE_GETHOSTNAME
337
    gethostname((char *)buf, sizeof(buf));
338
    hent = gethostbyname((char *)buf);
339
 
340
    if (hent && hent->h_addrtype == AF_INET) {
341
      memcpy(bufp+5, hent->h_addr_list[0], hent->h_length);
342
 
343
#ifdef AF_INET6
344
    } else if (hent && hent->h_addrtype == AF_INET6) {
345
      bufp[4] = 2;
346
      memcpy(bufp+5, hent->h_addr_list[0], hent->h_length);
347
#endif
348
 
349
    } else {            /* Unknown address type.  Default to 127.0.0.1. */
350
 
351
      bufp[5] = 127;
352
      bufp[6] = 0;
353
      bufp[7] = 0;
354
      bufp[8] = 1;
355
    }
356
#else /* HAVE_GETHOSTNAME */
357
    /* Unknown address type.  Default to 127.0.0.1. */
358
 
359
    bufp[5] = 127;
360
    bufp[6] = 0;
361
    bufp[7] = 0;
362
    bufp[8] = 1;
363
#endif /* HAVE_GETHOSTNAME */
364
 
365
  }  /* endif -- text (2) */
366
 
367
 
368
  /*
369
   * Pass the string back to the calling environment, or use it for
370
   * our local engineID.
371
   */
372
  if (localsetup) {
373
        SNMP_FREE(engineID);
374
        engineID        = bufp;
375
        engineIDLength  = len;
376
 
377
  } else {
378
        *eidp = bufp;
379
  }
380
 
381
 
382
  return len;
383
 
384
}  /* end setup_engineID() */
385
 
386
void
387
usm_parse_create_usmUser(const char *token, char *line) {
388
  char *cp;
389
  char buf[SNMP_MAXBUF_MEDIUM];
390
  struct usmUser *newuser;
391
  u_char          userKey[SNMP_MAXBUF_SMALL];
392
  size_t          userKeyLen = SNMP_MAXBUF_SMALL;
393
  int ret;
394
 
395
  newuser = usm_create_user();
396
 
397
  /* READ: Security Name */
398
  cp = copy_word(line, buf);
399
  newuser->secName = strdup(buf);
400
  newuser->name = strdup(buf);
401
 
402
  newuser->engineID = snmpv3_generate_engineID(&ret);
403
  if ( ret < 0 ) {
404
    usm_free_user(newuser);
405
    return;
406
  }
407
  newuser->engineIDLen = ret;
408
 
409
  if (!cp)
410
    goto add; /* no authentication or privacy type */
411
 
412
  /* READ: Authentication Type */
413
  if (strncmp(cp, "MD5", 3) == 0) {
414
    memcpy(newuser->authProtocol, usmHMACMD5AuthProtocol,
415
           sizeof(usmHMACMD5AuthProtocol));
416
  } else if (strncmp(cp, "SHA", 3) == 0) {
417
    memcpy(newuser->authProtocol, usmHMACSHA1AuthProtocol,
418
           sizeof(usmHMACSHA1AuthProtocol));
419
  } else {
420
    config_perror("Unknown authentication protocol");
421
    usm_free_user(newuser);
422
    return;
423
  }
424
 
425
  cp = skip_token(cp);
426
 
427
  /* READ: Authentication Pass Phrase */
428
  if (!cp) {
429
    config_perror("no authentication pass phrase");
430
    usm_free_user(newuser);
431
    return;
432
  }
433
  cp = copy_word(cp, buf);
434
  /* And turn it into a localized key */
435
  ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen,
436
                    (u_char *)buf, strlen(buf),
437
                    userKey, &userKeyLen );
438
  if (ret != SNMPERR_SUCCESS) {
439
    config_perror("Error generating auth key from pass phrase.");
440
    usm_free_user(newuser);
441
    return;
442
  }
443
  newuser->authKeyLen =
444
    sc_get_properlength(newuser->authProtocol, newuser->authProtocolLen);
445
  newuser->authKey = (u_char *) malloc(newuser->authKeyLen);
446
  ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen,
447
                     newuser->engineID, newuser->engineIDLen,
448
                     userKey, userKeyLen,
449
                     newuser->authKey, &newuser->authKeyLen );
450
  if (ret != SNMPERR_SUCCESS) {
451
    config_perror("Error generating localized auth key (Kul) from Ku.");
452
    usm_free_user(newuser);
453
    return;
454
  }
455
 
456
  if (!cp)
457
    goto add; /* no privacy type (which is legal) */
458
 
459
  /* READ: Privacy Type */
460
  if (strncmp(cp, "DES", 3) == 0) {
461
    memcpy(newuser->privProtocol, usmDESPrivProtocol,
462
           sizeof(usmDESPrivProtocol));
463
  } else {
464
    config_perror("Unknown privacy protocol");
465
    usm_free_user(newuser);
466
    return;
467
  }
468
 
469
  cp = skip_token(cp);
470
  /* READ: Authentication Pass Phrase */
471
  if (!cp) {
472
    /* assume the same as the authentication key */
473
    memdup(&newuser->privKey, newuser->authKey, newuser->authKeyLen);
474
  } else {
475
    cp = copy_word(cp, buf);
476
    /* And turn it into a localized key */
477
    ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen,
478
                      (u_char *)buf, strlen(buf),
479
                      userKey, &userKeyLen );
480
    if (ret != SNMPERR_SUCCESS) {
481
      config_perror("Error generating priv key from pass phrase.");
482
      usm_free_user(newuser);
483
      return;
484
    }
485
 
486
    ret = sc_get_properlength(newuser->authProtocol, newuser->authProtocolLen);
487
    if (ret < 0) {
488
      config_perror("Error getting proper key length for priv algorithm.");
489
      usm_free_user(newuser);
490
      return;
491
    }
492
    newuser->privKeyLen = ret;
493
 
494
    newuser->privKey = (u_char *) malloc(newuser->privKeyLen);
495
    ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen,
496
                       newuser->engineID, newuser->engineIDLen,
497
                       userKey, userKeyLen,
498
                       newuser->privKey, &newuser->privKeyLen );
499
    if (ret != SNMPERR_SUCCESS) {
500
      config_perror("Error generating localized priv key (Kul) from Ku.");
501
      usm_free_user(newuser);
502
      return;
503
    }
504
  }
505
add:
506
  usm_add_user(newuser);
507
  DEBUGMSGTL(("usmUser","created a new user %s\n", newuser->secName));
508
}
509
 
510
/*******************************************************************-o-******
511
 * engineBoots_conf
512
 *
513
 * Parameters:
514
 *      *word
515
 *      *cptr
516
 *
517
 * Line syntax:
518
 *      engineBoots <num_boots>
519
 */
520
void
521
engineBoots_conf(const char *word, char *cptr)
522
{
523
  engineBoots = atoi(cptr)+1;
524
  DEBUGMSGTL(("snmpv3","engineBoots: %d\n",engineBoots));
525
}
526
 
527
 
528
 
529
/*******************************************************************-o-******
530
 * engineID_conf
531
 *
532
 * Parameters:
533
 *      *word
534
 *      *cptr
535
 *
536
 * This function reads a string from the configuration file and uses that
537
 * string to initialize the engineID.  It's assumed to be human readable.
538
 */
539
void
540
engineID_conf(const char *word, char *cptr)
541
{
542
  setup_engineID(NULL, cptr);
543
  DEBUGMSGTL(("snmpv3","initialized engineID with: %s\n",cptr));
544
}
545
 
546
void
547
version_conf(const char *word, char *cptr)
548
{
549
  if (strcmp(cptr,"1") == 0) {
550
    ds_set_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION, SNMP_VERSION_1);
551
  } else if (strcasecmp(cptr,"2c") == 0) {
552
    ds_set_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION, SNMP_VERSION_2c);
553
  } else if (strcmp(cptr,"3") == 0) {
554
    ds_set_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION, SNMP_VERSION_3);
555
  } else {
556
    config_perror("Unknown version specification");
557
    return;
558
  }
559
  DEBUGMSGTL(("snmpv3","set default version to %d\n",
560
              ds_get_int(DS_LIBRARY_ID, DS_LIB_SNMPVERSION)));
561
}
562
 
563
/* engineID_old_conf(const char *, char *):
564
 
565
   Reads a octet string encoded engineID into the oldEngineID and
566
   oldEngineIDLen pointers.
567
*/
568
void
569
oldengineID_conf(const char *word, char *cptr)
570
{
571
  read_config_read_octet_string(cptr, &oldEngineID, &oldEngineIDLength);
572
}
573
 
574
 
575
/*******************************************************************-o-******
576
 * init_snmpv3
577
 *
578
 * Parameters:
579
 *      *type   Label for the config file "type" used by calling entity.
580
 *
581
 * Set time and engineID.
582
 * Set parsing functions for config file tokens.
583
 * Initialize SNMP Crypto API (SCAPI).
584
 */
585
void
586
init_snmpv3(const char *type) {
587
  gettimeofday(&snmpv3starttime, NULL);
588
 
589
  if (type == NULL)
590
     type = "snmpapp";
591
 
592
  if (type && !strcmp(type,"snmpapp")) {
593
     setup_engineID(NULL,"__snmpapp__");
594
  } else {
595
     setup_engineID(NULL, NULL);
596
  }
597
 
598
  /* initialize submodules */
599
  init_usm();
600
 
601
  /* we need to be called back later */
602
  snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_POST_READ_CONFIG,
603
                         init_snmpv3_post_config, NULL);
604
  /* we need to be called back later */
605
  snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
606
                         snmpv3_store, (void *) type);
607
 
608
 
609
#if             !defined(USE_INTERNAL_MD5)
610
  /* doesn't belong here at all */
611
  sc_init();
612
#endif          /* !USE_INTERNAL_MD5 */
613
 
614
  /* register all our configuration handlers (ack, there's a lot) */
615
 
616
  /* handle engineID setup before everything else which may depend on it */
617
  register_premib_handler(type,"engineID", engineID_conf, NULL, "string");
618
  register_premib_handler(type,"oldEngineID", oldengineID_conf, NULL, NULL);
619
  register_config_handler(type,"engineBoots", engineBoots_conf, NULL, NULL);
620
 
621
  /* default store config entries */
622
  ds_register_config(ASN_OCTET_STR, "snmp", "defSecurityName", DS_LIBRARY_ID,
623
                     DS_LIB_SECNAME);
624
  ds_register_config(ASN_OCTET_STR, "snmp", "defContext", DS_LIBRARY_ID,
625
                     DS_LIB_CONTEXT);
626
  ds_register_config(ASN_OCTET_STR, "snmp", "defPassphrase", DS_LIBRARY_ID,
627
                     DS_LIB_PASSPHRASE);
628
  ds_register_config(ASN_OCTET_STR, "snmp", "defAuthPassphrase", DS_LIBRARY_ID,
629
                     DS_LIB_AUTHPASSPHRASE);
630
  ds_register_config(ASN_OCTET_STR, "snmp", "defPrivPassphrase", DS_LIBRARY_ID,
631
                     DS_LIB_PRIVPASSPHRASE);
632
  register_config_handler("snmp","defVersion", version_conf, NULL, "1|2c|3");
633
 
634
  register_config_handler("snmp","defAuthType", snmpv3_authtype_conf, NULL,
635
                          "MD5|SHA");
636
  register_config_handler("snmp","defPrivType", snmpv3_privtype_conf, NULL,
637
                          "DES (currently the only possible value)");
638
  register_config_handler("snmp","defSecurityLevel", snmpv3_secLevel_conf,
639
                          NULL, "noAuthNoPriv|authNoPriv|authPriv");
640
  register_config_handler(type,"userSetAuthPass", usm_set_password, NULL,
641
                          "secname engineIDLen engineID pass");
642
  register_config_handler(type,"userSetPrivPass", usm_set_password, NULL,
643
                          "secname engineIDLen engineID pass");
644
  register_config_handler(type,"userSetAuthKey", usm_set_password, NULL,
645
                          "secname engineIDLen engineID KuLen Ku");
646
  register_config_handler(type,"userSetPrivKey", usm_set_password, NULL,
647
                          "secname engineIDLen engineID KuLen Ku");
648
  register_config_handler(type,"userSetAuthLocalKey", usm_set_password, NULL,
649
                          "secname engineIDLen engineID KulLen Kul");
650
  register_config_handler(type,"userSetPrivLocalKey", usm_set_password, NULL,
651
                          "secname engineIDLen engineID KulLen Kul");
652
}
653
 
654
/*
655
 * initializations for SNMPv3 to be called after the configuration files
656
 * have been read.
657
 */
658
 
659
int
660
init_snmpv3_post_config(int majorid, int minorid, void *serverarg,
661
                        void *clientarg) {
662
 
663
  int engineIDLen;
664
  u_char *c_engineID;
665
 
666
  c_engineID = snmpv3_generate_engineID(&engineIDLen);
667
 
668
  if ( engineIDLen < 0 ) {
669
    /* Somethine went wrong - help! */
670
    return SNMPERR_GENERR;
671
  }
672
 
673
  /* if our engineID has changed at all, the boots record must be set to 1 */
674
  if (engineIDLen != (int)oldEngineIDLength ||
675
      oldEngineID == NULL || c_engineID == NULL ||
676
      memcmp(oldEngineID, c_engineID, engineIDLen) != 0) {
677
    engineBoots = 1;
678
  }
679
 
680
  /* set our local engineTime in the LCD timing cache */
681
  set_enginetime(c_engineID, engineIDLen,
682
                 snmpv3_local_snmpEngineBoots(),
683
                 snmpv3_local_snmpEngineTime(),
684
                 TRUE);
685
 
686
  free(c_engineID);
687
  return SNMPERR_SUCCESS;
688
}
689
 
690
/*******************************************************************-o-******
691
 * store_snmpv3
692
 *
693
 * Parameters:
694
 *      *type
695
 */
696
int
697
snmpv3_store(int majorID, int minorID, void *serverarg, void *clientarg) {
698
  char line[SNMP_MAXBUF_SMALL];
699
  u_char c_engineID[SNMP_MAXBUF_SMALL];
700
  int  engineIDLen;
701
  const char *type = (const char *) clientarg;
702
 
703
  if (type == NULL)  /* should never happen, since the arg is ours */
704
    type = "unknown";
705
 
706
  sprintf(line, "engineBoots %ld", engineBoots);
707
  read_config_store(type, line);
708
 
709
  engineIDLen = snmpv3_get_engineID(c_engineID, SNMP_MAXBUF_SMALL);
710
 
711
  if (engineIDLen) {
712
    /* store the engineID used for this run */
713
    sprintf(line, "oldEngineID ");
714
    read_config_save_octet_string(line+strlen(line), c_engineID,
715
                                  engineIDLen);
716
    read_config_store(type, line);
717
  }
718
  return SNMPERR_SUCCESS;
719
}  /* snmpv3_store() */
720
 
721
u_long
722
snmpv3_local_snmpEngineBoots(void)
723
{
724
  return engineBoots;
725
}
726
 
727
 
728
/*******************************************************************-o-******
729
 * snmpv3_get_engineID
730
 *
731
 * Parameters:
732
 *      *buf
733
 *       buflen
734
 *
735
 * Returns:
736
 *      Length of engineID      On Success
737
 *      SNMPERR_GENERR          Otherwise.
738
 *
739
 *
740
 * Store engineID in buf; return the length.
741
 *
742
 */
743
int
744
snmpv3_get_engineID(u_char *buf, size_t buflen)
745
{
746
  /*
747
   * Sanity check.
748
   */
749
  if ( !buf || (buflen < engineIDLength) ) {
750
    return SNMPERR_GENERR;
751
  }
752
 
753
  memcpy(buf,engineID,engineIDLength);
754
  return engineIDLength;
755
 
756
}  /* end snmpv3_get_engineID() */
757
 
758
/*******************************************************************-o-******
759
 * snmpv3_clone_engineID
760
 *
761
 * Parameters:
762
 *      **dest
763
 *       *dest_len
764
 *       src
765
 *       srclen
766
 *
767
 * Returns:
768
 *      Length of engineID      On Success
769
 *      0                        Otherwise.
770
 *
771
 *
772
 * Clones engineID, creates memory
773
 *
774
 */
775
int
776
snmpv3_clone_engineID(u_char **dest, size_t* destlen, u_char*src, size_t srclen)
777
{
778
  if ( !dest || !destlen ) return 0;
779
 
780
  *dest = NULL; *destlen = 0;
781
 
782
  if (srclen && src) {
783
    *dest = (u_char*)malloc((unsigned)srclen * sizeof(u_char));
784
    if (*dest == NULL) return 0;
785
    memmove(*dest, src, srclen * sizeof(u_char));
786
    *destlen = srclen;
787
  }
788
  return *destlen;
789
}  /* end snmpv3_clone_engineID() */
790
 
791
 
792
/*******************************************************************-o-******
793
 * snmpv3_generate_engineID
794
 *
795
 * Parameters:
796
 *      *length
797
 *
798
 * Returns:
799
 *      Pointer to copy of engineID     On Success.
800
 *      NULL                            If malloc() or snmpv3_get_engineID()
801
 *                                              fail.
802
 *
803
 * Generates a malloced copy of our engineID.
804
 *
805
 * 'length' is set to the length of engineID  -OR-  < 0 on failure.
806
 */
807
u_char *
808
snmpv3_generate_engineID(int *length)
809
{
810
  u_char *newID;
811
  newID = (u_char *) malloc(engineIDLength);
812
 
813
  if (newID) {
814
    *length = snmpv3_get_engineID(newID, engineIDLength);
815
  }
816
 
817
  if (*length < 0) {
818
    SNMP_FREE(newID);
819
    newID = NULL;
820
  }
821
 
822
  return newID;
823
 
824
}  /* end snmpv3_generate_engineID() */
825
 
826
/* snmpv3_local_snmpEngineTime(): return the number of seconds since the
827
   snmpv3 engine last incremented engine_boots */
828
u_long
829
snmpv3_local_snmpEngineTime(void)
830
{
831
  struct timeval now;
832
 
833
  gettimeofday(&now, NULL);
834
  return calculate_time_diff(&now, &snmpv3starttime)/100;
835
}
836
 
837
#ifdef SNMP_TESTING_CODE
838
/* snmpv3_set_engineBootsAndTime(): this function does not exist.  Go away. */
839
/*   It certainly should never be used, unless in a testing scenero,
840
     which is why it was created */
841
void
842
snmpv3_set_engineBootsAndTime(int boots, int ttime) {
843
  engineBoots = boots;
844
  gettimeofday(&snmpv3starttime, NULL);
845
  snmpv3starttime.tv_sec -= ttime;
846
}
847
#endif
848
 
849
#endif /* CYGPKG_SNMPAGENT_V3_SUPPORT */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.