]> andersk Git - moira.git/blob - server/mr_sauth.c
fixup reference to errno
[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         extern int errno;
45
46         auth.length = cl->args->sms_argl[0];
47         bcopy(cl->args->sms_argv[0], (char *)auth.dat, auth.length);
48         auth.mbz = 0;
49         if (gethostname(hostbuf, sizeof(hostbuf)) < 0)
50           com_err(whoami, errno, "Unable to get local hostname");
51         host = canonicalize_hostname(strsave(hostbuf));
52         for (p = host; *p && *p != '.'; p++)
53           if (isupper(*p))
54             *p = tolower(*p);
55         *p = 0;
56
57         if ((status = krb_rd_req (&auth, MOIRA_SNAME, host, cl->haddr.sin_addr,
58                                  &ad, "")) != 0) {
59                 status += ERROR_TABLE_BASE_krb;
60                 cl->reply.sms_status = status;
61                 if (log_flags & LOG_RES)
62                         com_err(whoami, status, "(authentication failed)");
63                 return;
64         }
65         free(host);
66
67         bcopy(ad.pname, cl->kname.name, ANAME_SZ);
68         bcopy(ad.pinst, cl->kname.inst, INST_SZ);
69         bcopy(ad.prealm, cl->kname.realm, REALM_SZ);
70         strcpy(cl->clname, kname_unparse(ad.pname, ad.pinst, ad.prealm));
71
72         if (ad.pinst[0] == 0 && !strcmp(ad.prealm, krb_realm))
73           ok = 1;
74         else
75           ok = 0;
76         /* this is in a separate function because it accesses the database */
77         set_krb_mapping(cl->clname, ad.pname, ok,
78                         &cl->client_id, &cl->users_id);
79
80         if (cl->args->sms_version_no == SMS_VERSION_2) {
81             bcopy(cl->args->sms_argv[1], cl->entity, 8);
82             cl->entity[8] = 0;
83         } else {
84             strcpy(cl->entity, "???");
85         }
86         bzero(&ad, sizeof(ad)); /* Clean up session key, etc. */
87
88         if (log_flags & LOG_RES)
89             com_err(whoami, 0, "Auth to %s using %s, uid %d cid %d",
90                     cl->clname, cl->entity, cl->users_id, cl->client_id);
91         if (cl->users_id == 0)
92           cl->reply.sms_status = SMS_USER_AUTH;
93 }
94
95
96 /* Turn a principal, instance, realm triple into a single non-ambiguous 
97  * string.  This is the inverse of kname_parse().  It returns a pointer
98  * to a static buffer, or NULL on error.
99  */
100
101 char *kname_unparse(p, i, r)
102 char *p;
103 char *i;
104 char *r;
105 {
106     static char name[MAX_K_NAME_SZ];
107     char *s;
108
109     s = name;
110     if (!p || strlen(p) > ANAME_SZ)
111       return(NULL);
112     while (*p) {
113         switch (*p) {
114         case '@':
115             *s++ = '\\';
116             *s++ = '@';
117             break;
118         case '.':
119             *s++ = '\\';
120             *s++ = '.';
121             break;
122         case '\\':
123             *s++ = '\\';
124             *s++ = '\\';
125             break;
126         default:
127             *s++ = *p;
128         }
129         p++;
130     }
131     if (i && *i) {
132         if (strlen(i) > INST_SZ)
133           return(NULL);
134         *s++ = '.';
135         while (*i) {
136             switch (*i) {
137             case '@':
138                 *s++ = '\\';
139                 *s++ = '@';
140                 break;
141             case '.':
142                 *s++ = '\\';
143                 *s++ = '.';
144                 break;
145             case '\\':
146                 *s++ = '\\';
147                 *s++ = '\\';
148                 break;
149             default:
150                 *s++ = *i;
151             }
152             i++;
153         }
154     }
155     *s++ = '@';
156     if (!r || strlen(r) > REALM_SZ)
157       return(NULL);
158     while (*r) {
159         switch (*r) {
160         case '@':
161             *s++ = '\\';
162             *s++ = '@';
163             break;
164         case '\\':
165             *s++ = '\\';
166             *s++ = '\\';
167             break;
168         default:
169             *s++ = *r;
170         }
171         r++;
172     }
173     *s = '\0';
174     return(&name[0]);
175 }
This page took 0.039731 seconds and 5 git commands to generate.