xref: /freebsd-src/crypto/heimdal/lib/krb5/get_default_principal.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "krb5_locl.h"
35b528cefcSMark Murray 
36b528cefcSMark Murray /*
37b528cefcSMark Murray  * Try to find out what's a reasonable default principal.
38b528cefcSMark Murray  */
39b528cefcSMark Murray 
40adb0ddaeSAssar Westerlund static const char*
get_env_user(void)41adb0ddaeSAssar Westerlund get_env_user(void)
42adb0ddaeSAssar Westerlund {
43adb0ddaeSAssar Westerlund     const char *user = getenv("USER");
44adb0ddaeSAssar Westerlund     if(user == NULL)
45adb0ddaeSAssar Westerlund 	user = getenv("LOGNAME");
46adb0ddaeSAssar Westerlund     if(user == NULL)
47adb0ddaeSAssar Westerlund 	user = getenv("USERNAME");
48adb0ddaeSAssar Westerlund     return user;
49adb0ddaeSAssar Westerlund }
50adb0ddaeSAssar Westerlund 
51*ae771770SStanislav Sedov #ifndef _WIN32
52*ae771770SStanislav Sedov 
53c19800e8SDoug Rabson /*
54c19800e8SDoug Rabson  * Will only use operating-system dependant operation to get the
55c19800e8SDoug Rabson  * default principal, for use of functions that in ccache layer to
56c19800e8SDoug Rabson  * avoid recursive calls.
57c19800e8SDoug Rabson  */
58c19800e8SDoug Rabson 
59b528cefcSMark Murray krb5_error_code
_krb5_get_default_principal_local(krb5_context context,krb5_principal * princ)60c19800e8SDoug Rabson _krb5_get_default_principal_local (krb5_context context,
61b528cefcSMark Murray 				   krb5_principal *princ)
62b528cefcSMark Murray {
63b528cefcSMark Murray     krb5_error_code ret;
64b528cefcSMark Murray     const char *user;
65adb0ddaeSAssar Westerlund     uid_t uid;
66b528cefcSMark Murray 
67c19800e8SDoug Rabson     *princ = NULL;
68adb0ddaeSAssar Westerlund 
69adb0ddaeSAssar Westerlund     uid = getuid();
70adb0ddaeSAssar Westerlund     if(uid == 0) {
71adb0ddaeSAssar Westerlund 	user = getlogin();
72b528cefcSMark Murray 	if(user == NULL)
73adb0ddaeSAssar Westerlund 	    user = get_env_user();
74adb0ddaeSAssar Westerlund 	if(user != NULL && strcmp(user, "root") != 0)
75b528cefcSMark Murray 	    ret = krb5_make_principal(context, princ, NULL, user, "root", NULL);
76adb0ddaeSAssar Westerlund 	else
77adb0ddaeSAssar Westerlund 	    ret = krb5_make_principal(context, princ, NULL, "root", NULL);
78b528cefcSMark Murray     } else {
79adb0ddaeSAssar Westerlund 	struct passwd *pw = getpwuid(uid);
80adb0ddaeSAssar Westerlund 	if(pw != NULL)
81adb0ddaeSAssar Westerlund 	    user = pw->pw_name;
82adb0ddaeSAssar Westerlund 	else {
83adb0ddaeSAssar Westerlund 	    user = get_env_user();
84adb0ddaeSAssar Westerlund 	    if(user == NULL)
85adb0ddaeSAssar Westerlund 		user = getlogin();
86adb0ddaeSAssar Westerlund 	}
87adb0ddaeSAssar Westerlund 	if(user == NULL) {
88*ae771770SStanislav Sedov 	    krb5_set_error_message(context, ENOTTY,
89*ae771770SStanislav Sedov 				   N_("unable to figure out current "
90*ae771770SStanislav Sedov 				      "principal", ""));
91adb0ddaeSAssar Westerlund 	    return ENOTTY; /* XXX */
92adb0ddaeSAssar Westerlund 	}
93b528cefcSMark Murray 	ret = krb5_make_principal(context, princ, NULL, user, NULL);
94b528cefcSMark Murray     }
95b528cefcSMark Murray     return ret;
96b528cefcSMark Murray }
97c19800e8SDoug Rabson 
98*ae771770SStanislav Sedov #else  /* _WIN32 */
99*ae771770SStanislav Sedov 
100*ae771770SStanislav Sedov #define SECURITY_WIN32
101*ae771770SStanislav Sedov #include <security.h>
102*ae771770SStanislav Sedov 
103*ae771770SStanislav Sedov krb5_error_code
_krb5_get_default_principal_local(krb5_context context,krb5_principal * princ)104*ae771770SStanislav Sedov _krb5_get_default_principal_local(krb5_context context,
105*ae771770SStanislav Sedov 				  krb5_principal *princ)
106*ae771770SStanislav Sedov {
107*ae771770SStanislav Sedov     /* See if we can get the principal first.  We only expect this to
108*ae771770SStanislav Sedov        work if logged into a domain. */
109*ae771770SStanislav Sedov     {
110*ae771770SStanislav Sedov 	char username[1024];
111*ae771770SStanislav Sedov 	ULONG sz = sizeof(username);
112*ae771770SStanislav Sedov 
113*ae771770SStanislav Sedov 	if (GetUserNameEx(NameUserPrincipal, username, &sz)) {
114*ae771770SStanislav Sedov 	    return krb5_parse_name_flags(context, username,
115*ae771770SStanislav Sedov 					 KRB5_PRINCIPAL_PARSE_ENTERPRISE,
116*ae771770SStanislav Sedov 					 princ);
117*ae771770SStanislav Sedov 	}
118*ae771770SStanislav Sedov     }
119*ae771770SStanislav Sedov 
120*ae771770SStanislav Sedov     /* Just get the Windows username.  This should pretty much always
121*ae771770SStanislav Sedov        work. */
122*ae771770SStanislav Sedov     {
123*ae771770SStanislav Sedov 	char username[1024];
124*ae771770SStanislav Sedov 	DWORD dsz = sizeof(username);
125*ae771770SStanislav Sedov 
126*ae771770SStanislav Sedov 	if (GetUserName(username, &dsz)) {
127*ae771770SStanislav Sedov 	    return krb5_make_principal(context, princ, NULL, username, NULL);
128*ae771770SStanislav Sedov 	}
129*ae771770SStanislav Sedov     }
130*ae771770SStanislav Sedov 
131*ae771770SStanislav Sedov     /* Failing that, we look at the environment */
132*ae771770SStanislav Sedov     {
133*ae771770SStanislav Sedov 	const char * username = get_env_user();
134*ae771770SStanislav Sedov 
135*ae771770SStanislav Sedov 	if (username == NULL) {
136*ae771770SStanislav Sedov 	    krb5_set_error_string(context,
137*ae771770SStanislav Sedov 				  "unable to figure out current principal");
138*ae771770SStanislav Sedov 	    return ENOTTY;	/* Really? */
139*ae771770SStanislav Sedov 	}
140*ae771770SStanislav Sedov 
141*ae771770SStanislav Sedov 	return krb5_make_principal(context, princ, NULL, username, NULL);
142*ae771770SStanislav Sedov     }
143*ae771770SStanislav Sedov }
144*ae771770SStanislav Sedov 
145*ae771770SStanislav Sedov #endif
146*ae771770SStanislav Sedov 
147*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_default_principal(krb5_context context,krb5_principal * princ)148c19800e8SDoug Rabson krb5_get_default_principal (krb5_context context,
149c19800e8SDoug Rabson 			    krb5_principal *princ)
150c19800e8SDoug Rabson {
151c19800e8SDoug Rabson     krb5_error_code ret;
152c19800e8SDoug Rabson     krb5_ccache id;
153c19800e8SDoug Rabson 
154c19800e8SDoug Rabson     *princ = NULL;
155c19800e8SDoug Rabson 
156c19800e8SDoug Rabson     ret = krb5_cc_default (context, &id);
157c19800e8SDoug Rabson     if (ret == 0) {
158c19800e8SDoug Rabson 	ret = krb5_cc_get_principal (context, id, princ);
159c19800e8SDoug Rabson 	krb5_cc_close (context, id);
160c19800e8SDoug Rabson 	if (ret == 0)
161c19800e8SDoug Rabson 	    return 0;
162c19800e8SDoug Rabson     }
163c19800e8SDoug Rabson 
164c19800e8SDoug Rabson     return _krb5_get_default_principal_local(context, princ);
165c19800e8SDoug Rabson }
166