Initial import from FreeBSD RELENG_4:
[games.git] / crypto / kerberosIV / appl / kauth / kauthd.c
1 /* $FreeBSD: src/crypto/kerberosIV/appl/kauth/kauthd.c,v 1.1.1.3.2.1 2000/07/20 14:04:33 assar Exp $ */
2
3 /*
4  * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "kauth.h"
37
38 RCSID("$Id: kauthd.c,v 1.25.2.1 2000/06/28 19:07:58 assar Exp $");
39
40 krb_principal princ;
41 static char locuser[SNAME_SZ];
42 static int  lifetime;
43 static char tktfile[MaxPathLen];
44
45 struct remote_args {
46      int sock;
47      des_key_schedule *schedule;
48      des_cblock *session;
49      struct sockaddr_in *me, *her;
50 };
51
52 static int
53 decrypt_remote_tkt (const char *user,
54                     const char *inst,
55                     const char *realm,
56                     const void *varg,
57                     key_proc_t key_proc,
58                     KTEXT *cipp)
59 {
60      char buf[BUFSIZ];
61      void *ptr;
62      int len;
63      KTEXT cip  = *cipp;
64      struct remote_args *args = (struct remote_args *)varg;
65
66      write_encrypted (args->sock, cip->dat, cip->length,
67                       *args->schedule, args->session, args->me,
68                       args->her);
69      len = read_encrypted (args->sock, buf, sizeof(buf), &ptr, *args->schedule,
70                            args->session, args->her, args->me);
71      memcpy(cip->dat, ptr, cip->length);
72           
73      return 0;
74 }
75
76 static int
77 doit(int sock)
78 {
79      int status;
80      KTEXT_ST ticket;
81      AUTH_DAT auth;
82      char instance[INST_SZ];
83      des_key_schedule schedule;
84      struct sockaddr_in thisaddr, thataddr;
85      int addrlen;
86      int len;
87      char buf[BUFSIZ];
88      void *data;
89      struct passwd *passwd;
90      char version[KRB_SENDAUTH_VLEN + 1];
91      char remotehost[MaxHostNameLen];
92
93      addrlen = sizeof(thisaddr);
94      if (getsockname (sock, (struct sockaddr *)&thisaddr, &addrlen) < 0 ||
95          addrlen != sizeof(thisaddr)) {
96           return 1;
97      }
98      addrlen = sizeof(thataddr);
99      if (getpeername (sock, (struct sockaddr *)&thataddr, &addrlen) < 0 ||
100          addrlen != sizeof(thataddr)) {
101           return 1;
102      }
103
104      inaddr2str (thataddr.sin_addr, remotehost, sizeof(remotehost));
105
106      k_getsockinst (sock, instance, sizeof(instance));
107      status = krb_recvauth (KOPT_DO_MUTUAL, sock, &ticket, "rcmd", instance,
108                             &thataddr, &thisaddr, &auth, "", schedule,
109                             version);
110      if (status != KSUCCESS ||
111          strncmp(version, KAUTH_VERSION, KRB_SENDAUTH_VLEN) != 0) {
112           return 1;
113      }
114      len = read_encrypted (sock, buf, sizeof(buf), &data, schedule,
115                            &auth.session, &thataddr, &thisaddr);
116      if (len < 0) {
117           write_encrypted (sock, "read_enc failed",
118                            sizeof("read_enc failed") - 1, schedule,
119                            &auth.session, &thisaddr, &thataddr);
120           return 1;
121      }
122      if (unpack_args(data, &princ, &lifetime, locuser,
123                      tktfile)) {
124           write_encrypted (sock, "unpack_args failed",
125                            sizeof("unpack_args failed") - 1, schedule,
126                            &auth.session, &thisaddr, &thataddr);
127           return 1;
128      }
129
130      if( kuserok(&auth, locuser) != 0) {
131          snprintf(buf, sizeof(buf), "%s cannot get tickets for %s",
132                   locuser, krb_unparse_name(&princ));
133          syslog (LOG_ERR, "%s", buf);
134          write_encrypted (sock, buf, strlen(buf), schedule,
135                           &auth.session, &thisaddr, &thataddr);
136          return 1;
137      }
138      passwd = k_getpwnam (locuser);
139      if (passwd == NULL) {
140           snprintf (buf, sizeof(buf), "No user '%s'", locuser);
141           syslog (LOG_ERR, "%s", buf);
142           write_encrypted (sock, buf, strlen(buf), schedule,
143                            &auth.session, &thisaddr, &thataddr);
144           return 1;
145      }
146      if (setgid (passwd->pw_gid) ||
147          initgroups(passwd->pw_name, passwd->pw_gid) ||
148          setuid(passwd->pw_uid)) {
149           snprintf (buf, sizeof(buf), "Could not change user");
150           syslog (LOG_ERR, "%s", buf);
151           write_encrypted (sock, buf, strlen(buf), schedule,
152                            &auth.session, &thisaddr, &thataddr);
153           return 1;
154      }
155      write_encrypted (sock, "ok", sizeof("ok") - 1, schedule,
156                       &auth.session, &thisaddr, &thataddr);
157
158      if (*tktfile == 0)
159          snprintf(tktfile, sizeof(tktfile), "%s%u", TKT_ROOT, (unsigned)getuid());
160      krb_set_tkt_string (tktfile);
161
162      {
163           struct remote_args arg;
164
165           arg.sock     = sock;
166           arg.schedule = &schedule;
167           arg.session  = &auth.session;
168           arg.me       = &thisaddr;
169           arg.her      = &thataddr;
170
171           status = krb_get_in_tkt (princ.name, princ.instance, princ.realm,
172                                    KRB_TICKET_GRANTING_TICKET,
173                                    princ.realm,
174                                    lifetime, NULL, decrypt_remote_tkt, &arg);
175      }
176      if (status == KSUCCESS) {
177          syslog (LOG_INFO, "from %s(%s): %s -> %s",
178                  remotehost,
179                  inet_ntoa(thataddr.sin_addr),
180                  locuser,
181                  krb_unparse_name (&princ));
182           write_encrypted (sock, "ok", sizeof("ok") - 1, schedule,
183                            &auth.session, &thisaddr, &thataddr);
184           return 0;
185      } else {
186           snprintf (buf, sizeof(buf), "TGT failed: %s", krb_get_err_text(status));
187           syslog (LOG_NOTICE, "%s", buf);
188           write_encrypted (sock, buf, strlen(buf), schedule,
189                            &auth.session, &thisaddr, &thataddr);
190           return 1;
191      }
192 }
193
194 int
195 main (int argc, char **argv)
196 {
197     openlog ("kauthd", LOG_ODELAY, LOG_AUTH);
198
199     if(argc > 1 && strcmp(argv[1], "-i") == 0)
200         mini_inetd (k_getportbyname("kauth", "tcp", htons(KAUTH_PORT)));
201     return doit(STDIN_FILENO);
202 }