1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
5 >Libpwmd Tutorial
</TITLE
8 CONTENT=
"Modular DocBook HTML Stylesheet Version 1.79"></HEAD
37 >This is a tutorial showing the basic usage of libpwmd
<A
50 >is a library making it easy for applications to use pwmd
<A
61 >Password Manager Daemon). Password Manager Daemon
</I
63 > is a universal data server. It began as a way to keep track of my passwords for accounts like email and websites but can be used to store anything you want.
</P
65 >There are other password management tools but pwmd has a couple of distinguishing features:
</P
71 >It does not depend on a desktop environment but has the ability for applications to connect to it like a desktop solution provides.
</P
75 >Some portion of a stored data can be shared with another portion in the same data file. This feature behaves a lot like a symbolic link on a file system, XML entities, or HTML targets if you're familiar with those, but implemented in a different way. This means less duplication of content. See
<A
76 HREF=
"#SEC.LINKING-ELEMENTS-1"
77 >the Section called
<I
84 >The XML document is encrypted with libgcrypt and can also use gpg-agent. Libgcrypt provides symmetric encryption while gpg-agent supports both PKI and smartcards.
</P
88 >Other features include:
</P
94 >Multi-threaded. More than one client may access the same data file while optionally locking out other clients.
</P
98 >Secure memory management. Pwmd will zero out memory before freeing it and the contents of the cached document are encrypted.
</P
102 >Remote connections over TLS with client certificates used as authentication.
</P
106 >Remote connections over SSH.
</P
116 >File Format and Element Paths
</A
119 >The document is saved as an XML document and is manipulated by commands sent from a client. Commands that access data take what is called an
<SPAN
125 > as an argument. An element path is a character string containing XML element names representing branches of an element tree. The branches are separated with a TAB (ASCII
0x09) character. Why a TAB? So other characters can be used in the element name. If for example a '/' were used as the separator, then a URL (i.e., http://sf.net/) could not be used as an element name.
</P
127 >For the rest of this tutorial, when you see
<TAB
>, replace it with a real TAB character. For example, the element path
"root<TAB>child<TAB>last" has the following element tree:
</P
139 Content or XML CDATA of the
"last" element.
147 >I should say that the XML structure that pwmd uses is a little more complicated. It really looks like the following internally, but we will use the above format in this tutorial for simplicity:
</P
156 ><element
_name=
"root">
157 <element
_name=
"child">
158 <element
_name=
"last">
159 Content or CDATA of the
"last" element.
162 </element
></PRE
167 >Every element created has an element named
<SPAN
173 > with an attribute
<SPAN
179 > associated with it. The value of the
<SPAN
185 > attribute is what an element in an element path refers to. It is done this way so that a wider range of characters can be used in an element name while maintaining valid XML. In fact the only restriction of an element name is that it not contain whitespace characters and not begin with an exclamation point (ASCII
0x21 or !). See
<SPAN
190 HREF=
"#SEC.LINKING-ELEMENTS-1"
191 >the Section called
<I
196 >for an exception.
</P
198 >There is one other difference from your normal XML document in that only the first match of an element is considered for the current element tree depth. When an element of the current depth of an element tree is found, the next element in the element path is searched for beginning at the child node of the found element. From the example above:
</P
210 Content or XML CDATA of the
"last" element.
214 This element will never be reached.
221 >The second
"child" element is never reached.
</P
229 >Connecting to PWMD
</A
232 >You will need a
<SPAN
238 > client to send commands to the pwmd server.
<SPAN
244 > includes a client named
<SPAN
250 >. It is command-line based and there are not any fancy graphics, but it is good for understanding how
<SPAN
256 > commands are processed. If you want a more user friendly client that resembles a file manager and has a point and click interface, try QPwmc
<A
263 > which uses the Qt4 toolkit and is also a full featured client.
</P
265 >In this tutorial we will use the
<SPAN
271 > client included with libpwmd.
<SPAN
277 > has two modes that commands are read from: interactive and stdin. The interactive mode uses the readline library and has command history, while reading from standard input (stdin) makes shell scripting and automation easy. For the examples, we will be connecting to the default local unix domain socket that pwmd waits for connections on. Remote connections are possible over TLS or an SSH channel but those are not covered here. Read the pwmc(
1) manual page for details about how to do that.
</P
279 >The example filename we will use is
"datafile". It is initially a non-existent file but it will be created once we have saved to it. Now let us connect to the server in interactive mode and open the data file:
</P
295 >The
"pwm>" prompt is a readline prompt that has command history and can do filename completion. It also is annoying because, as mentioned, elements in element paths are TAB delimited, but command completion in readline uses the TAB character, too. To insert a real TAB character in interactive mode rather than do readline completion, you will need to first press CTRL-V and then press TAB.
</P
306 >There are two different types of commands: client commands and protocol commands. The client commands are
<SPAN
312 > specific and only available to the
<SPAN
318 > client. Protocol commands are commands sent from any client and to the
<SPAN
324 > server. All pwmc client commands are prefixed with a dot '.' followed by the command name. Protocol commands are sent without any prefix. To see the available pwmc and protocol commands, send the HELP command:
</P
352 >To store some data in an element path, you will need to know what element path to create. It is up to you how you want your data organized. If for example you will be storing account information it may be good to categorize what the account is for: email, instant messaging, blogging, etc. An application that uses libpwmd may require that a certain element path exists. Refer to that applications documentation to determine what element paths need to be created.
</P
354 >In the following example we will setup mail server element paths which can be used for other applications requiring a mail server configuration. First, lets create the hostname element path:
</P
368 >The STORE command is a
<SPAN
374 > protocol command. This and a few other commands, use what is called a
<SPAN
380 > to retrieve data from the client while other commands retrieve the data only from the command arguments. The server inquire will wait for the client to finish sending its data before completing the command. When responding to a server inquire in pwmc, pwmc will show a message telling you that it is waiting for data to be sent. Enter the element path and its content, then press
<CTRL-D
> twice to finish sending the data and to let
<SPAN
386 > complete the command:
</P
396 email
<TAB
>isp
<TAB
>IMAP
<TAB
>hostname
<TAB
>imap.server.com
<CTRL-D
><CTRL-D
></PRE
401 >Remember that you should replace
<TAB
> in the examples with a real TAB character. It should be known that when sending data via a server inquire there is no need to press
<CTRL-V
> to insert the
<TAB
> character. That is only needed to be done from the readline command prompt and when not doing a server inquire.
</P
403 >After pressing the first
<CTRL-D
>, the characters that were entered are sent to
<SPAN
409 > and the inquire continues. To terminate the inquire and finish sending data for the command press
<CTRL-D
> a second time.
</P
411 >If you were to have pressed
<RETURN
> before any
<CTRL-D
> then the inquired line would have been sent in full including a newline character. Since in this example a newline character in a hostname is not a valid hostname, this is not what we want.
</P
413 >We have just created an element path whose XML structure looks like the following:
</P
425 <hostname
>imap.server.com
</hostname
>
428 </email
></PRE
433 >The GET protocol command returns the value, or content, of the last element of an element path. To retrieve the hostname of the element path we just created, do:
</P
442 >pwm
> GET email
<TAB
>isp
<TAB
>IMAP
<TAB
>hostname
448 >Remember that in interactive mode and when not doing a server inquire, you will need to press
<CTRL-V
> before pressing
<TAB
>.
</P
450 >Let us create the rest of the needed elements:
</P
460 email
<TAB
>isp
<TAB
>IMAP
<TAB
>port
<TAB
>993<CTRL-D
><CTRL-D
>
462 email
<TAB
>isp
<TAB
>IMAP
<TAB
>ssl
<TAB
>1<CTRL-D
><CTRL-D
>
464 email
<TAB
>isp
<TAB
>username
<TAB
>myusername
<CTRL-D
><CTRL-D
>
466 email
<TAB
>isp
<TAB
>password
<TAB
>mypassword
<CTRL-D
><CTRL-D
></PRE
471 >Now the element structure for the
"email" element looks like this:
</P
483 <hostname
>imap.server.com
</hostname
>
484 <port
>993</port
>
485 <ssl
>1</ssl
>
487 <username
>myusername
</username
>
488 <password
>mypassword
</password
>
490 </email
></PRE
495 >If you wanted to change your password (after changing it on the mail server, of course) just do as you did when initially creating the password element path. The new content will overwrite the existing content:
</P
505 email
<TAB
>isp
<TAB
>password
<TAB
>newpassword
<CTRL-D
><CTRL-D
></PRE
510 >An application using libpwmd that needs to know mail server information now has all the information it needs. The only thing left to do now is to save the changes:
</P
519 >pwm
> <CTRL-D
></PRE
524 >After pressing
<CTRL-D
> a prompt will be shown asking what to do next. Press
“s
” to save what we have created.
</P
526 >Since this is a new file, you will be prompted for a passphrase to use for encryption. Once done, the data file is encrypted and written to disk and the passphrase is cached in pwmd so you do not need to enter it the next time your data file is opened or saved. There are a few pwmd configuration parameters and commands that affect how this operates. See the
<SPAN
532 > documentation for details.
</P
539 NAME=
"SEC.LINKING-ELEMENTS-1"
543 >One distinguishing feature of pwmd is the ability to share data of one element path with another. If for example your ISP lets you host a blog on their server and you use a blogging client that can use libpwmd for authentication details, you can share authentication information with the email example above when the account details are the same. When element linking is used, this avoids the need to change the content for both the blogging and email password elements.
</P
545 >This is done by setting a special
“target
” attribute for an element. It behaves similarly to the HTML
“target
” attribute or XML entities or a symbolic link on a filesystem.
</P
547 >Let's create an example blogging element path:
</P
557 blog
<TAB
>isp
<TAB
>hostname
<TAB
>blog.myisp.com
<CTRL-D
><CTRL-D
>
559 pwm
> ATTR SET target blog
<TAB
>isp
<TAB
>username email
<TAB
>isp
<TAB
>username
560 pwm
> ATTR SET target blog
<TAB
>isp
<TAB
>password email
<TAB
>isp
<TAB
>password
</PRE
565 >Now each access of the
"blog/isp/username" and
"blog/isp/password" element paths, which were created if they did not already exist, will point to
"email/isp/username" and
"email/isp/password", respectively. To retrieve the value or content of an element that contains a
"target" attribute without following any
“target
” attribute, prefix the element with a '!' when specifying it in an element path. For example:
</P
574 >pwm
> GET blog
<TAB
>isp
<TAB
>!password
575 ERR
536870938: No value
</PRE
580 >An error is returned because we have not assigned a value to the literal element. A literal element is an element that either contains no
"target" attribute or ignores it by prefixing the element with the mentioned literal element character '!'. Let us try storing an example password to the literal element to show usage:
</P
590 blog
<TAB
>isp
<TAB
>!password
<TAB
>literalpassword
<CTRL-D
><CTRL-D
></PRE
595 >Now when we redo the previous GET command:
</P
604 >pwm
> GET blog
<TAB
>isp
<TAB
>!password
610 >This is the value of the literal password element of the element path. To follow the element path stored in the
"target" attribute, omit the '!' in the password element. This will return the
“email/isp/password
” element content:
</P
619 >pwm
> GET blog
<TAB
>isp
<TAB
>password
625 >A
"target" attribute may also refer to another element with a
"target" attribute. Every
"target" attribute will be followed until there are no others to resolve. To get the real element path and resolve all
"target" attributes, use the REALPATH command:
</P
634 >pwm
> REALPATH blog
<TAB
>isp
<TAB
>password
635 !email
<TAB
>!isp
<TAB
>!password
</PRE
640 >As you can see each element is prefixed with the literal '!' character meaning that no
"target" attribute exists in any element of the resulting element path.
</P
642 >Using the LIST command is useful to show the element structure of a document:
</P
656 !email
<TAB
>!isp
657 !email
<TAB
>!isp
<TAB
>!username
658 !email
<TAB
>!isp
<TAB
>!password
659 !email
<TAB
>!isp
<TAB
>!IMAP
<TAB
>!hostname
660 !email
<TAB
>!isp
<TAB
>!IMAP
<TAB
>!port
661 !email
<TAB
>!isp
<TAB
>!IMAP
<TAB
>!ssl
664 !blog
<TAB
>!isp
665 !blog
<TAB
>!isp
<TAB
>!username
666 !blog
<TAB
>!isp
<TAB
>username
667 !blog
<TAB
>!isp
<TAB
>!password
668 !blog
<TAB
>!isp
<TAB
>password
</PRE
673 >Notice that there are two username and password elements in the
“blog
” element path: each with and without the literal element character prefix. This shows that both the username and password elements contain a
"target" attribute.
</P
675 >For the final example, we will remove the
“target
” attribute of the blogging password element:
</P
684 >pwm
> ATTR DELETE target blog
<TAB
>isp
<TAB
>!password
</PRE
689 >If we were to have omitted the '!' from the password element then pwmd would try to remove the
"target" attribute from the
"email/isp/password" element path. That would return an error because no
“target
” attribute had been set for that element.
</P
691 >Note that the
"blog" and
"isp" elements have no literal element character prepended to them in the example. It is not needed because they have no
“target
” attribute themselves.
</P
693 >Using
“target
” attributes can be confusing. The best way to get the hang of linking elements is to experiment and use the LIST and DUMP commands to show the document structure. Remember to use the literal element prefix '!' when you do not want to follow any
"target" attributes.
</P
722 >bjk@luxsci.net
</FONT
744 >http://libpwmd.sourceforge.net/
</FONT
766 >http://pwmd.sourceforge.net/
</FONT
788 >http://qpwmc.sourceforge.net/
</FONT