]> andersk Git - moira.git/blame - clients/lib/utils.c
move partial_canonicalize_hostname into libmrclient
[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>
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 34RCSID("$Header$");
35
36extern char *whoami;
37
4342cf40 38int mrcl_connect(char *server, char *client, int version, int auth)
013096e5 39{
40 int status;
41 char *motd;
42
43 status = mr_connect(server);
44 if (status)
45 {
46 com_err(whoami, status, "while connecting to Moira");
47 return MRCL_FAIL;
48 }
49
50 status = mr_motd(&motd);
51 if (status)
52 {
53 mr_disconnect();
54 com_err(whoami, status, "while checking server status");
55 return MRCL_FAIL;
56 }
57 if (motd)
58 {
59 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
60 motd);
61 mr_disconnect();
62 return MRCL_FAIL;
63 }
64
4342cf40 65 status = mr_version(version);
66 if (status)
67 {
68 if (status == MR_UNKNOWN_PROC)
69 {
70 if (version > 2)
2e899a98 71 status = MR_VERSION_HIGH;
4342cf40 72 else
2e899a98 73 status = MR_SUCCESS;
4342cf40 74 }
75
76 if (status == MR_VERSION_HIGH)
77 {
78 com_err(whoami, 0, "Warning: This client is running newer code than the server.");
79 com_err(whoami, 0, "Some operations may not work.");
80 }
2e899a98 81 else if (status && status != MR_VERSION_LOW)
4342cf40 82 {
83 com_err(whoami, status, "while setting query version number.");
84 mr_disconnect();
85 return MRCL_FAIL;
86 }
87 }
88
013096e5 89 if (auth)
90 {
91 status = mr_auth(client);
92 if (status)
93 {
94 com_err(whoami, status, "while authenticating to Moira.");
95 mr_disconnect();
96 return MRCL_AUTH_ERROR;
97 }
98 }
99
100 return MRCL_SUCCESS;
101}
102
103char *mrcl_krb_user(void)
104{
105 int status;
106 static char pname[ANAME_SZ];
107
108 status = tf_init(TKT_FILE, R_TKT_FIL);
109 if (status == KSUCCESS)
110 {
111 status = tf_get_pname(pname);
112 tf_close();
113 }
114
115 if (status != KSUCCESS)
116 {
117 /* In case mr_init hasn't been called yet. */
118 initialize_krb_error_table();
119 com_err(whoami, status, "reading Kerberos ticket file.");
120 return NULL;
121 }
122
123 return pname;
124}
654e3753 125
126char *partial_canonicalize_hostname(char *s)
127{
128 char buf[256], *cp;
129 static char *def_domain = NULL;
130
131 if (!def_domain)
132 {
133 if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
134 {
135 cp = strchr(buf, '.');
136 if (cp)
137 def_domain = strdup(++cp);
138 }
139 else
140 {
141 struct hostent *hp;
142#ifdef HAVE_UNAME
143 struct utsname name;
144 uname(&name);
145 hp = gethostbyname(name.nodename);
146#else
147 char name[256];
148 gethostname(name, sizeof(name));
149 name[sizeof(name)-1] = 0;
150 hp = gethostbyname(name);
151#endif /* HAVE_UNAME */
152 cp = strchr(hp->h_name, '.');
153 if (cp)
154 def_domain = strdup(++cp);
155 }
156 if (!def_domain)
157 def_domain = "";
158 }
159
160 if (strchr(s, '.') || strchr(s, '*'))
161 return s;
162 sprintf(buf, "%s.%s", s, def_domain);
163 free(s);
164 return strdup(buf);
165}
This page took 1.22432 seconds and 5 git commands to generate.