summaryrefslogtreecommitdiffstats
path: root/libseaudit/src/sort.c
blob: 2d119b9aa641fb49411c1cd77aae0de4d5059f94 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
 *  @file
 *  Implementation of seaudit sort routines.
 *
 *  @author Jeremy A. Mowery jmowery@tresys.com
 *  @author Jason Tang jtang@tresys.com
 *  @author Jeremy Solt jsolt@tresys.com
 *
 *  Copyright (C) 2003-2007 Tresys Technology, LLC
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "seaudit_internal.h"

#include <apol/util.h>

#include <errno.h>
#include <string.h>

/**
 * Callback that compares two messages.
 */
typedef int (sort_comp_func) (const seaudit_sort_t * sort, const seaudit_message_t * a, const seaudit_message_t * b);

/**
 * Callback that returns non-zero if the sort routine can handle the
 * given message, 0 if not supported.
 */
typedef int (sort_supported_func) (const seaudit_sort_t * sort, const seaudit_message_t * m);

struct seaudit_sort
{
	const char *name;
	sort_comp_func *comp;
	sort_supported_func *support;
	int direction;
};

seaudit_sort_t *seaudit_sort_create_from_sort(const seaudit_sort_t * sort)
{
	seaudit_sort_t *s;
	if (sort == NULL) {
		errno = EINVAL;
		return NULL;
	}
	if ((s = calloc(1, sizeof(*s))) == NULL) {
		return NULL;
	}
	s->name = sort->name;
	s->comp = sort->comp;
	s->support = sort->support;
	s->direction = sort->direction;
	return s;
}

void seaudit_sort_destroy(seaudit_sort_t ** sort)
{
	if (sort != NULL && *sort != NULL) {
		free(*sort);
		*sort = NULL;
	}
}

static seaudit_sort_t *sort_create(const char *name, sort_comp_func * comp, sort_supported_func support, const int direction)
{
	seaudit_sort_t *s = calloc(1, sizeof(*s));
	if (s == NULL) {
		return NULL;
	}
	s->name = name;
	s->comp = comp;
	s->support = support;
	s->direction = direction;
	return s;
}

seaudit_sort_t *sort_create_from_sort(const seaudit_sort_t * sort)
{
	if (sort == NULL) {
		errno = EINVAL;
		return NULL;
	}
	return sort_create(sort->name, sort->comp, sort->support, sort->direction);
}

static int sort_message_type_comp(const seaudit_sort_t * sort
				  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	if (a->type != b->type) {
		return a->type - b->type;
	}
	if (a->type == SEAUDIT_MESSAGE_TYPE_AVC) {
		return a->data.avc->msg - b->data.avc->msg;
	}
	return 0;
}

static int sort_message_type_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type != SEAUDIT_MESSAGE_TYPE_INVALID;
}

seaudit_sort_t *seaudit_sort_by_message_type(const int direction)
{
	return sort_create("message_type", sort_message_type_comp, sort_message_type_support, direction);
}

/**
 * Given two dates compare them, checking to see if the dates passed
 * in have valid years and correcting if not before comparing.
 */
static int sort_date_comp(const seaudit_sort_t * sort
			  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	/* tm has year, month, day, hour, min, sec */
	/* if we should compare the years */
	struct tm *t1 = a->date_stamp;
	struct tm *t2 = b->date_stamp;
	int retval;
	if (t1->tm_year != 0 && t2->tm_year != 0 && (retval = t1->tm_year - t2->tm_year) != 0) {
		return retval;
	}
	if ((retval = t1->tm_mon - t2->tm_mon) != 0) {
		return retval;
	}
	if ((retval = t1->tm_mday - t2->tm_mday) != 0) {
		return retval;
	}
	if ((retval = t1->tm_hour - t2->tm_hour) != 0) {
		return retval;
	}
	if ((retval = t1->tm_min - t2->tm_min) != 0) {
		return retval;
	}
	if ((retval = t1->tm_sec - t2->tm_sec) != 0) {
		return retval;
	}
	return 0;
}

static int sort_date_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->date_stamp != NULL;
}

seaudit_sort_t *seaudit_sort_by_date(const int direction)
{
	return sort_create("date", sort_date_comp, sort_date_support, direction);
}

static int sort_host_comp(const seaudit_sort_t * sort
			  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->host, b->host);
}

static int sort_host_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->host != NULL;
}

