rpm  5.4.15
rpmsp.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #if defined(WITH_SEPOL)
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 #include <sepol/sepol.h>
12 #ifdef __cplusplus
13 }
14 #endif
15 #endif
16 
17 #define _RPMSP_INTERNAL
18 #include <rpmsp.h>
19 #include <rpmlog.h>
20 #include <rpmmacro.h>
21 
22 #include "debug.h"
23 
24 /*@unchecked@*/
25 int _rpmsp_debug = 0;
26 
27 /*@unchecked@*/ /*@relnull@*/
28 rpmsp _rpmspI = NULL;
29 
30 static void rpmspFini(void * _sp)
31  /*@globals fileSystem @*/
32  /*@modifies *_sp, fileSystem @*/
33 {
34  rpmsp sp = (rpmsp) _sp;
35 
36 #if defined(WITH_SEPOL)
37  if (sp->P)
38  sepol_module_package_free(sp->P);
39  if (sp->C)
40  sepol_context_free(sp->C);
41  if (sp->F)
42  sepol_policy_file_free(sp->F);
43  if (sp->DB)
44  sepol_policydb_free(sp->DB);
45  if (sp->I)
46  sepol_handle_destroy(sp->I);
47 #endif
48  sp->fn = _free(sp->fn);
49  sp->flags = 0;
50  sp->I = NULL;
51  sp->DB = NULL;
52  sp->F = NULL;
53  sp->C = NULL;
54  sp->P = NULL;
55 }
56 
57 /*@unchecked@*/ /*@only@*/ /*@null@*/
59 
60 static rpmsp rpmspGetPool(/*@null@*/ rpmioPool pool)
61  /*@globals _rpmspPool, fileSystem @*/
62  /*@modifies pool, _rpmspPool, fileSystem @*/
63 {
64  rpmsp sp;
65 
66  if (_rpmspPool == NULL) {
67  _rpmspPool = rpmioNewPool("sp", sizeof(*sp), -1, _rpmsp_debug,
68  NULL, NULL, rpmspFini);
69  pool = _rpmspPool;
70  }
71  return (rpmsp) rpmioGetPool(pool, sizeof(*sp));
72 }
73 
74 rpmsp rpmspNew(const char * fn, unsigned int flags)
75 {
76  rpmsp sp = rpmspGetPool(_rpmspPool);
77 
78  sp->fn = NULL;
79  sp->flags = 0;
80  sp->I = NULL;
81  sp->DB = NULL;
82  sp->F = NULL;
83  sp->C = NULL;
84  sp->P = NULL;
85 
86 #if defined(WITH_SEPOL)
87  { sepol_handle_t *I = sp->I = sepol_handle_create();
88  int xx;
89 
90  if (I == NULL) {
91 if (_rpmsp_debug)
92 fprintf(stderr, "--> %s(%s,0x%x): sepol_handle_create() failed\n", __FUNCTION__, fn, flags);
93  (void)rpmspFree(sp);
94  return NULL;
95  }
96 
97  if ((xx = sepol_context_create(sp->I, (sepol_context_t **)&sp->C)) < 0) {
98 if (_rpmsp_debug)
99 fprintf(stderr, "--> %s: sepol_context_create: %s\n", __FUNCTION__, strerror(errno)); /* XXX errno? */
100  (void)rpmspFree(sp);
101  return NULL;
102  }
103 
104  if ((xx = sepol_module_package_create(&sp->P)) < 0) {
105 if (_rpmsp_debug)
106 fprintf(stderr, "--> %s: sepol_module_package_create: %s\n", __FUNCTION__, strerror(errno)); /* XXX errno? */
107  (void)rpmspFree(sp);
108  return NULL;
109  }
110 
111  if (fn != NULL) {
112  FILE * fp = fopen(fn, "r");
113 
114  if (fp == NULL || ferror(fp)) {
115 if (_rpmsp_debug)
116 fprintf(stderr, "--> %s: fopen(%s)\n", __FUNCTION__, fn);
117  if (fp) xx = fclose(fp);
118  fp = NULL;
119  (void)rpmspFree(sp);
120  return NULL;
121  }
122 
123  if ((xx = sepol_policy_file_create(&sp->F)) < 0) {
124 if (_rpmsp_debug)
125 fprintf(stderr, "--> %s: sepol_policy_file_create: %s\n", __FUNCTION__, strerror(errno)); /* XXX errno? */
126  if (fp) xx = fclose(fp);
127  fp = NULL;
128  (void)rpmspFree(sp);
129  return NULL;
130  }
131  sepol_policy_file_set_handle(sp->F, sp->I);
132  sepol_policy_file_set_fp(sp->F, fp);
133 
134  if ((xx = sepol_policydb_create(&sp->DB)) < 0) {
135 if (_rpmsp_debug)
136 fprintf(stderr, "--> %s: sepol_policydb_create: %s\n", __FUNCTION__, strerror(errno)); /* XXX errno? */
137  (void)rpmspFree(sp);
138  return NULL;
139  }
140 
141  if ((xx = sepol_policydb_read(sp->DB, sp->F)) < 0) {
142 if (_rpmsp_debug)
143 fprintf(stderr, "--> %s: sepol_policydb_read: %s\n", __FUNCTION__, strerror(errno)); /* XXX errno? */
144  }
145 
146  (void) fclose(fp);
147 
148  }
149  }
150 #endif
151 
152  return rpmspLink(sp);
153 }
154 
155 #if defined(WITH_SEPOL)
156 /*@unchecked@*/ /*@null@*/
157 static const char * _rpmspI_fn = "minimum";
158 
159 /*@unchecked@*/
160 static int _rpmspI_flags;
161 
162 static rpmsp rpmspI(void)
163  /*@globals _rpmspI @*/
164  /*@modifies _rpmspI @*/
165 {
166  if (_rpmspI == NULL)
167  _rpmspI = rpmspNew(_rpmspI_fn, _rpmspI_flags);
168  return _rpmspI;
169 }
170 #endif
rpmioPool _rpmspPool
Definition: rpmsp.c:58
static void rpmspFini(void *_sp)
Definition: rpmsp.c:30
int errno
rpmsp rpmspFree(rpmsp sp)
Destroy a sepol wrapper.
Yet Another syslog(3) API clone.
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
Definition: rpmmalloc.c:220
rpmsp _rpmspI
Definition: rpmsp.c:28
rpmsp rpmspNew(const char *fn, unsigned int flags)
Create and load a sepol wrapper.
Definition: rpmsp.c:74
rpmsp rpmspLink(rpmsp sp)
Reference a sepol wrapper instance.
const char const bson int mongo_write_concern int flags
Definition: mongo.h:485
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
Definition: rpmmalloc.c:109
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:756
struct rpmsp_s * rpmsp
Definition: rpmsp.h:11
int _rpmsp_debug
Definition: rpmsp.c:25
static rpmsp rpmspGetPool(rpmioPool pool)
Definition: rpmsp.c:60