| 1 |
|
| 2 |
/* |
| 3 |
* Note: this file originally auto-generated by mib2c using |
| 4 |
* : mib2c.access_functions.conf,v 1.3 2003/05/31 00:11:57 hardaker Exp $ |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <net-snmp/net-snmp-config.h> |
| 8 |
#include <net-snmp/net-snmp-includes.h> |
| 9 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 10 |
#include "netSnmpHostsTable_access.h" |
| 11 |
#include "netSnmpHostsTable_enums.h" |
| 12 |
#include <netinet/in.h> |
| 13 |
#include <arpa/inet.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
#define MAX_HOSTS_LINE 4096 |
| 18 |
|
| 19 |
/* XXX: make .conf token */ |
| 20 |
#define HOSTS_FILE "/etc/hosts" |
| 21 |
|
| 22 |
typedef struct my_loop_info_s { |
| 23 |
FILE *filep; |
| 24 |
in_addr_t theaddr; |
| 25 |
char line[MAX_HOSTS_LINE]; |
| 26 |
char hostname[64]; |
| 27 |
int lineno; |
| 28 |
char *current_ptr; |
| 29 |
} my_loop_info; |
| 30 |
|
| 31 |
typedef struct my_data_info_s { |
| 32 |
in_addr_t theaddr; |
| 33 |
in_addr_t theoldaddr; |
| 34 |
char hostname[64]; |
| 35 |
int lineno; |
| 36 |
} my_data_info; |
| 37 |
|
| 38 |
/** NOTE: |
| 39 |
* - these get_ routines MUST return data that will not be freed (ie, |
| 40 |
* use static variables or persistent data). It will be copied, if |
| 41 |
* needed, immediately after the get_ routine has been called. |
| 42 |
* - these SET routines must copy the incoming data and can not take |
| 43 |
* ownership of the memory passed in by the val pointer. |
| 44 |
*/ |
| 45 |
|
| 46 |
|
| 47 |
/** returns the first data point within the netSnmpHostsTable table data. |
| 48 |
|
| 49 |
Set the my_loop_context variable to the first data point structure |
| 50 |
of your choice (from which you can find the next one). This could |
| 51 |
be anything from the first node in a linked list, to an integer |
| 52 |
pointer containing the beginning of an array variable. |
| 53 |
|
| 54 |
Set the my_data_context variable to something to be returned to |
| 55 |
you later that will provide you with the data to return in a given |
| 56 |
row. This could be the same pointer as what my_loop_context is |
| 57 |
set to, or something different. |
| 58 |
|
| 59 |
The put_index_data variable contains a list of snmp variable |
| 60 |
bindings, one for each index in your table. Set the values of |
| 61 |
each appropriately according to the data matching the first row |
| 62 |
and return the put_index_data variable at the end of the function. |
| 63 |
*/ |
| 64 |
netsnmp_variable_list * |
| 65 |
netSnmpHostsTable_get_first_data_point(void **my_loop_context, |
| 66 |
void **my_data_context, |
| 67 |
netsnmp_variable_list * |
| 68 |
put_index_data, |
| 69 |
netsnmp_iterator_info *mydata) |
| 70 |
{ |
| 71 |
my_loop_info *loopctx; |
| 72 |
|
| 73 |
loopctx = SNMP_MALLOC_TYPEDEF(my_loop_info); |
| 74 |
|
| 75 |
if (!loopctx) |
| 76 |
return NULL; /*XXX log err */ |
| 77 |
|
| 78 |
loopctx->filep = fopen("/etc/hosts","r"); |
| 79 |
|
| 80 |
if (!loopctx->filep) { |
| 81 |
free(loopctx); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
/* at this point, we need to get the first name and address from |
| 86 |
the file. But since our get_next_data_point function does |
| 87 |
this, we'll use it instead of duplicating code */ |
| 88 |
*my_loop_context = loopctx; |
| 89 |
|
| 90 |
return netSnmpHostsTable_get_next_data_point(my_loop_context, |
| 91 |
my_data_context, |
| 92 |
put_index_data, |
| 93 |
mydata); |
| 94 |
} |
| 95 |
|
| 96 |
/** functionally the same as netSnmpHostsTable_get_first_data_point, but |
| 97 |
my_loop_context has already been set to a previous value and should |
| 98 |
be updated to the next in the list. For example, if it was a |
| 99 |
linked list, you might want to cast it to your local data type and |
| 100 |
then return my_loop_context->next. The my_data_context pointer |
| 101 |
should be set to something you need later and the indexes in |
| 102 |
put_index_data updated again. */ |
| 103 |
netsnmp_variable_list * |
| 104 |
netSnmpHostsTable_get_next_data_point(void **my_loop_context, |
| 105 |
void **my_data_context, |
| 106 |
netsnmp_variable_list * |
| 107 |
put_index_data, |
| 108 |
netsnmp_iterator_info *mydata) |
| 109 |
{ |
| 110 |
my_loop_info *loopctx = *my_loop_context; |
| 111 |
char tmpstring[64]; |
| 112 |
|
| 113 |
if (!loopctx) |
| 114 |
return NULL; |
| 115 |
|
| 116 |
while(loopctx->filep) { |
| 117 |
if (!loopctx->current_ptr) { |
| 118 |
if (!fgets(loopctx->line, sizeof(loopctx->line), loopctx->filep)) { |
| 119 |
/* we're done */ |
| 120 |
fclose(loopctx->filep); |
| 121 |
loopctx->filep = NULL; |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
loopctx->lineno++; |
| 125 |
loopctx->current_ptr = loopctx->line; |
| 126 |
loopctx->current_ptr = skip_white(loopctx->current_ptr); |
| 127 |
|
| 128 |
if (loopctx->current_ptr == NULL || *loopctx->current_ptr == '#') { |
| 129 |
loopctx->current_ptr = NULL; |
| 130 |
continue; |
| 131 |
} |
| 132 |
|
| 133 |
loopctx->current_ptr = |
| 134 |
copy_nword(loopctx->current_ptr, tmpstring, sizeof(tmpstring)); |
| 135 |
loopctx->theaddr = inet_addr(tmpstring); |
| 136 |
|
| 137 |
if (!loopctx->current_ptr) |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
loopctx->current_ptr = |
| 142 |
copy_nword(loopctx->current_ptr, loopctx->hostname, sizeof(loopctx->hostname)); |
| 143 |
|
| 144 |
snmp_set_var_value(put_index_data, (u_char *) loopctx->hostname, |
| 145 |
strlen(loopctx->hostname)); |
| 146 |
return put_index_data; |
| 147 |
} |
| 148 |
|
| 149 |
/* we're out of data */ |
| 150 |
*my_loop_context = NULL; |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
void * |
| 155 |
netSnmpHostsTable_context_convert_function(void *loop_context, |
| 156 |
netsnmp_iterator_info *iinfo) |
| 157 |
{ |
| 158 |
my_loop_info *loopctx = loop_context; |
| 159 |
my_data_info *datactx = SNMP_MALLOC_TYPEDEF(my_data_info); |
| 160 |
if (!datactx) |
| 161 |
return NULL; |
| 162 |
datactx->theoldaddr = datactx->theaddr = loopctx->theaddr; |
| 163 |
datactx->lineno = loopctx->lineno; |
| 164 |
strcpy(datactx->hostname, loopctx->hostname); |
| 165 |
return datactx; |
| 166 |
} |
| 167 |
|
| 168 |
/** Create a data_context for non-existent rows that SETs are performed on. |
| 169 |
* return a void * pointer which will be passed to subsequent get_XXX |
| 170 |
* and set_XXX functions for data retrieval and modification during |
| 171 |
* this SET request. |
| 172 |
* |
| 173 |
* The indexs are encoded (in order) into the index_data pointer if it |
| 174 |
* would be helpful to use that information. |
| 175 |
*/ |
| 176 |
void * |
| 177 |
netSnmpHostsTable_create_data_context(netsnmp_variable_list * index_data) |
| 178 |
{ |
| 179 |
my_data_info *datactx = SNMP_MALLOC_TYPEDEF(my_data_info); |
| 180 |
|
| 181 |
if (!datactx) |
| 182 |
return NULL; |
| 183 |
strlcpy(datactx->hostname, (const char *) index_data->val.string, |
| 184 |
sizeof(datactx->hostname)); |
| 185 |
return datactx; |
| 186 |
} |
| 187 |
|
| 188 |
void |
| 189 |
netSnmpHostsTable_data_free(void *data, netsnmp_iterator_info *iinfo) |
| 190 |
{ |
| 191 |
free(data); |
| 192 |
} |
| 193 |
|
| 194 |
void |
| 195 |
netSnmpHostsTable_loop_free(void *loopctx, netsnmp_iterator_info *iinfo) |
| 196 |
{ |
| 197 |
free(loopctx); |
| 198 |
} |
| 199 |
|
| 200 |
/** If the implemented set_* functions don't operate directly on the |
| 201 |
real-live data (which is actually recommended), then this function |
| 202 |
can be used to take a given my_data_context pointer and "commit" it |
| 203 |
to whereever the modified data needs to be put back to. For |
| 204 |
example, if this was a routing table you could publish the modified |
| 205 |
routes back into the kernel at this point. |
| 206 |
|
| 207 |
rowStatus will be set to 1 if new, 0 if not or -1 if it should |
| 208 |
be deleted. |
| 209 |
|
| 210 |
If you free the data yourself, make sure to *my_data_context = NULL */ |
| 211 |
int |
| 212 |
netSnmpHostsTable_commit_row(void **my_data_context, int new_or_del) |
| 213 |
{ |
| 214 |
/** Add any necessary commit code here */ |
| 215 |
FILE *in, *out; |
| 216 |
char line[MAX_HOSTS_LINE], line2[MAX_HOSTS_LINE]; |
| 217 |
char myaddr[64], *cp; |
| 218 |
my_data_info *datactx = *my_data_context; |
| 219 |
size_t line2_sz; |
| 220 |
int foundit = 0; |
| 221 |
|
| 222 |
if (datactx->theaddr == datactx->theoldaddr && new_or_del != -1) |
| 223 |
return SNMP_ERR_NOERROR; /* no change in the value */ |
| 224 |
|
| 225 |
if ((out = fopen(HOSTS_FILE ".snmp", "w")) == NULL) |
| 226 |
return SNMP_ERR_COMMITFAILED; |
| 227 |
|
| 228 |
if ((in = fopen(HOSTS_FILE, "r")) == NULL) { |
| 229 |
fclose(out); |
| 230 |
return SNMP_ERR_COMMITFAILED; |
| 231 |
} |
| 232 |
|
| 233 |
while(fgets(line, sizeof(line), in)) { |
| 234 |
copy_nword(line,myaddr,sizeof(myaddr)); |
| 235 |
if (inet_addr(myaddr) == datactx->theaddr && new_or_del != -1) { |
| 236 |
foundit = 1; |
| 237 |
/* right line to append to */ |
| 238 |
line[strlen(line)-1] = '\0'; /* nuke the new line */ |
| 239 |
fprintf(out, "%s %s\n", line, datactx->hostname); |
| 240 |
} else if (inet_addr(myaddr) == datactx->theoldaddr) { |
| 241 |
/* find and remove the name from the current line */ |
| 242 |
int count = 0; |
| 243 |
cp = copy_nword(line, line2, sizeof(line2)); /* pass the addr */ |
| 244 |
if (strlen(line2) > sizeof(line2)-2) { |
| 245 |
errorit: |
| 246 |
fclose(in); |
| 247 |
fclose(out); |
| 248 |
unlink(HOSTS_FILE ".snmp"); |
| 249 |
return SNMP_ERR_RESOURCEUNAVAILABLE; |
| 250 |
} |
| 251 |
line2_sz = strlen(line2); |
| 252 |
line2[line2_sz++] = '\t'; |
| 253 |
while(cp) { |
| 254 |
cp = copy_nword(cp, &line2[line2_sz], sizeof(line2)-line2_sz); |
| 255 |
if (strcmp(&line2[line2_sz], datactx->hostname) == 0) { |
| 256 |
/* a match, so don't add it to line2 (which means |
| 257 |
don't update the write line2_sz index */ |
| 258 |
} else { |
| 259 |
if (strlen(line2) > sizeof(line2)-2) { |
| 260 |
goto errorit; |
| 261 |
} |
| 262 |
line2_sz = strlen(line2); |
| 263 |
line2[line2_sz++] = ' '; |
| 264 |
count++; |
| 265 |
} |
| 266 |
} |
| 267 |
if (count) { |
| 268 |
/* at least one name was still present on the line, so |
| 269 |
save it to the new file */ |
| 270 |
line2[line2_sz] = '\0'; |
| 271 |
fprintf(out, "%s\n", line2); |
| 272 |
} |
| 273 |
} else { |
| 274 |
fputs(line, out); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (!foundit && new_or_del != -1) { |
| 279 |
/* couldn't add it to an existing line, so append a new one */ |
| 280 |
fprintf(out, "%d.%d.%d.%d\t%s\n", |
| 281 |
(0x000000ff & datactx->theaddr), |
| 282 |
(0x0000ff00 & datactx->theaddr) >> 8, |
| 283 |
(0x00ff0000 & datactx->theaddr) >> 16, |
| 284 |
(0xff000000 & datactx->theaddr) >> 24, |
| 285 |
datactx->hostname); |
| 286 |
} |
| 287 |
fclose(out); /* close out first to minimize race condition */ |
| 288 |
fclose(in); |
| 289 |
/* |
| 290 |
* race condition here - someone else could open the file after |
| 291 |
* we close it but before we can rename it. |
| 292 |
*/ |
| 293 |
if (!rename(HOSTS_FILE ".snmp", HOSTS_FILE)) |
| 294 |
return SNMP_ERR_COMMITFAILED; |
| 295 |
|
| 296 |
/* |
| 297 |
* return no errors. And there shouldn't be any!!! Ever!!! You |
| 298 |
* should have checked the values long before this. |
| 299 |
*/ |
| 300 |
return SNMP_ERR_NOERROR; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
/* |
| 305 |
* User-defined data access functions (per column) for data in table |
| 306 |
* netSnmpHostsTable |
| 307 |
*/ |
| 308 |
|
| 309 |
|
| 310 |
long * |
| 311 |
get_netSnmpHostAddressType(void *data_context, size_t * ret_len) |
| 312 |
{ |
| 313 |
static long ret = NETSNMPHOSTADDRESSTYPE_IPV4; |
| 314 |
*ret_len = sizeof(ret); |
| 315 |
return &ret; |
| 316 |
} |
| 317 |
|
| 318 |
int |
| 319 |
set_netSnmpHostAddressType(void *data_context, long *val, size_t val_len) |
| 320 |
{ |
| 321 |
return SNMP_ERR_NOERROR; /* always ipv4 */ |
| 322 |
} |
| 323 |
|
| 324 |
char * |
| 325 |
get_netSnmpHostAddress(void *data_context, size_t * ret_len) |
| 326 |
{ |
| 327 |
my_data_info *datainfo = data_context; |
| 328 |
*ret_len = sizeof(in_addr_t); /* XXX: make sure it's 4 */ |
| 329 |
return (char *) &datainfo->theaddr; |
| 330 |
} |
| 331 |
|
| 332 |
int |
| 333 |
set_netSnmpHostAddress(void *data_context, char *val, size_t val_len) |
| 334 |
{ |
| 335 |
my_data_info *datainfo = data_context; |
| 336 |
memcpy(&datainfo->theaddr, val, val_len); |
| 337 |
return SNMP_ERR_NOERROR; |
| 338 |
} |
| 339 |
|
| 340 |
long * |
| 341 |
get_netSnmpHostStorage(void *data_context, size_t * ret_len) |
| 342 |
{ |
| 343 |
static long ret = ST_NONVOLATILE; |
| 344 |
*ret_len = sizeof(ret); |
| 345 |
return &ret; |
| 346 |
} |
| 347 |
|
| 348 |
int |
| 349 |
set_netSnmpHostStorage(void *data_context, long *val, size_t val_len) |
| 350 |
{ |
| 351 |
return SNMP_ERR_NOERROR; |
| 352 |
} |
| 353 |
|
| 354 |
long * |
| 355 |
get_netSnmpHostRowStatus(void *data_context, size_t * ret_len) |
| 356 |
{ |
| 357 |
static long ret = RS_ACTIVE; |
| 358 |
*ret_len = sizeof(ret); |
| 359 |
return &ret; |
| 360 |
} |
| 361 |
|
| 362 |
int |
| 363 |
set_netSnmpHostRowStatus(void *data_context, long *val, size_t val_len) |
| 364 |
{ |
| 365 |
/* XXX */ |
| 366 |
return SNMP_ERR_NOERROR; |
| 367 |
} |