/** ** abac_util.c **/ #include #include #include #include #include #include #include #include "abac_util.h" static int debug=0; #define SHA1_LENGTH 40 #define ABAC_VERSION "0.2.1" char *abac_version() { return ABAC_VERSION; } void abac_errx(int eval, const char *msg) { errx(eval,"%s",msg); } /** * Malloc, fatal on error. */ void *abac_xmalloc(size_t size) { void *ret; ret = malloc(size); if (ret == NULL) err(1, "malloc"); return ret; } void *abac_xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); if (ret == NULL) err(1, "couldn't realloc %d bytes\n", size); return ret; } /** * strdup fatal on error */ char *abac_xstrdup(char *source) { char *ret; if (source == NULL) { return NULL; } ret = strdup(source); if (ret == NULL) err(1, "strdup"); return ret; } /* make a brand new string with or without being trimmed */ char *abac_trim_quotes(char *string) { char *tmp=abac_xstrdup(string); int len=strlen(tmp); if( (tmp[len-1] == '\'' || tmp[len-1] == '"') && (tmp[0] == '\'' || tmp[0] == '"')) { tmp[len-1]='\0'; char *ntmp=abac_xstrdup(&tmp[1]); free(tmp); return ntmp; } return tmp; } /** * Split a string based on the given delimiter. */ void abac_split(char *string, char *delim, char **ret, int *num) { int len = strlen(delim); char *start = string; int count = 0; // split the string by the delim while ((start = strstr(string, delim)) != NULL) { *start = 0; ret[count++] = string; string = start + len; } ret[count++] = string; *num = count; } int abac_validate_clean_name(char *string) { int i; assert(string != NULL); // must start with a letter/number if (!isalnum(string[0])) return 0; // Name must be alphanumeric or - for (i = 1; string[i] != '\0'; ++i) if (!isalnum(string[i]) && string[i] != '-') return 0; return 1; } /* check up to first ( if its there.. a.role(this) a.role(?,?Year:[100..200],?Value:[one,two,three]) a.role(isi) */ int abac_validate_clean_aspect_name(char *string) { int i; assert(string != NULL); // must start with a letter/number if (!isalnum(string[0])) return 0; // Name must be alphanumeric or - or _ up to the ( if there is one for (i = 1; string[i] != '\0' && string[i] != '(' ; ++i) if (!isalnum(string[i]) && string[i] != '-' && string[i] != '_' ) return 0; return 1; } chunk_t abac_generate_serial() { chunk_t serial = chunk_empty; // create a serial (stolen from strongswan pki) rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK); if (!rng) errx(1, "no random number generator"); rng->allocate_bytes(rng, 8, &serial); while (serial.ptr[0] == 0) { // don't get leading 0's rng->get_bytes(rng, 1, serial.ptr); } rng->destroy(rng); if(debug) printf( "abac_generate_serial, serial generated is %#B\n", &serial); return serial; } // validate a princpal's name // makes sure it's a valid SHA1 identifier // return values: // success: malloc'd copy with all hex digits lowercase // fail: NULL char *abac_validate_principal(char *keyid) { int i; char *copy = NULL; if (strlen(keyid) != SHA1_LENGTH) return NULL; int len = strlen(keyid) + 1; copy = abac_xmalloc(len); sprintf(copy, "%s", keyid); i=0; for (; i < SHA1_LENGTH; ++i) { copy[i] = tolower(copy[i]); if (!isxdigit(copy[i])) goto error; } return copy; error: free(copy); return NULL; } // parse&validate the validity int abac_validate_validity_string(char *validity_str) { int validity=0; if (validity_str != NULL) { char suffix = 'd'; // default suffix is days int multiplier; int len = strlen(validity_str); assert(len > 0); // get the suffix char if it's alphabetical if (isalpha(validity_str[len - 1])) { suffix = validity_str[len - 1]; // truncate validity_str[len - 1] = '\0'; --len; // make sure it's not only a suffix if (len == 0) { errx(1,"Invalid validity\n"); } } // convert the suffix to a multiplier switch(suffix) { case 's': multiplier = 1; break; case 'm': multiplier = 60; break; case 'h': multiplier = 3600; break; case 'd': multiplier = 86400; break; case 'y': multiplier = 31536000; break; default: errx(1,"Invalid suffix, must be s m h d y\n"); } // ascii to int char *end; validity = strtol(validity_str, &end, 10); if (end - validity_str < len) { errx(1,"Invalid validity\n"); } if (validity <= 0) { errx(1,"Invalid validity: must be > 0\n"); } // multiply! validity *= multiplier; } return validity; } int file_exist(char *filename) { struct stat stbuf; if(stat(filename,&stbuf) == -1) return 0; return 1; }