Use a list head struct when storing the semi-circular lists and

convert to tail queues in the process.  This will allow us to
reverse foreach loops more easily and it makes it clearer which
functions expect a list as opposed to a single member.

Add macros for manipulating lists.  Some of these should become functions.

When freeing up a list, just pop off the last item in the queue instead
of going from head to tail.  This is simpler since we don't have to
stash a pointer to the next member, we always just use the last one
in the queue until the queue is empty.

Rename match functions that take a list to have list in the name.
Break cmnd_matches() into cmnd_matches() and cmndlist_matches.
This commit is contained in:
Todd C. Miller
2007-08-30 17:26:35 +00:00
parent a05abf5c88
commit 8cdea0b941
8 changed files with 307 additions and 191 deletions

134
parse.h
View File

@@ -60,12 +60,40 @@ struct cmndtag {
* pointer of the last entry is NULL and does not point back to the head.
*/
/*
* Tail queue list head structure.
*/
struct defaults_list {
struct defaults *first;
struct defaults *last;
};
struct userspec_list {
struct userspec *first;
struct userspec *last;
};
struct member_list {
struct member *first;
struct member *last;
};
struct privilege_list {
struct privilege *first;
struct privilege *last;
};
struct cmndspec_list {
struct cmndspec *first;
struct cmndspec *last;
};
/*
* Structure describing a user specification and list thereof.
*/
struct userspec {
struct member *user; /* list of users */
struct privilege *privileges; /* list of privileges */
struct member_list users; /* list of users */
struct privilege_list privileges; /* list of privileges */
struct userspec *prev, *next;
};
@@ -73,8 +101,8 @@ struct userspec {
* Structure describing a privilege specification.
*/
struct privilege {
struct member *hostlist; /* list of hosts */
struct cmndspec *cmndlist; /* list of Cmnd_Specs */
struct member_list hostlist; /* list of hosts */
struct cmndspec_list cmndlist; /* list of Cmnd_Specs */
struct privilege *prev, *next;
};
@@ -82,7 +110,7 @@ struct privilege {
* Structure describing a linked list of Cmnd_Specs.
*/
struct cmndspec {
struct member *runaslist; /* list of runas users */
struct member_list runaslist; /* list of runas users */
struct member *cmnd; /* command to allow/deny */
struct cmndtag tags; /* tag specificaion */
struct cmndspec *prev, *next;
@@ -105,7 +133,7 @@ struct member {
struct alias {
char *name; /* alias name */
int type; /* {USER,HOST,RUNAS,CMND}ALIAS */
struct member *first_member; /* list of alias members */
struct member_list members; /* list of alias members */
};
/*
@@ -114,7 +142,7 @@ struct alias {
struct defaults {
char *var; /* variable name */
char *val; /* variable value */
struct member *binding; /* user/host/runas binding */
struct member_list binding; /* user/host/runas binding */
int type; /* DEFAULTS{,_USER,_RUNAS,_HOST} */
int op; /* TRUE, FALSE, '+', '-' */
struct defaults *prev, *next;
@@ -123,6 +151,7 @@ struct defaults {
/*
* Allocate space for a defaults entry and populate it.
*/
#undef NEW_DEFAULT
#define NEW_DEFAULT(r, v1, v2, o) do { \
(r) = emalloc(sizeof(struct defaults)); \
(r)->var = (v1); \
@@ -135,6 +164,7 @@ struct defaults {
/*
* Allocate space for a member and populate it.
*/
#undef NEW_MEMBER
#define NEW_MEMBER(r, n, t) do { \
(r) = emalloc(sizeof(struct member)); \
(r)->name = (n); \
@@ -147,6 +177,7 @@ struct defaults {
* Append one queue (or single entry) to another using the
* circular properties of the prev pointer to simplify the logic.
*/
#undef LIST_APPEND
#define LIST_APPEND(h, e) do { \
void *_tail = (e)->prev; \
(h)->prev->next = (e); \
@@ -154,6 +185,88 @@ struct defaults {
(h)->prev = _tail; \
} while (0)
/*
* Append the list of entries to the head node and convert
* e from a semi-circle queue to normal doubly-linked list.
*/
#undef HEAD_APPEND
#define HEAD_APPEND(h, e) do { \
void *_tail = (e)->prev; \
if ((h).first == NULL) \
(h).first = (e); \
else \
(h).last->next = (e); \
(e)->prev = (h).last; \
(h).last = _tail; \
} while (0)
/*
* Convert from a semi-circle queue to normal doubly-linked list
* with a head node.
*/
#undef LIST2HEAD
#define LIST2HEAD(h, e) do { \
if ((e) != NULL) { \
(h).first = (e); \
(h).last = (e)->prev; \
(e)->prev = NULL; \
} else { \
(h).first = NULL; \
(h).last = NULL; \
} \
} while (0)
#undef LH_FOREACH_FWD
#define LH_FOREACH_FWD(h, v) \
for ((v) = (h).first; (v) != NULL; (v) = (v)->next)
#undef LH_FOREACH_REV
#define LH_FOREACH_REV(h, v) \
for ((v) = (h).last; (v) != NULL; (v) = (v)->prev)
/*
* Pop the last element off the end of h.
* XXX - really should return the popped element.
*/
#undef LH_POP
#define LH_POP(h) do { \
if (!LH_EMPTY(h)) { \
if ((h).first == (h).last) \
(h).first = (h).last = NULL; \
else { \
(h).last = (h).last->prev; \
(h).last->next = NULL; \
} \
} \
} while (0)
#undef LH_INIT
#define LH_INIT(h) do { \
(h).first = NULL; \
(h).last = NULL; \
} while (0)
#undef LH_EMPTY
#define LH_EMPTY(h) ((h).first == NULL)
#undef LH_FIRST
#define LH_FIRST(h) ((h).first)
#undef LH_LAST
#define LH_LAST(h) ((h).last)
#undef LIST_NEXT
#define LIST_NEXT(e) ((e)->next)
#undef LIST_PREV
#define LIST_PREV(e) ((e)->prev)
/*
* Parsed sudoers info.
*/
extern struct userspec_list userspecs;
extern struct defaults_list defaults;
/*
* Prototypes
*/
@@ -161,13 +274,14 @@ char *alias_add __P((char *, int, struct member *));
int addr_matches __P((char *));
int alias_remove __P((char *, int));
int cmnd_matches __P((struct member *));
int cmndlist_matches __P((struct member_list *));
int command_matches __P((char *, char *));
int host_matches __P((struct member *));
int hostlist_matches __P((struct member_list *));
int hostname_matches __P((char *, char *, char *));
int netgr_matches __P((char *, char *, char *, char *));
int no_aliases __P((void));
int runas_matches __P((struct member *));
int user_matches __P((struct passwd *, struct member *));
int runaslist_matches __P((struct member_list *));
int userlist_matches __P((struct passwd *, struct member_list *));
int usergr_matches __P((char *, char *, struct passwd *));
int userpw_matches __P((char *, char *, struct passwd *));
struct alias *find_alias __P((char *, int));