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