static void initialize_logging(void)
{
    logclient = asl_open(NULL, kmDNSHelperServiceName, (opt_debug ? ASL_OPT_STDERR : 0));
    if (NULL == logclient) {
        fprintf(stderr, "Could not initialize ASL logging.\n");
        fflush(stderr);
        return;
    }
    if (opt_debug) asl_set_filter(logclient, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG));
}
Exemple #2
0
/*
 * Open and set up system logging.
 */
void
auditd_openlog(int debug, gid_t gid)
{
	uint32_t opt = 0;	
	char *cp = NULL;

	if (debug)
		opt = ASL_OPT_STDERR;

	au_aslclient = asl_open("auditd", "com.apple.auditd", opt);
	au_aslmsg = asl_new(ASL_TYPE_MSG); 

#ifdef ASL_KEY_READ_UID
	/*
	 * Make it only so the audit administrator and members of the audit
	 * review group (if used) have access to the auditd system log messages.
	 */
	asl_set(au_aslmsg, ASL_KEY_READ_UID, "0");
	asprintf(&cp, "%u", gid);	
	if (cp != NULL) {
#ifdef ASL_KEY_READ_GID
		asl_set(au_aslmsg, ASL_KEY_READ_GID, cp);
#endif
		free(cp);
	}
#endif

	/*
	 * Set the client-side system log filtering.
	 */
	if (debug)
		asl_set_filter(au_aslclient,
		    ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG));
	else
		asl_set_filter(au_aslclient,
		    ASL_FILTER_MASK_UPTO(ASL_LEVEL_INFO)); 	
}
Exemple #3
0
CAMLprim value stub_asl_add_output_file(
  value t, value fd, value msg_fmt, value time_fmt, value level_up_to
) {
  CAMLparam5(t, fd, msg_fmt, time_fmt, level_up_to);
  CAMLlocal1(result);
  aslclient c_asl = Asl_val(t);
  int c_descriptor = Int_val(fd); /* assume type Unix.file_descr = int */
  const char *c_msg_fmt = String_val(msg_fmt);
  const char *c_time_fmt = String_val(time_fmt);
  int c_filter = ASL_FILTER_MASK_UPTO(Int_val(level_up_to));
  int c_text_encoding = ASL_ENCODE_SAFE;
  result = Val_int(0); /* false */
  if (asl_add_output_file(c_asl, c_descriptor, c_msg_fmt, c_time_fmt, c_filter, c_text_encoding) == 0) {
    result = Val_int(1); /* true */
  }
  CAMLreturn(result);
}
Exemple #4
0
void
openlog(const char *ident, int opts, int logfac)
{
	const char *facility;
	uint32_t asl_opts;

	pthread_mutex_lock(&_sl_lock);

	/* close existing aslclient */
	if (_sl_asl != NULL) asl_close(_sl_asl);
	_sl_asl = NULL;

	if (_sl_ident != NULL) free(_sl_ident);
	_sl_ident = NULL;

	/* open with specified parameters */

	if (ident != NULL) _sl_ident = strdup(ident);
	/* NB we allow the strdup to fail silently */

	_sl_fac = logfac;
	facility = asl_syslog_faciliy_num_to_name(_sl_fac);

	_sl_opts = opts;
	asl_opts = ASL_OPT_SYSLOG_LEGACY;

	if (_sl_opts & LOG_NO_NOTIFY) asl_opts |= ASL_OPT_NO_REMOTE;
	if (_sl_opts & LOG_PERROR) asl_opts |= ASL_OPT_STDERR;

	_sl_mask = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG);

	_sl_asl = asl_open(_sl_ident, facility, asl_opts);
	asl_set_filter(_sl_asl, _sl_mask);

	pthread_mutex_unlock(&_sl_lock);
}
Exemple #5
0
aslclient
asl_open_from_file(int fd, const char *ident, const char *facility)
{
	char *name, *x;
	asl_client_t *asl;
	uint32_t status;

	asl = (asl_client_t *)calloc(1, sizeof(asl_client_t));
	if (asl == NULL)
	{
		errno = ENOMEM;
		return NULL;
	}

	asl->options = ASL_OPT_NO_REMOTE;
	asl->sock = -1;

	asl->pid = getpid();
	asl->uid = getuid();
	asl->gid = getgid();

	asl->filter = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG);

	if (ident != NULL)
	{
		asl->name = strdup(ident);
		if (asl->name == NULL)
		{
			free(asl);
			return NULL;
		}
	}
	else
	{
		name = *(*_NSGetArgv());
		if (name != NULL)
		{
			x = strrchr(name, '/');
			if (x != NULL) x++;
			else x = name;
			asl->name = strdup(x);
			if (asl->name == NULL)
			{
				free(asl);
				return NULL;
			}
		}
	}

	asl->facility = NULL;
	if (facility != NULL) asl->facility = strdup(facility);
	else asl->facility = strdup(asl_syslog_faciliy_num_to_name(LOG_USER));
	if (asl->facility == NULL)
	{
		if (asl->name != NULL) free(asl->name);
		free(asl);
		return NULL;
	}

	status = asl_file_open_write_fd(fd, &(asl->aslfile));
	if (status != ASL_STATUS_OK)
	{
		if (asl->name != NULL) free(asl->name);
		if (asl->facility != NULL) free(asl->facility);
		free(asl);
		return NULL;
	}

	asl->aslfileid = 1;
	asl->refcount = 1;

	return (aslclient)asl;
}
Exemple #6
0
aslclient
asl_open(const char *ident, const char *facility, uint32_t opts)
{
	char *name, *x;
	asl_client_t *asl;

	asl = (asl_client_t *)calloc(1, sizeof(asl_client_t));
	if (asl == NULL)
	{
		errno = ENOMEM;
		return NULL;
	}

	asl->options = opts;

	asl->sock = -1;

	_asl_global_init();

	asl->pid = getpid();
	asl->uid = getuid();
	asl->gid = getgid();

	asl->filter = ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE);

	if (ident != NULL)
	{
		asl->name = strdup(ident);
		if (asl->name == NULL)
		{
			if (asl->sock >= 0) close(asl->sock);
			free(asl);
			return NULL;
		}
	}
	else
	{
		name = *(*_NSGetArgv());
		if (name != NULL)
		{
			x = strrchr(name, '/');
			if (x != NULL) x++;
			else x = name;
			asl->name = strdup(x);
			if (asl->name == NULL)
			{
				if (asl->sock >= 0) close(asl->sock);
				free(asl);
				return NULL;
			}
		}
	}

	asl->facility = NULL;
	if (facility != NULL) asl->facility = strdup(facility);
	else asl->facility = strdup(asl_syslog_faciliy_num_to_name(LOG_USER));
	if (asl->facility == NULL)
	{
		if (asl->sock >= 0) close(asl->sock);
		if (asl->name != NULL) free(asl->name);
		free(asl);
		return NULL;
	}

	if (!(asl->options & ASL_OPT_NO_REMOTE)) _asl_notify_open(1);

	if (asl->options & ASL_OPT_STDERR) asl_add_output((aslclient)asl, fileno(stderr), ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_ENCODE_SAFE);

	asl->refcount = 1;

	return (aslclient)asl;
}
Exemple #7
0
int main(int argc, char *argv[])
{
    OSStatus            err;
    int                 argIndex;
    Boolean             delay;
    Boolean             waitForWindowServerSession;
    Boolean             forceShow;
    Boolean             cleanExit;
    IBNibRef            nibRef;
    WindowRef           window;
    
    // Initialise our ASL state.

    gASLClient = asl_open(NULL, "PreLoginAgents", 0);
    assert(gASLClient != NULL);
    
    (void) asl_set_filter(gASLClient, ASL_FILTER_MASK_UPTO(ASL_LEVEL_INFO));
    
    gASLMessage = asl_new(ASL_TYPE_MSG);
    assert(gASLMessage != NULL);

    (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Start");

    // Parse our arguments.  We support arguments that allow you to turn off 
    // various special cases within the code.  This makes it easy to test whether 
    // the special cases are still required.
    
    delay = false;
    waitForWindowServerSession = false;
    forceShow = true;
    cleanExit = true;
    for (argIndex = 1; argIndex < argc; argIndex++) {
        if ( strcasecmp(argv[argIndex], "--nodelay") == 0 ) {
            delay = false;
        } else if ( strcasecmp(argv[argIndex], "--delay") == 0 ) {
            delay = true;
        } else if ( strcasecmp(argv[argIndex], "--nowait") == 0 ) {
            waitForWindowServerSession = false;
        } else if ( strcasecmp(argv[argIndex], "--wait") == 0 ) {
            waitForWindowServerSession = true;
        } else if ( strcasecmp(argv[argIndex], "--noforce") == 0 ) {
            forceShow = false;
        } else if ( strcasecmp(argv[argIndex], "--force") == 0 ) {
            forceShow = true;
        } else if ( strcasecmp(argv[argIndex], "--nocleanexit") == 0 ) {
            cleanExit = false;
        } else if ( strcasecmp(argv[argIndex], "--cleanexit") == 0 ) {
            cleanExit = true;
        } else {
            (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Unrecognised argument '%s'", argv[argIndex]);
        }
    }

    // Handle various options.
    
    if (waitForWindowServerSession) {
        WaitForWindowServerSession();
    } else {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Not waiting for CGSessionCopyCurrentDictionary");
    }
    
    if (delay) {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Delaying");
        
        sleep(3);
    } else {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Not delaying");
    }
    
    // Set up our UI.
    
    err = CreateNibReference(CFSTR("main"), &nibRef);
    assert(err == noErr);

    err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
    assert(err == noErr);

    DisposeNibReference(nibRef);

    // We have to set kHIWindowBitCanBeVisibleWithoutLogin to let the 
    // system know that we're not accidentally trying to display a window 
    // pre-login.
    //
    // Also, window is a utility window and, by default, these have the 
    // kWindowHideOnSuspendAttribute attribute set.  As our application is a 
    // UI element which never activates, and we want our panel to show regardless, 
    // we must clear kWindowHideOnSuspendAttribute.
    
    static const int    kAttributesToSet[] = {
        kHIWindowBitCanBeVisibleWithoutLogin, 
        0
    };
    static const int    kAttributesToClear[] = {
        kHIWindowBitHideOnSuspend, 
        0
    };
    err = HIWindowChangeAttributes(
        window,
        kAttributesToSet,
        kAttributesToClear
    );
    assert(err == noErr);

    // Due to a problem with the relationship between the UI frameworks and the 
    // window server <rdar://problem/5136400>, ShowWindow is not sufficient to 
    // show the window.  We also have to use BringToFront.
    
    ShowWindow(window);
    if (forceShow) {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Showing window with extreme prejudice");
        BringToFront(window);
    } else {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Showing window normally");
    }

    // Set up our SIGTERM handler.
    
    if (cleanExit) {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Installing SIGTERM handler");
        InstallHandleSIGTERMFromRunLoop();
    } else {
        (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Not installing SIGTERM handler");
    }

    // Go go gadget Carbon!

    (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "RunApplicationEventLoop");
    RunApplicationEventLoop();

    (void) asl_log(gASLClient, gASLMessage, ASL_LEVEL_INFO, "Stop");
    
    return EXIT_SUCCESS;
}