1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 2014 - 2021, Steve Holme, <steve_holme@hotmail.com>. |
9 | * |
10 | * This software is licensed as described in the file COPYING, which |
11 | * you should have received as part of this distribution. The terms |
12 | * are also available at https://curl.se/docs/copyright.html. |
13 | * |
14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | * copies of the Software, and permit persons to whom the Software is |
16 | * furnished to do so, under the terms of the COPYING file. |
17 | * |
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | * KIND, either express or implied. |
20 | * |
21 | ***************************************************************************/ |
22 | |
23 | #include "curl_setup.h" |
24 | |
25 | #include <curl/curl.h> |
26 | |
27 | #include "vauth.h" |
28 | #include "curl_multibyte.h" |
29 | #include "curl_printf.h" |
30 | |
31 | /* The last #include files should be: */ |
32 | #include "curl_memory.h" |
33 | #include "memdebug.h" |
34 | |
35 | /* |
36 | * Curl_auth_build_spn() |
37 | * |
38 | * This is used to build a SPN string in the following formats: |
39 | * |
40 | * service/host@realm (Not currently used) |
41 | * service/host (Not used by GSS-API) |
42 | * service@realm (Not used by Windows SSPI) |
43 | * |
44 | * Parameters: |
45 | * |
46 | * service [in] - The service type such as http, smtp, pop or imap. |
47 | * host [in] - The host name. |
48 | * realm [in] - The realm. |
49 | * |
50 | * Returns a pointer to the newly allocated SPN. |
51 | */ |
52 | #if !defined(USE_WINDOWS_SSPI) |
53 | char *Curl_auth_build_spn(const char *service, const char *host, |
54 | const char *realm) |
55 | { |
56 | char *spn = NULL; |
57 | |
58 | /* Generate our SPN */ |
59 | if(host && realm) |
60 | spn = aprintf("%s/%s@%s" , service, host, realm); |
61 | else if(host) |
62 | spn = aprintf("%s/%s" , service, host); |
63 | else if(realm) |
64 | spn = aprintf("%s@%s" , service, realm); |
65 | |
66 | /* Return our newly allocated SPN */ |
67 | return spn; |
68 | } |
69 | #else |
70 | TCHAR *Curl_auth_build_spn(const char *service, const char *host, |
71 | const char *realm) |
72 | { |
73 | char *utf8_spn = NULL; |
74 | TCHAR *tchar_spn = NULL; |
75 | TCHAR *dupe_tchar_spn = NULL; |
76 | |
77 | (void) realm; |
78 | |
79 | /* Note: We could use DsMakeSPN() or DsClientMakeSpnForTargetServer() rather |
80 | than doing this ourselves but the first is only available in Windows XP |
81 | and Windows Server 2003 and the latter is only available in Windows 2000 |
82 | but not Windows95/98/ME or Windows NT4.0 unless the Active Directory |
83 | Client Extensions are installed. As such it is far simpler for us to |
84 | formulate the SPN instead. */ |
85 | |
86 | /* Generate our UTF8 based SPN */ |
87 | utf8_spn = aprintf("%s/%s" , service, host); |
88 | if(!utf8_spn) |
89 | return NULL; |
90 | |
91 | /* Allocate and return a TCHAR based SPN. Since curlx_convert_UTF8_to_tchar |
92 | must be freed by curlx_unicodefree we'll dupe the result so that the |
93 | pointer this function returns can be normally free'd. */ |
94 | tchar_spn = curlx_convert_UTF8_to_tchar(utf8_spn); |
95 | free(utf8_spn); |
96 | if(!tchar_spn) |
97 | return NULL; |
98 | dupe_tchar_spn = _tcsdup(tchar_spn); |
99 | curlx_unicodefree(tchar_spn); |
100 | return dupe_tchar_spn; |
101 | } |
102 | #endif /* USE_WINDOWS_SSPI */ |
103 | |
104 | /* |
105 | * Curl_auth_user_contains_domain() |
106 | * |
107 | * This is used to test if the specified user contains a Windows domain name as |
108 | * follows: |
109 | * |
110 | * Domain\User (Down-level Logon Name) |
111 | * Domain/User (curl Down-level format - for compatibility with existing code) |
112 | * User@Domain (User Principal Name) |
113 | * |
114 | * Note: The user name may be empty when using a GSS-API library or Windows |
115 | * SSPI as the user and domain are either obtained from the credentials cache |
116 | * when using GSS-API or via the currently logged in user's credentials when |
117 | * using Windows SSPI. |
118 | * |
119 | * Parameters: |
120 | * |
121 | * user [in] - The user name. |
122 | * |
123 | * Returns TRUE on success; otherwise FALSE. |
124 | */ |
125 | bool Curl_auth_user_contains_domain(const char *user) |
126 | { |
127 | bool valid = FALSE; |
128 | |
129 | if(user && *user) { |
130 | /* Check we have a domain name or UPN present */ |
131 | char *p = strpbrk(user, "\\/@" ); |
132 | |
133 | valid = (p != NULL && p > user && p < user + strlen(user) - 1 ? TRUE : |
134 | FALSE); |
135 | } |
136 | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
137 | else |
138 | /* User and domain are obtained from the GSS-API credentials cache or the |
139 | currently logged in user from Windows */ |
140 | valid = TRUE; |
141 | #endif |
142 | |
143 | return valid; |
144 | } |
145 | |