]> andersk Git - moira.git/blob - clients/lib/utils.c
Add ERROR_TABLE_BASE_krb to the return value of tf_init(), so we're
[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
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
34 RCSID("$Header$");
35
36 extern char *whoami;
37
38 int mrcl_connect(char *server, char *client, int version, int auth)
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
65   status = mr_version(version);
66   if (status)
67     {
68       if (status == MR_UNKNOWN_PROC)
69         {
70           if (version > 2)
71             status = MR_VERSION_HIGH;
72           else
73             status = MR_SUCCESS;
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         }
81       else if (status && status != MR_VERSION_LOW)
82         {
83           com_err(whoami, status, "while setting query version number.");
84           mr_disconnect();
85           return MRCL_FAIL;
86         }
87     }
88
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
103 char *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       status += ERROR_TABLE_BASE_krb;
120       com_err(whoami, status, "reading Kerberos ticket file.");
121       return NULL;
122     }
123
124   return pname;
125 }
126
127 char *partial_canonicalize_hostname(char *s)
128 {
129   char buf[256], *cp;
130   static char *def_domain = NULL;
131
132   if (!def_domain)
133     {
134       if (mr_host(buf, sizeof(buf)) == MR_SUCCESS)
135         {
136           cp = strchr(buf, '.');
137           if (cp)
138             def_domain = strdup(++cp);
139         }
140       else
141         {
142           struct hostent *hp;
143 #ifdef HAVE_UNAME
144           struct utsname name;
145           uname(&name);
146           hp = gethostbyname(name.nodename);
147 #else
148           char  name[256];
149           gethostname(name, sizeof(name));
150           name[sizeof(name)-1] = 0;
151           hp = gethostbyname(name);
152 #endif /* HAVE_UNAME */
153           cp = strchr(hp->h_name, '.');
154           if (cp)
155             def_domain = strdup(++cp);
156         }
157       if (!def_domain)
158         def_domain = "";
159     }
160
161   if (strchr(s, '.') || strchr(s, '*'))
162     return s;
163   sprintf(buf, "%s.%s", s, def_domain);
164   free(s);
165   return strdup(buf);
166 }
This page took 0.049712 seconds and 5 git commands to generate.