Imported Upstream version 2.4.3
[platform/upstream/audit.git] / tools / aulast / aulast-llist.c
1 /*
2 * aulast-llist.c - Minimal linked list library
3 * Copyright (c) 2008 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved. 
5 *
6 * This software may be freely redistributed and/or modified under the
7 * terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Authors:
21 *   Steve Grubb <sgrubb@redhat.com>
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include "aulast-llist.h"
27
28 void list_create(llist *l)
29 {
30         l->head = NULL;
31         l->cur = NULL;
32 }
33
34 lnode *list_next(llist *l)
35 {
36         if (l->cur == NULL)
37                 return NULL;
38         l->cur = l->cur->next;
39         return l->cur;
40 }
41
42 static void list_append(llist *l, lnode *node)
43 {
44         node->next = NULL;
45
46         // if we are at top, fix this up
47         if (l->head == NULL)
48                 l->head = node;
49         else if (l->cur) {
50                 // Make sure we are at the end
51                 while (l->cur->next)
52                         l->cur = l->cur->next;
53
54                 l->cur->next = node;
55         }
56
57         // make newnode current
58         l->cur = node;
59 }
60
61 void list_clear(llist* l)
62 {
63         lnode* nextnode;
64         register lnode* current;
65
66         current = l->head;
67         while (current) {
68                 nextnode=current->next;
69                 free((void *)current->name);
70                 free((void *)current->term);
71                 free((void *)current->host);
72                 free(current);
73                 current=nextnode;
74         }
75         l->head = NULL;
76         l->cur = NULL;
77 }
78
79 int list_create_session(llist *l, uid_t auid, int pid, int session,
80         unsigned long serial)
81 {
82         lnode *n = malloc(sizeof(lnode));
83         if (n == NULL)
84                 return 0;
85         n->session = session;
86         n->start = 0;
87         n->end = 0;
88         n->auid = auid;
89         n->pid = pid;
90         n->result = -1;
91         n->name = NULL;
92         n->term = NULL;
93         n->host = NULL;
94         n->status = LOG_IN;
95         n->loginuid_proof = serial;
96         n->user_login_proof = 0;
97         n->user_end_proof = 0;
98         list_append(l, n);
99         return 1;
100 }
101
102 int list_update_start(llist* l, time_t start, const char *host,
103         const char *term, int res, unsigned long serial)
104 {
105         register lnode* cur;
106         if (l == NULL)
107                 return 0;
108
109         cur=list_get_cur(l);
110         cur->start = start;
111         cur->status = SESSION_START;
112         if (term)
113                 cur->term = strdup(term);
114         if (host)
115                 cur->host = strdup(host);
116         cur->result = res;
117         cur->user_login_proof = serial;
118         return 1;
119 }
120
121 int list_update_logout(llist* l, time_t t, unsigned long serial)
122 {
123         register lnode* cur;
124         if (l == NULL)
125                 return 0;
126
127         cur=list_get_cur(l);
128         cur->end = t;
129         cur->status = LOG_OUT;
130         cur->user_end_proof = serial;
131         return 1;
132 }
133
134 lnode *list_delete_cur(llist *l)
135 {
136         register lnode *cur, *prev;
137                                                                                 
138         prev = cur = l->head;   /* start at the beginning */
139         while (cur) {
140                 if (cur == l->cur) {
141                         if (cur == prev && cur == l->head) {
142                                 l->head = cur->next;
143                                 l->cur = cur->next;
144                                 free((void *)cur->name);
145                                 free((void *)cur->term);
146                                 free((void *)cur->host);
147                                 free(cur);
148                                 prev = NULL;
149                         } else {
150                                 prev->next = cur->next;
151                                 free((void *)cur->name);
152                                 free((void *)cur->term);
153                                 free((void *)cur->host);
154                                 free(cur);
155                                 l->cur = prev;
156                         }
157                         return prev;
158                 } else {
159                         prev = cur;
160                         cur = cur->next;
161                 }
162         }
163         return NULL;
164 }
165
166 lnode *list_find_auid(llist *l, uid_t auid, int pid, unsigned int session)
167 {
168         register lnode* cur;
169                                                                                 
170         cur = l->head;  /* start at the beginning */
171         while (cur) {
172                 if (cur->pid == pid && cur->auid == auid &&
173                                         cur->session == session) {
174                         l->cur = cur;
175                         return cur;
176                 } else
177                         cur = cur->next;
178         }
179         return NULL;
180 }
181
182 lnode *list_find_session(llist *l, unsigned int session)
183 {
184         register lnode* cur;
185                                                                                 
186         cur = l->head;  /* start at the beginning */
187         while (cur) {
188                 if (cur->session == session) {
189                         l->cur = cur;
190                         return cur;
191                 } else
192                         cur = cur->next;
193         }
194         return NULL;
195 }
196