Debian Codegraph @

agent/mibgroup/host/data_access/swinst_rpm.c

raw
1 /*
2 * swinst_rpm.c:
3 * hrSWInstalledTable data access:
4 */
5 #include <net-snmp/net-snmp-config.h>
6 #include <net-snmp/net-snmp-features.h>
7
8 #include <stdio.h>
9 #ifdef HAVE_STDLIB_H
10 #include <stdlib.h>
11 #endif
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #else
21 #include <strings.h>
22 #endif
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
26
27 #ifdef HAVE_RPM_RPMLIB_H
28 #include <rpm/rpmlib.h>
29 #endif
30 #ifdef HAVE_RPM_RPMLIB_H
31 #include <rpm/header.h>
32 #endif
33 #ifdef HAVE_RPMGETPATH /* HAVE_RPM_RPMMACRO_H */
34 #include <rpm/rpmmacro.h>
35 #endif
36 #ifdef HAVE_RPM_RPMTS_H
37 #include <rpm/rpmts.h>
38 #include <rpm/rpmdb.h>
39 #endif
40
41 #include <net-snmp/net-snmp-includes.h>
42 #include <net-snmp/agent/net-snmp-agent-includes.h>
43 #include <net-snmp/library/container.h>
44 #include <net-snmp/library/snmp_debug.h>
45 #include <net-snmp/data_access/swinst.h>
46 #include "swinst_private.h"
47
48 netsnmp_feature_require(date_n_time);
49
50 /*
51 * Location of RPM package directory.
52 * Used for:
53 * - reporting hrSWInstalledLast* objects
54 * - detecting when the cached contents are out of date.
55 */
56 static char pkg_directory[SNMP_MAXPATH];
57
58 /* ---------------------------------------------------------------------
59 */
60 void
61 netsnmp_swinst_arch_init(void)
62 {
63 char *rpmdbpath = NULL;
64 const char *dbpath;
65 struct stat stat_buf;
66
67 #ifdef HAVE_RPMGETPATH
68 rpmReadConfigFiles( NULL, NULL );
69 rpmdbpath = rpmGetPath( "%{_dbpath}", NULL );
70 dbpath = rpmdbpath;
71 #else
72 dbpath = "/var/lib/rpm"; /* Most likely */
73 #endif
74
75 snprintf( pkg_directory, SNMP_MAXPATH, "%s/Packages", dbpath );
76
77 if (-1 == stat( pkg_directory, &stat_buf )) {
78
79 /* check for SQLite DB backend */
80 snprintf( pkg_directory, SNMP_MAXPATH, "%s/rpmdb.sqlite", dbpath );
81
82 if (-1 == stat( pkg_directory, &stat_buf )) {
83 snmp_log(LOG_ERR, "Can't find directory of RPM packages\n");
84 pkg_directory[0] = '\0';
85 }
86 }
87
88 SNMP_FREE(rpmdbpath);
89 dbpath = NULL;
90 #ifdef HAVE_RPMGETPATH
91 rpmFreeRpmrc();
92 #endif
93 }
94
95 void
96 netsnmp_swinst_arch_shutdown(void)
97 {
98 /* Nothing to do */
99 return;
100 }
101
102 /* ---------------------------------------------------------------------
103 */
104 int
105 netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags)
106 {
107 rpmts ts;
108
109 rpmdbMatchIterator mi;
110 Header h;
111 #ifdef HAVE_HEADERGET
112 const char *g;
113 rpmtd td_name, td_version, td_release, td_group, td_time;
114 #else
115 char *n, *v, *r, *g;
116 int32_t *t;
117 #endif
118 time_t install_time;
119 size_t date_len;
120 int i = 1;
121 netsnmp_swinst_entry *entry;
122
123 #ifdef HAVE_HEADERGET
124 td_name = rpmtdNew();
125 td_version = rpmtdNew();
126 td_release = rpmtdNew();
127 td_group = rpmtdNew();
128 td_time = rpmtdNew();
129 #endif
130 ts = rpmtsCreate();
131 rpmtsSetVSFlags( ts, (_RPMVSF_NOSIGNATURES|_RPMVSF_NODIGESTS));
132
133 mi = rpmtsInitIterator( ts, RPMDBI_PACKAGES, NULL, 0);
134 if (mi == NULL)
135 NETSNMP_LOGONCE((LOG_ERR, "rpmdbOpen() failed\n"));
136
137 while (NULL != (h = rpmdbNextIterator( mi )))
138 {
139 const u_char *dt;
140
141 entry = netsnmp_swinst_entry_create( i++ );
142 if (NULL == entry)
143 continue; /* error already logged by function */
144 CONTAINER_INSERT(container, entry);
145
146 h = headerLink( h );
147 #ifdef HAVE_HEADERGET
148 headerGet(h, RPMTAG_NAME, td_name, HEADERGET_EXT);
149 headerGet(h, RPMTAG_VERSION, td_version, HEADERGET_EXT);
150 headerGet(h, RPMTAG_RELEASE, td_release, HEADERGET_EXT);
151 headerGet(h, RPMTAG_GROUP, td_group, HEADERGET_EXT);
152 headerGet(h, RPMTAG_INSTALLTIME, td_time, HEADERGET_EXT);
153 entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
154 "%s-%s-%s", rpmtdGetString(td_name),
155 rpmtdGetString(td_version),
156 rpmtdGetString(td_release));
157 install_time = rpmtdGetNumber(td_time);
158 g = rpmtdGetString(td_group);
159 #else
160 headerGetEntry( h, RPMTAG_NAME, NULL, (void**)&n, NULL);
161 headerGetEntry( h, RPMTAG_VERSION, NULL, (void**)&v, NULL);
162 headerGetEntry( h, RPMTAG_RELEASE, NULL, (void**)&r, NULL);
163 headerGetEntry( h, RPMTAG_GROUP, NULL, (void**)&g, NULL);
164 headerGetEntry( h, RPMTAG_INSTALLTIME, NULL, (void**)&t, NULL);
165 entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
166 "%s-%s-%s", n, v, r);
167 install_time = *t;
168 #endif
169 entry->swType = (g && NULL != strstr( g, "System Environment"))
170 ? 2 /* operatingSystem */
171 : 4; /* application */
172 if (entry->swName_len > sizeof(entry->swName))
173 entry->swName_len = sizeof(entry->swName);
174
175 dt = date_n_time( &install_time, &date_len );
176 if (date_len != 8 && date_len != 11) {
177 snmp_log(LOG_ERR, "Bogus length from date_n_time for %s", entry->swName);
178 entry->swDate_len = 0;
179 }
180 else {
181 entry->swDate_len = date_len;
182 memcpy(entry->swDate, dt, entry->swDate_len);
183 }
184
185 #ifdef HAVE_HEADERGET
186 rpmtdFreeData(td_name);
187 rpmtdFreeData(td_version);
188 rpmtdFreeData(td_release);
189 rpmtdFreeData(td_group);
190 rpmtdFreeData(td_time);
191 #endif
192 headerFree( h );
193 }
194 rpmdbFreeIterator( mi );
195 rpmtsFree( ts );
196 #ifdef HAVE_HEADERGET
197 rpmtdFree(td_name);
198 rpmtdFree(td_version);
199 rpmtdFree(td_release);
200 rpmtdFree(td_group);
201 rpmtdFree(td_time);
202 #endif
203
204 DEBUGMSGTL(("swinst:load:arch", "loaded %d entries\n",
205 (int)CONTAINER_SIZE(container)));
206
207 return 0;
208 }