]> andersk Git - moira.git/blob - server/mr_sauth.c
change kerberos name from "sms.sms" to "[MOIRA_SNAME].[hostname]" (i.e.
[moira.git] / server / mr_sauth.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  */
11
12 #ifndef lint
13 static char *rcsid_sms_sauth_c = "$Header$";
14 #endif lint
15
16 #include <mit-copyright.h>
17 #include <strings.h>
18 #include "sms_server.h"
19 #include <ctype.h>
20 #include <krb_et.h>
21
22 extern char buf1[];
23 extern char *whoami;
24 extern char *malloc();
25
26 char *kname_unparse();
27
28 /*
29  * Handle a SMS_AUTH RPC request.
30  *
31  * argv[0] is a kerberos authenticator.  Decompose it, and if
32  * successful, store the name the user authenticated to in 
33  * cl->cl_name.
34  */
35
36 void
37 do_auth(cl)
38         client *cl;
39 {
40         KTEXT_ST auth;
41         AUTH_DAT ad;
42         int status, ok;
43         char buf[REALM_SZ+INST_SZ+ANAME_SZ], hostbuf[BUFSIZ], *host, *p;
44
45         auth.length = cl->args->sms_argl[0];
46         bcopy(cl->args->sms_argv[0], (char *)auth.dat, auth.length);
47         auth.mbz = 0;
48         if (gethostname(hostbuf, sizeof(hostbuf)) < 0)
49           com_err(whoami, errno, "Unable to get local hostname");
50         host = canonicalize_hostname(strsave(hostbuf));
51         for (p = host; *p && *p != '.'; p++)
52           if (isupper(*p))
53             *p = tolower(*p);
54         *p = 0;
55
56         if ((status = krb_rd_req (&auth, MOIRA_SNAME, host, cl->haddr.sin_addr,
57                                  &ad, "")) != 0) {
58                 status += ERROR_TABLE_BASE_krb;
59                 cl->reply.sms_status = status;
60                 if (log_flags & LOG_RES)
61                         com_err(whoami, status, "(authentication failed)");
62                 return;
63         }
64         free(host);
65
66         bcopy(ad.pname, cl->kname.name, ANAME_SZ);
67         bcopy(ad.pinst, cl->kname.inst, INST_SZ);
68         bcopy(ad.prealm, cl->kname.realm, REALM_SZ);
69         strcpy(cl->clname, kname_unparse(ad.pname, ad.pinst, ad.prealm));
70
71         if (ad.pinst[0] == 0 && !strcmp(ad.prealm, krb_realm))
72           ok = 1;
73         else
74           ok = 0;
75         /* this is in a separate function because it accesses the database */
76         set_krb_mapping(cl->clname, ad.pname, ok,
77                         &cl->client_id, &cl->users_id);
78
79         if (cl->args->sms_version_no == SMS_VERSION_2) {
80             bcopy(cl->args->sms_argv[1], cl->entity, 8);
81             cl->entity[8] = 0;
82         } else {
83             strcpy(cl->entity, "???");
84         }
85         bzero(&ad, sizeof(ad)); /* Clean up session key, etc. */
86
87         if (log_flags & LOG_RES)
88             com_err(whoami, 0, "Auth to %s using %s, uid %d cid %d",
89                     cl->clname, cl->entity, cl->users_id, cl->client_id);
90         if (cl->users_id == 0)
91           cl->reply.sms_status = SMS_USER_AUTH;
92 }
93
94
95 /* Turn a principal, instance, realm triple into a single non-ambiguous 
96  * string.  This is the inverse of kname_parse().  It returns a pointer
97  * to a static buffer, or NULL on error.
98  */
99
100 char *kname_unparse(p, i, r)
101 char *p;
102 char *i;
103 char *r;
104 {
105     static char name[MAX_K_NAME_SZ];
106     char *s;
107
108     s = name;
109     if (!p || strlen(p) > ANAME_SZ)
110       return(NULL);
111     while (*p) {
112         switch (*p) {
113         case '@':
114             *s++ = '\\';
115             *s++ = '@';
116             break;
117         case '.':
118             *s++ = '\\';
119             *s++ = '.';
120             break;
121         case '\\':
122             *s++ = '\\';
123             *s++ = '\\';
124             break;
125         default:
126             *s++ = *p;
127         }
128         p++;
129     }
130     if (i && *i) {
131         if (strlen(i) > INST_SZ)
132           return(NULL);
133         *s++ = '.';
134         while (*i) {
135             switch (*i) {
136             case '@':
137                 *s++ = '\\';
138                 *s++ = '@';
139                 break;
140             case '.':
141                 *s++ = '\\';
142                 *s++ = '.';
143                 break;
144             case '\\':
145                 *s++ = '\\';
146                 *s++ = '\\';
147                 break;
148             default:
149                 *s++ = *i;
150             }
151             i++;
152         }
153     }
154     *s++ = '@';
155     if (!r || strlen(r) > REALM_SZ)
156       return(NULL);
157     while (*r) {
158         switch (*r) {
159         case '@':
160             *s++ = '\\';
161             *s++ = '@';
162             break;
163         case '\\':
164             *s++ = '\\';
165             *s++ = '\\';
166             break;
167         default:
168             *s++ = *r;
169         }
170         r++;
171     }
172     *s = '\0';
173     return(&name[0]);
174 }
This page took 0.06381 seconds and 5 git commands to generate.