forked from jabberd2/jabberd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_iq_version.c
284 lines (233 loc) · 9.37 KB
/
mod_iq_version.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* jabberd - Jabber Open Source Server
* Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
* Ryan Eatmon, Robert Norris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*/
#include "sm.h"
/** @file sm/mod_iq_version.c
* @brief software version
* @author Robert Norris
* $Date: 2005/08/17 07:48:28 $
* $Revision: 1.16 $
*/
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
typedef struct _mod_iq_version_config_st {
char *app_name;
char *app_version;
char *app_signature;
char *os_name;
char *os_release;
} *mod_iq_version_config_t;
static int ns_VERSION = 0;
void _iq_version_get_os_version(mod_iq_version_config_t config) {
#if defined(HAVE_UNAME)
struct utsname un;
#elif defined(_WIN32)
char sysname[64];
char release[64];
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
BOOL bSomeError = FALSE;
sysname[0] = '\0';
release[0] = '\0';
#endif
/* figure out the os type */
#if defined(HAVE_UNAME)
if(uname(&un) == 0) {
config->os_name = strdup(un.sysname);
config->os_release = strdup(un.machine);
return;
}
#elif defined(_WIN32)
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
/* If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO. */
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
{
snprintf(sysname, 64, "unknown");
bSomeError = TRUE;
}
}
if (!bSomeError)
{
switch (osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
/* Test for the product. */
if ( osvi.dwMajorVersion <= 4 )
snprintf(sysname, 64, "Microsoft Windows NT");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
snprintf(sysname, 64, "Microsoft Windows 2000");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
snprintf(sysname, 64, "Microsoft Windows XP");
/* Test for product type. */
if( bOsVersionInfoEx )
{
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
snprintf(release, 64, "Personal" );
else
snprintf(release, 64, "Professional" );
}
else if ( osvi.wProductType == VER_NT_SERVER )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
snprintf(release, 64, "DataCenter Server" );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
snprintf(release, 64, "Advanced Server" );
else
snprintf(release, 64, "Server" );
}
}
else
{
HKEY hKey;
char szProductType[80];
DWORD dwBufLen;
RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
0, KEY_QUERY_VALUE, &hKey );
RegQueryValueEx( hKey, "ProductType", NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
RegCloseKey( hKey );
if ( lstrcmpi( "WINNT", szProductType) == 0 )
snprintf(release, 64, "Professional" );
if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
snprintf(release, 64, "Server" );
if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
snprintf(release, 64, "Advanced Server" );
}
break;
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
snprintf(sysname, 64, "Microsoft Windows 95");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
snprintf(release, 64, "OSR2" );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
snprintf(sysname, 64, "Microsoft Windows 98");
if ( osvi.szCSDVersion[1] == 'A' )
snprintf(release, 64, "SE" );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
snprintf(sysname, 64, "Microsoft Windows Me");
}
break;
case VER_PLATFORM_WIN32s:
snprintf(sysname, 64, "Microsoft Win32s");
break;
}
}
config->os_name = strdup(sysname);
config->os_release = strdup(release);
return;
#endif
}
static mod_ret_t _iq_version_pkt_sm(mod_instance_t mi, pkt_t pkt) {
module_t mod = mi->mod;
mod_iq_version_config_t config = (mod_iq_version_config_t) mod->private;
char buf[256];
/* we only want to play with iq:version gets */
if(pkt->type != pkt_IQ || pkt->ns != ns_VERSION)
return mod_PASS;
nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "name", config->app_name);
nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "version", config->app_version);
/* figure out the os type */
if(config->os_name != NULL) {
if(config->os_release)
snprintf(buf, 256, "%s %s", config->os_name, config->os_release);
else
snprintf(buf, 256, "%s", config->os_name);
nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "os", buf);
}
/* tell them */
nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
pkt_router(pkt_tofrom(pkt));
return mod_HANDLED;
}
static void _iq_version_disco_extend(mod_instance_t mi, pkt_t pkt)
{
module_t mod = mi->mod;
mod_iq_version_config_t config = (mod_iq_version_config_t) mod->private;
int ns;
log_debug(ZONE, "in mod_iq_version disco-extend");
ns = nad_add_namespace(pkt->nad, uri_XDATA, NULL);
/* there may be several XDATA siblings, so need to enforce the NS */
pkt->nad->scope = ns;
nad_append_elem(pkt->nad, ns, "x", 3);
nad_append_attr(pkt->nad, -1, "type", "result");
/* hidden form type field*/
nad_append_elem(pkt->nad, -1, "field", 4);
nad_append_attr(pkt->nad, -1, "var", "FORM_TYPE");
nad_append_attr(pkt->nad, -1, "type", "hidden");
nad_append_elem(pkt->nad, -1, "value", 5);
nad_append_cdata(pkt->nad, urn_SOFTWAREINFO, strlen(urn_SOFTWAREINFO), 6);
nad_append_elem(pkt->nad, -1, "field", 4);
nad_append_attr(pkt->nad, -1, "var", "software");
nad_append_elem(pkt->nad, -1, "value", 5);
nad_append_cdata(pkt->nad, config->app_name, strlen(config->app_name), 6);
nad_append_elem(pkt->nad, -1, "field", 4);
nad_append_attr(pkt->nad, -1, "var", "software_version");
nad_append_elem(pkt->nad, -1, "value", 5);
nad_append_cdata(pkt->nad, config->app_version, strlen(config->app_version), 6);
if(config->os_name != NULL) {
nad_append_elem(pkt->nad, -1, "field", 4);
nad_append_attr(pkt->nad, -1, "var", "os");
nad_append_elem(pkt->nad, -1, "value", 5);
nad_append_cdata(pkt->nad, config->os_name, strlen(config->os_name), 6);
}
if(config->os_name != NULL) {
nad_append_elem(pkt->nad, -1, "field", 4);
nad_append_attr(pkt->nad, -1, "var", "os_version");
nad_append_elem(pkt->nad, -1, "value", 5);
nad_append_cdata(pkt->nad, config->os_release, strlen(config->os_release), 6);
}
}
static void _iq_version_free(module_t mod) {
mod_iq_version_config_t config = (mod_iq_version_config_t) mod->private;
sm_unregister_ns(mod->mm->sm, uri_VERSION);
feature_unregister(mod->mm->sm, uri_VERSION);
if(config->os_name != NULL) free(config->os_name);
if(config->os_release != NULL) free(config->os_release);
free(config);
}
DLLEXPORT int module_init(mod_instance_t mi, char *arg) {
mod_iq_version_config_t config;
module_t mod = mi->mod;
if(mod->init) return 0;
config = (mod_iq_version_config_t) calloc(1, sizeof(struct _mod_iq_version_config_st));
config->app_name = PACKAGE;
config->app_version = VERSION;
config->app_signature = mi->sm->signature;
_iq_version_get_os_version(config);
mod->private = config;
mod->pkt_sm = _iq_version_pkt_sm;
mod->disco_extend = _iq_version_disco_extend;
mod->free = _iq_version_free;
ns_VERSION = sm_register_ns(mod->mm->sm, uri_VERSION);
feature_register(mod->mm->sm, uri_VERSION);
return 0;
}