summaryrefslogtreecommitdiff
path: root/agent/mibgroup/host/data_access/swinst_darwin.c
blob: e7a0294bf907994625b5c4cc557cd4f8f1d5dd55 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * swinst.c : hrSWInstalledTable data access
 */
/*
 * Copyright (C) 2007 Apple, Inc. All rights reserved.
 * Use is subject to license terms specified in the COPYING file
 * distributed with the Net-SNMP package.
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/library/container.h>
#include <net-snmp/library/dir_utils.h>
#include <net-snmp/library/snmp_debug.h>
#include <net-snmp/data_access/swinst.h>

#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>

#define __APPLE_API_EVOLVING 1
#include <sys/acl.h> /* or else CoreFoundation.h barfs */
#undef __APPLE_API_EVOLVING 

#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>

netsnmp_feature_require(container_directory)
netsnmp_feature_require(date_n_time)

/* ---------------------------------------------------------------------
 */
static int _add_applications_in_dir(netsnmp_container *, const char* path);
static int32_t _index;
static int _check_bundled_app(CFURLRef currentURL, CFStringRef *name,
                              CFStringRef *info, const char* path);
static int _check_classic_app(CFURLRef currentURL, CFStringRef *name,
                              CFStringRef *info, const char* path);
static netsnmp_container *dirs = NULL;

/* ---------------------------------------------------------------------
 */
void
netsnmp_swinst_arch_init( void )
{
    struct stat stat_buf;
    const char *default_dirs[] = {
        "/Applications",
        "/Applications (Mac OS 9)",
        "/System/Library/CoreServices",
        "/System/Library/Extensions",
        "/System/Library/Services"
#ifdef TEST
        , "/Developer/Applications"
        , "/Volumes/audX/Applications (Mac OS 9)"
#endif
    };
    int i, count = sizeof(default_dirs)/sizeof(default_dirs[0]);

    /*
     * create the container, if needed
     */
    if (NULL == dirs) {
        dirs = netsnmp_container_find("directory_container:cstring");
        if (NULL == dirs) {
            snmp_log(LOG_ERR, "couldn't allocate container for dir list\n");
            return;
        }
        dirs->container_name = strdup("directory search list");
        netsnmp_binary_array_options_set(dirs, 1, CONTAINER_KEY_UNSORTED);
    }

    /*
     * add dirs
     */
    for(i = 0; i < count; ++i) {
        char *      tmp;
        /** xxx: get/save the last mod date? */
        if(-1 == stat(default_dirs[i], &stat_buf)) {
            DEBUGMSGTL(("swinst:arch:darwin", "skipping dir %s\n",
                        default_dirs[i]));
            continue;
        }
        DEBUGMSGTL(("swinst:arch:darwin", "adding dir %s\n",
                        default_dirs[i]));
        tmp = strdup(default_dirs[i]);
        if (NULL == tmp) {
            snmp_log(LOG_ERR,"strdup failed\n");
            break;
        }
        CONTAINER_INSERT(dirs, tmp);
    }
}

void
netsnmp_swinst_arch_shutdown( void )
{
    netsnmp_directory_container_free(dirs);
}

/* ---------------------------------------------------------------------
 */

int
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags )
{
    netsnmp_iterator   *it;
    const char         *dir;
    int                 rc;

    DEBUGMSGTL(("swinst:arch:darwin", "load\n"));

    if (NULL == dirs) {
        DEBUGMSGTL(("swinst:arch:darwin", "no dirs to scan!\n"));
        return -1;
    }

    _index = 0;
    
    it = CONTAINER_ITERATOR(dirs);
    for (dir = ITERATOR_FIRST(it); dir; dir = ITERATOR_NEXT(it)) {
        rc = _add_applications_in_dir(container, dir);
    }
    ITERATOR_RELEASE(it);
    DEBUGMSGTL(("swinst:arch:darwin", "loaded %d apps\n",_index));

    return 0;
}

void  _dump_flags(u_long flags)
{
    static struct {
        const char*name;
        u_long bits;
    } names[] = {
        { "kLSItemInfoIsPlainFile", 0x00000001 },
        { "kLSItemInfoIsPackage", 0x00000002 },
        { "kLSItemInfoIsApplication", 0x00000004 },
        { "kLSItemInfoIsContainer", 0x00000008 },
        { "kLSItemInfoIsAliasFile", 0x00000010 },
        { "kLSItemInfoIsSymlink", 0x00000020 },
        { "kLSItemInfoIsInvisible", 0x00000040 },
        { "kLSItemInfoIsNativeApp", 0x00000080 },
        { "kLSItemInfoIsClassicApp", 0x00000100 },
        { "kLSItemInfoAppPrefersNative", 0x00000200 },
        { "kLSItemInfoAppPrefersClassic", 0x00000400 },
        { "kLSItemInfoAppIsScriptable", 0x00000800 },
        { "kLSItemInfoIsVolume", 0x00001000 },
        { "kLSItemInfoExtensionIsHidden", 0x00100000 }
    };
    int i, count = sizeof(names)/sizeof(names[0]);

    for(i = 0; i < count; ++i) {
        if (flags & names[i].bits) {
            DEBUGMSGTL(("swinst:arch:darwin:flags", "\t%s\n",
                       names[i].name));
        }
    }
}

