]> andersk Git - moira.git/blob - clients/lib/utils.c
540fee7a3fc67a13da77485a4c38ae8a0660a67e
[moira.git] / clients / lib / utils.c
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 <krb.h>
16 #include <krb5.h>
17
18 #include <sys/types.h>
19
20 #ifdef HAVE_UNAME
21 #include <sys/utsname.h>
22 #endif
23
24 #ifndef _WIN32
25 #include <sys/socket.h>
26 #include <netdb.h>
27 #include <netinet/in.h>
28 #endif /* _WIN32 */
29
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 RCSID("$Header$");
36
37 extern char *whoami;
38 extern krb5_context context;
39
40 int mrcl_connect(char *server, char *client, int version, int auth)
41 {
42   int status;
43   char *motd;
44
45   status = mr_connect(server);
46   if (status)
47     {
48       com_err(whoami, status, "while connecting to Moira");
49       return MRCL_FAIL;
50     }
51
52   status = mr_motd(&motd);
53   if (status)
54     {
55       mr_disconnect();
56       com_err(whoami, status, "while checking server status");
57       return MRCL_FAIL;
58     }
59   if (motd)
60     {
61       fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
62               motd);
63       mr_disconnect();
64       return MRCL_FAIL;
65     }
66
67   status = mr_version(version);
68   if (status)
69     {
70       if (status == MR_UNKNOWN_PROC)
71         {
72           if (version > 2)
73             status = MR_VERSION_HIGH;
74           else
75             status = MR_SUCCESS;
76         }
77
78       if (status == MR_VERSION_HIGH)
79         {
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.");
82         }
83       else if (status && status != MR_VERSION_LOW)
84         {
85           com_err(whoami, status, "while setting query version number.");
86           mr_disconnect();
87           return MRCL_FAIL;
88         }
89     }
90
91   if (auth)
92     {
93       status = mr_krb5_auth(client);
94       if (status)
95         {
96           com_err(whoami, status, "while authenticating to Moira.");
97           mr_disconnect();
98           return MRCL_AUTH_ERROR;
99         }
100     }
101
102   return MRCL_SUCCESS;
103 }
104
105 char *mrcl_krb_user(void)
106 {
107   int flags = 0;
108   krb5_ccache cache = NULL;
109   krb5_principal princ;
110   krb5_error_code status;
111   char *name;
112
113   if (!context)
114     krb5_init_context(&context);
115
116   status = krb5_cc_default(context, &cache);
117   if (status)
118     {
119       com_err(whoami, status, "while reading Kerberos ticket file.");
120       return NULL;
121     }
122
123   status = krb5_cc_get_principal(context, cache, &princ);
124   if (status)
125     {
126       com_err(whoami, status, "while retrieving principal name.");
127       return NULL;
128     }
129
130   return (char *)krb5_princ_component(context, princ, 0);
131
132 }
133
134 char *partial_canonicalize_hostname(char *s)
135 {
136   char buf[256], *cp;
137   static char *def_domain = NULL;
138
139   if (!def_domain)
140     {
141       if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
142         {
143           cp = strchr(buf, '.');
144           if (cp)
145             def_domain = strdup(++cp);
146         }
147       else
148         {
149           struct hostent *hp;
150 #ifdef HAVE_UNAME
151           struct utsname name;
152           uname(&name);
153           hp = gethostbyname(name.nodename);
154 #else
155           char  name[256];
156           gethostname(name, sizeof(name));
157           name[sizeof(name)-1] = 0;
158           hp = gethostbyname(name);
159 #endif /* HAVE_UNAME */
160           cp = strchr(hp->h_name, '.');
161           if (cp)
162             def_domain = strdup(++cp);
163         }
164       if (!def_domain)
165         def_domain = "";
166     }
167
168   if (strchr(s, '.') || strchr(s, '*'))
169     return s;
170   sprintf(buf, "%s.%s", s, def_domain);
171   free(s);
172   return strdup(buf);
173 }
This page took 0.186026 seconds and 3 git commands to generate.