Updated docs.
[pwmd.git] / COMMANDS
blob0d4f9e2fb1e7bee5117293204b7ac179c642fcdd
1 The server uses a protocol provided by libassuan to communicate with a client.
2 An OK response is returned when a command succeeds or ERR along with an error
3 code and description, if not. When a command requests data for retrieval
4 (e.g., GET) the output is prefixed with D then a single SPACE then the actual
5 data followed by an OK response if successful. Read the libassuan docs for
6 more info about the protocol.
9 PROTOCOL COMMANDS
10 -----------------
12 OPEN <filename> [<key>]
13     Opens <filename> using <key>. If file is not found on the file-system, then
14     a new document will be created. If the file is found, it is looked for in
15     the file cache for an existing key. When found, the existing key will be
16     used for decryption. When not found, pinentry(1) will be used to retrieve
17     the key (see OPTIONS below). You can also open another file using the
18     same connection. When using an empty key and you want to avoid the
19     pinentry dialog, set PINENTRY to 0 (see OPTIONS below).
22 SAVE [<key>]
23     Writes the XML document to disk. The file written to is the file that was
24     opened using the OPEN command. If <key> is not specified then the
25     currently cached key will be used. If the file is a new file or the file
26     isn't found in the file cache, <key> may be used. If <key> is not
27     specified then pinentry(1) will be used to retrieve the key (see OPTIONS
28     below). When using an empty key and you want to avoid the pinentry dialog,
29     set PINENTRY to 0 (see OPTIONS below).
31     When the SAVE command is sent and a key is found in the file cache, the
32     cached key will be used. To reset the key to a new value, send the
33     CLEARCACHE command followed by the filename, then send the SAVE command to
34     enter a new key.
37 ISCACHED <filename>
38     An OK response is returned if the specified file is in the file cache.
41 CLEARCACHE [<filename>]
42     Clears a file cache entry. This will forget the timeout and key for all or
43     the specified file.
46 CACHETIMEOUT <seconds> <filename>
47     Specify the number of seconds the specified file will be cached. -1 will
48     keep the cache entry forever, 0 will require the key each time the OPEN or
49     SAVE commands are used. Also see the "cache_timeout" configuration option.
52 LIST [[!]element[<TAB>[!]element[...]]]
53     If no element path is given then a list of root elements is returned with
54     the data response code. If given, then all reachable elements for the
55     specified element path are returned. Each element in the path is prefixed
56     with the literal '!' character when the element contains no "target"
57     attribute (See THE TARGET ATTRIBUTE below).
60 REALPATH [!]element[<TAB>[!]element[...]]
61     Resolves all "target" attributes of the specified element path and returns
62     the result with a data response.
65 STORE [!]element[[<TAB>[!]element[...]]<TAB>[content]]
66     Creates a new element tree or modifies the content of an existing element
67     path. If only a single element is specified, a new root element is
68     created. Otherwise, elements are TAB delimited and the content will be set
69     to the last TAB delimited argument. If no content is specified after the
70     last TAB then the content for the last specified element will be removed
71     or the content will be empty when creating a new element.
72     
73     The only restriction of element names is that they not begin with a
74     punctuation character (the literal '!' character is an exception) or digit
75     and not contain any whitespace. There is no whitespace between the TAB
76     delimited elements. It is recommended that the value be base 64 encoded to
77     prevent libXML and pwmd parsing errors.
78     
79     PWMD reads the element path from the client via the Assuan INQUIRE
80     protocol response. The STORE command is sent by itself without arguments,
81     then the server responds with INQUIRE. The client then sends the element
82     path prefixed by a "D " data response. When finished, the client
83     sends "END" on an empty line. This is needed so an element path and value
84     can be more than 1000 bytes long, the Assuan protocol line limit.
87 DELETE [!]element[<TAB>[!]element[...]]
88     Removes an element tree from the specified element path.
91 GET [!]element[<TAB>[!]element[...]]
92     Retrieves the content of the specified element path. The data is returned
93     with a data response.
96 ATTR SET|GET|DELETE|LIST [<attribute>] [!]<arg1> [!][arg2]
97     ATTR SET attribute [!]element[<TAB>[!]element[...]] attribute_value
98         Stores or updates an attribute value to an element path.
100     ATTR DELETE attribute [!]element[<TAB>[!]element[...]]
101         Removes an attribute from an element path.
103     ATTR LIST [!]element[<TAB>[!]element[...]]
104         Gets a list of attributes from an element path.
106     ATTR GET attribute [!]element[<TAB>[!]element[...]]
107         Gets the value of an attribute from an element path.
109     The "name" attribute (case sensitive) cannot be removed with ATTR DELETE
110     if the element path is only a root element. Although it can be SET to
111     change the root element name.
113     Also see THE TARGET ATTRIBUTE below.
116 XPATH <expression>[<TAB>value]
117     Evaluates an XPath expression. If no value argument is specified, it is
118     assumed the expression is a request to return a result. Otherwise, the
119     result is set to the value argument and the document is updated. If there
120     is no value after the <TAB> character, the value is assumed to be empty
121     and the document is updated.
124 DUMP
125     Shows the in memory XML document with indenting.
128 GETCONFIG <parameter>
129     Returns the value of a pwmd configuration variable with a data response.
130     If no file is open then the default value will be returned. The "key" and
131     "key_file" variables are ignored.
134 OPTION NAME=VALUE
135     Sets an option NAME to VALUE. See OPTIONS below.
139     Closes the connection. Use the SAVE command before this command as any
140     changes will be lost.
143 If a command fails then the ERR response is returned followed by a protocol
144 error code and description. See src/pwmd_error.h or libpwmd/libpwmd.h for
145 error codes.
148 OPTIONS
149 -------
150 Commands that require a key that is neither cached or specified will use
151 pinentry(1) to retrieve the key. Pinentry options can be set with the OPTION
152 command followed by the option name and value. Below are the available
153 pinentry options:
155     NAME       VALUE      Description
156     ---------|----------|----------------------------------------------------
157     PINENTRY  0|1        When 0, disable use of pinentry. The default is 1.
158     PATH      <string>   Full path to the pinentry binary. The default is
159                          specified at compile time (/usr/bin/pinentry).
160     TTYNAME   <string>   Same as the --ttyname option to pinentry(1).
161     TTYTYPE   <string>   Same as the --ttytype option to pinentry(1).
162     DISPLAY   <string>   Same as the --display option to pinentry(1).
163     TITLE     <string>   Sets the title string of the dialog.
164     PROMPT    <string>   Sets the prompt string of the dialog.
165     DESC      <string>   Sets the error or description string of the dialog.
167 When pinentry is used with the SAVE command the key will be asked for
168 confirmation. If the confirmation fails, the process is started over again
169 until either the keys match or until Cancel is selected. The OPEN command will
170 only ask for the key once without retrying on failure. It is up to the client
171 to retry the OPEN command. Empty keys are allowed.
173 There is also a CLIENT option that contains other sub-options. The format is
174 OPTION CLIENT NAME=VALUE, where NAME is one of:
176     NAME       VALUE      Description
177     ---------|----------|----------------------------------------------------
178     NAME      <string>   Associates the thread ID of the connection with the
179                          specified textual representation. Useful for
180                          debugging log messages.
183 STATUS MESSAGES
184 ---------------
185 Some commands send a status message to the client when successful or as a
186 progress indicator. Status messages begin with a KEYWORD (see below) followed
187 by the status description. What messages are sent, and how often, depend on
188 configuration settings:
190     COMMAND             KEYWORD
191     --------------------------------
192     OPEN                DECRYPT
193                         DECOMPRESS
194                         CACHE
195                         LOCKED
197     SAVE                COMPRESS
198                         ENCRYPT
199                         CACHE
200                         LOCKED
202     CLEARCACHE          CACHE
203                         LOCKED
205     CACHETIMEOUT        CACHE
206                         LOCKED
208     ISCACHED            LOCKED
211     KEYWORD             OUTPUT FORMAT
212     ---------------------------------
213     CACHE               <slots used> <slots available>
214     ENCRYPT             <iterations so far>
215     DECRYPT             <iterations so far>
216     COMPRESS            <bytes so far> <total bytes>
217     DECOMPRESS          <bytes so far> <total bytes>
218     LOCKED              When another thread owns the file/key cache lock,
219                         this is sent once and the thread blocks until the
220                         lock can be obtained and the command completes.
221     KEEPALIVE           Sent the after every configured amount of seconds.
222                         This lets the client know that the connection is still
223                         active for commands that take a while to complete.
226 THE TARGET ATTRIBUTE
227 --------------------
228 There is a special attribute "target" (case sensitive) that can be set with
229 ATTR SET. The value of this attribute is an element path somewhere else in the
230 XML document:
232 ATTR SET target [!]element[<TAB>[!]element[..]] [!]element[<TAB>[!]element[..]]
233                    arg1^^^^^^^^^^^^^^^^^^^^^^^^    arg2^^^^^^^^^^^^^^^^^^^^^^^^
235 If the element path of the "target" attribute (arg1) doesn't exist, it is
236 created. This is the only time the ATTR command will create elements.
238 When a protocol command requests <arg1> as the element path, the remaining
239 elements after the element with the "target" attribute will be appended to
240 <arg2>. This is useful if you have elements that share the same data. If the
241 target is modified, the other elements "pointing" to the target will have the
242 same content. To get the real or literal element and ignore any "target"
243 attributes, prefix an element with a '!' character. Here's an example:
245     C> STORE
246     S> INQUIRE STORE
247     C> D host1<TAB>username<TAB>original username
248     C> END
249     S> OK
250     C> STORE
251     S> INQUIRE STORE
252     C> D host2<TAB>smtp<TAB>username<TAB>someuser
253     C> END
254     S> OK
255     C> ATTR SET target host1<TAB>username host2<TAB>smtp<TAB>username
256     S> OK
258 Now host1's "target" attribute will be used:
260     C> GET host1<TAB>username
261     S> D someuser
262     S> OK
264 If you want host1's username, prefix the element of the "target" attribute
265 with a '!':
267     C> GET host1<TAB>!username
268     S> D original username
269     S> OK
271 The target value (arg2) element can also have a "target" attribute:
273     C> ATTR SET target new_account host1
274     S> OK
275     C> GET new_account<TAB>username
276     S> D someuser
277     S> OK
279 The value of the "target" attribute may also be prefixed with a '!' to set the
280 target to the actual element path and not a target of the element path:
282     C> ATTR DELETE target !new_account
283     S> OK
284     C> ATTR SET target new_account<TAB>username host1<TAB>!username
285     S> OK
286     C> GET new_account<TAB>username
287     S> D original username
288     S> OK
290 The "target" attribute is considered for all commands that support an element
291 path. If the target element has been renamed or deleted afterwards, the
292 command will fail.
294 Clients should be careful of creating target loops. See the "recursion_depth"
295 configuration parameter for details.
298 XML DOCUMENT STRUCTURE
299 ----------------------
300 When importing an XML data file with the -I command line option, the document
301 should have the following DTD:
303     <?xml version="1.0"?>
304     <!DOCTYPE accounts [
305     <!ELEMENT accounts (account*)>
306     <!ATTLIST account name CDATA #REQUIRED>
307     ]>
309 "accounts" is the document root element while each root element mentioned in
310 the protocol commands use the "account" element. So if you have a root element
311 "isp" to be shown with the LIST command ("LIST isp"), the document structure
312 looks like this:
314     <accounts>
315         <account name="isp">
316         [...]
317         </account>
318     </accounts>
320 The DUMP command can be useful to show the current document structure.
323 DESIGN
324 ------
325 The key cache is protected with a mutex and is locked with each access. Each
326 cache entry also contains a mutex for the opened file and a reference count
327 which is adjusted each time a client connects or disconnects. This allows
328 client A to issue commands that aren't related to the file opened by client B.
329 The refcount is needed to prevent the mutex being overwritten when the entry
330 needs to be reset (CACHETIMEOUT for example) and there are any client
331 connected.
333 The client_thread() uses an event to wait for the client FD to become ready
334 for reading. When ready, it uses the libassuan assuan_process_next() function
335 to prevent blocking. This allows for the keepalive_thread() and any signals
336 send to client_thread() to continue to work and lets the Pth scheduler let
337 other (client) threads do their work too.
339 The cleanup_thread() is a loop that sleeps at some interval and is responsible
340 for freeing the resources of the client after the associated client_thread()
341 terminates or exits. How it determines if the client exited is done with the
342 PTH_UNTIL_TID_DEAD event; it checks a list of recorded connections for that
343 event.
345 When a key is required for the OPEN or SAVE commands, and OPTION PINENTRY=1,
346 pinentry(1) is used to retrieve the key. This is done by creating a pipe()
347 with the client_thread(), then pth_fork()'s the pinentry process which will
348 write the key or error to the pipe that client_thread() will read from. After
349 the read, the pinentry should have exited and client_thread() calls the
350 finalization function for the command that used pinentry.
353            -------------       ---------------
354  ----------[server_loop]-_-_-_-[accept_thread]
355  |         -------------       ---------------
356  |     not joinable :                |
357  |  ---------------------     ---------------
358  |  [adjust_timer_thread]     [client_thread]<------------=====
359  |  ---------------------     ---------------   |   |    |    =
360  |                              :   | |    |    |   |    |    =
361  |---|                          :   | |    |    |   |    |    =
362      |                          :   | |    |    |   |    |    =
363  ----------------               :   | |    |    |   |    |    =
364  [cleanup_thread]       :::::::::   | |    |    |   |    |    =
365  ----------------       :           | |  ---------  |    |    =
366        | pth_cancel()   :           | |  [command]  |    |    =
367        |        ------------------  | |  ---------  |    |    = pipe()
368        |--------[keepalive_thread]  | |    |   |    |    |    =
369                 ------------------  | |    |  ---------  |    =
370                                     | |    |  [inquire]  |    =
371                                     | |    |  ---------  |    =
372                                     | |    |    -----------   =
373                                     | |    |----[OPEN/SAVE]   = 
374                                     | |         -----------   =
375                           --------------------       : fork() =
376                           [ command finalize ]   ----------   =
377                           --------------------   [pinentry]====
378                                                  ----------
381 Questions, bugs or feature requests can be sent to
382 Ben Kibbey <bjk@luxsci.net>.