static int
_add_applications_in_dir(netsnmp_container *container, const char* path)
{
    netsnmp_container  *files;
    netsnmp_iterator   *it;
    const char         *file;
    netsnmp_swinst_entry *entry = NULL;
    struct stat	        stat_buf;
    size_t              date_len;
    u_char             *date_buf;
    int                 rc = 0;

    CFStringRef         currentPath = NULL;
    CFURLRef            currentURL = NULL;
    LSItemInfoRecord    itemInfoRecord;
    CFStringRef         prodName = NULL;
    CFStringRef         version = NULL;
    
    DEBUGMSGTL(("swinst:arch:darwin", " adding files from %s\n", path));
    files = netsnmp_directory_container_read(NULL, path, 0);
    if (NULL == files) {
        snmp_log(LOG_ERR, "swinst: could not read directory %s\n", path);
        return -1;
    }

    it = CONTAINER_ITERATOR(files);
    if (NULL == it) {
        snmp_log(LOG_ERR, "could not get iterator\n");
        netsnmp_directory_container_free(files);
        return -1;
    }
    for (file = ITERATOR_FIRST(it);
         file;
         file = ITERATOR_NEXT(it),
             CFRelease(currentPath),
             CFRelease(currentURL)) {

        int                 rc2 = 0;
        
        currentPath =
            CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, file,
                                            kCFStringEncodingUTF8,
                                            kCFAllocatorNull);
        currentURL =
            CFURLCreateWithFileSystemPath(kCFAllocatorDefault, currentPath,
                                          kCFURLPOSIXPathStyle, true); 
        LSCopyItemInfoForURL(currentURL,
                             kLSRequestBasicFlagsOnly|kLSRequestAppTypeFlags,
                             &itemInfoRecord); 
        if((0 == itemInfoRecord.flags) ||
           (kLSItemInfoIsPlainFile == itemInfoRecord.flags) ||
           (itemInfoRecord.flags & kLSItemInfoIsInvisible) ||
           (itemInfoRecord.flags & kLSItemInfoIsAliasFile)) {
            continue;
        }
        /** recurse on non-application containers (i.e. directory) */
        if ((itemInfoRecord.flags & kLSItemInfoIsContainer) &&
            (!(itemInfoRecord.flags & kLSItemInfoIsApplication))) {
            netsnmp_directory_container_read(files, file, 0);
            continue;
       }

        /** skip any other non-application files */
        if (!(itemInfoRecord.flags & kLSItemInfoIsApplication)) {
            continue;
       }

        if ((itemInfoRecord.flags & kLSItemInfoIsPackage) ||           
            (itemInfoRecord.flags & kLSItemInfoIsContainer)) {
            rc2 = _check_bundled_app(currentURL, &prodName, &version, file);
        } 
        else if ((itemInfoRecord.flags & kLSItemInfoIsClassicApp) ||
                 (itemInfoRecord.flags & kLSItemInfoIsPlainFile)) {
            rc2 = _check_classic_app(currentURL, &prodName, &version, file);
        } else {
            snmp_log(LOG_ERR,"swinst shouldn't get here: %s\n", file);
            _dump_flags(itemInfoRecord.flags);
            continue;
        }
        if (rc2) { /* not an app. if directory, recurse; else continue */
            _dump_flags(itemInfoRecord.flags);
            if (1 == rc2)
                netsnmp_directory_container_read(files, file, 0);
            continue;
        }
        
        /*
         * allocate entry
         */
        entry = netsnmp_swinst_entry_create(++_index);
        if (NULL == entry) {
            snmp_log(LOG_ERR, "error creating swinst entry\n");
            rc = -1;
            SNMP_CFRelease(prodName);
            SNMP_CFRelease(version);
            break;
        }

        entry->swName_len =
            snprintf(entry->swName, sizeof(entry->swName),
                     "%s %s", CFStringGetCStringPtr(prodName,0),
                     CFStringGetCStringPtr(version,0));
	if (entry->swName_len >= sizeof(entry->swName))
	    entry->swName_len = sizeof(entry->swName)-1;

        DEBUGMSGTL(("swinst:arch:darwin", "\t%s %s\n", file, entry->swName));

        /** get the last mod date */
        if(stat(file, &stat_buf) != -1) {
            date_buf = date_n_time(&stat_buf.st_mtime, &date_len);
            entry->swDate_len = date_len;
            memcpy(entry->swDate, date_buf, entry->swDate_len);
        }
        
        CONTAINER_INSERT(container, entry);
        entry = NULL;
        SNMP_CFRelease(prodName);
        SNMP_CFRelease(version);
    }
    ITERATOR_RELEASE(it);
    netsnmp_directory_container_free(files);

    return rc;
}

