aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/audit/tools/aulast/aulast-llist.c
blob: be05e9764927a8af12bb1d5756d55bcef55b6af1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* aulast-llist.c - Minimal linked list library
* Copyright (c) 2008 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved. 
*
* This software may be freely redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Authors:
*   Steve Grubb <sgrubb@redhat.com>
*/

#include <stdlib.h>
#include <string.h>
#include "aulast-llist.h"

void list_create(llist *l)
{
	l->head = NULL;
	l->cur = NULL;
}

lnode *list_next(llist *l)
{
	if (l->cur == NULL)
		return NULL;
	l->cur = l->cur->next;
	return l->cur;
}

static void list_append(llist *l, lnode *node)
{
	node->next = NULL;

	// if we are at top, fix this up
	if (l->head == NULL)
		l->head = node;
	else if (l->cur) {
		// Make sure we are at the end
		while (l->cur->next)
			l->cur = l->cur->next;

		l->cur->next = node;
	}

	// make newnode current
	l->cur = node;
}

void list_clear(llist* l)
{
	lnode* nextnode;
	register lnode* current;

	current = l->head;
	while (current) {
		nextnode=current->next;
		free((void *)current->name);
		free((void *)current->term);
		free((void *)current->host);
		free(current);
		current=nextnode;
	}
	l->head = NULL;
	l->cur = NULL;
}

int list_create_session(llist *l, uid_t auid, int pid, int session,
	unsigned long serial)
{
	lnode *n = malloc(sizeof(lnode));
	if (n == NULL)
		return 0;
	n->session = session;
	n->start = 0;
	n->end = 0;
	n->auid = auid;
	n->pid = pid;
	n->result = -1;
	n->name = NULL;
	n->term = NULL;
	n->host = NULL;
	n->status = LOG_IN;
	n->loginuid_proof = serial;
	n->user_login_proof = 0;
	n->user_end_proof = 0;
	list_append(l, n);
	return 1;
}

int list_update_start(llist* l, time_t start, const char *host,
	const char *term, int res, unsigned long serial)
{
        register lnode* cur;
	if (l == NULL)
		return 0;

	cur=list_get_cur(l);
	cur->start = start;
	cur->status = SESSION_START;
	if (term)
		cur->term = strdup(term);
	if (host)
		cur->host = strdup(host);
	cur->result = res;
	cur->user_login_proof = serial;
	return 1;
}

int list_update_logout(llist* l, time_t t, unsigned long serial)
{
        register lnode* cur;
	if (l == NULL)
		return 0;

	cur=list_get_cur(l);
	cur->end = t;
	cur->status = LOG_OUT;
	cur->user_end_proof = serial;
	return 1;
}

lnode *list_delete_cur(llist *l)
{
        register lnode *cur, *prev;
                                                                                
       	prev = cur = l->head;	/* start at the beginning */
	while (cur) {
		if (cur == l->cur) {
			if (cur == prev && cur == l->head) {
				l->head = cur->next;
				l->cur = cur->next;
				free((void *)cur->name);
				free((void *)cur->term);
				free((void *)cur->host);
				free(cur);
				prev = NULL;
			} else {
				prev->next = cur->next;
				free((void *)cur->name);
				free((void *)cur->term);
				free((void *)cur->host);
				free(cur);
				l->cur = prev;
			}
			return prev;
		} else {
			prev = cur;
			cur = cur->next;
		}
	}
	return NULL;
}

lnode *list_find_auid(llist *l, uid_t auid, int pid, unsigned int session)
{
        register lnode* cur;
                                                                                
       	cur = l->head;	/* start at the beginning */
	while (cur) {
		if (cur->pid == pid && cur->auid == auid &&
					cur->session == session) {
			l->cur = cur;
			return cur;
		} else
			cur = cur->next;
	}
	return NULL;
}

lnode *list_find_session(llist *l, unsigned int session)
{
        register lnode* cur;
                                                                                
       	cur = l->head;	/* start at the beginning */
	while (cur) {
		if (cur->session == session) {
			l->cur = cur;
			return cur;
		} else
			cur = cur->next;
	}
	return NULL;
}