|Main index||Section 3||Options|
The application capability (represented by the cap_channel_t type) is a communication channel between the caller and the casper process daemon or an instance of one of its services. A capability to the casper process obtained with the cap_init() function allows to create capabilities to casper's services via the cap_service_open() function.
The cap_init() function opens capability to the casper process.
The cap_wrap() function creates cap_channel_t based on the given socket. The function is used when capability is inherited through execve(2) or send over unix(4) domain socket as a regular file descriptor and has to be represented as cap_channel_t again.
The cap_unwrap() function is the opposite of the cap_wrap() function. It frees the cap_channel_t structure and returns unix(4) domain socket associated with it.
The cap_clone() function clones the given capability.
The cap_close() function closes the given capability.
The cap_limit_get() function stores current limits of the given capability in the limitsp argument. If the function return 0 and NULL is stored in limitsp it means there are no limits set.
The cap_limit_set() function sets limits for the given capability. The limits are provided as nvlist. The exact format depends on the service the capability represents.
The cap_send_nvlist() function sends the given nvlist over the given capability. This is low level interface to communicate with casper services. Most services should provide higher level API.
The cap_recv_nvlist() function receives the given nvlist over the given capability. The flags argument defines what type the top nvlist is expected to be. If the nvlist flags do not match the flags passed to cap_recv_nvlist(), the nvlist will not be returned.
The cap_xfer_nvlist() function sends the given nvlist, destroys it and receives new nvlist in response over the given capability. The flags argument defines what type the top nvlist is expected to be. If the nvlist flags do not match the flags passed to cap_xfer_nvlist(), the nvlist will not be returned. It does not matter if the function succeeds or fails, the nvlist given for sending will always be destroyed once the function returns.
The cap_service_open() function opens casper service of the given name through casper capability obtained via the cap_init() function. The function returns capability that provides access to opened service.
The cap_limit_get(), cap_limit_set() and cap_send_nvlist() functions return -1 and set the errno variable on failure.
The cap_close(), cap_sock() and cap_unwrap() functions always succeed.
cap_channel_t *capcas, *capdns; nvlist_t *limits; const char *ipstr = "127.0.0.1"; struct in_addr ip; struct hostent *hp;
/* Open capability to the Casper. */ capcas = cap_init(); if (capcas == NULL) err(1, "Unable to contact Casper");
/* Enter capability mode sandbox. */ if (cap_enter() < 0 && errno != ENOSYS) err(1, "Unable to enter capability mode");
/* Use Casper capability to create capability to the system.dns service. */ capdns = cap_service_open(capcas, "system.dns"); if (capdns == NULL) err(1, "Unable to open system.dns service");
/* Close Casper capability, we don't need it anymore. */ cap_close(capcas);
/* Limit system.dns to reverse DNS lookups and IPv4 addresses. */ limits = nvlist_create(0); nvlist_add_string(limits, "type", "ADDR"); nvlist_add_number(limits, "family", (uint64_t)AF_INET); if (cap_limit_set(capdns, limits) < 0) err(1, "Unable to limit access to the system.dns service");
/* Convert IP address in C-string to in_addr. */ if (!inet_aton(ipstr, &ip)) errx(1, "Unable to parse IP address %s.", ipstr);
/* Find hostname for the given IP address. */ hp = cap_gethostbyaddr(capdns, (const void *)&ip, sizeof(ip), AF_INET); if (hp == NULL) errx(1, "No name associated with %s.", ipstr);
printf("Name associated with %s is %s. , ipstr, hp->h_name);
|LIBCASPER (3)||February 25, 2016|
|Main index||Section 3||Options|
|“||As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.||”|
|— Maurice Wilkes|