seaudit_sort_t *seaudit_sort_by_host(const int direction)
{
	return sort_create("host", sort_host_comp, sort_host_support, direction);
}

static int sort_perm_comp(const seaudit_sort_t * sort
			  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	size_t i;
	return apol_vector_compare(a->data.avc->perms, b->data.avc->perms, apol_str_strcmp, NULL, &i);
}

static int sort_perm_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC &&
		msg->data.avc->perms != NULL && apol_vector_get_size(msg->data.avc->perms) >= 1;
}

seaudit_sort_t *seaudit_sort_by_permission(const int direction)
{
	return sort_create("permission", sort_perm_comp, sort_perm_support, direction);
}

static int sort_source_user_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->suser, b->data.avc->suser);
}

static int sort_source_user_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->suser != NULL;
}

seaudit_sort_t *seaudit_sort_by_source_user(const int direction)
{
	return sort_create("source_user", sort_source_user_comp, sort_source_user_support, direction);
}

static int sort_source_role_comp(const seaudit_sort_t * sort __attribute((unused)), const seaudit_message_t * a,
				 const seaudit_message_t * b)
{
	return strcmp(a->data.avc->srole, b->data.avc->srole);
}

static int sort_source_role_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->srole != NULL;
}

seaudit_sort_t *seaudit_sort_by_source_role(const int direction)
{
	return sort_create("source_role", sort_source_role_comp, sort_source_role_support, direction);
}

static int sort_source_type_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->stype, b->data.avc->stype);
}

static int sort_source_type_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->stype != NULL;
}

seaudit_sort_t *seaudit_sort_by_source_type(const int direction)
{
	return sort_create("source_type", sort_source_type_comp, sort_source_type_support, direction);
}

static int sort_source_mls_lvl_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->smls_lvl, b->data.avc->smls_lvl);
}

static int sort_source_mls_lvl_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->smls_lvl != NULL;
}

seaudit_sort_t *seaudit_sort_by_source_mls_lvl(const int direction)
{
	return sort_create("source_mls_lvl", sort_source_mls_lvl_comp, sort_source_mls_lvl_support, direction);
}

static int sort_source_mls_clr_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->smls_clr, b->data.avc->smls_clr);
}

static int sort_source_mls_clr_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->smls_clr != NULL;
}

seaudit_sort_t *seaudit_sort_by_source_mls_clr(const int direction)
{
	return sort_create("source_mls_clr", sort_source_mls_clr_comp, sort_source_mls_clr_support, direction);
}

static int sort_target_user_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->tuser, b->data.avc->tuser);
}

static int sort_target_user_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->tuser != NULL;
}

seaudit_sort_t *seaudit_sort_by_target_user(const int direction)
{
	return sort_create("target_user", sort_target_user_comp, sort_target_user_support, direction);
}

static int sort_target_role_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->trole, b->data.avc->trole);
}

static int sort_target_role_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->trole != NULL;
}

seaudit_sort_t *seaudit_sort_by_target_role(const int direction)
{
	return sort_create("target_role", sort_target_role_comp, sort_target_role_support, direction);
}

static int sort_target_type_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->ttype, b->data.avc->ttype);
}

static int sort_target_type_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->ttype != NULL;
}

seaudit_sort_t *seaudit_sort_by_target_type(const int direction)
{
	return sort_create("target_type", sort_target_type_comp, sort_target_type_support, direction);
}

static int sort_target_mls_lvl_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->tmls_lvl, b->data.avc->tmls_lvl);
}

static int sort_target_mls_lvl_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->tmls_lvl != NULL;
}

seaudit_sort_t *seaudit_sort_by_target_mls_lvl(const int direction)
{
	return sort_create("target_mls_lvl", sort_target_mls_lvl_comp, sort_target_mls_lvl_support, direction);
}

static int sort_target_mls_clr_comp(const seaudit_sort_t * sort
				 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->tmls_clr, b->data.avc->tmls_clr);
}

static int sort_target_mls_clr_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->tmls_clr != NULL;
}

seaudit_sort_t *seaudit_sort_by_target_mls_clr(const int direction)
{
	return sort_create("target_mls_clr", sort_target_mls_clr_comp, sort_target_mls_clr_support, direction);
}

static int sort_object_class_comp(const seaudit_sort_t * sort
				  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->tclass, b->data.avc->tclass);
}

static int sort_object_class_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->tclass != NULL;
}

