1 /* Copyright (C) 2005 Red Hat, Inc. */
2
3 struct semanage_user;
4 struct semanage_user_key;
5 typedef struct semanage_user_key record_key_t;
6 typedef struct semanage_user record_t;
7 #define DBASE_RECORD_DEFINED
8
9 #include <string.h>
10 #include <stdlib.h>
11 #include "user_internal.h"
12 #include "seuser_internal.h"
13 #include "handle.h"
14 #include "database.h"
15 #include "errno.h"
16 #include "debug.h"
17
semanage_user_modify_local(semanage_handle_t * handle,const semanage_user_key_t * key,const semanage_user_t * data)18 int semanage_user_modify_local(semanage_handle_t * handle,
19 const semanage_user_key_t * key,
20 const semanage_user_t * data)
21 {
22
23 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
24 return dbase_modify(handle, dconfig, key, data);
25 }
26
lookup_seuser(semanage_handle_t * handle,const semanage_user_key_t * k)27 static int lookup_seuser(semanage_handle_t * handle, const semanage_user_key_t *k) {
28 semanage_user_t *user;
29 semanage_seuser_t **records;
30 const char *name;
31 const char *sename;
32 unsigned int count;
33 size_t i;
34 int rc = 0;
35 if (semanage_user_query(handle, k, &user) < 0)
36 return 0;
37 name = semanage_user_get_name(user);
38 semanage_seuser_list_local(handle,
39 &records,
40 &count);
41 for(i = 0; i < count; i++) {
42 sename = semanage_seuser_get_sename(records[i]);
43 if (strcmp(name, sename) == 0) {
44 errno = EINVAL;
45 ERR(handle, "%s is being used by %s login record",
46 sename, semanage_seuser_get_name(records[i]));
47 rc = -1;
48 }
49 }
50 for(i = 0; i < count; i++)
51 semanage_seuser_free(records[i]);
52 free(records);
53 semanage_user_free(user);
54 if (rc)
55 errno = EINVAL;
56 return rc;
57 }
58
semanage_user_del_local(semanage_handle_t * handle,const semanage_user_key_t * key)59 int semanage_user_del_local(semanage_handle_t * handle,
60 const semanage_user_key_t * key)
61 {
62 if (lookup_seuser(handle, key))
63 return -1;
64
65 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
66 return dbase_del(handle, dconfig, key);
67 }
68
semanage_user_query_local(semanage_handle_t * handle,const semanage_user_key_t * key,semanage_user_t ** response)69 int semanage_user_query_local(semanage_handle_t * handle,
70 const semanage_user_key_t * key,
71 semanage_user_t ** response)
72 {
73
74 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
75 return dbase_query(handle, dconfig, key, response);
76 }
77
semanage_user_exists_local(semanage_handle_t * handle,const semanage_user_key_t * key,int * response)78 int semanage_user_exists_local(semanage_handle_t * handle,
79 const semanage_user_key_t * key, int *response)
80 {
81
82 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
83 return dbase_exists(handle, dconfig, key, response);
84 }
85
semanage_user_count_local(semanage_handle_t * handle,unsigned int * response)86 int semanage_user_count_local(semanage_handle_t * handle,
87 unsigned int *response)
88 {
89
90 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
91 return dbase_count(handle, dconfig, response);
92 }
93
semanage_user_iterate_local(semanage_handle_t * handle,int (* handler)(const semanage_user_t * record,void * varg),void * handler_arg)94 int semanage_user_iterate_local(semanage_handle_t * handle,
95 int (*handler) (const semanage_user_t * record,
96 void *varg), void *handler_arg)
97 {
98
99 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
100 return dbase_iterate(handle, dconfig, handler, handler_arg);
101 }
102
semanage_user_list_local(semanage_handle_t * handle,semanage_user_t *** records,unsigned int * count)103 int semanage_user_list_local(semanage_handle_t * handle,
104 semanage_user_t *** records, unsigned int *count)
105 {
106
107 dbase_config_t *dconfig = semanage_user_dbase_local(handle);
108 return dbase_list(handle, dconfig, records, count);
109 }
110