mach_client_utilities.c [plain text]
#include <CoreFoundation/CoreFoundation.h>
#include <Kerberos/KerberosDebug.h>
#include <mach/mach.h>
#include <mach/boolean.h>
#include <mach/mach_error.h>
#include <mach/notify.h>
#include <servers/bootstrap.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pwd.h>
#include <Kerberos/mach_client_utilities.h>
kern_return_t __KerberosIPCError (kern_return_t inError, const char *function, const char *file, int line)
{
if ((inError != KERN_SUCCESS) && (ddebuglevel () > 0)) {
dprintf ("%s() got %d ('%s') at %s: %d", function, inError, mach_error_string (inError), file, line);
dprintf ("Session is %s", LoginSessionGetSecuritySessionName ());
}
return inError;
}
#pragma mark -
static kern_return_t
mach_client_check_server_registration (mach_port_t inBootstrapPort,
const char *inServiceName,
const char *inServerPath)
{
kern_return_t err = KERN_SUCCESS;
boolean_t active = FALSE;
if (inBootstrapPort == MACH_PORT_NULL) { err = EINVAL; }
if (inServiceName == NULL ) { err = EINVAL; }
if (inServerPath == NULL ) { err = EINVAL; }
if (err == KERN_SUCCESS) {
err = bootstrap_status (inBootstrapPort, (char *) inServiceName, &active);
if ((err != KERN_SUCCESS) || (active == BOOTSTRAP_STATUS_INACTIVE)) {
if (!LoginSessionIsRootSession ()) { mach_port_t serverPrivPort = MACH_PORT_NULL;
mach_port_t servicePort = MACH_PORT_NULL;
uid_t serverUID = LoginSessionGetSessionUID ();
if (serverUID == 0 ) { serverUID = LoginSessionGetSecurityAgentUID (); }
err = bootstrap_create_server (inBootstrapPort, (char *) inServerPath, serverUID, TRUE , &serverPrivPort);
dprintf ("mach_client_check_server_registration(): bootstrap_create_server() returned err %d", err);
if (err == KERN_SUCCESS) {
err = bootstrap_create_service (serverPrivPort, (char *) inServiceName, &servicePort);
dprintf ("mach_client_check_server_registration(): bootstrap_create_service() returned err %d", err);
}
if (err == KERN_SUCCESS) {
dprintf ("mach_client_check_server_registration(): registered '%s' for service '%s' and uid '%d'.",
inServerPath, inServiceName, serverUID);
}
if (serverPrivPort != MACH_PORT_NULL) { mach_port_deallocate (mach_task_self (), serverPrivPort); }
if (servicePort != MACH_PORT_NULL) { mach_port_deallocate (mach_task_self (), servicePort); }
}
}
}
return KerberosIPCError_ (err);
}
kern_return_t
mach_client_lookup_server (const char *inServiceName,
mach_port_t *outServicePort)
{
kern_return_t err = KERN_SUCCESS;
mach_port_t bootstrapPort = MACH_PORT_NULL;
boolean_t active = FALSE;
if (inServiceName == NULL) { err = EINVAL; }
if (outServicePort == NULL) { err = EINVAL; }
if (err == KERN_SUCCESS) {
err = task_get_bootstrap_port (mach_task_self (), &bootstrapPort);
}
if (err == KERN_SUCCESS) {
err = bootstrap_status (bootstrapPort, (char *) inServiceName, &active);
}
if (err == KERN_SUCCESS) {
if (active != BOOTSTRAP_STATUS_ACTIVE) { err = BOOTSTRAP_UNKNOWN_SERVICE; }
}
if (err == KERN_SUCCESS) {
err = bootstrap_look_up (bootstrapPort, (char *) inServiceName, outServicePort);
}
if (bootstrapPort != MACH_PORT_NULL) { mach_port_deallocate (mach_task_self (), bootstrapPort); }
if (err == BOOTSTRAP_UNKNOWN_SERVICE) {
return err; } else {
return KerberosIPCError_ (err);
}
}
kern_return_t
mach_client_lookup_and_launch_server (const char *inServiceName,
const char *inServerPath,
mach_port_t *outServicePort)
{
kern_return_t err = KERN_SUCCESS;
mach_port_t bootstrapPort = MACH_PORT_NULL;
if (inServiceName == NULL) { err = EINVAL; }
if (inServerPath == NULL) { err = EINVAL; }
if (outServicePort == NULL) { err = EINVAL; }
if (err == KERN_SUCCESS) {
err = task_get_bootstrap_port (mach_task_self (), &bootstrapPort);
}
if (err == KERN_SUCCESS) {
err = mach_client_check_server_registration (bootstrapPort, inServiceName, inServerPath);
}
if (err == KERN_SUCCESS) {
err = bootstrap_look_up (bootstrapPort, (char *) inServiceName, outServicePort);
}
if (bootstrapPort != MACH_PORT_NULL) { mach_port_deallocate (mach_task_self (), bootstrapPort); }
return KerberosIPCError_ (err);
}
#pragma mark -