1 | /*- |
---|
2 | * Copyright (c) 2013 Universitetet i Oslo |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 3. The name of the author may not be used to endorse or promote |
---|
14 | * products derived from this software without specific prior written |
---|
15 | * permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * $Id: oath_key_from_file.c 786 2014-03-10 15:37:55Z des $ |
---|
30 | */ |
---|
31 | |
---|
32 | #ifdef HAVE_CONFIG_H |
---|
33 | # include "config.h" |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <sys/types.h> |
---|
37 | |
---|
38 | #include <inttypes.h> |
---|
39 | #include <stdio.h> |
---|
40 | #include <stdlib.h> |
---|
41 | |
---|
42 | #include <security/pam_appl.h> |
---|
43 | #include <security/openpam.h> |
---|
44 | |
---|
45 | #include "openpam_strlcmp.h" |
---|
46 | |
---|
47 | #include <security/oath.h> |
---|
48 | |
---|
49 | /* |
---|
50 | * OATH |
---|
51 | * |
---|
52 | * Loads an OATH key from a file |
---|
53 | */ |
---|
54 | |
---|
55 | struct oath_key * |
---|
56 | oath_key_from_file(const char *filename) |
---|
57 | { |
---|
58 | struct oath_key *key; |
---|
59 | FILE *f; |
---|
60 | char *line; |
---|
61 | size_t len; |
---|
62 | |
---|
63 | if ((f = fopen(filename, "r")) == NULL) |
---|
64 | return (NULL); |
---|
65 | /* get first non-empty non-comment line */ |
---|
66 | line = openpam_readline(f, NULL, &len); |
---|
67 | if (strlcmp("otpauth://", line, len) == 0) { |
---|
68 | key = oath_key_from_uri(line); |
---|
69 | } else { |
---|
70 | openpam_log(PAM_LOG_ERROR, |
---|
71 | "unrecognized key file format: %s", filename); |
---|
72 | key = NULL; |
---|
73 | } |
---|
74 | fclose(f); |
---|
75 | return (key); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * The =oath_key_from_file function loads a key from the specified file. |
---|
80 | * The file format is automatically detected. |
---|
81 | * |
---|
82 | * The following key file formats are supported: |
---|
83 | * |
---|
84 | * - otpauth URI |
---|
85 | * |
---|
86 | * Keys created with =oath_key_from_file must be freed using |
---|
87 | * =oath_key_free. |
---|
88 | * |
---|
89 | * >oath_key_alloc |
---|
90 | * >oath_key_free |
---|
91 | * >oath_key_from_uri |
---|
92 | * |
---|
93 | * AUTHOR UIO |
---|
94 | */ |
---|