summaryrefslogtreecommitdiff
path: root/ntpsnmpd/ntpSnmpSubagentObject.c
blob: f65fedf600de221546ce1e532684265f3f14f8bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*****************************************************************************
 *
 *  ntpSnmpSubAgentObject.c
 *
 *  This file provides the callback functions for net-snmp and registers the 
 *  serviced MIB objects with the master agent.
 * 
 *  Each object has its own callback function that is called by the 
 *  master agent process whenever someone queries the corresponding MIB
 *  object. 
 * 
 *  At the moment this triggers a full send/receive procedure for each
 *  queried MIB object, one of the things that are still on my todo list:
 *  a caching mechanism that reduces the number of requests sent to the
 *  ntpd process.
 *
 ****************************************************************************/
#include <ntp_snmp.h>
#include <ctype.h>
#include <ntp.h>
#include <libntpq.h>

/* general purpose buffer length definition */
#define NTPQ_BUFLEN 2048

char ntpvalue[NTPQ_BUFLEN];


/*****************************************************************************
 *
 * ntpsnmpd_parse_string
 *
 *  This function will parse a given NULL terminated string and cut it
 *  into a fieldname and a value part (using the '=' as the delimiter. 
 *  The fieldname will be converted to uppercase and all whitespace 
 *  characters are removed from it.
 *  The value part is stripped, e.g. all whitespace characters are removed
 *  from the beginning and end of the string.
 *  If the value is started and ended with quotes ("), they will be removed
 *  and everything between the quotes is left untouched (including 
 *  whitespace)
 *  Example:
 *     server host name =   hello world!
 *  will result in a field string "SERVERHOSTNAME" and a value
 *  of "hello world!".
 *     My first Parameter		=		"  is this!    "
  * results in a field string "MYFIRSTPARAMETER" and a value " is this!    "
 ****************************************************************************
 * Parameters:
 *	string		const char *	The source string to parse.
 *					NOTE: must be NULL terminated!
 *	field		char *		The buffer for the field name.
 *	fieldsize	size_t		The size of the field buffer.
 *	value		char *		The buffer for the value.
 *	valuesize	size_t		The size of the value buffer.
 *
 * Returns:
 *	size_t			length of value string 
 ****************************************************************************/

size_t
ntpsnmpd_parse_string(
	const char *	string,
	char *		field,
	size_t		fieldsize,
	char *		value,
	size_t		valuesize
	)
{
	int i;
	int j;
	int loop;
	size_t str_cnt;
	size_t val_cnt;

	/* we need at least one byte to work with to simplify */
	if (fieldsize < 1 || valuesize < 1)
		return 0;

	str_cnt = strlen(string);

	/* Parsing the field name */
	j = 0;
	loop = TRUE;
	for (i = 0; loop && i <= str_cnt; i++) {
		switch (string[i]) {

		case '\t': 	/* Tab */
		case '\n':	/* LF */
		case '\r':	/* CR */
		case ' ':  	/* Space */
			break;

		case '=':
			loop = FALSE;
			break;

		default:
			if (j < fieldsize)
				field[j++] = toupper(string[i]);
		}
	}

	j = min(j, fieldsize - 1);
	field[j] = '\0';

	/* Now parsing the value */
	value[0] = '\0';
	j = 0; 
	for (val_cnt = 0; i < str_cnt; i++) {
		if (string[i] > 0x0D && string[i] != ' ')
			val_cnt = min(j + 1, valuesize - 1);
		
		if (value[0] != '\0' ||
		    (string[i] > 0x0D && string[i] != ' ')) {
			if (j < valuesize)
				value[j++] = string[i];
		}
	}
	value[val_cnt] = '\0';

	if (value[0] == '"') {
		val_cnt--;
		strlcpy(value, &value[1], valuesize);
		if (val_cnt > 0 && value[val_cnt - 1] == '"') {
			val_cnt--;
			value[val_cnt] = '\0';
		}
	}

	return val_cnt;
}


/*****************************************************************************
 *
 * ntpsnmpd_cut_string
 *
 *  This function will parse a given NULL terminated string and cut it
 *  into fields using the specified delimiter character. 
 *  It will then copy the requested field into a destination buffer
 *  Example:
 *     ntpsnmpd_cut_string(read:my:lips:fool, RESULT, ':', 2, sizeof(RESULT))
 *  will copy "lips" to RESULT.
 ****************************************************************************
 * Parameters:
 *	src		const char *	The name of the source string variable
 *					NOTE: must be NULL terminated!
 *	dest		char *		The name of the string which takes the
 *					requested field content
 * 	delim		char		The delimiter character
 *	fieldnumber	int		The number of the required field
 *					(start counting with 0)
 *	maxsize		size_t		The maximum size of dest
 *
 * Returns:
 *	size_t		length of resulting dest string 
 ****************************************************************************/

