]> andersk Git - moira.git/blame - clients/lib/utils.c
Make mrcl_krb_user() not broken.
[moira.git] / clients / lib / utils.c
CommitLineData
013096e5 1/* $Id$
2 *
3 * Random client utilities.
4 *
5 * Copyright (C) 1999 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
10#include <mit-copyright.h>
11#include <moira.h>
12#include <mrclient.h>
13
013096e5 14#include <com_err.h>
15#include <krb.h>
991417e4 16#include <krb5.h>
013096e5 17
654e3753 18#include <sys/types.h>
19
20#ifdef HAVE_UNAME
21#include <sys/utsname.h>
22#endif
23
24#ifndef _WIN32
25#include <sys/socket.h>
26#include <netdb.h>
27#include <netinet/in.h>
28#endif /* _WIN32 */
29
30#include <ctype.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
013096e5 35RCSID("$Header$");
36
37extern char *whoami;
991417e4 38extern krb5_context context;
013096e5 39
4342cf40 40int mrcl_connect(char *server, char *client, int version, int auth)
013096e5 41{
42 int status;
43 char *motd;
44
45 status = mr_connect(server);
46 if (status)
47 {
48 com_err(whoami, status, "while connecting to Moira");
49 return MRCL_FAIL;
50 }
51
52 status = mr_motd(&motd);
53 if (status)
54 {
55 mr_disconnect();
56 com_err(whoami, status, "while checking server status");
57 return MRCL_FAIL;
58 }
59 if (motd)
60 {
61 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
62 motd);
63 mr_disconnect();
64 return MRCL_FAIL;
65 }
66
4342cf40 67 status = mr_version(version);
68 if (status)
69 {
70 if (status == MR_UNKNOWN_PROC)
71 {
72 if (version > 2)
2e899a98 73 status = MR_VERSION_HIGH;
4342cf40 74 else
2e899a98 75 status = MR_SUCCESS;
4342cf40 76 }
77
78 if (status == MR_VERSION_HIGH)
79 {
80 com_err(whoami, 0, "Warning: This client is running newer code than the server.");
81 com_err(whoami, 0, "Some operations may not work.");
82 }
2e899a98 83 else if (status && status != MR_VERSION_LOW)
4342cf40 84 {
85 com_err(whoami, status, "while setting query version number.");
86 mr_disconnect();
87 return MRCL_FAIL;
88 }
89 }
90
013096e5 91 if (auth)
92 {
991417e4 93 status = mr_krb5_auth(client);
e357a0ec 94
95 /* New client talking to old server, try krb4. */
96 if (status == MR_UNKNOWN_PROC)
97 status = mr_auth(client);
98
013096e5 99 if (status)
100 {
101 com_err(whoami, status, "while authenticating to Moira.");
102 mr_disconnect();
103 return MRCL_AUTH_ERROR;
104 }
105 }
106
107 return MRCL_SUCCESS;
108}
109
110char *mrcl_krb_user(void)
111{
991417e4 112 int flags = 0;
113 krb5_ccache cache = NULL;
e357a0ec 114 krb5_principal princ = NULL;
991417e4 115 krb5_error_code status;
e357a0ec 116 char *username = NULL;
991417e4 117
118 if (!context)
119 krb5_init_context(&context);
013096e5 120
991417e4 121 status = krb5_cc_default(context, &cache);
122 if (status)
013096e5 123 {
991417e4 124 com_err(whoami, status, "while reading Kerberos ticket file.");
e357a0ec 125 goto out;
013096e5 126 }
127
991417e4 128 status = krb5_cc_get_principal(context, cache, &princ);
129 if (status)
013096e5 130 {
991417e4 131 com_err(whoami, status, "while retrieving principal name.");
e357a0ec 132 goto out;
013096e5 133 }
134
e357a0ec 135 username = malloc(krb5_princ_component(context, princ, 0)->length + 1);
136 if (!username)
137 goto out;
138
139 strncpy(username, krb5_princ_component(context, princ, 0)->data,
140 krb5_princ_component(context, princ, 0)->length);
141 username[krb5_princ_component(context, princ, 0)->length] = '\0';
142
143 out:
144 if (cache)
145 krb5_cc_close(context, cache);
146 if (princ)
147 krb5_free_principal(context, princ);
991417e4 148
e357a0ec 149 return username;
013096e5 150}
654e3753 151
152char *partial_canonicalize_hostname(char *s)
153{
154 char buf[256], *cp;
155 static char *def_domain = NULL;
156
157 if (!def_domain)
158 {
159 if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
160 {
161 cp = strchr(buf, '.');
162 if (cp)
163 def_domain = strdup(++cp);
164 }
165 else
166 {
167 struct hostent *hp;
168#ifdef HAVE_UNAME
169 struct utsname name;
170 uname(&name);
171 hp = gethostbyname(name.nodename);
172#else
173 char name[256];
174 gethostname(name, sizeof(name));
175 name[sizeof(name)-1] = 0;
176 hp = gethostbyname(name);
177#endif /* HAVE_UNAME */
178 cp = strchr(hp->h_name, '.');
179 if (cp)
180 def_domain = strdup(++cp);
181 }
182 if (!def_domain)
183 def_domain = "";
184 }
185
186 if (strchr(s, '.') || strchr(s, '*'))
187 return s;
188 sprintf(buf, "%s.%s", s, def_domain);
189 free(s);
190 return strdup(buf);
191}
This page took 0.134483 seconds and 5 git commands to generate.