get_default_principal.c revision 178825
1178825Sdfr/*
2178825Sdfr * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr * All rights reserved.
5178825Sdfr *
6178825Sdfr * Redistribution and use in source and binary forms, with or without
7178825Sdfr * modification, are permitted provided that the following conditions
8178825Sdfr * are met:
9178825Sdfr *
10178825Sdfr * 1. Redistributions of source code must retain the above copyright
11178825Sdfr *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors
18178825Sdfr *    may be used to endorse or promote products derived from this software
19178825Sdfr *    without specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178825Sdfr * SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34233294Sstas#include "krb5_locl.h"
35178825Sdfr
36178825SdfrRCSID("$Id: get_default_principal.c 14870 2005-04-20 20:53:29Z lha $");
37178825Sdfr
38178825Sdfr/*
39178825Sdfr * Try to find out what's a reasonable default principal.
40178825Sdfr */
41178825Sdfr
42178825Sdfrstatic const char*
43178825Sdfrget_env_user(void)
44178825Sdfr{
45178825Sdfr    const char *user = getenv("USER");
46178825Sdfr    if(user == NULL)
47233294Sstas	user = getenv("LOGNAME");
48178825Sdfr    if(user == NULL)
49178825Sdfr	user = getenv("USERNAME");
50233294Sstas    return user;
51178825Sdfr}
52178825Sdfr
53233294Sstas/*
54233294Sstas * Will only use operating-system dependant operation to get the
55233294Sstas * default principal, for use of functions that in ccache layer to
56233294Sstas * avoid recursive calls.
57233294Sstas */
58178825Sdfr
59178825Sdfrkrb5_error_code
60178825Sdfr_krb5_get_default_principal_local (krb5_context context,
61178825Sdfr				   krb5_principal *princ)
62178825Sdfr{
63178825Sdfr    krb5_error_code ret;
64178825Sdfr    const char *user;
65178825Sdfr    uid_t uid;
66178825Sdfr
67178825Sdfr    *princ = NULL;
68178825Sdfr
69178825Sdfr    uid = getuid();
70178825Sdfr    if(uid == 0) {
71178825Sdfr	user = getlogin();
72178825Sdfr	if(user == NULL)
73233294Sstas	    user = get_env_user();
74233294Sstas	if(user != NULL && strcmp(user, "root") != 0)
75233294Sstas	    ret = krb5_make_principal(context, princ, NULL, user, "root", NULL);
76233294Sstas	else
77233294Sstas	    ret = krb5_make_principal(context, princ, NULL, "root", NULL);
78233294Sstas    } else {
79233294Sstas	struct passwd *pw = getpwuid(uid);
80233294Sstas	if(pw != NULL)
81233294Sstas	    user = pw->pw_name;
82233294Sstas	else {
83178825Sdfr	    user = get_env_user();
84178825Sdfr	    if(user == NULL)
85178825Sdfr		user = getlogin();
86178825Sdfr	}
87178825Sdfr	if(user == NULL) {
88178825Sdfr	    krb5_set_error_string(context,
89178825Sdfr				  "unable to figure out current principal");
90178825Sdfr	    return ENOTTY; /* XXX */
91178825Sdfr	}
92178825Sdfr	ret = krb5_make_principal(context, princ, NULL, user, NULL);
93178825Sdfr    }
94178825Sdfr    return ret;
95178825Sdfr}
96178825Sdfr
97178825Sdfrkrb5_error_code KRB5_LIB_FUNCTION
98178825Sdfrkrb5_get_default_principal (krb5_context context,
99178825Sdfr			    krb5_principal *princ)
100178825Sdfr{
101178825Sdfr    krb5_error_code ret;
102178825Sdfr    krb5_ccache id;
103178825Sdfr
104178825Sdfr    *princ = NULL;
105178825Sdfr
106178825Sdfr    ret = krb5_cc_default (context, &id);
107178825Sdfr    if (ret == 0) {
108178825Sdfr	ret = krb5_cc_get_principal (context, id, princ);
109178825Sdfr	krb5_cc_close (context, id);
110178825Sdfr	if (ret == 0)
111178825Sdfr	    return 0;
112178825Sdfr    }
113178825Sdfr
114178825Sdfr    return _krb5_get_default_principal_local(context, princ);
115178825Sdfr}
116178825Sdfr