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