size_t
ntpsnmpd_cut_string(
	const char *	string,
	char *		dest,
	char		delim,
	int		fieldnumber,
	size_t		maxsize
	)
{
	size_t i;
	size_t j;
	int l;
	size_t str_cnt;

	if (maxsize < 1)
		return 0;

	str_cnt = strlen(string);
	j = 0;
	memset(dest, 0, maxsize);

	/* Parsing the field name */
	for (i = 0, l = 0; i < str_cnt && l <= fieldnumber; i++) {
		if (string[i] == delim)
			l++;	/* next field */
		else if (l == fieldnumber && j < maxsize)
			dest[j++] = string[i]; 
	}
	j = min(j, maxsize - 1);
	dest[j] = '\0';

	return j;
}


/*****************************************************************************
 *
 *  read_ntp_value
 *
 *  This function retrieves the value for a given variable, currently
 *  this only supports sysvars. It starts a full mode 6 send/receive/parse
 *  iteration and needs to be optimized, e.g. by using a caching mechanism
 *  
 ****************************************************************************
 * Parameters:
 *	variable	char*	The name of the required variable
 *	rbuffer		char*	The buffer where the value goes
 *	maxlength	int	Max. number of bytes for resultbuf
 *
 * Returns:
 *	u_int		number of chars that have been copied to 
 *			rbuffer 
 ****************************************************************************/

size_t
read_ntp_value(
	const char *	variable,
	char *		value,
	size_t		valuesize
	)
{
	size_t	sv_len;
	char	sv_data[NTPQ_BUFLEN];
	
	memset(sv_data, 0, sizeof(sv_data));
	sv_len = ntpq_read_sysvars(sv_data, sizeof(sv_data));

	if (0 == sv_len)
		return 0;
	else
		return ntpq_getvar(sv_data, sv_len, variable, value,
				   valuesize);
}


/*****************************************************************************
 *
 *  The get_xxx functions
 *
 *  The following function calls are callback functions that will be 
 *  used by the master agent process to retrieve a value for a requested 
 *  MIB object. 
 *
 ****************************************************************************/


int get_ntpEntSoftwareName (netsnmp_mib_handler *handler,
                               netsnmp_handler_registration *reginfo,
                               netsnmp_agent_request_info *reqinfo,
                               netsnmp_request_info *requests)
{
 char ntp_softwarename[NTPQ_BUFLEN];
	
   memset (ntp_softwarename, 0, NTPQ_BUFLEN);
	
   switch (reqinfo->mode) {
   case MODE_GET:
   {
	if ( read_ntp_value("product", ntpvalue, NTPQ_BUFLEN) )
       {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntpvalue,
                             strlen(ntpvalue)
                            );
       } 
    else  if ( read_ntp_value("version", ntpvalue, NTPQ_BUFLEN) )
    {
	ntpsnmpd_cut_string(ntpvalue, ntp_softwarename, ' ', 0, sizeof(ntp_softwarename)-1);
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntp_softwarename,
                             strlen(ntp_softwarename)
                            );
    } else {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)"N/A",
                             3
                            );
    }
    break;
    
  }


  default:
	  /* If we cannot get the information we need, we will return a generic error to the SNMP client */
        return SNMP_ERR_GENERR;
  }

  return SNMP_ERR_NOERROR;
}


int get_ntpEntSoftwareVersion (netsnmp_mib_handler *handler,
                               netsnmp_handler_registration *reginfo,
                               netsnmp_agent_request_info *reqinfo,
                               netsnmp_request_info *requests)
{

   switch (reqinfo->mode) {
   case MODE_GET:
   {
    
    if ( read_ntp_value("version", ntpvalue, NTPQ_BUFLEN) )
    {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntpvalue,
                             strlen(ntpvalue)
                            );
    } else {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)"N/A",
                             3
                            );
    }
    break;
    
  }


  default:
	  /* If we cannot get the information we need, we will return a generic error to the SNMP client */
        return SNMP_ERR_GENERR;
  }

  return SNMP_ERR_NOERROR;
}


int get_ntpEntSoftwareVendor (netsnmp_mib_handler *handler,
                               netsnmp_handler_registration *reginfo,
                               netsnmp_agent_request_info *reqinfo,
                               netsnmp_request_info *requests)
{

   switch (reqinfo->mode) {
   case MODE_GET:
   {
    
    if ( read_ntp_value("vendor", ntpvalue, NTPQ_BUFLEN) )
    {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntpvalue,
                             strlen(ntpvalue)
                            );
    } else {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)"N/A",
                             3
                            );
    }
    break;

  default:
	  /* If we cannot get the information we need, we will return a generic error to the SNMP client */
        return SNMP_ERR_GENERR;
   }
  }
  return SNMP_ERR_NOERROR;
}


