Open System Services Programmer's Guide

Example 59 Using acl() and aclsort()
/* This program provides simple examples of acl(2) and aclsort(3) usage.
* It adds a GROUP ACL entry (with read permissions) to the ACL of the
* file. The file pathname and group ID number are passed as command
* arguments.
* To run:
* addACLgroup <pathname> <group ID number>
* This program performs the following steps:
* 1. Acquires the count of ACL entries in the ACL on the file
* using acl(ACL_CNT).
* 2. Allocates memory for the ACL buffer using malloc().
* 3. Acquires the existing ACL on the file using acl(ACL_GET).
* 4. Adds a new GROUP ACL entry to the end of the ACL buffer.
* 5. Calls aclsort() to sort the ACL entries in the ACL buffer
* into the proper order.
* 6. Sets the new ACL on the file using acl(ACL_SET).
* If you run this program twice on the same file, it will report
* an error in aclsort() as you are trying to add a second group ACL entry
* for the same group id. aclsort() points to the ACL entry in error.
*/
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <acl.h>
#include <errno.h>
#define READPERM 4
#define CALCCLASS 1
typedef struct acl acl_t;
void printAcl( char *header, acl_t *aclEnt, int count )
{
int i;
printf("%s\n",header);
for (i= 0; i < count; i++) {
printf("acl entry %d ", i);
printf("\ta_type = %d ", aclEnt[i].a_type );
printf("\ta_id = %d ", aclEnt[i].a_id );
printf("\ta_perm = %o\n", aclEnt[i].a_perm );
}
}
main( int argc, char *argv[])
{
acl_t *aclEnt = 0; /* pointer to ACL buffer */
char *pathname = 0; /* pointer to pathanme */
int prevCount, newCount; /* counts of ACL entries */
int groupId; /* group ID number for new ACL entry */
int error; /* error variable */
pathname = argv[1]; /* get ptr to pathname command argument */
groupId = atoi(argv[2]); /* get groupId command argument value */
printf("Input pathname = %s, input groupId = %d\n", pathname, groupId);
/* find out how many ACL entries in the existing ACL on the object */
if (( prevCount = acl(pathname, ACL_CNT, NACLENTRIES, aclEnt)) == -1 ) {
printf("acl(ACL_CNT) error= %d, text = %s\n", errno, strerror(errno));
return 1;
}
Using OSS Access Control Lists (ACLs) 269