int
_check_bundled_app(CFURLRef currentURL, CFStringRef *prodName,
                   CFStringRef *version, const char* file)
{
    CFBundleRef         theBundle = NULL;
    CFDictionaryRef     infoDict = NULL;
            
    if ((NULL == prodName) || (NULL == version))
       return -1;

    theBundle = CFBundleCreate (kCFAllocatorDefault, currentURL);
    if(theBundle == NULL)
        return -1; /* not a bundle */

    infoDict = CFBundleGetInfoDictionary(theBundle);
    if(0 == CFDictionaryGetCount(infoDict)) {
        SNMP_CFRelease(theBundle);
        return 1; /* directory */
    }

    *prodName = (CFStringRef)
        CFDictionaryGetValue (infoDict, CFSTR("CFBundleName"));
    if (NULL == *prodName) {
        *prodName = (CFStringRef)
            CFDictionaryGetValue (infoDict, CFSTR("CFBundleDisplayName"));
        if (NULL == *prodName) {
            *prodName = (CFStringRef) CFDictionaryGetValue (infoDict,
                                      CFSTR("CFBundleExecutable"));
        }
    }
    if(NULL == *prodName) {
        DEBUGMSGTL(("swinst:arch:darwin", "\tmissing name: %s\n",file));
        /*CFShow(infoDict);*/
        SNMP_CFRelease(theBundle);
        return -1;
    }

    *version = (CFStringRef)
        CFDictionaryGetValue (infoDict, CFSTR("CFBundleShortVersionString"));
    if(NULL == *version) {
        *version = (CFStringRef)
            CFDictionaryGetValue (infoDict, CFSTR("CFBundleVersion"));
        if (*version == NULL) 
            *version = (CFStringRef) CFDictionaryGetValue (infoDict,
                                      CFSTR("CFBundleGetInfoString"));
    }
    if(NULL == *version) {
        DEBUGMSGTL(("swinst:arch:darwin", "\tmissing version: %s\n",file));
        /*CFShow(infoDict);*/
        SNMP_CFRelease(theBundle);
        return -1;
    }
    
    if(theBundle != NULL) {
        CFRetain(*prodName);
        CFRetain(*version);
        SNMP_CFRelease(theBundle);
    }
    return 0;
}

static int
_check_classic_app(CFURLRef currentURL, CFStringRef *prodName,
                   CFStringRef *version, const char* file)
{
    /*
     * get info for classic or single-file apps
     */
    FSRef theFSRef;
    int theResFile;

    if ((NULL == prodName) || (NULL == version))
       return -1;

    *prodName = CFURLCopyLastPathComponent(currentURL);
    if (! CFURLGetFSRef(currentURL, &theFSRef)) {
        DEBUGMSGTL(("swinst:arch:darwin", "GetFSRef failed: %s\n", file));
        SNMP_CFRelease(*prodName);
        return -1;
    }
    theResFile = FSOpenResFile(&theFSRef, fsRdPerm);
    if (ResError() != noErr) {
        DEBUGMSGTL(("swinst:arch:darwin", "FSOpenResFile failed: %s\n", file));
        SNMP_CFRelease(*prodName);
        return -1;
    }
    VersRecHndl versHandle = (VersRecHndl)Get1IndResource('vers', 1);
    if (versHandle != NULL) {
        *version = CFStringCreateWithPascalString(kCFAllocatorDefault,
                       (**versHandle).shortVersion, kCFStringEncodingMacRoman);
        if (*version == NULL) {
            StringPtr longVersionPtr = (**versHandle).shortVersion;
            longVersionPtr = (StringPtr)(((Ptr) longVersionPtr) +
                              1 + ((unsigned char) *longVersionPtr));
            *version = CFStringCreateWithPascalString(kCFAllocatorDefault,
                          longVersionPtr,  kCFStringEncodingMacRoman);
        }
        ReleaseResource((Handle)versHandle);
    }
    CloseResFile(theResFile);
    if(*version == NULL) {
        DEBUGMSGTL(("swinst:arch:darwin",
                    "\tmissing classic/file version: %s\n", file));
        SNMP_CFRelease(*prodName);
        return -1;
    }

    return 0;
}