]>
Commit | Line | Data |
---|---|---|
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 | ||
14 | #include <com_err.h> | |
15 | #include <krb5.h> | |
16 | ||
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 | ||
34 | RCSID("$Header$"); | |
35 | ||
36 | extern char *whoami; | |
37 | extern krb5_context context; | |
38 | ||
39 | int mrcl_connect(char *server, char *client, int version, int auth) | |
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 | ||
66 | status = mr_version(version); | |
67 | if (status) | |
68 | { | |
69 | if (status == MR_UNKNOWN_PROC) | |
70 | { | |
71 | if (version > 2) | |
72 | status = MR_VERSION_HIGH; | |
73 | else | |
74 | status = MR_SUCCESS; | |
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 | } | |
82 | else if (status && status != MR_VERSION_LOW) | |
83 | { | |
84 | com_err(whoami, status, "while setting query version number."); | |
85 | mr_disconnect(); | |
86 | return MRCL_FAIL; | |
87 | } | |
88 | } | |
89 | ||
90 | if (auth) | |
91 | { | |
92 | status = mr_krb5_auth(client); | |
93 | ||
94 | /* New client talking to old server, try krb4. */ | |
95 | if (status == MR_UNKNOWN_PROC) | |
96 | status = mr_auth(client); | |
97 | ||
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 | { | |
111 | int flags = 0; | |
112 | krb5_ccache cache = NULL; | |
113 | krb5_principal princ = NULL; | |
114 | krb5_error_code status; | |
115 | char *username = NULL; | |
116 | ||
117 | if (!context) | |
118 | krb5_init_context(&context); | |
119 | ||
120 | status = krb5_cc_default(context, &cache); | |
121 | if (status) | |
122 | { | |
123 | com_err(whoami, status, "while reading Kerberos ticket file."); | |
124 | goto out; | |
125 | } | |
126 | ||
127 | status = krb5_cc_get_principal(context, cache, &princ); | |
128 | if (status) | |
129 | { | |
130 | com_err(whoami, status, "while retrieving principal name."); | |
131 | goto out; | |
132 | } | |
133 | ||
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); | |
147 | ||
148 | return username; | |
149 | } | |
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 | } |