Veil
veil_mainpage.c
1 /* ----------
2  * veil_mainpage.c
3  *
4  * Doxygen documentation root for Veil
5  *
6  * Copyright (c) 2005 - 2018 Marc Munro
7  * Author: Marc Munro
8  * License: BSD
9  *
10  */
11 
12 
13 /*! \mainpage Veil
14 \version 9.5.0 (Stable))
15 \section license License
16 BSD
17 \section intro_sec Introduction
18 
19 Veil is a data security add-on for Postgres. It provides an API
20 allowing you to control access to data at the row, or even column,
21 level. Different users will be able to run the same query and see
22 different results. Other database vendors describe this as a Virtual
23 Private Database.
24 
25 \section Why Why do I need this?
26 If you have a database-backed application that stores sensitive data,
27 you will be taking at least some steps to protect that data. Veil
28 provides a way of protecting your data with a security mechanism
29 within the database itself. No matter how you access the database,
30 whether you are a legitimate user or not, you cannot by-pass Veil
31 without superuser privileges.
32 
33 \subsection Advantages Veil Advantages
34 By placing security mechanisms within the database itself we get a
35 number of advantages:
36 - Ubiquity. Security is always present, no matter what application or
37 tool is used to connect to the database. If your application is
38 compromised, your data is still protected by Veil. If an intruder gets
39 past your outer defences and gains access to psql, your data is still
40 protected.
41 - Single Security Policy and Implementation. If you have N applications
42 to secure, you have to implement your security policy N times. With
43 Veil, all applications may be protected by a single implementation.
44 - Strength in Depth. For the truly security conscious, Veil provides
45 yet another level of security. If you want strength in depth, with
46 layers and layers of security like an onion, Veil gives you that extra
47 layer.
48 - Performance. Veil is designed to be both flexible and efficient.
49 With a good implementation it is possible to build access controls with
50 a very low overhead, typically much lower than building the equivalent
51 security in each application.
52 - Cooperation. The Veil security model is designed to cooperate with your
53 applications. Although Veil is primarily concerned with data access
54 controls, it can also be used to provide function-level privileges. If
55 your application has a sensitive function X, it can query the database,
56 through Veil functions, to ask the question, "Does the current user have
57 execute_X privilege?". Also, that privilege can be managed in exactly
58 the same way as any other privilege.
59 - Flexibility. Veil is a set of tools rather than a product. How you
60 use it is up to you.
61 
62 \subsection Limitations Veil Limitations
63 Veil can restrict the data returned by queries but cannot prevent the
64 query engine itself from seeing restricted data. In particular,
65 functions executed during evaluation of the where clause of a query may
66 be able to see data that Veil is supposed to restrict access to.
67 
68 As an example let's assume that we have a secured view, users, that
69 allows a user to see only their own record. When Alice queries the
70 view, she will see this:
71 
72 \verbatim
73 select * from users;
74 
75  userid | username
76 ----------+-----------
77  12345 | Alice
78 \endverbatim
79 
80 Alice should not be able to see any details for Bob or even, strictly
81 speaking, tell whether there is an entry for Bob. This query though:
82 
83 \verbatim
84 select * from users
85 where 0 = 9 / (case username when 'Bob' then 0 else 1 end);
86 \endverbatim
87 
88 will raise an exception if the where clause encounters the username
89 'Bob'. So Alice can potentially craft queries that will enable her to
90 discover whether specific names appear in the database.
91 
92 This is not something that Veil is intended to, or is able to, prevent.
93 
94 A more serious problem occurs if a user is able to create user defined
95 functions as these can easily provide covert channels for leaking data.
96 Consider this query:
97 
98 \verbatim
99 select * from users where leak_this(username);
100 \endverbatim
101 
102 Although the query will only return what the secured view allows it to,
103 the where clause, if the function is deemed inexpensive enough, will see
104 every row in the table and so will be able to leak supposedly protected
105 data. This type of exploit can be protected against easily enough by
106 preventing users from defining their own functions, however there are
107 postgres builtins that can be potentially be exploited in the same way.
108 
109 \subsection GoodNews The Good News
110 
111 The news is not all bad. Although Veil can be circumvented, as shown
112 above, a database protected by Veil is a lot more secure than one which
113 is not. Veil can provide extra security over and above that provided by
114 your application, and in combination with a well designed application
115 can provide security that is more than adequate for most commercial
116 purposes.
117 
118 \section the-rest Veil Documentation
119 - \subpage overview-page
120 - \subpage API-page
121 - \subpage Building
122 - \subpage Demo
123 - \subpage Management
124 - \subpage Esoteria
125 - \subpage install
126 - \subpage History
127 - \subpage Feedback
128 - \subpage Performance
129 - \subpage Credits
130 
131 \subsection BetterNews Better News
132 
133 In the latest versions of PostgreSQL, some improvements have been made
134 in the area of security, particularly with respect to security functions
135 and ensuring that untrusted functions may not leak data that should be
136 hidden.
137 
138 Note that there are likely to be costs associated with some of these
139 improvements, as the query engine will apply untrusted functions later
140 in the query execution plan. If those untrusted functions are used to
141 significantly reduce the size of a dataset, moving their execution to
142 later in the plan may have an adverse effect on performance. For this
143 reason, you should test and benchmark and decide for yourself whether
144 there is a performance hit, and whether the value of improved security
145 is worth any measured loss of performance.
146 
147 You are also advised to follow the progress of Row Level Security
148 support in later versions of Postgres, as this may obviate your need for
149 Veil, or change the way in which you will use it.
150 
151 Next: \ref overview-page
152 
153 */
154 /*! \page overview-page Overview: a quick introduction to Veil
155 
156 \section Overview-section Introduction
157 The section introduces a number of key concepts, and shows the basic
158 components of a Veil-protected system:
159 - \ref over-views
160 - \ref over-connections
161 - \ref over-privs
162 - \ref over-contexts
163 - \ref over-funcs2
164 - \ref over-roles
165 
166 \subsection over-views Secured Views and Access Functions
167 Access controls are implemented using secured views and instead-of triggers.
168 Users connect to an account that has access only to the secured views.
169 For a table defined thus:
170 \verbatim
171 create table persons (
172  person_id integer not null,
173  person_name varchar(80) not null
174 );
175 \endverbatim
176 The secured view would be defined something like this:
177 \verbatim
178 create view persons(
179  person_id, person_name) as
180 select person_id, person_name
181  from persons
182  where i_have_personal_priv(10013, person_id);
183 \endverbatim
184 
185 A query performed on the view will return rows only for those persons
186 where the current user has privilege 10013
187 (<code>SELECT_PERSONS</code>). We call the function
188 <code>i_have_personal_priv()</code>, an access function. Such
189 functions are user-defined, and are used to determine whether the
190 connected user has a specific privilege in any of a number of security
191 contexts (see \ref over-contexts). The example above is
192 taken from the Veil demo application (\ref demo-sec) and
193 checks for privilege in the global and personal contexts.
194 
195 \subsection over-connections The Connected User and Connection Functions
196 To determine a user's privileges, we have to know who that user is.
197 At the start of each database session the user must be identified, and
198 their privileges must be determined. This is done by calling a
199 connection function, eg:
200 \verbatim
201 select connect_person('Wilma', 'AuthenticationTokenForWilma');
202 \endverbatim
203 The connection function performs authentication, and stores the user's
204 access privileges in Veil state variables. These variables are then
205 interrogated by the access functions used in the secured views.
206 
207 Prior to connection, or in the event of the connection failing, the
208 session will have no privileges and will probably be unable to see any
209 data. Like access functions, connection functions are user-defined and
210 may be written in any language supported by PostgreSQL.
211 
212 \subsection over-privs Privileges
213 Veil-based systems define access rights in terms of privileges. A
214 privilege is a named thing with a numerical value (actually, the name
215 is kind of optional).
216 
217 An example will probably help. Here is a definition of a privileges
218 table and a subset of its data:
219 \verbatim
220 create table privileges (
221  privilege_id integer not null,
222  privilege_name varchar(80) not null
223 );
224 
225 copy privileges (privilege_id, privilege_name) from stdin;
226 10001 select_privileges
227 10002 insert_privileges
228 10003 update_privileges
229 10004 delete_privileges
230 . . .
231 10013 select_persons
232 10014 insert_persons
233 10015 update_persons
234 10016 delete_persons
235 10017 select_projects
236 10018 insert_projects
237 10019 update_projects
238 10020 delete_projects
239 . . .
240 10100 can_connect
241 \.
242 
243 \endverbatim
244 Each privilege describes something that a user can do. It is up to the
245 access and connection functions to make use of these privileges; the
246 name of the privilege is only a clue to its intended usage. In the
247 example we might expect that a user that has not been given the
248 <code>can_connect</code> privilege would not be able to authenticate
249 using a connection function but this is entirely dependent on the
250 implementation.
251 
252 \subsection over-contexts Security Contexts
253 
254 Users may be assigned privileges in a number of different ways. They
255 may be assigned directly, indirectly through various relationships, or
256 may be inferred by some means. To aid in the discussion and design of a
257 Veil-based security model we introduce the concept of security
258 contexts, and we say that a user has a given set of privileges in a
259 given context. There are three types of security context:
260 
261  - Global Context. This refers to privileges that a user has been given
262  globally. If a user has <code>select_persons</code> privilege in the
263  global context, they will be able to select every record in the
264  persons table. Privileges in global context are exactly like
265  database-level privileges: there is no row-level element to them.
266 
267  - Personal Context. This context provides privileges on data that you
268  may be said to own. If you have <code>select_persons</code>
269  privilege in only the personal context, you will only be able to
270  select your own persons record. Assignment of privileges in the
271  personal context is often defined implicitly or globally, for all
272  users, rather than granted explicitly to each user. It is likely
273  that everyone should have the same level of access to their own data
274  so it makes little sense to have to explicitly assign the privileges
275  for each individual user.
276 
277  - Relational Contexts. These are the key to most row-level access
278  controls. Privileges assigned in a relational context are assigned
279  through relationships between the connected user and the data to be
280  accessed. Examples of relational contexts include: assignments to
281  projects, in which a user will gain access to project data only if
282  they have been assigned to the project; and the management hierarchy
283  within a business, in which a manager may have specific access to
284  data about a staff member. Note that determining a user's access
285  rights in a relational context may require extra queries to be
286  performed for each function call. Your design should aim to minimise
287  this. Some applications may require several distinct relational
288  contexts.
289 
290 \subsection over-funcs2 Access Functions and Security Contexts
291 Each access function will operate on privileges for a specific set of
292 contexts. For some tables, access will only be through global context.
293 For others, it may be through global and personal as well as a number of
294 different relational contexts. Here, from the demo application, are a
295 number of view definitions, each using a different access function that
296 checks different contexts.
297 \verbatim
298 create view privileges(
299  privilege_id,
300  privilege_name) as
301 select privilege_id,
302  privilege_name
303 from privileges
304 where i_have_global_priv(10001);
305 
306 . . .
307 
308 create view persons(
309  person_id,
310  person_name) as
311 select person_id,
312  person_name
313 from persons
314 where i_have_personal_priv(10013, person_id);
315 
316 . . .
317 
318 create view projects(
319  project_id,
320  project_name) as
321 select project_id,
322  project_name
323 from projects
324 where i_have_project_priv(10017, project_id);
325 
326 . . .
327 
328 create view assignments (
329  project_id,
330  person_id,
331  role_id) as
332 select project_id,
333  person_id,
334  role_id
335 from assignments
336 where i_have_proj_or_pers_priv(10025, project_id, person_id);
337 \endverbatim
338 
339 In the <code>privileges</code> view, we only check for privilege in the
340 global context. This is a look-up view, and should be visible to all
341 authenticated users.
342 
343 The <code>persons</code> view checks for privilege in both the global
344 and personal contexts. It takes an extra parameter identifying the
345 person who owns the record. If that person is the same as the connected
346 user, then privileges in the personal context may be checked. If not,
347 only the global context applies.
348 
349 The <code>projects</code> view checks global and project contexts. The
350 project context is a relational context. In the demo application, a
351 user gains privileges in the project context through assignments. An
352 assignment is a relationship between a person and a project. Each
353 assignment record has a role. This role describes the set of privileges
354 the assignee (person) has within the project context.
355 
356 The <code>assignments</code> view checks all three contexts (global,
357 personal and project). An assignment contains data about a person and a
358 project so privileges may be acquired in either of the relational
359 contexts, or globally.
360 
361 \subsection over-roles Grouping Privileges by Roles
362 Privileges operate at a very low-level. In a database of 100 tables,
363 there are likely to be 500 to 1,000 privileges in use. Managing
364 users access at the privilege level is, at best, tedious. Instead, we
365 tend to group privileges into roles, and assign only roles to individual
366 users. Roles act as function-level collections of privileges. For
367 example, the role <code>project-readonly</code> might contain all of the
368 <code>select_xxx</code> privileges required to read all project data.
369 
370 A further refinement allows roles to be collections of sub-roles.
371 Defining suitable roles for a system is left as an exercise for the
372 reader.
373 
374 Next: \ref API-page
375 
376 */
377 /*! \page API-page The Veil API
378 \section API-sec The Veil API
379 This section describes the Veil API. It consists of the following
380 sections
381 
382 - \ref API-intro
383 - \subpage API-variables
384 - \subpage API-simple
385 - \subpage API-bitmaps
386 - \subpage API-bitmap-arrays
387 - \subpage API-bitmap-hashes
388 - \subpage API-int-arrays
389 - \subpage API-serialisation
390 - \subpage API-control
391 
392 Note that all veil objects are placed in the veil schema.
393 
394 \section API-intro Veil API Overview
395 Veil is an API that simply provides a set of state variable types, and
396 operations on those variable types, which are optimised for privilege
397 examination and manipulation.
398 
399 The fundamental data type is the bitmap. Bitmaps are used to
400 efficiently record and test sets of privileges. Bitmaps may be combined
401 into bitmap arrays, which are contiguous groups of bitmaps indexed by
402 integer, and bitmap hashes which are non-contiguous and may be indexed
403 by text strings.
404 
405 In addition to the bitmap-based types, there are a small number of
406 support types that just help things along. If you think you have a case
407 for defining a new type, please
408 \ref Feedback "contact"
409 the author.
410 
411 Next: \ref API-variables
412 */
413 /*! \page API-variables Variables
414 Veil variables exist to record session and system state. They retain
415 their values across transactions. Variables may be defined as either
416 session variables or shared variables.
417 
418 All variables are referenced by name; the name of the variable is
419 passed as a text string to Veil functions.
420 
421 Session variables are private to the connected session. They are
422 created when first referenced and, once defined, their type is set for
423 the life of the session.
424 
425 Shared variables are global across all sessions. Once a shared variable
426 is defined, all sessions will have access to it. Shared variables are
427 defined in two steps. First, the variable is defined as shared, and
428 then it is initialised and accessed in the same way as for session
429 variables. Note that shared variables should only be created within
430 \ref API-control-registered-init or \ref API-control-init.
431 
432 Note that bitmap refs and bitmap hashes may not be stored in shared
433 variables.
434 
435 The following types of variable are supported by Veil, and are described
436 in subsequent sections:
437 - integers
438 - ranges
439 - bitmaps
440 - bitmap refs
441 - bitmap arrays
442 - bitmap hashes
443 - integer arrays
444 
445 The following functions comprise the Veil variables API:
446 
447 - <code>\ref API-variables-share</code>
448 - <code>\ref API-variables-var</code>
449 
450 Note again that session variables are created on usage. Their is no
451 specific function for creating a variable in the variables API. For an
452 example of a function to create a variable see \ref API-bitmap-init.
453 
454 \section API-variables-share share(name text)
455 \verbatim
456 function veil.share(name text) returns bool
457 \endverbatim
458 
459 Implemented by C function veil_share(), this is used to define a
460 specific variable as being shared. A shared variable is accessible to
461 all sessions and exists to reduce the need for multiple copies of
462 identical data. For instance in the Veil demo, role_privileges are
463 recorded in a shared variable as they will be identical for all
464 sessions, and to create a copy for each session would be an unnecessary
465 overhead. This function should only be called from
466 \ref API-control-registered-init or \ref API-control-init.
467 
468 \section API-variables-var veil_variables()
469 \verbatim
470 function veil.veil_variables() returns setof veil_variable_t
471 \endverbatim
472 
473 This function, implemented by C function veil_variables(), returns a
474 description for each variable known to the session. It provides the
475 name, the type, and whether the variable is shared. It is primarily
476 intended for interactive use when developing and debugging Veil-based
477 systems.
478 
479 Next: \ref API-simple
480 */
481 /*! \page API-simple Basic Types: Integers and Ranges
482 
483 Veil's basic types are those that do not contain repeating groups
484 (arrays, hashes, etc).
485 
486 Ranges, implemented by the type \ref veil_range_t,
487 consist of a pair of values and are generally used to initialise the
488 bounds of array and bitmap types.
489 
490 \anchor veil_range_t \ref veil_range_t is defined as:
491 
492 \verbatim
493 create type veil.veil_range_t as (
494  min int4,
495  max int4
496 );
497 \endverbatim
498 
499 Ranges may not contain nulls.
500 
501 The int4 type is used to record a simple nullable integer. This is
502 typically used to record the id of the connected user in a session.
503 
504 The following functions comprise the Veil basic types API:
505 
506 - <code>\ref API-basic-init-range</code>
507 - <code>\ref API-basic-range</code>
508 - <code>\ref API-basic-int4-set</code>
509 - <code>\ref API-basic-int4-get</code>
510 
511 \section API-basic-init-range init_range(name text, min int4, max int4)
512 \verbatim
513 function veil.init_range(name text, min int4, max int4) returns int4
514 \endverbatim
515 
516 This, implemented by veil_init_range() defines a range, and returns the
517 extent of that range.
518 
519 \section API-basic-range range(name text)
520 \verbatim
521 function veil.range(name text) returns veil.range_t
522 \endverbatim
523 
524 This, implemented by C function veil_range() returns the contents
525 of a range. It is intended primarily for interactive use.
526 
527 \section API-basic-int4-set int4_set(name text, value int4)
528 \verbatim
529 function veil.int4_set(name text, value int4) returns int4
530 \endverbatim
531 
532 Sets an int4 variable to a value, returning that same value. It is
533 implemented by C function veil_int4_set().
534 
535 \section API-basic-int4-get int4_get(name text)
536 \verbatim
537 function veil.int4_get(name text) returns int4
538 \endverbatim
539 
540 Returns the value of the int4 variable given by name. Implemented by C
541 function veil_int4_get().
542 
543 
544 Next: \ref API-bitmaps
545 */
546 /*! \page API-bitmaps Bitmaps and Bitmap Refs
547 Bitmaps are used to implement bounded sets. Each bit in the bitmap may
548 be on or off representing presence or absence of a value in the set.
549 Typically bitmaps are used to record sets of privileges.
550 
551 A bitmap ref is a variable that may temporarily reference another
552 bitmap. These are useful for manipulating specific bitmaps within
553 bitmap arrays or bitmap hashes. All bitmap operations except for \ref
554 API-bitmap-init may take the name of a bitmap ref instead of a bitmap.
555 
556 Bitmap refs may not be shared, and the reference is only accessible
557 within the transaction that created it. These restrictions exist to
558 eliminate the possibility of references to deleted objects or to objects
559 from other sessions.
560 
561 The following functions comprise the Veil bitmaps API:
562 
563 - <code>\ref API-bitmap-init</code>
564 - <code>\ref API-bitmap-clear</code>
565 - <code>\ref API-bitmap-setbit</code>
566 - <code>\ref API-bitmap-clearbit</code>
567 - <code>\ref API-bitmap-testbit</code>
568 - <code>\ref API-bitmap-union</code>
569 - <code>\ref API-bitmap-intersect</code>
570 - <code>\ref API-bitmap-bits</code>
571 - <code>\ref API-bitmap-range</code>
572 
573 \section API-bitmap-init init_bitmap(bitmap_name text, range_name text)
574 \verbatim
575 function veil.init_bitmap(bitmap_name text, range_name text) returns bool
576 \endverbatim
577 This is used to create or resize a bitmap. The first parameter provides
578 the name of the bitmap, the second is the name of a range variable that
579 will govern the size of the bitmap. It is implemented by C function
580 veil_init_bitmap().
581 
582 \section API-bitmap-clear clear_bitmap(bitmap_name text)
583 \verbatim
584 function veil.clear_bitmap(bitmap_name text) returns bool
585 \endverbatim
586 This is used to clear (set to zero) all bits in the bitmap. It is
587 implemented by C function veil_clear_bitmap().
588 
589 \section API-bitmap-setbit bitmap_setbit(bitmap_name text, bit_number int4)
590 \verbatim
591 function veil.bitmap_setbit(bitmap_name text, bit_number int4) returns bool
592 \endverbatim
593 This is used to set a specified bit, given by bit_number in the bitmap
594 identified by bitmap_name. It is implemented by C function
595 veil_bitmap_setbit().
596 
597 \section API-bitmap-clearbit bitmap_clearbit(bitmap_name text, bit_number int4)
598 \verbatim
599 function veil.bitmap_clearbit(bitmap_name text, bit_number int4) returns bool
600 \endverbatim
601 This is used to clear (set to zero) a specified bit in a bitmap. It is
602 implemented by C function veil_bitmap_clearbit().
603 
604 \section API-bitmap-testbit bitmap_testbit(bitmap_name text, bit_number int4)
605 \verbatim
606 function veil.bitmap_testbit(bitmap_name text, bit_number int4) returns bool
607 \endverbatim
608 This is used to test a specified bit in a bitmap. It returns true if
609 the bit is set, false otherwise. It is implemented by C function
610 veil_bitmap_testbit().
611 
612 \section API-bitmap-union bitmap_union(result_name text, bm2_name text)
613 \verbatim
614 function veil.bitmap_union(result_name text, bm2_name text) returns bool
615 \endverbatim
616 Form the union of two bitmaps with the result going into the first.
617 Implemented by C function veil_bitmap_union().
618 
619 \section API-bitmap-intersect bitmap_intersect(result_name text, bm2_name text)
620 \verbatim
621 function veil.bitmap_intersect(result_name text, bm2_name text) returns bool
622 \endverbatim
623 Form the intersection of two bitmaps with the result going into the
624 first. Implemented by C function veil_bitmap_intersect().
625 
626 \section API-bitmap-bits bitmap_bits(bitmap_name text)
627 \verbatim
628 function veil.bitmap_bits(bitmap_name text) returns setof int4
629 \endverbatim
630 This is used to list all bits set within a bitmap. It is primarily for
631 interactive use during development and debugging of Veil-based systems.
632 It is implemented by C function veil_bitmap_bits().
633 
634 \section API-bitmap-range bitmap_range(bitmap_name text)
635 \verbatim
636 function veil.bitmap_range(bitmap_name text) returns veil.range_t
637 \endverbatim
638 This returns the range, as a \ref veil_range_t, of a
639 bitmap. It is primarily intended for interactive use. It is
640 implemented by C function veil_bitmap_range().
641 
642 Next: \ref API-bitmap-arrays
643 */
644 /*! \page API-bitmap-arrays Bitmap Arrays
645 A bitmap array is an array of identically-ranged bitmaps, indexed
646 by an integer value. They are initialised using two ranges, one for the
647 range of each bitmap, and one providing the range of indices for the
648 array.
649 
650 Typically bitmap arrays are used for collections of privileges, where
651 each element of the collection is indexed by something like a role_id.
652 
653 The following functions comprise the Veil bitmap arrays API:
654 
655 - <code>\ref API-bmarray-init</code>
656 - <code>\ref API-bmarray-clear</code>
657 - <code>\ref API-bmarray-bmap</code>
658 - <code>\ref API-bmarray-testbit</code>
659 - <code>\ref API-bmarray-setbit</code>
660 - <code>\ref API-bmarray-clearbit</code>
661 - <code>\ref API-bmarray-union</code>
662 - <code>\ref API-bmarray-intersect</code>
663 - <code>\ref API-bmarray-bits</code>
664 - <code>\ref API-bmarray-arange</code>
665 - <code>\ref API-bmarray-brange</code>
666 
667 \section API-bmarray-init init_bitmap_array(bmarray text, array_range text, bitmap_range text)
668 \verbatim
669 function veil.init_bitmap_array(bmarray text, array_range text, bitmap_range text) returns bool
670 \endverbatim
671 Creates or resets (clears) the bitmap array named <code>bmarray</code>.
672 The last two parameters are the names of ranges used to bound the
673 dimensions of the array, and the range of bits within the array's
674 bitmaps. Implemented by C function veil_init_bitmap_array().
675 
676 \section API-bmarray-clear clear_bitmap_array(bmarray text)
677 \verbatim
678 function veil.clear_bitmap_array(bmarray text) returns bool
679 \endverbatim
680 Clear all bits in all bitmaps of the bitmap array named
681 <code>bmarray</code>. Implemented by C function veil_clear_bitmap_array().
682 
683 \section API-bmarray-bmap bitmap_from_array(bmref_name text, bmarray text, index int4)
684 \verbatim
685 function veil.bitmap_from_array(bmref_name text, bmarray text, index int4) returns text
686 \endverbatim
687 Place a reference into <code>bmref_name</code> to the bitmap identified
688 by <code>index</code> in bitmap array <code>bmarray</code>. Implemented
689 by C function veil_bitmap_from_array().
690 
691 \section API-bmarray-testbit bitmap_array_testbit(bmarray text, arr_idx int4, bitno int4)
692 \verbatim
693 function veil.bitmap_array_testbit(bmarray text, arr_idx int4, bitno int4) returns bool
694 \endverbatim
695 Test a specific bit in a bitmap array. Implemented by C function
696 veil_bitmap_array_testbit().
697 
698 \section API-bmarray-setbit bitmap_array_setbit(bmarray text, arr_idx int4, bitno int4)
699 \verbatim
700 function veil.bitmap_array_setbit(bmarray text, arr_idx int4, bitno int4) returns bool
701 \endverbatim
702 Set a specific bit in a bitmap array. Implemented by C function
703 veil_bitmap_array_setbit().
704 
705 \section API-bmarray-clearbit bitmap_array_clearbit(bmarray text, arr_idx int4, bitno int4)
706 \verbatim
707 function veil.bitmap_array_clearbit(bmarray text, arr_idx int4, bitno int4) returns bool
708 \endverbatim
709 Clear a specific bit in a bitmap array. Implemented by C function
710 veil_bitmap_array_clearbit().
711 
712 \section API-bmarray-union union_from_bitmap_array(bitmap text, bmarray text, arr_idx int4)
713 \verbatim
714 function veil.union_from_bitmap_array(bitmap text, bmarray text, arr_idx int4) returns bool
715 \endverbatim
716 Union a bitmap with a specified bitmap from an array, with the result in
717 the bitmap. Implemented by C function
718 veil_union_from_bitmap_array(). This is a faster shortcut for the
719 following logical construction:
720 
721 \verbatim
722 veil.bitmap_union(<bitmap>, veil.bitmap_from_array(<bitmap_array>, <index>))
723 \endverbatim
724 
725 \section API-bmarray-intersect intersect_from_bitmap_array(bitmap text, bmarray text, arr_idx int4)
726 \verbatim
727 function veil.intersect_from_bitmap_array(bitmap text, bmarray text, arr_idx int4) returns bool
728 \endverbatim
729 Intersect a bitmap with a specified bitmap from an array, with the result in
730 the bitmap. Implemented by C function
731 veil_intersect_from_bitmap_array(). This is a faster shortcut for the
732 following logical construction:
733 
734 \verbatim
735 veil.bitmap_intersect(<bitmap>, veil.bitmap_from_array(<bitmap_array>,<index>))
736 \endverbatim
737 
738 \section API-bmarray-bits bitmap_array_bits(bmarray text, arr_idx int4)
739 \verbatim
740 function veil.bitmap_array_bits(bmarray text, arr_idx int4) returns setof int4
741 \endverbatim
742 Show all bits in the specific bitmap within an array. This is primarily
743 intended for interactive use when developing and debugging Veil-based
744 systems. Implemented by C function veil_bitmap_array_bits().
745 
746 \section API-bmarray-arange bitmap_array_arange(bmarray text)
747 \verbatim
748 function veil.bitmap_array_arange(bmarray text) returns veil_range_t
749 \endverbatim
750 Return the range of array indices, as a \ref veil_range_t, for the
751 specified bitmap array. Primarily for interactive use. Implemented by
752 C function veil_bitmap_array_arange().
753 
754 \section API-bmarray-brange bitmap_array_brange(bmarray text)
755 \verbatim
756 function veil.bitmap_array_brange(bmarray text) returns veil_range_t
757 \endverbatim
758 Show the range, as a \ref veil_range_t, of all bitmaps in the specified
759 bitmap array. Primarily for interactive use. Implemented by
760 C function veil_bitmap_array_range().
761 
762 
763 Next: \ref API-bitmap-hashes
764 */
765 /*! \page API-bitmap-hashes Bitmap Hashes
766 A bitmap hashes is a hash table of identically-ranged bitmaps, indexed
767 by a text key.
768 
769 Typically bitmap hashes are used for sparse collections of privileges.
770 
771 Note that bitmap hashes may not be stored in shared variables as hashes
772 in shared memory are insufficiently dynamic.
773 
774 The following functions comprise the Veil bitmap hashes API:
775 
776 - <code>\ref API-bmhash-init</code>
777 - <code>\ref API-bmhash-clear</code>
778 - <code>\ref API-bmhash-key-exists</code>
779 - <code>\ref API-bmhash-from</code>
780 - <code>\ref API-bmhash-testbit</code>
781 - <code>\ref API-bmhash-setbit</code>
782 - <code>\ref API-bmhash-clearbit</code>
783 - <code>\ref API-bmhash-union-into</code>
784 - <code>\ref API-bmhash-union-from</code>
785 - <code>\ref API-bmhash-intersect-from</code>
786 - <code>\ref API-bmhash-bits</code>
787 - <code>\ref API-bmhash-range</code>
788 - <code>\ref API-bmhash-entries</code>
789 
790 \section API-bmhash-init init_bitmap_hash(bmhash text, range text)
791 \verbatim
792 function veil.init_bitmap_hash(bmhash text, range text) returns bool
793 \endverbatim
794 Creates, or resets, a bitmap hash. Implemented by
795 C function veil_init_bitmap_hash().
796 
797 \section API-bmhash-clear clear_bitmap_hash(bmhash text)
798 \verbatim
799 function veil.clear_bitmap_hash(bmhash text) returns bool
800 \endverbatim
801 Clear all bits in all bitmaps of a bitmap hash. Implemented by
802 C function veil_clear_bitmap_hash(). Implemented by
803 C function veil_clear_bitmap_hash().
804 
805 \section API-bmhash-key-exists bitmap_hash_key_exists(bmhash text, key text)
806 \verbatim
807 function veil.bitmap_hash_key_exists(bmhash text, key text) returns bool
808 \endverbatim
809 Determine whether a given key exists in the hash (contains a bitmap).
810 Implemented by C function veil_bitmap_hash_key_exists().
811 
812 \section API-bmhash-from bitmap_from_hash(bmref text, bmhash text, key text)
813 \verbatim
814 function veil.bitmap_from_hash(bmref text, bmhash text, key text) returns text
815 \endverbatim
816 Generate a reference to a specific bitmap in a bitmap hash. Implemented by
817 C function veil_bitmap_from_hash().
818 
819 \section API-bmhash-testbit bitmap_hash_testbit(bmhash text, key text, bitno int4)
820 \verbatim
821 function veil.bitmap_hash_testbit(bmhash text, key text, bitno int4) returns bool
822 \endverbatim
823 Test a specific bit in a bitmap hash. Implemented by
824 C function veil_bitmap_hash_testbit().
825 
826 \section API-bmhash-setbit bitmap_hash_setbit(bmhash text, kay text, bitno int4)
827 \verbatim
828 function veil.bitmap_hash_setbit(bmhash text, key text, bitno int4) returns bool
829 \endverbatim
830 Set a specific bit in a bitmap hash. Implemented by
831 C function veil_bitmap_hash_setbit().
832 
833 \section API-bmhash-clearbit bitmap_hash_clearbit(bmhash text, key text, bitno int4)
834 \verbatim
835 function veil.bitmap_hash_clearbit(bmhash text, key text, bitno int4) returns bool
836 \endverbatim
837 Clear a specific bit in a bitmap hash. Implemented by
838 C function veil_bitmap_hash_clearbit().
839 
840 \section API-bmhash-union-into union_into_bitmap_hash(bmhash text, key text, bitmap text)
841 \verbatim
842 function veil.union_into_bitmap_hash(bmhash text, key text, bitmap text) returns bool
843 \endverbatim
844 Union a specified bitmap from a hash with a bitmap, with the result in
845 the bitmap hash. Implemented by C function
846 veil_union_into_bitmap_hash(). This is a faster shortcut for the
847 following logical construction:
848 
849 \verbatim
850 veil.bitmap_union(veil.bitmap_from_hash(<bitmap_hash>, <key>), <bitmap>)
851 \endverbatim
852 
853 \section API-bmhash-union-from union_from_bitmap_hash(bmhash text, key text, bitmap text)
854 \verbatim
855 function veil.union_from_bitmap_hash(bmhash text, key text, bitmap text) returns bool
856 \endverbatim
857 Union a bitmap with a specified bitmap from a hash, with the result in
858 the bitmap. Implemented by C function veil_union_from_bitmap_hash().
859 This is a faster shortcut for the following logical construction:
860 
861 \verbatim
862 veil.bitmap_union(<bitmap>, veil.bitmap_from_hash(<bitmap_array>, <key>))
863 \endverbatim
864 
865 \section API-bmhash-intersect-from intersect_from_bitmap_hash(bitmap text, bmhash text, key text)
866 \verbatim
867 function veil.intersect_from_bitmap_hash(bitmap text, bmhash text, key text) returns bool
868 \endverbatim
869 Intersect a bitmap with a specified bitmap from a hash, with the result
870 in the bitmap. Implemented by C function
871 veil_intersect_from_bitmap_hash(). This is a faster shortcut for the
872 following logical construction:
873 
874 \verbatim
875 veil.bitmap_intersect(<bitmap>, veil.bitmap_from_hash(<bitmap_array>, <key>))
876 \endverbatim
877 
878 \section API-bmhash-bits bitmap_hash_bits(bmhash text, key text)
879 \verbatim
880 function veil.bitmap_hash_bits(bmhash text, key text) returns setof int4
881 \endverbatim
882 Show all bits in the specific bitmap within a hash. This is primarily
883 intended for interactive use when developing and debugging Veil-based
884 systems. Implemented by C function veil_bitmap_hash_bits().
885 
886 \section API-bmhash-range bitmap_hash_range(bmhash text)
887 \verbatim
888 function veil.bitmap_hash_range(bmhash text) returns veil_range_t
889 \endverbatim
890 Show the range, as a \ref veil_range_t, of all bitmaps in the hash.
891 Primarily intended for interactive use. Implemented by
892 C function veil_bitmap_hash_range().
893 
894 \section API-bmhash-entries bitmap_hash_entries(bmhash text)
895 \verbatim
896 function veil.bitmap_hash_entries(bmhash text) returns setof text
897 \endverbatim
898 Show every key in the hash. Primarily intended for interactive use.
899 Implemented by C function veil_bitmap_hash_entries().
900 
901 Next: \ref API-int-arrays
902 */
903 /*! \page API-int-arrays Integer Arrays
904 Integer arrays are used to store simple mappings of keys to values. In
905 the Veil demo (\ref demo-sec) they are used to record the extra privilege
906 required to access person_details and project_details of each
907 detail_type: the integer array being used to map the detail_type_id to
908 the privilege_id.
909 
910 Note that integer array elements cannot be null.
911 
912 The following functions comprise the Veil int arrays API:
913 
914 - <code>\ref API-intarray-init</code>
915 - <code>\ref API-intarray-clear</code>
916 - <code>\ref API-intarray-set</code>
917 - <code>\ref API-intarray-get</code>
918 
919 \section API-intarray-init init_int4array(arrayname text, range text)
920 \verbatim
921 function veil.init_int4array(arrayname text, range text) returns bool
922 \endverbatim
923 Creates, or resets the ranges of, an int array. Implemented by
924 C function veil_init_int4array().
925 
926 \section API-intarray-clear clear_int4array(arrayname text)
927 \verbatim
928 function veil.clear_int4array(arrayname text) returns bool
929 \endverbatim
930 Clears (zeroes) an int array. Implemented by
931 C function veil_clear_int4array().
932 
933 \section API-intarray-set int4array_set(arrayname text, idx int4, value int4)
934 \verbatim
935 function veil.int4array_set(arrayname text, idx int4, value int4) returns int4
936 \endverbatim
937 Set the value of an element in an int array. Implemented by
938 C function veil_int4array_set().
939 
940 \section API-intarray-get int4array_get(arrayname text, idx int4)
941 \verbatim
942 function int4array_get(arrayname text, idx int4) returns int4
943 \endverbatim
944 Get the value of an element from an int array. Implemented by
945 C function veil_int4array_get().
946 
947 Next: \ref API-serialisation
948 */
949 /*! \page API-serialisation Veil Serialisation Functions
950 With modern web-based applications, database connections are often
951 pooled, with each connection representing many different users. In
952 order to reduce the overhead of connection functions for such
953 applications, Veil provides a serialisation API. This allows session
954 variables for a connected user to be saved for subsequent re-use. This
955 is particularly effective in combination with pgmemcache
956 http://pgfoundry.org/projects/pgmemcache/
957 
958 Only session variables may be serialised.
959 
960 The following functions comprise the Veil serialisatation API:
961 
962 - <code>\ref API-serialise</code>
963 - <code>\ref API-deserialise</code>
964 - <code>\ref API-serialize</code>
965 - <code>\ref API-deserialize</code>
966 
967 \section API-serialise serialise(varname text)
968 \verbatim
969 function veil.serialise(varname text) returns text
970 \endverbatim
971 This creates a serialised textual representation of the named session
972 variable. The results of this function may be concatenated into a
973 single string, which can be deserialised in a single call to
974 veil_deserialise(). Implemented by C function veil_serialise().
975 
976 \section API-deserialise deserialise(stream text)
977 \verbatim
978 function veil.deserialise(stream text) returns text
979 \endverbatim
980 This takes a serialised representation of one or more variables as
981 created by concatenating the results of veil_serialise(), and
982 de-serialises them, creating new variables as needed and resetting their
983 values to those they had when they were serialised. Implemented by C
984 function veil_deserialise().
985 
986 \section API-serialize serialize(varname text)
987 \verbatim
988 function veil.serialize(varname text) returns text
989 \endverbatim
990 Synonym for veil_serialise()
991 
992 \section API-deserialize deserialize(stream text)
993 \verbatim
994 function veil.deserialize(stream text) returns text
995 \endverbatim
996 Synonym for veil_deserialise()
997 
998 Next: \ref API-control
999 */
1000 /*! \page API-control Veil Control Functions
1001 Veil generally requires no management. The exception to this is when
1002 you wish to reset shared variables. You may wish to do this because
1003 your underlying security definitions have changed, or because you have
1004 added new features. In this case, you may use veil_perform_reset() to
1005 re-initialise your shared variables. This function replaces the current
1006 set of shared variables with a new set in a transaction-safe manner.
1007 All current transactions will complete with the old set of variables in
1008 place. All subsequent transactions will see the new set.
1009 
1010 The following functions comprise the Veil control functions API:
1011 
1012 - <code>\ref API-control-registered-init</code>
1013 - <code>\ref API-control-init</code>
1014 - <code>\ref API-control-reset</code>
1015 - <code>\ref API-version</code>
1016 
1017 \section API-control-registered-init registered initialisation functions
1018 
1019 A registered initialisation function is one which will be called from
1020 the standard veil \ref API-control-init function. Such functions are
1021 responsible for defining and, usually, initialising shared variables.
1022 
1023 Initialisation functions may be written in any language supported by
1024 PostgreSQL, and must conform to the following function prototype:
1025 
1026 \verbatim
1027 function init_function(doing_reset bool) returns bool
1028 \endverbatim
1029 
1030 The <code>doing_reset</code> parameter will be set to true if we are
1031 completely resetting veil and redefining all of its variables. In this
1032 case, we must declare and, probably, initialise shared variables prior to
1033 any session initialisation actions. The parameter will be false, if the
1034 function is solely being called to initialise a new session. Check \ref
1035 demo-sec for an example.
1036 
1037 Initialisation functions are registered by inserting their name into
1038 the configuration table <code>veil.veil_init_fns</code>. The functions
1039 listed in this table are executed in order of the <code>priority</code>
1040 column. Eg, to register <code>veil.init_roles()</code> to execute
1041 before <code>veil.init_role_privs()</code>, we would use the following
1042 insert statement:
1043 
1044 \verbatim
1045 insert into veil.veil_init_fns
1046  (fn_name, priority)
1047 values ('veil.init_roles', 1),
1048  ('veil.init_role_privs', 2);
1049 \endverbatim
1050 
1051 \section API-control-init veil_init(doing_reset bool)
1052 \verbatim
1053 function veil.veil_init(doing_reset bool) returns bool
1054 \endverbatim
1055 
1056 This function, implemented by the C function veil_init(), is reponsible
1057 for initialising each veil session. The <code>doing_reset</code>
1058 parameter is true if we are to completely reset Veil, redefining all
1059 shared variables.
1060 
1061 The builtin implementation of veil_init() will call each registered
1062 initialisation function (see \ref API-control-registered-init) in turn.
1063 
1064 If no initialisation functions are registered, veil_init() raises an
1065 exception.
1066 
1067 As an alternative to registering initialisation functions, a Veil-based
1068 application may instead simply redefine veil.veil_init(), though this
1069 usage is deprecated.
1070 
1071 \section API-control-reset veil_perform_reset()
1072 \verbatim
1073 function veil.veil_perform_reset() returns bool
1074 \endverbatim
1075 This is used to reset Veil's shared variables. It causes \ref
1076 API-control-init to be called. Implemented by C function veil_perform_reset().
1077 
1078 \section API-version version()
1079 \verbatim
1080 function veil.version() returns text
1081 \endverbatim
1082 This function returns a string describing the installed version of
1083 veil. Implemented by C function veil_version().
1084 
1085 Next: \ref Building
1086 
1087 */
1088 /*! \page Building Building a Veil-based secure database
1089 \section Build-sec Building a Veil-based secure database
1090 
1091 This section describes the steps necessary to secure a database using
1092 Veil. The steps are:
1093 - \ref Policy
1094 - \ref Schemas
1095 - \ref Design
1096 - \ref Implementation
1097 - \ref Implementation2
1098 - \ref Implementation3
1099 - \ref Implementation4
1100 - \ref Testing
1101 
1102 \subsection Policy Determine your Policies
1103 
1104 You must identify which security contexts exist for your application,
1105 and how privileges should be assigned to users in those contexts. You
1106 must also figure out how privileges, roles, and the assignment of roles
1107 to users are to be managed. You must identify each object that is to be
1108 protected by Veil, identify the security contexts applicable for that
1109 object, and determine the privileges that will apply to each object in
1110 each possible mode of use. Use the Veil demo application (\ref
1111 demo-sec) as a guide.
1112 
1113 For data access controls, typically you will want specific privileges
1114 for select, insert, update and delete on each table. You may also want
1115 separate admin privileges that allow you to grant those rights.
1116 
1117 At the functional level, you will probably have an execute privilege for
1118 each callable function, and you will probably want similar privileges
1119 for individual applications and components of applications. Eg, to
1120 allow the user to execute the role_manager component of admintool, you
1121 would probably create a privilege called
1122 <code>exec_admintool_roleman</code>.
1123 
1124 The hardest part of this is figuring out how you will securely manage
1125 these privileges. A useful, minimal policy is to not allow anyone to
1126 assign a role that they themselves have not been assigned.
1127 
1128 \subsection Schemas Design Your Database-Level Security
1129 
1130 Veil operates within the security provided by PostgreSQL. If you wish
1131 to use Veil to protect underlying tables, then those tables must not be
1132 directly accessible to the user. Also, the Veil functions themselves,
1133 as they provide privileged operations, must not be accessible to user
1134 accounts.
1135 
1136 A sensible basic division of schema responsibilities would be as follows:
1137 
1138 - An "owner" user will own the underlying objects (tables, views,
1139  functions, etc) that are to be secured. Access to these objects will
1140  be granted only to "Veil". The "owner" user will connect only when
1141  the underlying objects are to be modified. No-one but a DBA will ever
1142  connect to this account, and generally, the password for this account
1143  should be disabled.
1144 
1145 - A "Veil" user will own all secured views and access functions (see
1146  \ref over-views). Access to these objects will be granted to the
1147  "Accessor" user. Like the "owner" user, this user should not be
1148  directly used except by DBAs performing maintenance. It will also own
1149  the Veil API, ie this is the account where Veil itself will be
1150  installed. Direct access to Veil API functions should not be granted
1151  to other users. If access to a specific function is needed, it should
1152  be wrapped in a local function to which access may then be granted.
1153 
1154 - "Accessor" users are the primary point of contact. These must have no
1155  direct access to the underlying objects owned by owner. They will have
1156  access only to the secured views and access functions. All
1157  applications may connect to these user accounts.
1158 
1159 \subsection Design Design Your Access Functions
1160 
1161 Provide a high-level view of the workings of each access function. You
1162 will need this in order to figure out what session and shared variables
1163 you will need. The following is part of the design from the Veil demo
1164 application:
1165 \verbatim
1166 Access Functions are required for:
1167 - Global context only (lookup-data, eg privileges, roles, etc)
1168 - Personal and Global Context (personal data, persons, assignments, etc)
1169 - Project and Global (projects, project_details)
1170 - All 3 (assignments)
1171 
1172 Determining privilege in Global Context:
1173 
1174 User has priv X, if X is in role_privileges for any role R, that has
1175 been assigned to the user.
1176 
1177 Role privileges are essentially static so may be loaded into memory as a
1178 shared variable. When the user connects, the privileges associated with
1179 their roles may be loaded into a session variable.
1180 
1181 Shared initialisation code:
1182  role_privs ::= shared array of privilege bitmaps indexed by role.
1183  Populate role_privs with:
1184  select bitmap_array_setbit(role_privs, role_id, privilege_id)
1185  from role_privileges;
1186 
1187 Connection initialisation code:
1188  global_privs ::= session privileges bitmap
1189  Clear global_privs and then initialise with:
1190  select bitmap_union(global_privs, role_privs[role_id])
1191  from person_roles
1192  where person_id = connected_user;
1193 
1194 i_have_global_priv(x):
1195  return bitmap_testbit(global_privs, x);
1196 
1197 \endverbatim
1198 
1199 This gives us the basic structure of each function, and identifies what
1200 must be provided by session and system initialisation to support those
1201 functions. It also allows us to identify the overhead that Veil imposes.
1202 
1203 In the case above, there is a connect-time overhead of one extra query
1204 to load the global_privs bitmap. This is probably a quite acceptable
1205 overhead as typically each user will have relatively few roles.
1206 
1207 If the overhead of any of this seems too significant there are
1208 essentially 4 options:
1209 - Simplify the design.
1210 - Defer the overhead until it is absolutely necessary. This can be done
1211  with connection functions where we may be able to defer the overhead
1212  of loading relational context data until the time that we first need
1213  it.
1214 - Implement a caching solution (check out pgmemcache). Using an
1215  in-memory cache will save data set-up queries from having to be
1216  repeated. This is pretty complex though and may require you to write
1217  code in C.
1218 - Suffer the performance hit.
1219 
1220 \subsection Implementation Implement the Initialisation Function
1221 
1222 Proper initialisation of veil is critical. There are two ways to manage
1223 this. The traditional way is to write your own version of \ref
1224 API-control-init, replacing the supplied version. The newer, better,
1225 alternative is to register your own initialisation functions in the
1226 table veil.veil_init_fns, and have the standard \ref API-control-init,
1227 call them. If there are multiple initialisation functions, they are
1228 called in order of their priority values as specified in the table
1229 <code>veil.veil_init_fns</code>.
1230 
1231 The newer approach has a number of advantages:
1232 
1233 - it fully supports the PostgreSQL extension mechanism, allowing
1234  extensions to be created and dropped;
1235 - it allows different security subsystems to have their own separate
1236  initialisation routines, allowing more modular code and better
1237  separation of responsibilities;
1238 - it is way cooler.
1239 
1240 Initialisation functions \ref API-control-init are critical elements.
1241 They will be called by automatically by Veil, when the first in-built
1242 Veil function is invoked. Initialisation functions are responsible for
1243 three distinct tasks:
1244 
1245 - Initialisation of session variables
1246 - Initialisation of shared variables
1247 - Re-initialisation of variables during reset
1248 
1249 The boolean parameter to veil_init (which is passed to registered
1250 initialisation functions) will be false on initial session
1251 startup, and true when performing a reset (\ref API-control-reset).
1252 
1253 Shared variables are created using \ref API-variables-share. This
1254 returns a boolean result describing whether the variable already
1255 existed. If so, and we are not performing a reset, the current session
1256 need not initialise it.
1257 
1258 Session variables are simply created by using them. It is worth
1259 creating and initialising all session variables to "fix" their data
1260 types. This will prevent other functions from misusing them.
1261 
1262 If the boolean parameter to an initialisation fuction is true, then we
1263 are performing a memory reset, and all shared variables should be
1264 re-initialised. A memory reset will be performed whenever underlying,
1265 essentially static, data has been modified. For example, when new
1266 privileges have been added, we must rebuild all privilege bitmaps to
1267 accommodate the new values.
1268 
1269 \subsection Implementation2 Implement the Connection Functions
1270 
1271 The connection functions have to authenticate the connecting user, and
1272 then initialise the user's session.
1273 
1274 Authentication should use a secure process in which no plaintext
1275 passwords are ever sent across the wire. Veil does not provide
1276 authentication services. For your security needs you should probably
1277 check out pgcrypto.
1278 
1279 Initialising a user session is generally a matter of initialising
1280 bitmaps that describe the user's base privileges, and may also involve
1281 setting up bitmap hashes of their relational privileges. Take a look at
1282 the demo (\ref demo-sec) for a working example of this.
1283 
1284 \subsection Implementation3 Implement the Access Functions
1285 
1286 Access functions provide the low-level access controls to individual
1287 records. As such, their performance is critical. It is generally better
1288 to make the connection functions to more work, and the access functions
1289 less. Bear in mind that if you perform a query that returns 10,000 rows
1290 from a table, your access function for that view is going to be called
1291 10,000 times. It must be as fast as possible.
1292 
1293 When dealing with relational contexts, it is not always possible to keep
1294 all privileges for every conceivable relationship in memory. When this
1295 happens, your access function will have to perform a query itself to
1296 load the specific data into memory. If your application requires this,
1297 you should:
1298 
1299 - Ensure that each such query is as simple and efficient as possible
1300 - Cache your results in some way
1301 
1302 You may be able to trade-off between the overhead of connection
1303 functions and that of access functions. For instance if you have a
1304 relational security context based upon a tree of relationships, you may
1305 be able to load all but the lowest level branches of the tree at connect
1306 time. The access function then has only to load the lowest level branch
1307 of data at access time, rather than having to perform a full tree-walk.
1308 
1309 Caching can be very effective, particularly for nested loop joins. If
1310 you are joining A with B, and they both have the same access rules, once
1311 the necessary privilege to access a record in A has been determined and
1312 cached, we will be able to use the cached privileges when checking for
1313 matching records in B (ie we can avoid repeating the fetch).
1314 
1315 \subsection Implementation4 Implement the views and instead-of triggers
1316 
1317 This is the final stage of implementation. For every base table you
1318 must create a secured view and a set of instead-of triggers for insert,
1319 update and delete. Refer to the demo (\ref demo-sec) for details of
1320 this.
1321 
1322 \subsection Testing Testing
1323 
1324 Be sure to test it all. Specifically, test to ensure that failed
1325 connections do not provide any privileges, and to ensure that all
1326 privileges assigned to highly privileged users are cleared when a more
1327 lowly privileged user takes over a connection. Also ensure that
1328 the underlying tables and raw veil functions are not accessible from
1329 user accounts.
1330 
1331 \section Automation Automatic code generation
1332 
1333 Note that the bulk of the code in a Veil application is in the
1334 definition of secured views and instead-of triggers, and that this code
1335 is all very similar. Consider using a code-generation tool to implement
1336 this.
1337 
1338 Next: \ref Demo
1339 
1340 */
1341 /*! \page Demo A Full Example Application: The Veil Demo
1342 \section demo-sec The Veil Demo Application
1343 
1344 The Veil demo application serves two purposes:
1345 - it provides a demonstration of Veil-based access controls;
1346 - it provides a working example of how to build a secured system using Veil.
1347 
1348 This section covers the following topics:
1349 
1350 - \ref demo-install
1351 - \subpage demo-model
1352 - \subpage demo-security
1353 - \subpage demo-explore
1354 - \subpage demo-code
1355 - \subpage demo-uninstall
1356 
1357 \subsection demo-install Installing the Veil demo
1358 
1359 The veil_demo application, is packaged as an extension just like Veil
1360 itself, and is installed as part of the installation of Veil. To create
1361 a database containing the veil_demo application:
1362 - create a new database
1363 - connect to that database
1364 - execute <code>create extension veil;</code>
1365 - execute <code>create extension veil_demo;</code>
1366 
1367 Next: \ref demo-model
1368 
1369 */
1370 /*! \page demo-model The Demo Database ERD, Tables and Views
1371 \section demo-erd The Demo Database ERD
1372 
1373 \image html veil_demo.png "The Veil Demo Database" width=10cm
1374 
1375 \section demo-tables Table Descriptions
1376 
1377 \subsection demo-privs Privileges
1378 
1379 This table describes each privilege. A privilege is a right to do
1380 something. Most privileges are concerned with providing access to
1381 data. For each table, "X" there are 4 data privileges, SELECT_X, UPDATE_X,
1382 INSERT_X and DELETE_X. There are separate privileges to provide access
1383 to project and person details, and there is a single function privilege,
1384 <code>can_connect</code>.
1385 
1386 \subsection demo-roles Roles
1387 
1388 A role is a named collection of privileges. Privileges are assigned to
1389 roles through role_privileges. Roles exist to reduce the number of
1390 individual privileges that have to be assigned to users, etc. Instead
1391 of assigning twenty or more privileges, we assign a single role that
1392 contains those privileges.
1393 
1394 In this application there is a special role, <code>Personal
1395 Context</code> that contains the set of privileges that apply to all
1396 users in their personal context. Since all users have the same personal
1397 context privileges, the demo application provides this role to all users
1398 implicitly; there is no need for it to be explicitly assigned.
1399 
1400 Assignments of roles in the global context are made through
1401 person_roles, and in the project (relational) context through
1402 assignments.
1403 
1404 \subsection demo-role-privs Role_Privileges
1405 
1406 Role privileges describe the set of privileges for each role.
1407 
1408 \subsection demo-role-roles Role_Roles
1409 
1410 This is currently unused in the Veil demo application. Role roles
1411 provides the means to assign roles to other roles. This allows new
1412 roles to be created as combinations of existing roles. The use of this
1413 table is currently left as an exercise for the reader.
1414 
1415 \subsection demo-persons Persons
1416 
1417 This describes each person. A person is someone who owns data and who
1418 may connect to the database. This table should contain authentication
1419 information etc. In the demo it just maps a name to a person_id.
1420 
1421 \subsection demo-projects Projects
1422 
1423 A project represents a real-world project, to which many persons may be
1424 assigned to work.
1425 
1426 \subsection demo-person-roles Person_Roles
1427 
1428 This table describes the which roles have been assigned to users in the
1429 global context.
1430 
1431 \subsection demo-assignments Assignments
1432 
1433 This describes the roles that have been assigned to a person on a
1434 specific project. Assignments provide privilege to a user in the
1435 project context.
1436 
1437 \subsection demo-detail_types Detail_Types
1438 
1439 This is a lookup-table that describes general-purpose attributes that
1440 may be assigned to persons or project. An example of an attribute for a
1441 person might be birth-date. For a project it might be inception date.
1442 This allows new attributes to be recorded for persons, projects, etc
1443 without having to add columns to the table.
1444 
1445 Each detail_type has a required_privilege field. This identifies the
1446 privilege that a user must have in order to be able to see attributes of
1447 the specific type.
1448 
1449 \subsection demo-person_details Person_Details
1450 
1451 These are instances of specific attributes for specific persons.
1452 
1453 \subsection demo-project-details Project_Details
1454 
1455 These are instances of specific attributes for specific projects.
1456 
1457 \section Demo-Views The Demo Application's Helper Views
1458 
1459 Getting security right is difficult. The Veil demo provides a number of
1460 views that help you view the privileges you have in each context.
1461 
1462 - my_global_privs shows you the privileges you have in the global
1463  context
1464 - my_personal_privs shows you the privileges you have in the
1465  personal context
1466 - my_project_privs shows you the privileges you have for each project
1467  in the project context
1468 - my_privs shows you all your privileges in all contexts
1469 - my_projects shows you all the projects to which you have been assigned
1470 
1471 Using these views, access control mysteries may be more easily tracked
1472 down.
1473 
1474 Next: \ref demo-security
1475 
1476 */
1477 /*! \page demo-security The Demo Database Security Model
1478 \section demo-secmodel The Demo Database Security Model
1479 
1480 The Veil demo has three security contexts.
1481 
1482 - Personal Context applies to personal data that is owned by the
1483  connected user. All users have the same privileges in personal
1484  context, as defined by the role <code>Personal Context</code>.
1485 - Global Context applies equally to every record in a table. If a user
1486  has <code>SELECT_X</code> privilege in the global context, they will
1487  be able to select every record in <code>X</code>, regardless of
1488  ownership. Privileges in global context are assigned through
1489  <code>person_roles</code>.
1490 - Project Context is a relational context and applies to project data.
1491  If you are assigned a role on a project, you will be given specific
1492  access to certain project tables. The roles you have been assigned
1493  will define your access rights.
1494 
1495 The following sections identify which tables may be accessed in which
1496 contexts.
1497 
1498 \subsection demo-global-context The Global Context
1499 The global context applies to all tables. All privilege checking
1500 functions will always look for privileges in the global context.
1501 
1502 \subsection demo-personal-context Personal Context
1503 The following tables may be accessed using rights assigned in the
1504 personal context:
1505 - persons
1506 - assignments
1507 - person_details
1508 
1509 \subsection demo-project-context Project Context
1510 The following tables may be accessed using rights assigned in the
1511 project context:
1512 - projects
1513 - assignments
1514 - project_details
1515 
1516 Next: \ref demo-explore
1517 
1518 */
1519 /*! \page demo-explore Exploring the Demo
1520 \section demo-use Exploring the Demo
1521 \subsection demo-connect Accessing the Demo Database
1522 Using your favourite tool connect to your veil_demo database.
1523 
1524 You will be able to see all of the demo views, both the secured views and
1525 the helpers. But you will not initially be able to see any records:
1526 each view will appear to contain no data. To gain some privileges you
1527 must identify yourself using the \ref demo-code-connect-person function.
1528 
1529 There are 6 persons in the demo. You may connect as any of them and see
1530 different subsets of data. The persons are
1531 
1532 - 1 Deb (the DBA). Deb has global privileges on everything. She needs
1533  them as she is the DBA.
1534 - 2 Pat (the PM). Pat has the manager role globally, and is the project
1535  manager of project 102. Pat can see all but the most confidential
1536  personal data, and all data about her project.
1537 - 3 Derick (the director). Derick can see all personal and project
1538  data. He is also the project manager for project 101, the secret
1539  project.
1540 - 4 Will (the worker). Will has been assigned to both projects. He has
1541  minimal privileges and cannot access project confidential data.
1542 - 5 Wilma (the worker). Willma has been assigned to project 101. She has
1543  minimal privileges and cannot access project confidential data.
1544 - 6 Fred (the fired DBA). Fred has all of the privileges of Deb, except
1545  for can_connect privilege. This prevents Fred from being able to do
1546  anything.
1547 
1548 Here is a sample session, showing the different access enjoyed by
1549 different users.
1550 
1551 \verbatim
1552 veildemo=> select connect_person(4);
1553  connect_person
1554 ----------------
1555  t
1556 (1 row)
1557 
1558 veildemo=> select * from persons;
1559  person_id | person_name
1560 -----------+-------------------
1561  4 | Will (the worker)
1562 (1 row)
1563 
1564 veildemo=> select * from person_details;
1565  person_id | detail_type_id | value
1566 -----------+----------------+--------------
1567  4 | 1003 | 20050105
1568  4 | 1002 | Employee
1569  4 | 1004 | 30,000
1570  4 | 1005 | 19660102
1571  4 | 1006 | 123456789
1572  4 | 1007 | Subservience
1573 (6 rows)
1574 
1575 veildemo=> select * from project_details;
1576  project_id | detail_type_id | value
1577 ------------+----------------+----------
1578  102 | 1001 | 20050101
1579  102 | 1002 | Ongoing
1580 (2 rows)
1581 
1582 veildemo=> select connect_person(2);
1583  connect_person
1584 ----------------
1585  t
1586 (1 row)
1587 
1588 veildemo=> select * from person_details;
1589  person_id | detail_type_id | value
1590 -----------+----------------+-------------------
1591  1 | 1003 | 20050102
1592  2 | 1003 | 20050103
1593  3 | 1003 | 20050104
1594  4 | 1003 | 20050105
1595  5 | 1003 | 20050106
1596  6 | 1003 | 20050107
1597  1 | 1002 | Employee
1598  2 | 1002 | Employee
1599  3 | 1002 | Employee
1600  4 | 1002 | Employee
1601  5 | 1002 | Employee
1602  6 | 1002 | Terminated
1603  2 | 1004 | 50,000
1604  1 | 1005 | 19610102
1605  2 | 1005 | 19600102
1606  3 | 1005 | 19650102
1607  4 | 1005 | 19660102
1608  5 | 1005 | 19670102
1609  2 | 1006 | 123456789
1610  1 | 1007 | Oracle, C, SQL
1611  2 | 1007 | Soft peoply-stuff
1612  3 | 1007 | None at all
1613  4 | 1007 | Subservience
1614  5 | 1007 | Subservience
1615 (24 rows)
1616 
1617 veildemo=> select * from project_details;
1618  project_id | detail_type_id | value
1619 ------------+----------------+----------
1620  102 | 1001 | 20050101
1621  102 | 1002 | Ongoing
1622  102 | 1008 | $100,000
1623 (3 rows)
1624 
1625 veildemo=>
1626 
1627 \endverbatim
1628 
1629 Next: \ref demo-code
1630 
1631 */
1632 /*! \page demo-code The Demo Code
1633 \section demo-codesec The Code
1634 \subsection demo-code-veil-init veil.veil_demo_init(performing_reset bool)
1635 
1636 This function is called at the start of each session, and whenever
1637 \ref API-control-reset is called. The parameter, doing_reset, is
1638 false when called to initialise a session and true when called from
1639 veil_perform_reset(). It is registered with \ref API-control-init
1640 through the <code>veil.veil_demo_init_fns</code> table which is created
1641 as an inherited table of <code>veil.veil_init_fns</code>. By
1642 registering the initialisation functions using a veil_demo-specific
1643 inherited table, when the veil_demo extension is dropped, so is the
1644 registration data for \ref demo-code-veil-init.
1645 
1646 \dontinclude veil_demo.sqs
1647 \skip veil_demo_init(doing
1648 \until init_reqd =
1649 
1650 The first task of veil_init() is to declare a set of Veil shared
1651 variables. This is done by calling \ref API-variables-share. This function
1652 returns true if the variable already exists, and creates the variable
1653 and returns false, if not.
1654 
1655 These variables are defined as shared because they will be identical for
1656 each session. Making them shared means that only one session has to
1657 deal with the overhead of their initialisation.
1658 
1659 \dontinclude veil_demo.sqs
1660 \skip init_reqd =
1661 \until end if;
1662 
1663 We then check whether the shared variables must be initialised. We will
1664 initialise them if they have not already been initialised by another
1665 session, or if we are performing a reset (see \ref API-control-reset).
1666 
1667 Each variable is initialised in its own way.
1668 
1669 Ranges are set by a single call to \ref API-basic-init-range. Ranges are
1670 used to create bitmap and array types of a suitable size.
1671 
1672 Int4Arrays are used to record mappings of one integer to another. In
1673 the demo, they are used to record the mapping of detail_type_id to
1674 required_privilege_id. We use this variable so that we can look-up the
1675 privilege required to access a given project_detail or person_detail
1676 without having to explicitly fetch from attribute_detail_types.
1677 
1678 Int4Arrays are initialised by a call to \ref API-intarray-init, and
1679 are populated by calling \ref API-intarray-set for each value to
1680 be recorded. Note that rather than using a cursor to loop through each
1681 detail_type record, we use select count(). This requires less code and
1682 has the same effect.
1683 
1684 We use a BitmapArray to record the set of privileges for each role. Its
1685 initialisation and population is handled in much the same way as
1686 described above for Int4Arrays, using the functions \ref
1687 API-bmarray-init and \ref API-bmarray-setbit.
1688 
1689 \until language
1690 
1691 The final section of code defines and initialises a set of session
1692 variables. These are defined here to avoid getting undefined variable
1693 errors from any access function that may be called before an
1694 authenticated connection has been established.
1695 
1696 Note that this and all Veil related functions are defined with
1697 <code>security definer</code> attributes. This means that the function
1698 will be executed with the privileges of the function's owner, rather
1699 than those of the invoker. This is absolutely critical as the invoker
1700 must have no privileges on the base objects, or on the raw Veil
1701 functions themselves. The only access to objects protected by Veil must
1702 be through user-defined functions and views.
1703 
1704 \subsection demo-code-connect-person connect_person(_person_id int4)
1705 
1706 This function is used to establish a connection from a specific person.
1707 In a real application this function would be provided with some form of
1708 authentication token for the user. For the sake of simplicity the demo
1709 allows unauthenticated connection requests.
1710 
1711 \skip connect_person(_per
1712 \until language
1713 
1714 This function identifies the user, ensures that they have can_connect
1715 privilege. It initialises the global_context bitmap to contain the
1716 union of all privileges for each role the person is assigned through
1717 person_roles. It also sets up a bitmap hash containing a bitmap of
1718 privileges for each project to which the person is assigned.
1719 
1720 \subsection demo-code-global-priv i_have_global_priv(priv_id int4)
1721 
1722 This function is used to determine whether a user has a specified
1723 privilege in the global context. It tests that the user is connected
1724 using \ref API-basic-int4-get, and then checks whether the
1725 specified privilege is present in the <code>global_context</code>
1726 bitmap.
1727 
1728 \skip function i_have_global_priv(priv
1729 \until security definer;
1730 
1731 The following example shows this function in use by the secured view,
1732 <code>privileges</code>:
1733 
1734 \skip create view privileges
1735 \until grant
1736 
1737 The privileges used above are <code>select_privileges</code> (10001),
1738 <code>insert_privileges</code> (10002), <code>update_privileges</code>
1739 (10003), and <code>delete_privileges</code> (10004).
1740 
1741 \subsection demo-code-personal-priv i_have_personal_priv(priv_id int4, person_id int4)
1742 
1743 This function determines whether a user has a specified privilege to a
1744 specified user's data, in the global or personal contexts. It performs
1745 the same tests as for \ref demo-code-global-priv. If the user
1746 does not have access in the global context, and the connected user is
1747 the same user as the owner of the data we are looking at, then we test
1748 whether the specified privilege exists in the <code>role_privs</code>
1749 bitmap array for the <code>Personal Context</code> role.
1750 
1751 \dontinclude veil_demo.sqs
1752 \skip function i_have_personal_priv(pr
1753 \until language
1754 
1755 Here is an example of this function in use from the persons secured view:
1756 
1757 \skip create view persons
1758 \until grant
1759 
1760 \subsection demo-code-project-priv i_have_project_priv(priv_id int4, project_id int4)
1761 This function determines whether a user has a specified privilege in the
1762 global or project contexts. If the user does not have the global
1763 privilege, we check whether they have the privilege defined in the
1764 project_context BitmapHash.
1765 
1766 \dontinclude veil_demo.sqs
1767 \skip function i_have_project_priv(pr
1768 \until language
1769 
1770 Here is an example of this function in use from the instead-of insert
1771 trigger for the projects secured view:
1772 
1773 \skip create rule ii_projects
1774 \until i_have_project_priv(10018, new.project_id);
1775 
1776 \subsection demo-code-proj-pers-priv i_have_proj_or_pers_priv(priv_id int4, project_id int4, person_id int4)
1777 This function checks all privileges. It starts with the cheapest check
1778 first, and short-circuits as soon as a privilege is found.
1779 
1780 \dontinclude veil_demo.sqs
1781 \skip function i_have_proj_or_pers_priv(
1782 \until language
1783 
1784 Here is an example of this function in use from the instead-of update
1785 trigger for the assignments secured view:
1786 
1787 \skip create rule ii_assignments
1788 \until i_have_proj_or_pers_priv(10027, old.project_id, old.person_id);
1789 
1790 \subsection demo-code-pers-detail-priv i_have_person_detail_priv(detail_id int4, person_id int4)
1791 This function is used to determine which types of person details are
1792 accessible to each user. This provides distinct access controls to each
1793 attribute that may be recorded for a person.
1794 
1795 \dontinclude veil_demo.sqs
1796 \skip function i_have_person_detail_priv(
1797 \until language
1798 
1799 The function is shown in use, below, in the instead-of delete trigger
1800 for person_details. Note that two distinct access functions are being
1801 used here.
1802 
1803 \skip create rule id_person_details
1804 \until i_have_person_detail_priv(old.detail_type_id, old.person_id);
1805 
1806 Next: \ref demo-uninstall
1807 
1808 */
1809 /*! \page demo-uninstall Removing The Demo Database
1810 \section demo-clean-up Removing The Demo Database
1811 In your veil_demo database execute:
1812 - <code>drop extension veil_demo;</code>
1813 
1814 Next: \ref Management
1815 
1816 */
1817 /*! \page Management Managing Privileges, etc
1818 \section Management-sec Managing Privileges, etc
1819 The management of privileges and their assignments to roles, persons,
1820 etc are the key to securing a veil-based application. It is therefore
1821 vital that privilege assignment is itself a privileged operation.
1822 
1823 The veil demo does not provide an example of how to do this, and this
1824 section does little more than raise the issue.
1825 
1826 IT IS VITAL THAT YOU CAREFULLY LIMIT HOW PRIVILEGES ARE MANIPULATED AND
1827 ASSIGNED!
1828 
1829 Here are some possible rules of thumb that you may wish to apply:
1830 
1831 - give only the most senior and trusted users the ability to assign
1832  privileges;
1833 - allow only the DBAs to create privileges;
1834 - allow only 1 or 2 security administrators to manage roles;
1835 - allow roles or privileges to be assigned only by users that have both
1836  the "assign_privileges"/"assign_roles" privileges, and that themselves
1837  have the privilege or role they are assigning;
1838 - consider having an admin privilege for each table and only allow users
1839  to assign privileges on X if they have "admin_x" privilege;
1840 - limit the users who have access to the role/privilege management
1841  functions, and use function-level privileges to enforce this;
1842 - audit/log all assignments of privileges and roles;
1843 - send email to the security administrator whenever role_privileges are
1844  manipulated and when roles granting high-level privileges are granted.
1845 
1846 Next: \ref Esoteria
1847 
1848 */
1849 /*! \page Esoteria Exotic and Esoteric uses of Veil
1850 
1851 \section Esoteria-sec Exotic and Esoteric uses of Veil
1852 Actually this is neither exotic nor particularly esoteric. The title is
1853 simply wishful thinking on the author's part.
1854 \subsection layered-sessions Multi-Layered Connections
1855 So far we have considered access controls based only on the user. If we
1856 wish to be more paranoid, and perhaps we should, we may also consider
1857 limiting the access rights of each application.
1858 
1859 This might mean that reporting applications would have no ability to
1860 update data, that financial applications would have no access to
1861 personnel data, and that personnel apps would have no access to business
1862 data.
1863 
1864 This can be done in addition to the user-level checks, so that even if I
1865 have DBA privilege, I can not subvert the personnel reporting tools to
1866 modify financial data.
1867 
1868 All access functions would check the service's privileges in addition to
1869 the user's before allowing any operation.
1870 
1871 This could be implemented with a connect_service() function that would
1872 be called only once per session and that *must* be called prior to
1873 connecting any users. Alternatively, the connected service could be
1874 inferred from the account to which the service is connected.
1875 
1876 \subsection columns Column-Level Access Controls
1877 
1878 Although veil is primarily intended for row-based access controls,
1879 column-based is also possible. If this is required it may be better to
1880 use a highly normalised data model where columns are converted instead
1881 into attributes, much like the person_details and project_details tables
1882 from the demo application (\ref demo-sec).
1883 
1884 If this is not possible then defining access_views that only show
1885 certain columns can be done something like this:
1886 
1887 \verbatim
1888 create view wibble(key, col1, col2, col3) as
1889 select key,
1890  case when have_col_priv(100001) then col1 else null end,
1891  case when have_col_priv(100002) then col2 else null end,
1892  case when have_col_priv(100003) then col3 else null end
1893 where have_row_priv(1000);
1894 \endverbatim
1895 
1896 The instead-of triggers for this are left as an exercise.
1897 
1898 Next: \ref install
1899 
1900 */
1901 /*! \page install Installation and Configuration
1902 \section install_sec Installation
1903 \subsection Get Getting Veil
1904 Veil can be downloaded as a gzipped tarball from
1905 http://pgfoundry.org/projects/veil/
1906 
1907 Since version 9.1, git is no longer available from cvs on pgfoundry.
1908 Development has switched to using git. The primary git repository is
1909 git://github.com/marcmunro/veil.git
1910 
1911 To checkout from git, create a suitable directory and do:
1912 
1913 \verbatim
1914  git clone git://github.com/marcmunro/veil.git
1915 \endverbatim
1916 
1917 Alternative repositories are also available:
1918 \verbatim
1919  git clone git://bloodnok.com/veil
1920 \endverbatim
1921 
1922 or
1923 \verbatim
1924  git clone git@github.com:marcmunro/veil.git
1925 \endverbatim
1926 
1927 
1928 \subsection Pre-requisites Pre-requisites
1929 You must have a copy of the Postgresql header files available in order
1930 to build Veil. For this, you may need to install the postgres developer
1931 packages for your OS, or even build postgres from source.
1932 \subsection build-sub Building Veil
1933 Veil is built using the postgresql extension building infastructure,
1934 PGXS. In order to be able to build using PGXS, the following command
1935 must return the location of the PGXS makefile:
1936 \verbatim
1937 $ pg_config --pgxs
1938 \endverbatim
1939 You may need to modify your PATH variable to make pg_config usable, or
1940 ensure it is for the correct postgresql version.
1941 
1942 To build the postgres extensions, simply run make with no target:
1943 \verbatim
1944 $ make
1945 \endverbatim
1946 
1947 As part of figuring our the configuration, the makefile will attempt to
1948 work out which version of Postgres to build for. If it fails to figure
1949 this out, add PG_VERSION=<x.y> to the make command. eg:
1950 
1951 \verbatim
1952 $ make PG_VERSION="9.4"
1953 \endverbatim
1954 
1955 To build the veil documentation (the documentation you are now reading)
1956 use:
1957 
1958 \verbatim
1959 $ make docs
1960 \endverbatim
1961 
1962 Note that the build system deliberately avoids using make recursively.
1963 Search the Web for "Recursive Make Considered Harmful" for the reasons
1964 why. This makes the construction of the build system a little different
1965 from what you may be used to. This may or may not turn out to be a good
1966 thing. \ref Feedback "Feedback" is welcomed.
1967 
1968 \subsection Install Installing Veil
1969 As the postgres, pgsql, or root user, run <code>make install</code>.
1970 \verbatim
1971 make install
1972 \endverbatim
1973 
1974 \section configuration Configuration
1975 To configure Veil, the following lines should be added to your
1976 postgresql.conf:
1977 \code
1978 shared_preload_libraries = '<path to shared library>/veil.so'
1979 
1980 #veil.dbs_in_cluster = 1
1981 #veil.shared_hash_elems = 32
1982 #veil.shmem_context_size = 16384
1983 \endcode
1984 
1985 The three configuration options, commented out above, are:
1986 - dbs_in_cluster
1987  The number of databases, within the database cluster, that
1988  will use Veil. Each such database will be allocated 2 chunks of
1989  shared memory (of shmem_context_size), and a single LWLock.
1990  It defaults to 1.
1991 
1992 - shared_hash_elems
1993  This describes how large a hash table should be created for veil
1994  shared variables. It defaults to 32. If you have more than about 20
1995  shared variables you may want to increase this to improve
1996  performance. This setting does not limit the number of variables that
1997  may be defined, it just limits how efficiently they may be accessed.
1998 
1999 - shmem_context_size
2000  This sets an upper limit on the amount of shared memory for a single
2001  Veil shared memory context (there will be two of these). It defaults
2002  to 16K. Increase this if you have many shared memory structures.
2003 
2004 \subsection Regression Regression Tests
2005 Veil comes with a built-in regression test suite. Use <code>make
2006 regress</code> or <code>make check</code> (after installing and
2007 configuring Veil) to run this. You will need superuser access to
2008 Postgres in order to create the regression test database. The
2009 regression test assumes you will have a postgres superuser account named
2010 the same as your OS account. If pg_hba.conf disallows "trust"ed access
2011 locally, then you will need to provide a password for this account in
2012 your .pgpass file (see postgres documentation for details).
2013 
2014 The regression tests are all contained within the regress directory and
2015 are run by the regress.sh shell script. Use the -h option to get
2016 fairly detailed help.
2017 
2018 \subsection Debugging Debugging
2019 If you encounter problems with Veil, you may want to try building with
2020 debug enabled. Define the variable VEIL_DEBUG on the make command line
2021 to add extra debug code to the executable:
2022 \verbatim
2023 $ make clean; make VEIL_DEBUG=1 all
2024 \endverbatim
2025 
2026 This is a transient feature and not as pervasive as it could be. If you
2027 need help with debugging please contact the author.
2028 
2029 Next: \ref History
2030 
2031 */
2032 /*! \page History History and Compatibility
2033 \section past Changes History
2034 \subsection v10_3 Version 10.3.0 (Stable) (2018-05-04)
2035 This version supports PostgreSQL V10.3.
2036 
2037 This was updated following a report of being unable to build veil for
2038 Postgres 10.3. This was the result of changes made in postgres after
2039 version 9.5, to change the way that LWlocks are handled. This version
2040 of veil may work for earlier versions of Postgres 10.x but has not been
2041 tested.
2042 
2043 \subsection v9_5 Version 9.5.0 (Stable) (2016-03-14)
2044 This version supports PostgreSQL V9.5.
2045 
2046 Only minor updates have been made to the documentation, version info,
2047 and the build system.
2048 
2049 \subsection v9_4 Version 9.4.1 (Stable) (2015-11-12)
2050 This version supports PostgreSQL V9.4.
2051 
2052 Bugfix release to fix crash when shared_preload_libraries is defined.
2053 This is a critical fix, and version 9.4.0 is deprecated.
2054 \subsection v9_4 Version 9.4.0 (Stable) (2015-11-09)
2055 DEPRECATED
2056 
2057 This version supports PostgreSQL V9.4.
2058 
2059 Minor changes made to enable a build against the latest Postgres
2060 codebase.
2061 \subsection v9_3 Version 9.3.0 (Stable) (2014-06-30)
2062 This version supports PostgreSQL V9.3.
2063 
2064 It deals with the loss of the int4 C datatype, using int32 instead. It
2065 also modifies its bitmap usage to use 64-bit integers on 64-bit
2066 architectures. The older 32-bit version can be built by defining
2067 FORCE_32_BIT on the make command line, eg:
2068 
2069 \verbatim
2070 $ make all FORCE_32_BIT=1
2071 \endverbatim
2072 
2073 \subsection v9_2 Version 9.2.0 (Stable) (2014-06-25)
2074 This version supports PostgreSQL V9.2.
2075 
2076 Only documentation changes have been made. This means that both this
2077 and the previous version support both postgres 9.1 and 9.2.
2078 
2079 \subsection v1_0 Version 9.1.0 (Stable) (2011-07-22)
2080 This is the first version of Veil to be considered production ready and
2081 completely stable. It is for use only with PostgreSQL 9.1. Support for
2082 older versions of PostgreSQL has been removed in this version.
2083 
2084 Major changes include:
2085 - revamp of the build system to use PGXS and the new PostgreSQL 9.1
2086  extensions mechanism. Veil is now built as an extension.
2087 - modification to the veil_init() mechanism, allowing custom
2088  initialisation functions to be registered through the table
2089  veil.veil_init_fns
2090 - removal of the old veil_trial mechanism, which allowed Veil to be
2091  tried out without fully installing it. This has removed much
2092  unnecessary complexity.
2093 - much general code cleanup, including the removal of conditional code
2094  for older PostgreSQL versions.
2095 - documentation changes, including improved comments for Veil
2096  functions.
2097 
2098 \section forecast Change Forecast
2099 New versions will be released with each new major version of
2100 PostgreSQL if there is demand for them. If you would like such a
2101 version, please ask.
2102 
2103 \section compatibility Supported versions of Postgres
2104 <TABLE>
2105  <TR>
2106  <TD rowspan=2>Veil version</TD>
2107  <TD colspan=7>Postgres Version</TD>
2108  </TR>
2109  <TR>
2110  <TD>9.1</TD>
2111  <TD>9.2</TD>
2112  <TD>9.3</TD>
2113  <TD>9.4</TD>
2114  <TD>9.5</TD>
2115  <TD>10.x (x < 3)</TD>
2116  <TD>10.3</TD>
2117  </TR>
2118  <TR>
2119  <TD>9.1.0 (Stable)</TD>
2120  <TD>Yes</TD>
2121  <TD>Yes</TD>
2122  <TD>- </TD>
2123  <TD>- </TD>
2124  <TD>- </TD>
2125  <TD>- </TD>
2126  <TD>- </TD>
2127  </TR>
2128  <TR>
2129  <TD>9.2.0 (Stable)</TD>
2130  <TD>Yes</TD>
2131  <TD>Yes</TD>
2132  <TD>- </TD>
2133  <TD>- </TD>
2134  <TD>- </TD>
2135  <TD>- </TD>
2136  <TD>- </TD>
2137  </TR>
2138  <TR>
2139  <TD>9.3.0 (Stable)</TD>
2140  <TD>- </TD>
2141  <TD>- </TD>
2142  <TD>Yes</TD>
2143  <TD>- </TD>
2144  <TD>- </TD>
2145  <TD>- </TD>
2146  <TD>- </TD>
2147  </TR>
2148  <TR>
2149  <TD>9.4.1 (Stable)</TD>
2150  <TD>- </TD>
2151  <TD>- </TD>
2152  <TD>- </TD>
2153  <TD>Yes</TD>
2154  <TD>- </TD>
2155  <TD>- </TD>
2156  <TD>- </TD>
2157  </TR>
2158  <TR>
2159  <TD>9.5.0 (Stable)</TD>
2160  <TD>- </TD>
2161  <TD>- </TD>
2162  <TD>- </TD>
2163  <TD>- </TD>
2164  <TD>Yes</TD>
2165  <TD>- </TD>
2166  <TD>- </TD>
2167  </TR>
2168  <TR>
2169  <TD>10.3 (Stable)</TD>
2170  <TD>- </TD>
2171  <TD>- </TD>
2172  <TD>- </TD>
2173  <TD>- </TD>
2174  <TD>- </TD>
2175  <TD>? </TD>
2176  <TD>Yes</TD>
2177  </TR>
2178 </TABLE>
2179 
2180 \section platforms Supported Platforms
2181 Veil should be buildable on any platform supported by PostgreSQL and
2182 PGXS.
2183 
2184 Next: \ref Feedback
2185 
2186 */
2187 /*! \page Feedback Bugs and Feedback
2188 \section Feedback Bugs and Feedback
2189 For general feedback, to start and follow discussions, etc please join
2190 the veil-general@pgfoundry.org mailing list.
2191 
2192 If you wish to report a bug or request a feature, please send mail to
2193 veil-general@pgfoundry.org
2194 
2195 If you encounter a reproducible veil bug that causes a database server
2196 crash, a gdb backtrace would be much appreciated. To generate a
2197 backtrace, you will need to login to the postgres owner account on the
2198 database server. Then identify the postgres backend process associated
2199 with the database session that is going to crash. The following command
2200 identifies the backend pid for a database session started by marc:
2201 
2202 \verbatim
2203 $ ps auwwx | grep ^postgres.*ma[r]c | awk '{print $2}'
2204 \endverbatim
2205 
2206 Now you invoke gdb with the path to the postgres binary and the pid for
2207 the backend, eg:
2208 
2209 \verbatim
2210 $ gdb /usr/lib/postgresql/9.2/bin/postgres 5444
2211 \endverbatim
2212 
2213 Hit c and Enter to get gdb to allow the session to continue. Now,
2214 reproduce the crash from your database session. When the crash occurs,
2215 your gdb session will return to you. Now type bt and Enter to get a
2216 backtrace.
2217 
2218 If you wish to contact the author offlist, you can contact him using
2219 mailto:marc@bloodnok.com
2220 
2221 Next: \ref Performance
2222 
2223 */
2224 /*! \page Performance Performance
2225 \section perf Performance
2226 Attempts to benchmark veil using pgbench have not been very successful.
2227 It seems that the overhead of veil is small enough that it is
2228 overshadowed by the level of "noise" within pgbench.
2229 
2230 Based on this inability to properly benchmark veil, the author is going
2231 to claim that "it performs well".
2232 
2233 To put this into perspective, if your access functions do not require
2234 extra fetches to be performed in order to establish your access rights,
2235 you are unlikely to notice or be able to measure any performance hit
2236 from veil.
2237 
2238 If anyone can provide good statistical evidence of a performance hit,
2239 the author would be most pleased to hear from you.
2240 
2241 Next: \ref Credits
2242 
2243 */
2244 /*! \page Credits Credits
2245 \section Credits
2246 The Entity Relationship Diagram in section \ref demo-erd was produced
2247 automatically from an XML definition of the demo tables, using Autograph
2248 from CF Consulting. Thanks to Colin Fox for allowing its use.
2249 
2250 Thanks to the PostgreSQL core team for providing PostgreSQL.
2251 
2252 Thanks to pgfoundry for providing a home for this project.
2253 */
2254