seaudit_sort_t *seaudit_sort_by_object_class(const int direction)
{
	return sort_create("object_class", sort_object_class_comp, sort_object_class_support, direction);
}

static int sort_executable_comp(const seaudit_sort_t * sort
				__attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->exe, b->data.avc->exe);
}

static int sort_executable_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->exe != NULL;
}

seaudit_sort_t *seaudit_sort_by_executable(const int direction)
{
	return sort_create("executable", sort_executable_comp, sort_executable_support, direction);
}

static int sort_command_comp(const seaudit_sort_t * sort
			     __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->comm, b->data.avc->comm);
}

static int sort_command_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->comm != NULL;
}

seaudit_sort_t *seaudit_sort_by_command(const int direction)
{
	return sort_create("command", sort_command_comp, sort_command_support, direction);
}

static int sort_name_comp(const seaudit_sort_t * sort
			  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->name, b->data.avc->name);
}

static int sort_name_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->name != NULL;
}

seaudit_sort_t *seaudit_sort_by_name(const int direction)
{
	return sort_create("name", sort_name_comp, sort_name_support, direction);
}

static int sort_path_comp(const seaudit_sort_t * sort
			  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->path, b->data.avc->path);
}

static int sort_path_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->path != NULL;
}

seaudit_sort_t *seaudit_sort_by_path(const int direction)
{
	return sort_create("path", sort_path_comp, sort_path_support, direction);
}

static int sort_device_comp(const seaudit_sort_t * sort
			    __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->dev, b->data.avc->dev);
}

static int sort_device_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->dev != NULL;
}

seaudit_sort_t *seaudit_sort_by_device(const int direction)
{
	return sort_create("device", sort_device_comp, sort_device_support, direction);
}

static int sort_inode_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	/* need this logic because inodes are unsigned, so subtraction
	 * could overflow */
	if (a->data.avc->inode < b->data.avc->inode) {
		return -1;
	}
	return a->data.avc->inode - b->data.avc->inode;
}

static int sort_inode_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->inode > 0;
}

seaudit_sort_t *seaudit_sort_by_inode(const int direction)
{
	return sort_create("inode", sort_inode_comp, sort_inode_support, direction);
}

static int sort_pid_comp(const seaudit_sort_t * sort
			 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	/* need this logic because pids are unsigned, so subtraction
	 * could overflow */
	if (a->data.avc->pid < b->data.avc->pid) {
		return -1;
	}
	return a->data.avc->pid - b->data.avc->pid;
}

static int sort_pid_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->pid > 0;
}

seaudit_sort_t *seaudit_sort_by_pid(const int direction)
{
	return sort_create("pid", sort_pid_comp, sort_pid_support, direction);
}

static int sort_port_comp(const seaudit_sort_t * sort
			  __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->port - b->data.avc->port;
}

static int sort_port_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->port > 0;
}

seaudit_sort_t *seaudit_sort_by_port(const int direction)
{
	return sort_create("port", sort_port_comp, sort_port_support, direction);
}

static int sort_laddr_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->laddr, b->data.avc->laddr);
}

static int sort_laddr_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->laddr != NULL;
}

seaudit_sort_t *seaudit_sort_by_laddr(const int direction)
{
	return sort_create("laddr", sort_laddr_comp, sort_laddr_support, direction);
}

static int sort_lport_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->lport - b->data.avc->lport;
}

static int sort_lport_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->lport > 0;
}

seaudit_sort_t *seaudit_sort_by_lport(const int direction)
{
	return sort_create("lport", sort_lport_comp, sort_lport_support, direction);
}

static int sort_faddr_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->faddr, b->data.avc->faddr);
}

static int sort_faddr_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->faddr != NULL;
}

seaudit_sort_t *seaudit_sort_by_faddr(const int direction)
{
	return sort_create("faddr", sort_faddr_comp, sort_faddr_support, direction);
}

static int sort_fport_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->fport - b->data.avc->fport;
}

static int sort_fport_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->fport > 0;
}

seaudit_sort_t *seaudit_sort_by_fport(const int direction)
{
	return sort_create("fport", sort_fport_comp, sort_fport_support, direction);
}

static int sort_saddr_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->saddr, b->data.avc->saddr);
}

static int sort_saddr_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->saddr != NULL;
}

seaudit_sort_t *seaudit_sort_by_saddr(const int direction)
{
	return sort_create("saddr", sort_saddr_comp, sort_saddr_support, direction);
}

static int sort_sport_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->source - b->data.avc->source;
}

