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>
17 #include <sys/types.h>
20 #include <sys/utsname.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
37 extern krb5_context context;
39 int mrcl_connect(char *server, char *client, int version, int auth)
44 status = mr_connect(server);
47 com_err(whoami, status, "while connecting to Moira");
51 status = mr_motd(&motd);
55 com_err(whoami, status, "while checking server status");
60 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
66 status = mr_version(version);
69 if (status == MR_UNKNOWN_PROC)
72 status = MR_VERSION_HIGH;
77 if (status == MR_VERSION_HIGH)
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.");
82 else if (status && status != MR_VERSION_LOW)
84 com_err(whoami, status, "while setting query version number.");
92 status = mr_krb5_auth(client);
94 /* New client talking to old server, try krb4. */
95 if (status == MR_UNKNOWN_PROC)
96 status = mr_auth(client);
100 com_err(whoami, status, "while authenticating to Moira.");
102 return MRCL_AUTH_ERROR;
109 char *mrcl_krb_user(void)
112 krb5_ccache cache = NULL;
113 krb5_principal princ = NULL;
114 krb5_error_code status;
115 char *username = NULL;
118 krb5_init_context(&context);
120 status = krb5_cc_default(context, &cache);
123 com_err(whoami, status, "while reading Kerberos ticket file.");
127 status = krb5_cc_get_principal(context, cache, &princ);
130 com_err(whoami, status, "while retrieving principal name.");
134 username = malloc(krb5_princ_component(context, princ, 0)->length + 1);
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';
144 krb5_cc_close(context, cache);
146 krb5_free_principal(context, princ);
151 char *partial_canonicalize_hostname(char *s)
154 static char *def_domain = NULL;
158 if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
160 cp = strchr(buf, '.');
162 def_domain = strdup(++cp);
170 hp = gethostbyname(name.nodename);
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, '.');
179 def_domain = strdup(++cp);
185 if (strchr(s, '.') || strchr(s, '*'))
187 sprintf(buf, "%s.%s", s, def_domain);