1/*
2 * snmpd.c
3 *
4 * Copyright (C) 2007 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.
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#include <rc.h>
24#include <stdlib.h>
25#include <bcmnvram.h>
26#include <shutils.h>
27#include <utils.h>
28#include <syslog.h>
29//#include <signal.h>
30//#include <errno.h>
31//#include <sys/stat.h>
32
33void start_snmpd(void)
34{
35	int ret = 0;
36	FILE *fp;
37	char user[35], authType[5], privType[5], authPwd[256], privPwd[256];
38
39	if (!nvram_match("snmpd_enable", "1")) {
40		return;
41	}
42
43	// Create config file
44	fp = fopen("/tmp/snmpd.conf", "w");
45	if (fp == NULL) {
46		cprintf("Can't open /tmp/snmpd.conf!\n");
47		return;
48	}
49
50	memset(user, 0x0, 35);
51	memset(authType, 0x0, 5);
52	memset(privType, 0x0, 5);
53	memset(authPwd, 0x0, 256);
54	memset(privPwd, 0x0, 256);
55	sprintf(user, "%s", nvram_safe_get("http_username"));
56	sprintf(authType, "%s", nvram_safe_get("v3_auth_type"));
57	sprintf(authPwd, "%s", nvram_safe_get("v3_auth_passwd"));
58	sprintf(privType, "%s", nvram_safe_get("v3_priv_type"));
59	sprintf(privPwd, "%s", nvram_safe_get("v3_priv_passwd"));
60
61	cprintf("write config for snmpd!\n");
62
63	fprintf(fp, "agentAddress  udp:161\n");
64
65	if(!strcmp(authType, "MD5") || !strcmp(authType, "SHA")) {
66		if(!strcmp(privType, "DES") || !strcmp(privType, "AES")) {
67			fprintf(fp, "createUser %s %s %s %s %s\n", user, authType, authPwd, privType, privPwd);
68			fprintf(fp, "rwuser %s priv\n", user);
69		}
70		else if(!strcmp(privType, "NONE")) {
71			fprintf(fp, "createUser %s %s %s\n", user, authType, authPwd);
72			fprintf(fp, "rwuser %s auth\n", user);
73		}
74		else
75			cprintf("Wrong SNMPv3 Privacy Type!!\n");
76	}
77	else if(!strcmp(authType, "NONE"))
78	{
79		fprintf(fp, "createUser %s\n", user);
80		fprintf(fp, "rwuser %s noauth\n", user);
81	}
82	else
83		cprintf("Wrong SNMPv3 Authentication type!!\n");
84
85	if(strlen(nvram_safe_get("roCommunity")))
86		fprintf(fp, "rocommunity %s default\n", nvram_safe_get("roCommunity"));
87
88	if(strlen(nvram_safe_get("rwCommunity")))
89		fprintf(fp, "rwcommunity %s default\n", nvram_safe_get("rwCommunity"));
90
91#if 0
92	fprintf(fp, "com2sec	defROnlyUser	default	%s\n", nvram_get("roCommunity"));
93	fprintf(fp, "com2sec	defRWUser	default	%s\n", nvram_get("rwCommunity"));
94	fprintf(fp, "group	ROnlyGroup	v1	defROnlyUser\n");
95	fprintf(fp, "group	ROnlyGroup	v2c	defROnlyUser\n");
96	fprintf(fp, "group	RWGroup	v1	defRWUser\n");
97	fprintf(fp, "group	RWGroup	v2c	defRWUser\n");
98	fprintf(fp, "view	all	included	.1\n");
99	fprintf(fp, "access	ROnlyGroup	\"\"	any	noauth	exact	all	none	none\n");
100	fprintf(fp, "access	RWGroup		\"\"	any	noauth	exact	all	all	none\n");
101#endif
102
103	if(strlen(nvram_safe_get("sysName")))
104		fprintf(fp, "sysName %s\n", nvram_safe_get("sysName"));
105
106	if(strlen(nvram_safe_get("sysLocation")))
107		fprintf(fp, "sysLocation %s\n", nvram_safe_get("sysLocation"));
108
109	if(strlen(nvram_safe_get("sysContact")))
110		fprintf(fp, "sysContact %s\n", nvram_safe_get("sysContact"));
111
112	fclose(fp);
113
114	// Execute snmp daemon
115	ret = eval("snmpd", "-c", "/tmp/snmpd.conf");
116
117	_dprintf("start_snmpd: ret= %d\n", ret);
118
119	return;
120}
121
122void stop_snmpd(void)
123{
124	if (getpid() != 1) {
125		notify_rc("stop_snmpd");
126	}
127
128	killall_tk("snmpd");
129	return;
130}
131