1 /*
2  * Copyright (c) 2011-2014 Yubico AB
3  * Copyright (c) 2011 Tollef Fog Heen <tfheen@err.no>
4  * All rights reserved.
5  *
6  * Author : Fredrik Thulin <fredrik@yubico.com>
7  * Author : Tollef Fog Heen <tfheen@err.no>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  *
13  *     * Redistributions of source code must retain the above copyright
14  *       notice, this list of conditions and the following disclaimer.
15  *
16  *     * Redistributions in binary form must reproduce the above
17  *       copyright notice, this list of conditions and the following
18  *       disclaimer in the documentation and/or other materials provided
19  *       with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef __PAM_YUBICO_UTIL_H_INCLUDED__
35 #define __PAM_YUBICO_UTIL_H_INCLUDED__
36 
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <pwd.h>
40 
41 #define D(file, x...) do {							\
42   fprintf (file, "debug: %s:%d (%s): ", __FILE__, __LINE__, __FUNCTION__);	\
43   fprintf (file, x);								\
44   fprintf (file, "\n");							\
45 } while (0)
46 
47 /* Return values for authorize_user_token and authorize_user_token_ldap */
48 #define AUTH_NO_TOKENS -2 /* The user has no associated tokens */
49 #define AUTH_ERROR      0 /* Internal error when looking up associated tokens */
50 #define AUTH_FOUND      1 /* The requested token is associated to the user */
51 #define AUTH_NOT_FOUND -1 /* The requested token is not associated to the user */
52 
53 int get_user_cfgfile_path(const char *common_path, const char *filename, const struct passwd *user, char **fn);
54 int check_user_token(const char *authfile, const char *username, const char *otp_id, int verbose, FILE *debug_file);
55 
56 #if HAVE_CR
57 #include <ykcore.h>
58 
59 /* Challenges can be 0..63 or 64 bytes long, depending on YubiKey configuration.
60  * We settle for 63 bytes to have something that works with all configurations.
61  */
62 #define CR_CHALLENGE_SIZE	63
63 #define CR_RESPONSE_SIZE	20
64 #define CR_SALT_SIZE      32
65 
66 #define CR_DEFAULT_ITERATIONS 10000
67 
68 struct chalresp_state {
69   char challenge[CR_CHALLENGE_SIZE];
70   uint8_t challenge_len;
71   char response[CR_RESPONSE_SIZE];
72   uint8_t response_len;
73   char salt[CR_SALT_SIZE];
74   uint8_t salt_len;
75   uint8_t slot;
76   uint32_t iterations;
77 };
78 
79 typedef struct chalresp_state CR_STATE;
80 
81 int generate_random(void *buf, int len);
82 
83 int check_user_challenge_file(const char *chalresp_path, const struct passwd *user, FILE *debug_file);
84 int get_user_challenge_file(YK_KEY *yk, const char *chalresp_path, const struct passwd *user, char **fn, FILE *debug_file);
85 
86 int load_chalresp_state(FILE *f, CR_STATE *state, bool verbose, FILE *debug_file);
87 int write_chalresp_state(FILE *f, CR_STATE *state);
88 
89 int init_yubikey(YK_KEY **yk);
90 int check_firmware_version(YK_KEY *yk, bool verbose, bool quiet, FILE *debug_file);
91 int challenge_response(YK_KEY *yk, int slot,
92 		       char *challenge, unsigned int len,
93 		       bool hmac, bool may_block, bool verbose,
94 		       char *response, unsigned int res_size, unsigned int *res_len);
95 
96 #endif /* HAVE_CR */
97 
98 size_t filter_result_len(const char *filter, const char *user, char *output);
99 char *filter_printf(const char *filter, const char *user);
100 
101 #endif /* __PAM_YUBICO_UTIL_H_INCLUDED__ */
102