]> andersk Git - moira.git/blob - clients/lib/utils.c
7399c94dc6a281638d46274858c96d72bfe96d2e
[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
95       /* New client talking to old server, try krb4. */
96       if (status == MR_UNKNOWN_PROC)
97         status = mr_auth(client);
98
99       if (status)
100         {
101           com_err(whoami, status, "while authenticating to Moira.");
102           mr_disconnect();
103           return MRCL_AUTH_ERROR;
104         }
105     }
106
107   return MRCL_SUCCESS;
108 }
109
110 char *mrcl_krb_user(void)
111 {
112   int flags = 0;
113   krb5_ccache cache = NULL;
114   krb5_principal princ = NULL;
115   krb5_error_code status;
116   char *username = NULL;
117
118   if (!context)
119     krb5_init_context(&context);
120
121   status = krb5_cc_default(context, &cache);
122   if (status)
123     {
124       com_err(whoami, status, "while reading Kerberos ticket file.");
125       goto out;
126     }
127
128   status = krb5_cc_get_principal(context, cache, &princ);
129   if (status)
130     {
131       com_err(whoami, status, "while retrieving principal name.");
132       goto out;
133     }
134
135   username = malloc(krb5_princ_component(context, princ, 0)->length + 1);
136   if (!username)
137     goto out;
138
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';
142
143  out:
144   if (cache)
145     krb5_cc_close(context, cache);
146   if (princ)
147     krb5_free_principal(context, princ);
148
149   return username;
150 }
151
152 char *partial_canonicalize_hostname(char *s)
153 {
154   char buf[256], *cp;
155   static char *def_domain = NULL;
156
157   if (!def_domain)
158     {
159       if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
160         {
161           cp = strchr(buf, '.');
162           if (cp)
163             def_domain = strdup(++cp);
164         }
165       else
166         {
167           struct hostent *hp;
168 #ifdef HAVE_UNAME
169           struct utsname name;
170           uname(&name);
171           hp = gethostbyname(name.nodename);
172 #else
173           char  name[256];
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, '.');
179           if (cp)
180             def_domain = strdup(++cp);
181         }
182       if (!def_domain)
183         def_domain = "";
184     }
185
186   if (strchr(s, '.') || strchr(s, '*'))
187     return s;
188   sprintf(buf, "%s.%s", s, def_domain);
189   free(s);
190   return strdup(buf);
191 }
This page took 0.038167 seconds and 3 git commands to generate.