| 1 | /* |
|---|
| 2 | * snmp.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2006 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License, or (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #ifdef HAVE_SNMP |
|---|
| 24 | |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <signal.h> |
|---|
| 28 | #include <stdio.h> |
|---|
| 29 | #include <sys/types.h> |
|---|
| 30 | #include <bcmnvram.h> |
|---|
| 31 | #include <shutils.h> |
|---|
| 32 | #include <utils.h> |
|---|
| 33 | #include <syslog.h> |
|---|
| 34 | #include "snmp.h" |
|---|
| 35 | |
|---|
| 36 | #define SNMP_CONF_FILE "/var/snmp/snmpd.conf" |
|---|
| 37 | |
|---|
| 38 | void start_snmp(void) |
|---|
| 39 | { |
|---|
| 40 | int ret = 0; |
|---|
| 41 | pid_t pid; |
|---|
| 42 | |
|---|
| 43 | char *snmpd_argv[] = { "snmpd", "-c", SNMP_CONF_FILE, NULL }; |
|---|
| 44 | FILE *fp = NULL; |
|---|
| 45 | |
|---|
| 46 | stop_snmp(); |
|---|
| 47 | |
|---|
| 48 | if (!nvram_invmatch("snmpd_enable", "0")) |
|---|
| 49 | return; |
|---|
| 50 | |
|---|
| 51 | fp = fopen(SNMP_CONF_FILE, "w"); |
|---|
| 52 | if (NULL == fp) |
|---|
| 53 | return; |
|---|
| 54 | |
|---|
| 55 | if (strlen(nvram_safe_get("snmpd_syslocation")) > 0) |
|---|
| 56 | fprintf(fp, "syslocation %s\n", |
|---|
| 57 | nvram_safe_get("snmpd_syslocation")); |
|---|
| 58 | if (strlen(nvram_safe_get("snmpd_syscontact")) > 0) |
|---|
| 59 | fprintf(fp, "syscontact %s\n", |
|---|
| 60 | nvram_safe_get("snmpd_syscontact")); |
|---|
| 61 | if (strlen(nvram_safe_get("snmpd_sysname")) > 0) |
|---|
| 62 | fprintf(fp, "sysname %s\n", nvram_safe_get("snmpd_sysname")); |
|---|
| 63 | if (strlen(nvram_safe_get("snmpd_rocommunity")) > 0) |
|---|
| 64 | fprintf(fp, "rocommunity %s\n", |
|---|
| 65 | nvram_safe_get("snmpd_rocommunity")); |
|---|
| 66 | if (strlen(nvram_safe_get("snmpd_rwcommunity")) > 0) |
|---|
| 67 | fprintf(fp, "rwcommunity %s\n", |
|---|
| 68 | nvram_safe_get("snmpd_rwcommunity")); |
|---|
| 69 | fprintf(fp, "sysservices 9\n"); |
|---|
| 70 | fprintf(fp, "pass_persist .1.3.6.1.4.1.2021.255 /etc/wl_snmpd.sh\n"); |
|---|
| 71 | |
|---|
| 72 | fclose(fp); |
|---|
| 73 | ret = _evalpid(snmpd_argv, NULL, 0, &pid); |
|---|
| 74 | |
|---|
| 75 | cprintf("done\n"); |
|---|
| 76 | dd_syslog(LOG_INFO, "snmpd : SNMP daemon successfully started\n"); |
|---|
| 77 | |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | void stop_snmp(void) |
|---|
| 82 | { |
|---|
| 83 | int ret = 0; |
|---|
| 84 | |
|---|
| 85 | cprintf("done\n"); |
|---|
| 86 | if (pidof("snmpd") > 0) { |
|---|
| 87 | dd_syslog(LOG_INFO, |
|---|
| 88 | "snmpd : SNMP daemon successfully stopped\n"); |
|---|
| 89 | ret = killall("snmpd", SIGKILL); |
|---|
| 90 | } |
|---|
| 91 | return; |
|---|
| 92 | } |
|---|
| 93 | #endif |
|---|