Debian Codegraph @

snmplib/transports/snmpSTDDomain.c

raw
1 #include <net-snmp/net-snmp-config.h>
2
3 #include <net-snmp/library/snmpSTDDomain.h>
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <signal.h>
8 #include <errno.h>
9
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #else
13 #include <strings.h>
14 #endif
15 #ifdef HAVE_STDLIB_H
16 #include <stdlib.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #include <net-snmp/types.h>
23 #include <net-snmp/output_api.h>
24
25 #include <net-snmp/library/snmp_transport.h>
26 #include <net-snmp/library/tools.h>
27
28 oid netsnmp_snmpSTDDomain[] = { TRANSPORT_DOMAIN_STD_IP };
29 static netsnmp_tdomain stdDomain;
30
31 /*
32 * Return a string representing the address in data, or else the "far end"
33 * address if data is NULL.
34 */
35
36 static char *
37 netsnmp_std_fmtaddr(netsnmp_transport *t, const void *data, int len)
38 {
39 DEBUGMSGTL(("domain:std","formatting addr. data=%p\n",t->data));
40 if (t->data) {
41 netsnmp_std_data *data = (netsnmp_std_data*)t->data;
42 char *buf;
43
44 if (asprintf(&buf, "STD:%s", data->prog) < 0)
45 buf = NULL;
46 DEBUGMSGTL(("domain:std"," formatted:=%s\n",buf));
47 return buf;
48 }
49 return strdup("STDInOut");
50 }
51
52 static void
53 netsnmp_std_get_taddr(netsnmp_transport *t, void **addr, size_t *addr_len)
54 {
55 *addr_len = t->remote_length;
56 *addr = netsnmp_memdup(t->remote, *addr_len);
57 }
58
59 /*
60 * You can write something into opaque that will subsequently get passed back
61 * to your send function if you like. For instance, you might want to
62 * remember where a PDU came from, so that you can send a reply there...
63 */
64
65 static int
66 netsnmp_std_recv(netsnmp_transport *t, void *buf, int size,
67 void **opaque, int *olength)
68 {
69 int rc = -1;
70
71 DEBUGMSGTL(("domain:std","recv on sock %d. data=%p\n",t->sock, t->data));
72 while (rc < 0) {
73 rc = read(t->sock, buf, size);
74 DEBUGMSGTL(("domain:std"," bytes: %d.\n", rc));
75 if (rc < 0 && errno != EINTR) {
76 DEBUGMSGTL(("netsnmp_std", " read on fd %d failed: %d (\"%s\")\n",
77 t->sock, errno, strerror(errno)));
78 break;
79 }
80 if (rc == 0) {
81 /* 0 input is probably bad since we selected on it */
82 return -1;
83 }
84 DEBUGMSGTL(("netsnmp_std", "read on stdin got %d bytes\n", rc));
85 }
86
87 return rc;
88 }
89
90
91
92 static int
93 netsnmp_std_send(netsnmp_transport *t, const void *buf, int size,
94 void **opaque, int *olength)
95 {
96 int rc = -1;
97
98 DEBUGMSGTL(("domain:std","send on sock. data=%p\n", t->data));
99 while (rc < 0) {
100 if (t->data) {
101 netsnmp_std_data *data = (netsnmp_std_data*)t->data;
102 rc = write(data->outfd, buf, size);
103 } else {
104 /* straight to stdout */
105 rc = write(1, buf, size);
106 }
107 if (rc < 0 && errno != EINTR) {
108 break;
109 }
110 }
111 return rc;
112 }
113
114 static int
115 netsnmp_std_close(netsnmp_transport *t)
116 {
117 DEBUGMSGTL(("domain:std","close. data=%p\n", t->data));
118 if (t->data) {
119 netsnmp_std_data *data = (netsnmp_std_data*)t->data;
120 close(data->outfd);
121 close(t->sock);
122
123 /* kill the child too */
124 DEBUGMSGTL(("domain:std"," killing %d\n", data->childpid));
125 kill(data->childpid, SIGTERM);
126 sleep(1);
127 kill(data->childpid, SIGKILL);
128 /* XXX: set an alarm to kill harder the child */
129 } else {
130 /* close stdout/in */
131 close(STDOUT_FILENO);
132 close(STDIN_FILENO);
133 }
134 return 0;
135 }
136
137
138
139 static int
140 netsnmp_std_accept(netsnmp_transport *t)
141 {
142 DEBUGMSGTL(("domain:std"," accept data=%p\n", t->data));
143 /* nothing to do here */
144 return 0;
145 }
146
147 /*
148 * Open a STDIN/STDOUT -based transport for SNMP.
149 */
150
151 netsnmp_transport *
152 netsnmp_std_transport(const char *instring, size_t instring_len,
153 const char *default_target)
154 {
155 netsnmp_transport *t;
156
157 t = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
158 if (t == NULL) {
159 return NULL;
160 }
161
162 t->domain = netsnmp_snmpSTDDomain;
163 t->domain_length =
164 sizeof(netsnmp_snmpSTDDomain) / sizeof(netsnmp_snmpSTDDomain[0]);
165
166 t->sock = -1;
167 t->flags = NETSNMP_TRANSPORT_FLAG_STREAM | NETSNMP_TRANSPORT_FLAG_TUNNELED;
168
169 /*
170 * Message size is not limited by this transport (hence msgMaxSize
171 * is equal to the maximum legal size of an SNMP message).
172 */
173
174 t->msgMaxSize = SNMP_MAX_PACKET_LEN;
175 t->f_recv = netsnmp_std_recv;
176 t->f_send = netsnmp_std_send;
177 t->f_close = netsnmp_std_close;
178 t->f_accept = netsnmp_std_accept;
179 t->f_fmtaddr = netsnmp_std_fmtaddr;
180 t->f_get_taddr = netsnmp_std_get_taddr;
181
182 /*
183 * if instring is not null length, it specifies a path to a prog
184 * XXX: plus args
185 */
186 if (instring_len == 0 && default_target != NULL) {
187 instring = default_target;
188 instring_len = strlen(default_target);
189 }
190
191 if (instring_len != 0) {
192 int infd[2], outfd[2]; /* sockets to and from the client */
193 int childpid;
194
195 if (pipe(infd) || pipe(outfd)) {
196 snmp_log(LOG_ERR,
197 "Failed to create needed pipes for a STD transport");
198 netsnmp_transport_free(t);
199 return NULL;
200 }
201
202 childpid = fork();
203 /* parentpid => childpid */
204 /* infd[1] => infd[0] */
205 /* outfd[0] <= outfd[1] */
206
207 if (childpid) {
208 netsnmp_std_data *data;
209
210 /* we're in the parent */
211 close(infd[0]);
212 close(outfd[1]);
213
214 data = SNMP_MALLOC_TYPEDEF(netsnmp_std_data);
215 if (!data) {
216 snmp_log(LOG_ERR, "snmpSTDDomain: malloc failed");
217 netsnmp_transport_free(t);
218 return NULL;
219 }
220 t->data = data;
221 t->data_length = sizeof(netsnmp_std_data);
222 t->sock = outfd[0];
223 data->prog = strdup(instring);
224 data->outfd = infd[1];
225 data->childpid = childpid;
226 DEBUGMSGTL(("domain:std","parent. data=%p\n", t->data));
227 } else {
228 /* we're in the child */
229
230 dup2(infd[0], STDIN_FILENO);
231 dup2(outfd[1], STDOUT_FILENO);
232
233 /* close all the pipes themselves */
234 close(infd[0]);
235 close(infd[1]);
236 close(outfd[0]);
237 close(outfd[1]);
238
239 NETSNMP_IGNORE_RESULT(system(instring));
240 /* XXX: TODO: use exec form instead; needs args */
241 /* execv(instring, NULL); */
242 }
243 }
244
245 return t;
246 }
247
248 netsnmp_transport *
249 netsnmp_std_create_tstring(const char *instring, int local,
250 const char *default_target)
251 {
252 return netsnmp_std_transport(instring, strlen(instring), default_target);
253 }
254
255 netsnmp_transport *
256 netsnmp_std_create_ostring(const void *o, size_t o_len, int local)
257 {
258 return netsnmp_std_transport(o, o_len, NULL);
259 }
260
261 void
262 netsnmp_std_ctor(void)
263 {
264 stdDomain.name = netsnmp_snmpSTDDomain;
265 stdDomain.name_length = sizeof(netsnmp_snmpSTDDomain) / sizeof(oid);
266 stdDomain.prefix = (const char **)calloc(2, sizeof(char *));
267 stdDomain.prefix[0] = "std";
268
269 stdDomain.f_create_from_tstring_new = netsnmp_std_create_tstring;
270 stdDomain.f_create_from_ostring = netsnmp_std_create_ostring;
271
272 netsnmp_tdomain_register(&stdDomain);
273 }