]> andersk Git - moira.git/blame - clients/lib/utils.c
Use krb5_error_code instead of int for krb5 library function return values.
[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>
0bb1ca53 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;
0bb1ca53 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 {
0bb1ca53 93 status = mr_krb5_auth(client);
013096e5 94 if (status)
95 {
96 com_err(whoami, status, "while authenticating to Moira.");
97 mr_disconnect();
98 return MRCL_AUTH_ERROR;
99 }
100 }
101
102 return MRCL_SUCCESS;
103}
104
105char *mrcl_krb_user(void)
106{
fdb41991 107 int flags = 0;
0bb1ca53 108 krb5_ccache cache = NULL;
109 krb5_principal princ;
fdb41991 110 krb5_error_code status;
0bb1ca53 111 char *name;
112
113 if (!context)
114 krb5_init_context(&context);
013096e5 115
0bb1ca53 116 status = krb5_cc_default(context, &cache);
117 if (status)
013096e5 118 {
0bb1ca53 119 com_err(whoami, status, "while reading Kerberos ticket file.");
120 return NULL;
013096e5 121 }
122
0bb1ca53 123 status = krb5_cc_get_principal(context, cache, &princ);
124 if (status)
013096e5 125 {
0bb1ca53 126 com_err(whoami, status, "while retrieving principal name.");
013096e5 127 return NULL;
128 }
129
0bb1ca53 130 return (char *)krb5_princ_component(context, princ, 0);
131
013096e5 132}
654e3753 133
134char *partial_canonicalize_hostname(char *s)
135{
136 char buf[256], *cp;
137 static char *def_domain = NULL;
138
139 if (!def_domain)
140 {
141 if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
142 {
143 cp = strchr(buf, '.');
144 if (cp)
145 def_domain = strdup(++cp);
146 }
147 else
148 {
149 struct hostent *hp;
150#ifdef HAVE_UNAME
151 struct utsname name;
152 uname(&name);
153 hp = gethostbyname(name.nodename);
154#else
155 char name[256];
156 gethostname(name, sizeof(name));
157 name[sizeof(name)-1] = 0;
158 hp = gethostbyname(name);
159#endif /* HAVE_UNAME */
160 cp = strchr(hp->h_name, '.');
161 if (cp)
162 def_domain = strdup(++cp);
163 }
164 if (!def_domain)
165 def_domain = "";
166 }
167
168 if (strchr(s, '.') || strchr(s, '*'))
169 return s;
170 sprintf(buf, "%s.%s", s, def_domain);
171 free(s);
172 return strdup(buf);
173}
This page took 0.076019 seconds and 5 git commands to generate.