static int sort_sport_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->source > 0;
}

seaudit_sort_t *seaudit_sort_by_sport(const int direction)
{
	return sort_create("sport", sort_sport_comp, sort_sport_support, direction);
}

static int sort_daddr_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return strcmp(a->data.avc->daddr, b->data.avc->daddr);
}

static int sort_daddr_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->daddr != NULL;
}

seaudit_sort_t *seaudit_sort_by_daddr(const int direction)
{
	return sort_create("daddr", sort_daddr_comp, sort_daddr_support, direction);
}

static int sort_dport_comp(const seaudit_sort_t * sort
			   __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->dest - b->data.avc->dest;
}

static int sort_dport_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->dest > 0;
}

seaudit_sort_t *seaudit_sort_by_dport(const int direction)
{
	return sort_create("dport", sort_dport_comp, sort_dport_support, direction);
}

static int sort_key_comp(const seaudit_sort_t * sort
			 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->key - b->data.avc->key;
}

static int sort_key_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->is_key;
}

seaudit_sort_t *seaudit_sort_by_key(const int direction)
{
	return sort_create("key", sort_key_comp, sort_key_support, direction);
}

static int sort_cap_comp(const seaudit_sort_t * sort
			 __attribute__ ((unused)), const seaudit_message_t * a, const seaudit_message_t * b)
{
	return a->data.avc->capability - b->data.avc->capability;
}

static int sort_cap_support(const seaudit_sort_t * sort __attribute__ ((unused)), const seaudit_message_t * msg)
{
	return msg->type == SEAUDIT_MESSAGE_TYPE_AVC && msg->data.avc->is_capability;
}

seaudit_sort_t *seaudit_sort_by_cap(const int direction)
{
	return sort_create("cap", sort_cap_comp, sort_cap_support, direction);
}

/******************** protected functions below ********************/

struct sort_name_map
{
	const char *name;
	seaudit_sort_t *(*create_fn) (int);
};

static const struct sort_name_map create_map[] = {
	{"message_type", seaudit_sort_by_message_type},
	{"date", seaudit_sort_by_date},
	{"host", seaudit_sort_by_host},
	{"permission", seaudit_sort_by_permission},
	{"source_user", seaudit_sort_by_source_user},
	{"source_role", seaudit_sort_by_source_role},
	{"source_type", seaudit_sort_by_source_type},
	{"target_user", seaudit_sort_by_target_user},
	{"target_role", seaudit_sort_by_target_role},
	{"target_type", seaudit_sort_by_target_type},
	{"object_class", seaudit_sort_by_object_class},
	{"executable", seaudit_sort_by_executable},
	{"name", seaudit_sort_by_name},
	{"command", seaudit_sort_by_command},
	{"path", seaudit_sort_by_path},
	{"device", seaudit_sort_by_device},
	{"inode", seaudit_sort_by_inode},
	{"pid", seaudit_sort_by_pid},
	{"port", seaudit_sort_by_port},
	{"laddr", seaudit_sort_by_laddr},
	{"lport", seaudit_sort_by_lport},
	{"faddr", seaudit_sort_by_faddr},
	{"fport", seaudit_sort_by_fport},
	{"saddr", seaudit_sort_by_saddr},
	{"sport", seaudit_sort_by_sport},
	{"daddr", seaudit_sort_by_daddr},
	{"dport", seaudit_sort_by_dport},
	{"key", seaudit_sort_by_key},
	{"cap", seaudit_sort_by_cap},
	{NULL, NULL}
};

seaudit_sort_t *sort_create_from_name(const char *name, int direction)
{
	size_t i;
	for (i = 0; create_map[i].name != NULL; i++) {
		if (strcmp(create_map[i].name, name) == 0) {
			return create_map[i].create_fn(direction);
		}
	}
	errno = EINVAL;
	return NULL;
}

int sort_is_supported(const seaudit_sort_t * sort, const seaudit_message_t * msg)
{
	return sort->support(sort, msg);
}

int sort_comp(const seaudit_sort_t * sort, const seaudit_message_t * a, const seaudit_message_t * b)
{
	int retval = sort->comp(sort, a, b);
	return (sort->direction >= 0 ? retval : -1 * retval);
}

const char *sort_get_name(const seaudit_sort_t * sort)
{
	return sort->name;
}

int sort_get_direction(const seaudit_sort_t * sort)
{
	return sort->direction;
}