3 * Random client utilities.
5 * Copyright (C) 1999 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
18 #include <sys/types.h>
21 #include <sys/utsname.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
38 extern krb5_context context;
40 int mrcl_connect(char *server, char *client, int version, int auth)
45 status = mr_connect(server);
48 com_err(whoami, status, "while connecting to Moira");
52 status = mr_motd(&motd);
56 com_err(whoami, status, "while checking server status");
61 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
67 status = mr_version(version);
70 if (status == MR_UNKNOWN_PROC)
73 status = MR_VERSION_HIGH;
78 if (status == MR_VERSION_HIGH)
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.");
83 else if (status && status != MR_VERSION_LOW)
85 com_err(whoami, status, "while setting query version number.");
93 status = mr_krb5_auth(client);
95 /* New client talking to old server, try krb4. */
96 if (status == MR_UNKNOWN_PROC)
97 status = mr_auth(client);
101 com_err(whoami, status, "while authenticating to Moira.");
103 return MRCL_AUTH_ERROR;
110 char *mrcl_krb_user(void)
113 krb5_ccache cache = NULL;
114 krb5_principal princ = NULL;
115 krb5_error_code status;
116 char *username = NULL;
119 krb5_init_context(&context);
121 status = krb5_cc_default(context, &cache);
124 com_err(whoami, status, "while reading Kerberos ticket file.");
128 status = krb5_cc_get_principal(context, cache, &princ);
131 com_err(whoami, status, "while retrieving principal name.");
135 username = malloc(krb5_princ_component(context, princ, 0)->length + 1);
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';
145 krb5_cc_close(context, cache);
147 krb5_free_principal(context, princ);
152 char *partial_canonicalize_hostname(char *s)
155 static char *def_domain = NULL;
159 if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
161 cp = strchr(buf, '.');
163 def_domain = strdup(++cp);
171 hp = gethostbyname(name.nodename);
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, '.');
180 def_domain = strdup(++cp);
186 if (strchr(s, '.') || strchr(s, '*'))
188 sprintf(buf, "%s.%s", s, def_domain);