int get_ntpEntSystemType (netsnmp_mib_handler *handler,
                               netsnmp_handler_registration *reginfo,
                               netsnmp_agent_request_info *reqinfo,
                               netsnmp_request_info *requests)
{

   switch (reqinfo->mode) {
   case MODE_GET:
   {
    
    if ( read_ntp_value("systemtype", ntpvalue, NTPQ_BUFLEN) )
    {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntpvalue,
                             strlen(ntpvalue)
                            );
    }
	   
    if ( read_ntp_value("system", ntpvalue, NTPQ_BUFLEN) )
    {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntpvalue,
                             strlen(ntpvalue)
                            );
    } else {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)"N/A",
                             3
                            );
    }
    break;
    
  }


  default:
	  /* If we cannot get the information we need, we will return a generic error to the SNMP client */
        return SNMP_ERR_GENERR;
  }

  return SNMP_ERR_NOERROR;
}


/*
 * ntpEntTimeResolution
 *	"The time resolution in integer format, where the resolution
 *	 is represented as divisions of a second, e.g., a value of 1000
 *	 translates to 1.0 ms."
 *
 * ntpEntTimeResolution is a challenge for ntpd, as the resolution is
 * not known nor exposed by ntpd, only the measured precision (time to
 * read the clock).
 *
 * Logically the resolution must be at least the precision, so report
 * it as our best approximation of resolution until/unless ntpd provides
 * better.
 */
int
get_ntpEntTimeResolution(
	netsnmp_mib_handler *		handler,
	netsnmp_handler_registration *	reginfo,
	netsnmp_agent_request_info *	reqinfo,
	netsnmp_request_info *		requests
	)
{
	int	precision;
	u_int32 resolution;

	switch (reqinfo->mode) {

	case MODE_GET:
		if (!read_ntp_value("precision", ntpvalue,
				    sizeof(ntpvalue)))
			return SNMP_ERR_GENERR;
		if (1 != sscanf(ntpvalue, "%d", &precision))
			return SNMP_ERR_GENERR;
		if (precision >= 0)
			return SNMP_ERR_GENERR;
		precision = max(precision, -31);
		resolution = 1 << -precision;
		snmp_set_var_typed_value(
			requests->requestvb,
			ASN_UNSIGNED,
			(void *)&resolution,
			sizeof(resolution));
		break;

	default:
		return SNMP_ERR_GENERR;
	}

	return SNMP_ERR_NOERROR;
}


/*
 * ntpEntTimePrecision
 *	"The entity's precision in integer format, shows the precision.
 *	 A value of -5 would mean 2^-5 = 31.25 ms."
 */
int 
get_ntpEntTimePrecision(
	netsnmp_mib_handler *		handler,
	netsnmp_handler_registration *	reginfo,
	netsnmp_agent_request_info *	reqinfo,
	netsnmp_request_info *		requests
	)
{
	int	precision;
	int32	precision32;

	switch (reqinfo->mode) {

	case MODE_GET:
		if (!read_ntp_value("precision", ntpvalue, 
				    sizeof(ntpvalue)))
			return SNMP_ERR_GENERR;
		if (1 != sscanf(ntpvalue, "%d", &precision))
			return SNMP_ERR_GENERR;
		precision32 = (int32)precision;
		snmp_set_var_typed_value(
			requests->requestvb,
			ASN_INTEGER,
			(void *)&precision32,
			sizeof(precision32));
		break;

	default:
		return SNMP_ERR_GENERR;
	}

	return SNMP_ERR_NOERROR;
}


int get_ntpEntTimeDistance (netsnmp_mib_handler *handler,
                               netsnmp_handler_registration *reginfo,
                               netsnmp_agent_request_info *reqinfo,
                               netsnmp_request_info *requests)
{
   switch (reqinfo->mode) {
   case MODE_GET:
   {
    
    if ( read_ntp_value("rootdelay", ntpvalue, NTPQ_BUFLEN) )
    {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)ntpvalue,
                             strlen(ntpvalue)
                            );
    } else {
	snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                             (u_char *)"N/A",
                             3
                            );
    }
    break;
    
  }


  default:
	  /* If we cannot get the information we need, we will return a generic error to the SNMP client */
        return SNMP_ERR_GENERR;
  }

  return SNMP_ERR_NOERROR;
}


/*
 *
 * Initialize sub agent
 */

void
init_ntpSnmpSubagentObject(void)
{
	/* Register all MIB objects with the agentx master */
	NTP_OID_RO( ntpEntSoftwareName,		1, 1, 1, 0);
	NTP_OID_RO( ntpEntSoftwareVersion,	1, 1, 2, 0);
	NTP_OID_RO( ntpEntSoftwareVendor,	1, 1, 3, 0);
	NTP_OID_RO( ntpEntSystemType,		1, 1, 4, 0);
	NTP_OID_RO( ntpEntTimeResolution,	1, 1, 5, 0);
	NTP_OID_RO( ntpEntTimePrecision,	1, 1, 6, 0);
	NTP_OID_RO( ntpEntTimeDistance,		1, 1, 7, 0);
}