| 1 |
/* |
| 2 |
* swinst.c : hrSWInstalledTable data access |
| 3 |
*/ |
| 4 |
/* |
| 5 |
* Copyright (C) 2007 Apple, Inc. All rights reserved. |
| 6 |
* Use is subject to license terms specified in the COPYING file |
| 7 |
* distributed with the Net-SNMP package. |
| 8 |
*/ |
| 9 |
#include <net-snmp/net-snmp-config.h> |
| 10 |
#include <net-snmp/net-snmp-features.h> |
| 11 |
#include <net-snmp/net-snmp-includes.h> |
| 12 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 13 |
#include <net-snmp/library/container.h> |
| 14 |
#include <net-snmp/library/dir_utils.h> |
| 15 |
#include <net-snmp/library/snmp_debug.h> |
| 16 |
#include <net-snmp/data_access/swinst.h> |
| 17 |
#include "swinst_private.h" |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <unistd.h> |
| 21 |
#include <dirent.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
|
| 24 |
#define __APPLE_API_EVOLVING 1 |
| 25 |
#include <sys/acl.h> /* or else CoreFoundation.h barfs */ |
| 26 |
#undef __APPLE_API_EVOLVING |
| 27 |
|
| 28 |
#include <CoreFoundation/CoreFoundation.h> |
| 29 |
#include <ApplicationServices/ApplicationServices.h> |
| 30 |
|
| 31 |
netsnmp_feature_require(container_directory); |
| 32 |
netsnmp_feature_require(date_n_time); |
| 33 |
|
| 34 |
/* --------------------------------------------------------------------- |
| 35 |
*/ |
| 36 |
static int _add_applications_in_dir(netsnmp_container *, const char* path); |
| 37 |
static int32_t _index; |
| 38 |
static int _check_bundled_app(CFURLRef currentURL, CFStringRef *name, |
| 39 |
CFStringRef *info, const char* path); |
| 40 |
static int _check_classic_app(CFURLRef currentURL, CFStringRef *name, |
| 41 |
CFStringRef *info, const char* path); |
| 42 |
static netsnmp_container *dirs = NULL; |
| 43 |
|
| 44 |
/* --------------------------------------------------------------------- |
| 45 |
*/ |
| 46 |
void |
| 47 |
netsnmp_swinst_arch_init( void ) |
| 48 |
{ |
| 49 |
struct stat stat_buf; |
| 50 |
const char *default_dirs[] = { |
| 51 |
"/Applications", |
| 52 |
"/Applications (Mac OS 9)", |
| 53 |
"/System/Library/CoreServices", |
| 54 |
"/System/Library/Extensions", |
| 55 |
"/System/Library/Services" |
| 56 |
#ifdef TEST |
| 57 |
, "/Developer/Applications" |
| 58 |
, "/Volumes/audX/Applications (Mac OS 9)" |
| 59 |
#endif |
| 60 |
}; |
| 61 |
int i, count = sizeof(default_dirs)/sizeof(default_dirs[0]); |
| 62 |
|
| 63 |
/* |
| 64 |
* create the container, if needed |
| 65 |
*/ |
| 66 |
if (NULL == dirs) { |
| 67 |
dirs = netsnmp_container_find("directory_container:cstring"); |
| 68 |
if (NULL == dirs) { |
| 69 |
snmp_log(LOG_ERR, "couldn't allocate container for dir list\n"); |
| 70 |
return; |
| 71 |
} |
| 72 |
dirs->container_name = strdup("directory search list"); |
| 73 |
netsnmp_binary_array_options_set(dirs, 1, CONTAINER_KEY_UNSORTED); |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* add dirs |
| 78 |
*/ |
| 79 |
for(i = 0; i < count; ++i) { |
| 80 |
char * tmp; |
| 81 |
/** xxx: get/save the last mod date? */ |
| 82 |
if(-1 == stat(default_dirs[i], &stat_buf)) { |
| 83 |
DEBUGMSGTL(("swinst:arch:darwin", "skipping dir %s\n", |
| 84 |
default_dirs[i])); |
| 85 |
continue; |
| 86 |
} |
| 87 |
DEBUGMSGTL(("swinst:arch:darwin", "adding dir %s\n", |
| 88 |
default_dirs[i])); |
| 89 |
tmp = strdup(default_dirs[i]); |
| 90 |
if (NULL == tmp) { |
| 91 |
snmp_log(LOG_ERR,"strdup failed\n"); |
| 92 |
break; |
| 93 |
} |
| 94 |
CONTAINER_INSERT(dirs, tmp); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
netsnmp_swinst_arch_shutdown( void ) |
| 100 |
{ |
| 101 |
netsnmp_directory_container_free(dirs); |
| 102 |
} |
| 103 |
|
| 104 |
/* --------------------------------------------------------------------- |
| 105 |
*/ |
| 106 |
|
| 107 |
int |
| 108 |
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags ) |
| 109 |
{ |
| 110 |
netsnmp_iterator *it; |
| 111 |
const char *dir; |
| 112 |
int rc; |
| 113 |
|
| 114 |
DEBUGMSGTL(("swinst:arch:darwin", "load\n")); |
| 115 |
|
| 116 |
if (NULL == dirs) { |
| 117 |
DEBUGMSGTL(("swinst:arch:darwin", "no dirs to scan!\n")); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
_index = 0; |
| 122 |
|
| 123 |
it = CONTAINER_ITERATOR(dirs); |
| 124 |
for (dir = ITERATOR_FIRST(it); dir; dir = ITERATOR_NEXT(it)) { |
| 125 |
rc = _add_applications_in_dir(container, dir); |
| 126 |
} |
| 127 |
ITERATOR_RELEASE(it); |
| 128 |
DEBUGMSGTL(("swinst:arch:darwin", "loaded %d apps\n",_index)); |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
_dump_flags(u_long flags) |
| 135 |
{ |
| 136 |
static struct { |
| 137 |
const char*name; |
| 138 |
u_long bits; |
| 139 |
} names[] = { |
| 140 |
{ "kLSItemInfoIsPlainFile", 0x00000001 }, |
| 141 |
{ "kLSItemInfoIsPackage", 0x00000002 }, |
| 142 |
{ "kLSItemInfoIsApplication", 0x00000004 }, |
| 143 |
{ "kLSItemInfoIsContainer", 0x00000008 }, |
| 144 |
{ "kLSItemInfoIsAliasFile", 0x00000010 }, |
| 145 |
{ "kLSItemInfoIsSymlink", 0x00000020 }, |
| 146 |
{ "kLSItemInfoIsInvisible", 0x00000040 }, |
| 147 |
{ "kLSItemInfoIsNativeApp", 0x00000080 }, |
| 148 |
{ "kLSItemInfoIsClassicApp", 0x00000100 }, |
| 149 |
{ "kLSItemInfoAppPrefersNative", 0x00000200 }, |
| 150 |
{ "kLSItemInfoAppPrefersClassic", 0x00000400 }, |
| 151 |
{ "kLSItemInfoAppIsScriptable", 0x00000800 }, |
| 152 |
{ "kLSItemInfoIsVolume", 0x00001000 }, |
| 153 |
{ "kLSItemInfoExtensionIsHidden", 0x00100000 } |
| 154 |
}; |
| 155 |
int i, count = sizeof(names)/sizeof(names[0]); |
| 156 |
|
| 157 |
for(i = 0; i < count; ++i) { |
| 158 |
if (flags & names[i].bits) { |
| 159 |
DEBUGMSGTL(("swinst:arch:darwin:flags", "\t%s\n", |
| 160 |
names[i].name)); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
static int |
| 166 |
_add_applications_in_dir(netsnmp_container *container, const char* path) |
| 167 |
{ |
| 168 |
netsnmp_container *files; |
| 169 |
netsnmp_iterator *it; |
| 170 |
const char *file; |
| 171 |
netsnmp_swinst_entry *entry = NULL; |
| 172 |
struct stat stat_buf; |
| 173 |
size_t date_len; |
| 174 |
u_char *date_buf; |
| 175 |
int rc = 0; |
| 176 |
|
| 177 |
CFStringRef currentPath = NULL; |
| 178 |
CFURLRef currentURL = NULL; |
| 179 |
LSItemInfoRecord itemInfoRecord; |
| 180 |
CFStringRef prodName = NULL; |
| 181 |
CFStringRef version = NULL; |
| 182 |
|
| 183 |
DEBUGMSGTL(("swinst:arch:darwin", " adding files from %s\n", path)); |
| 184 |
files = netsnmp_directory_container_read(NULL, path, 0); |
| 185 |
if (NULL == files) { |
| 186 |
snmp_log(LOG_ERR, "swinst: could not read directory %s\n", path); |
| 187 |
return -1; |
| 188 |
} |
| 189 |
|
| 190 |
it = CONTAINER_ITERATOR(files); |
| 191 |
if (NULL == it) { |
| 192 |
snmp_log(LOG_ERR, "could not get iterator\n"); |
| 193 |
netsnmp_directory_container_free(files); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
for (file = ITERATOR_FIRST(it); |
| 197 |
file; |
| 198 |
file = ITERATOR_NEXT(it), |
| 199 |
CFRelease(currentPath), |
| 200 |
CFRelease(currentURL)) { |
| 201 |
|
| 202 |
int rc2 = 0; |
| 203 |
|
| 204 |
currentPath = |
| 205 |
CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, file, |
| 206 |
kCFStringEncodingUTF8, |
| 207 |
kCFAllocatorNull); |
| 208 |
currentURL = |
| 209 |
CFURLCreateWithFileSystemPath(kCFAllocatorDefault, currentPath, |
| 210 |
kCFURLPOSIXPathStyle, true); |
| 211 |
LSCopyItemInfoForURL(currentURL, |
| 212 |
kLSRequestBasicFlagsOnly|kLSRequestAppTypeFlags, |
| 213 |
&itemInfoRecord); |
| 214 |
if((0 == itemInfoRecord.flags) || |
| 215 |
(kLSItemInfoIsPlainFile == itemInfoRecord.flags) || |
| 216 |
(itemInfoRecord.flags & kLSItemInfoIsInvisible) || |
| 217 |
(itemInfoRecord.flags & kLSItemInfoIsAliasFile)) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
/** recurse on non-application containers (i.e. directory) */ |
| 221 |
if ((itemInfoRecord.flags & kLSItemInfoIsContainer) && |
| 222 |
(!(itemInfoRecord.flags & kLSItemInfoIsApplication))) { |
| 223 |
netsnmp_directory_container_read(files, file, 0); |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
/** skip any other non-application files */ |
| 228 |
if (!(itemInfoRecord.flags & kLSItemInfoIsApplication)) { |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
if ((itemInfoRecord.flags & kLSItemInfoIsPackage) || |
| 233 |
(itemInfoRecord.flags & kLSItemInfoIsContainer)) { |
| 234 |
rc2 = _check_bundled_app(currentURL, &prodName, &version, file); |
| 235 |
} |
| 236 |
else if ((itemInfoRecord.flags & kLSItemInfoIsClassicApp) || |
| 237 |
(itemInfoRecord.flags & kLSItemInfoIsPlainFile)) { |
| 238 |
rc2 = _check_classic_app(currentURL, &prodName, &version, file); |
| 239 |
} else { |
| 240 |
snmp_log(LOG_ERR,"swinst shouldn't get here: %s\n", file); |
| 241 |
_dump_flags(itemInfoRecord.flags); |
| 242 |
continue; |
| 243 |
} |
| 244 |
if (rc2) { /* not an app. if directory, recurse; else continue */ |
| 245 |
_dump_flags(itemInfoRecord.flags); |
| 246 |
if (1 == rc2) |
| 247 |
netsnmp_directory_container_read(files, file, 0); |
| 248 |
continue; |
| 249 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* allocate entry |
| 253 |
*/ |
| 254 |
entry = netsnmp_swinst_entry_create(++_index); |
| 255 |
if (NULL == entry) { |
| 256 |
snmp_log(LOG_ERR, "error creating swinst entry\n"); |
| 257 |
rc = -1; |
| 258 |
SNMP_CFRelease(prodName); |
| 259 |
SNMP_CFRelease(version); |
| 260 |
CFRelease(currentPath); |
| 261 |
CFRelease(currentURL); |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
entry->swName_len = |
| 266 |
snprintf(entry->swName, sizeof(entry->swName), |
| 267 |
"%s %s", CFStringGetCStringPtr(prodName,0), |
| 268 |
CFStringGetCStringPtr(version,0)); |
| 269 |
if (entry->swName_len >= sizeof(entry->swName)) |
| 270 |
entry->swName_len = sizeof(entry->swName)-1; |
| 271 |
|
| 272 |
DEBUGMSGTL(("swinst:arch:darwin", "\t%s %s\n", file, entry->swName)); |
| 273 |
|
| 274 |
/** get the last mod date */ |
| 275 |
if(stat(file, &stat_buf) != -1) { |
| 276 |
date_buf = date_n_time(&stat_buf.st_mtime, &date_len); |
| 277 |
entry->swDate_len = date_len; |
| 278 |
memcpy(entry->swDate, date_buf, entry->swDate_len); |
| 279 |
} |
| 280 |
|
| 281 |
CONTAINER_INSERT(container, entry); |
| 282 |
entry = NULL; |
| 283 |
SNMP_CFRelease(prodName); |
| 284 |
SNMP_CFRelease(version); |
| 285 |
} |
| 286 |
ITERATOR_RELEASE(it); |
| 287 |
netsnmp_directory_container_free(files); |
| 288 |
|
| 289 |
return rc; |
| 290 |
} |
| 291 |
|
| 292 |
static int |
| 293 |
_check_bundled_app(CFURLRef currentURL, CFStringRef *prodName, |
| 294 |
CFStringRef *version, const char* file) |
| 295 |
{ |
| 296 |
CFBundleRef theBundle = NULL; |
| 297 |
CFDictionaryRef infoDict = NULL; |
| 298 |
|
| 299 |
if ((NULL == prodName) || (NULL == version)) |
| 300 |
return -1; |
| 301 |
|
| 302 |
theBundle = CFBundleCreate (kCFAllocatorDefault, currentURL); |
| 303 |
if(theBundle == NULL) |
| 304 |
return -1; /* not a bundle */ |
| 305 |
|
| 306 |
infoDict = CFBundleGetInfoDictionary(theBundle); |
| 307 |
if(0 == CFDictionaryGetCount(infoDict)) { |
| 308 |
SNMP_CFRelease(theBundle); |
| 309 |
return 1; /* directory */ |
| 310 |
} |
| 311 |
|
| 312 |
*prodName = (CFStringRef) |
| 313 |
CFDictionaryGetValue (infoDict, CFSTR("CFBundleName")); |
| 314 |
if (NULL == *prodName) { |
| 315 |
*prodName = (CFStringRef) |
| 316 |
CFDictionaryGetValue (infoDict, CFSTR("CFBundleDisplayName")); |
| 317 |
if (NULL == *prodName) { |
| 318 |
*prodName = (CFStringRef) CFDictionaryGetValue (infoDict, |
| 319 |
CFSTR("CFBundleExecutable")); |
| 320 |
} |
| 321 |
} |
| 322 |
if(NULL == *prodName) { |
| 323 |
DEBUGMSGTL(("swinst:arch:darwin", "\tmissing name: %s\n",file)); |
| 324 |
/*CFShow(infoDict);*/ |
| 325 |
SNMP_CFRelease(theBundle); |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
*version = (CFStringRef) |
| 330 |
CFDictionaryGetValue (infoDict, CFSTR("CFBundleShortVersionString")); |
| 331 |
if(NULL == *version) { |
| 332 |
*version = (CFStringRef) |
| 333 |
CFDictionaryGetValue (infoDict, CFSTR("CFBundleVersion")); |
| 334 |
if (*version == NULL) |
| 335 |
*version = (CFStringRef) CFDictionaryGetValue (infoDict, |
| 336 |
CFSTR("CFBundleGetInfoString")); |
| 337 |
} |
| 338 |
if(NULL == *version) { |
| 339 |
DEBUGMSGTL(("swinst:arch:darwin", "\tmissing version: %s\n",file)); |
| 340 |
/*CFShow(infoDict);*/ |
| 341 |
SNMP_CFRelease(theBundle); |
| 342 |
return -1; |
| 343 |
} |
| 344 |
|
| 345 |
if(theBundle != NULL) { |
| 346 |
CFRetain(*prodName); |
| 347 |
CFRetain(*version); |
| 348 |
SNMP_CFRelease(theBundle); |
| 349 |
} |
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
static int |
| 354 |
_check_classic_app(CFURLRef currentURL, CFStringRef *prodName, |
| 355 |
CFStringRef *version, const char* file) |
| 356 |
{ |
| 357 |
/* |
| 358 |
* get info for classic or single-file apps |
| 359 |
*/ |
| 360 |
FSRef theFSRef; |
| 361 |
int theResFile; |
| 362 |
|
| 363 |
if ((NULL == prodName) || (NULL == version)) |
| 364 |
return -1; |
| 365 |
|
| 366 |
*prodName = CFURLCopyLastPathComponent(currentURL); |
| 367 |
if (! CFURLGetFSRef(currentURL, &theFSRef)) { |
| 368 |
DEBUGMSGTL(("swinst:arch:darwin", "GetFSRef failed: %s\n", file)); |
| 369 |
SNMP_CFRelease(*prodName); |
| 370 |
return -1; |
| 371 |
} |
| 372 |
theResFile = FSOpenResFile(&theFSRef, fsRdPerm); |
| 373 |
if (ResError() != noErr) { |
| 374 |
DEBUGMSGTL(("swinst:arch:darwin", "FSOpenResFile failed: %s\n", file)); |
| 375 |
SNMP_CFRelease(*prodName); |
| 376 |
return -1; |
| 377 |
} |
| 378 |
VersRecHndl versHandle = (VersRecHndl)Get1IndResource('vers', 1); |
| 379 |
if (versHandle != NULL) { |
| 380 |
*version = CFStringCreateWithPascalString(kCFAllocatorDefault, |
| 381 |
(**versHandle).shortVersion, kCFStringEncodingMacRoman); |
| 382 |
if (*version == NULL) { |
| 383 |
StringPtr longVersionPtr = (**versHandle).shortVersion; |
| 384 |
longVersionPtr = (StringPtr)(((Ptr) longVersionPtr) + |
| 385 |
1 + ((unsigned char) *longVersionPtr)); |
| 386 |
*version = CFStringCreateWithPascalString(kCFAllocatorDefault, |
| 387 |
longVersionPtr, kCFStringEncodingMacRoman); |
| 388 |
} |
| 389 |
ReleaseResource((Handle)versHandle); |
| 390 |
} |
| 391 |
CloseResFile(theResFile); |
| 392 |
if(*version == NULL) { |
| 393 |
DEBUGMSGTL(("swinst:arch:darwin", |
| 394 |
"\tmissing classic/file version: %s\n", file)); |
| 395 |
SNMP_CFRelease(*prodName); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
|
| 399 |
return 0; |
| 400 |
} |