I had done some touchup to the A stand alone policy rule engine rule verifier and hadn't recorded why. So, I'm just going to dump the current code here. Note that I did make the changes to let the user string just be a string...
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
typedef struct {
int uid;
int gid;
int day;
int hour;
char *user;
char *group;
char *path;
char *extension;
} policyAttributes_t;
int
main(int argc, char *argv[])
{
int i;
int iFlags = 0;
int ch;
int iFoundSome = 0;
policyAttributes_t pat;
char path[1024];
char rules[1204];
int rc = 0;
char *szJunk;
/*
* Do some initialization.
*/
memset(&pat, '\0', sizeof(pat));
pat.uid = 502;
pat.gid = 100;
/*
* Should pull from time of day ...
*/
pat.day = 1;
pat.hour = 12;
rules[0] = '\0';
while ((ch = getopt(argc, argv, "?vr:p:u:g:i:h:d:e:U:G:")) != -1) {
iFoundSome = 1;
switch (ch) {
case 'd' :
pat.day = (int)strtol(optarg, &szJunk, 0);
/*
* Do not worry about shorter months - buyers beware!
*/
if (szJunk[0] != '\0' || pat.day < 0 || pat.hour > 31) {
fprintf(stderr, "Invalid day = %s\n", optarg);
rc = EINVAL;
goto cleanup;
}
break;
case 'e' :
fprintf(stdout, "The ext is %s!\n", optarg);
break;
case 'g' :
pat.gid = (int)strtol(optarg, &szJunk, 0);
if (szJunk[0] != '\0' || pat.gid < 0) {
fprintf(stderr, "Invalid gid = %s\n", optarg);
rc = EINVAL;
goto cleanup;
}
break;
case 'G' :
pat.group = strdup(optarg);
if (pat.group == NULL) {
fprintf(stderr, "Out of memory on group name\n");
rc = ENOMEM;
goto cleanup;
}
break;
case 'h' :
pat.hour = (int)strtol(optarg, &szJunk, 0);
if (szJunk[0] != '\0' || pat.hour < 0 || pat.hour > 23) {
fprintf(stderr, "Invalid hour = %s\n", optarg);
rc = EINVAL;
goto cleanup;
}
break;
case 'i' :
fprintf(stdout, "in the %s,\n", optarg);
break;
case 'p' :
fprintf(stdout, "with the %s!\n", optarg);
break;
case 'r' :
fprintf(stdout, "The rules are in %s!\n", optarg);
break;
case 'u' :
pat.uid = (int)strtol(optarg, &szJunk, 0);
if (szJunk[0] != '\0' || pat.uid < 0) {
fprintf(stderr, "Invalid uid = %s\n", optarg);
rc = EINVAL;
goto cleanup;
}
break;
case 'U' :
pat.user = strdup(optarg);
if (pat.user == NULL) {
fprintf(stderr, "Out of memory on user name\n");
rc = ENOMEM;
goto cleanup;
}
break;
case 'v' :
fprintf(stdout, "Oh, be chatty!\n");
break;
case '?' :
default :
goto usage;
}
}
if (!iFoundSome) {
goto usage;
}
#if 0
if (rules[0] == '\0') {
fprintf(stderr, "Need to supply a policy ruleset file!\n");
goto usage;
}
#endif
argc -= optind;
argv += optind;
goto cleanup;
usage:
fprintf(stderr,
"speadm explain -r rules-file [-v]"
" [-p proposed-filename] [-u uid] [-g gid]"
" [-U user] [-G group] [-i ip]"
" [-h hour] [-d day]\n");
rc = 1;
cleanup:
if (pat.user) {
free(pat.user);
}
if (pat.group) {
free(pat.group);
}
return (rc);
}
Please use uid_t/gid_t for uid/gid we haven't used int for those in Solaris since (IIRC) around 2.5.1.
Posted by Darren Moffat on January 16, 2008 at 11:50 AM CST #
Darren,
Again, the disclaimer is that this is prototype code and one of the intents is to show how code changes during the life cycle of a project.
One of the things you should realize from my blog entries is that I will admit to doing braindead things to help others along. Sometimes I will even do them on purpose.
As such, having int as the type right now, or 1024 as the size of
the directory paths is part of the experience. Both have the potential to create interesting problems that need to be solved...
BTW: My current plan is that the final cut is to be code reviewed and the reviews shared here.
Thanks,
Tom
Posted by Tom Haynes on January 16, 2008 at 12:05 PM CST #