From owner-p4-projects@FreeBSD.ORG Thu Jul 23 13:56:48 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id C6B9E106568C; Thu, 23 Jul 2009 13:56:47 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 83F9C1065680 for ; Thu, 23 Jul 2009 13:56:47 +0000 (UTC) (envelope-from jona@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 6ADB58FC2F for ; Thu, 23 Jul 2009 13:56:47 +0000 (UTC) (envelope-from jona@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n6NDulf0028369 for ; Thu, 23 Jul 2009 13:56:47 GMT (envelope-from jona@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n6NDukpX028367 for perforce@freebsd.org; Thu, 23 Jul 2009 13:56:46 GMT (envelope-from jona@FreeBSD.org) Date: Thu, 23 Jul 2009 13:56:46 GMT Message-Id: <200907231356.n6NDukpX028367@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to jona@FreeBSD.org using -f From: Jonathan Anderson To: Perforce Change Reviews Cc: Subject: PERFORCE change 166441 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jul 2009 13:56:49 -0000 http://perforce.freebsd.org/chv.cgi?CH=166441 Change 166441 by jona@jona-trustedbsd-belle-vmware on 2009/07/23 13:56:30 Added ua_stat(), ua_access(), ua_opendir() and ua_unmarshall_bytes() Affected files ... .. //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.c#9 edit .. //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.h#11 edit Differences ... ==== //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.c#9 (text+ko) ==== @@ -37,6 +37,7 @@ #include +#include #include #include #include @@ -139,6 +140,64 @@ +int ua_access(const char *path, int mode) +{ + if(angel < 0) angel = ua_find(); + if(angel < 0) return -1; + + struct ua_datum *data[3]; + data[0] = ua_marshall_int(UA_CHECK_ACCESS); + data[1] = ua_marshall_string(path, strlen(path)); + data[2] = ua_marshall_int(mode); + + for(int i = 0; i < 3; i++) if(ua_send(angel, data[i], NULL, 0) < 0) return -1; + + free(data[0]); + free(data[1]); + free(data[2]); + + + + // retrieve the file descriptor(s) + struct ua_datum *d = ua_recv(angel, NULL, NULL); + if(!d) return -1; + + int response; + if(ua_unmarshall_int(d, &response) < 0) return -1; + + return response; +} + + + +int ua_stat(const char *path, struct stat *s) +{ + if(angel < 0) angel = ua_find(); + if(angel < 0) return -1; + + struct ua_datum *data[2]; + data[0] = ua_marshall_int(UA_STAT); + data[1] = ua_marshall_string(path, strlen(path)); + + for(int i = 0; i < 2; i++) + if(ua_send(angel, data[i], NULL, 0) < 0) + return -1; + + free(data[0]); + free(data[1]); + + + + struct ua_datum *d = ua_recv(angel, NULL, NULL); + if(!d) return -1; + + unsigned int len = sizeof(struct stat); + if(ua_unmarshall_bytes(d, (char*) s, &len) < 0) return -1; + + return 0; +} + + int ua_open(const char *path, int flags) { cap_rights_t rights = CAP_SEEK | CAP_FSYNC; @@ -158,8 +217,6 @@ if(angel < 0) angel = ua_find(); if(angel < 0) return -1; - printf("ua_ropen('%s', %i, %016llx)\n", path, flags, rights); - struct ua_datum *data[4]; data[0] = ua_marshall_int(UA_OPEN_PATH); data[1] = ua_marshall_string(path, strlen(path)); @@ -256,7 +313,16 @@ } +DIR* ua_opendir(const char *filename) +{ + int fd = ua_open(filename, O_RDONLY | O_DIRECTORY); + if(fd < 0) return NULL; + + return fdopendir(fd); +} + + int ua_send(int sock, datum *d, int32_t fds[], int32_t fdlen) { // the datum is the I/O vector @@ -434,6 +500,16 @@ int ua_unmarshall_string(const datum *d, char *value, unsigned int *len) { + (*len)--; + ua_unmarshall_bytes(d, value, len); + value[*len] = '\0'; + + return d->length; +} + + +int ua_unmarshall_bytes(const datum *d, char *value, unsigned int *len) +{ if(d == NULL) { errno = EINVAL; @@ -448,7 +524,7 @@ return -1; } } - else if(d->length >= *len) + else if(d->length > *len) { errno = EOVERFLOW; return -1; @@ -456,7 +532,6 @@ *len = d->length; memcpy(value, ((const char*) d) + sizeof(datum), d->length); - value[*len] = '\0'; return d->length; } ==== //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.h#11 (text+ko) ==== @@ -35,6 +35,9 @@ #define _LIBUSERANGEL_H_ #include +#include + +#include #include #include @@ -53,6 +56,12 @@ /** Set the user angel */ void ua_set(int fd); +/** Check access rights via the User Angel */ +int ua_access(const char *access, int mode); + +/** Check file status via the User Angel */ +int ua_stat(const char *path, struct stat *s); + /** Open a file via the User Angel */ int ua_open(const char *path, int flags); @@ -62,6 +71,9 @@ /** Open a file stream via the User Angel */ FILE* ua_fopen(const char *path, const char *mode); +/** Open a directory via the User Angel */ +DIR* ua_opendir(const char *filename); + /* Low-level API */ @@ -69,7 +81,10 @@ enum ua_request_t { UA_NO_OP = 0, /* do nothing (useful for debugging) */ + UA_CHECK_ACCESS, /* access() substitute */ + UA_STAT, /* stat() substitute */ UA_OPEN_PATH, /* open() substitute */ + UA_OPEN_DIR, /* opendir() substitute */ UA_LOAD_LIBRARY, /* load a shared library */ UA_POWERBOX /* ask the user for file descriptor(s) */ }; @@ -114,6 +129,7 @@ /* Unmarshalling functions; return the number of bytes unmarshalled (or -1) */ int ua_unmarshall_int(const struct ua_datum *d, int32_t *value); +int ua_unmarshall_bytes(const struct ua_datum *d, char *value, unsigned int *len); int ua_unmarshall_string(const struct ua_datum *d, char *value, unsigned int *len); int ua_unmarshall_error(const struct ua_datum *d, int *errnum, char *msg, int *msglen); int ua_unmarshall_powerbox(const struct ua_datum *d, struct ua_powerbox_options *options);