1 /* $FreeBSD: src/crypto/kerberosIV/appl/kauth/kauthd.c,v 1.1.1.3.2.1 2000/07/20 14:04:33 assar Exp $ */
4 * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
38 RCSID("$Id: kauthd.c,v 1.25.2.1 2000/06/28 19:07:58 assar Exp $");
41 static char locuser[SNAME_SZ];
43 static char tktfile[MaxPathLen];
47 des_key_schedule *schedule;
49 struct sockaddr_in *me, *her;
53 decrypt_remote_tkt (const char *user,
64 struct remote_args *args = (struct remote_args *)varg;
66 write_encrypted (args->sock, cip->dat, cip->length,
67 *args->schedule, args->session, args->me,
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);
82 char instance[INST_SZ];
83 des_key_schedule schedule;
84 struct sockaddr_in thisaddr, thataddr;
89 struct passwd *passwd;
90 char version[KRB_SENDAUTH_VLEN + 1];
91 char remotehost[MaxHostNameLen];
93 addrlen = sizeof(thisaddr);
94 if (getsockname (sock, (struct sockaddr *)&thisaddr, &addrlen) < 0 ||
95 addrlen != sizeof(thisaddr)) {
98 addrlen = sizeof(thataddr);
99 if (getpeername (sock, (struct sockaddr *)&thataddr, &addrlen) < 0 ||
100 addrlen != sizeof(thataddr)) {
104 inaddr2str (thataddr.sin_addr, remotehost, sizeof(remotehost));
106 k_getsockinst (sock, instance, sizeof(instance));
107 status = krb_recvauth (KOPT_DO_MUTUAL, sock, &ticket, "rcmd", instance,
108 &thataddr, &thisaddr, &auth, "", schedule,
110 if (status != KSUCCESS ||
111 strncmp(version, KAUTH_VERSION, KRB_SENDAUTH_VLEN) != 0) {
114 len = read_encrypted (sock, buf, sizeof(buf), &data, schedule,
115 &auth.session, &thataddr, &thisaddr);
117 write_encrypted (sock, "read_enc failed",
118 sizeof("read_enc failed") - 1, schedule,
119 &auth.session, &thisaddr, &thataddr);
122 if (unpack_args(data, &princ, &lifetime, locuser,
124 write_encrypted (sock, "unpack_args failed",
125 sizeof("unpack_args failed") - 1, schedule,
126 &auth.session, &thisaddr, &thataddr);
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);
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);
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);
155 write_encrypted (sock, "ok", sizeof("ok") - 1, schedule,
156 &auth.session, &thisaddr, &thataddr);
159 snprintf(tktfile, sizeof(tktfile), "%s%u", TKT_ROOT, (unsigned)getuid());
160 krb_set_tkt_string (tktfile);
163 struct remote_args arg;
166 arg.schedule = &schedule;
167 arg.session = &auth.session;
171 status = krb_get_in_tkt (princ.name, princ.instance, princ.realm,
172 KRB_TICKET_GRANTING_TICKET,
174 lifetime, NULL, decrypt_remote_tkt, &arg);
176 if (status == KSUCCESS) {
177 syslog (LOG_INFO, "from %s(%s): %s -> %s",
179 inet_ntoa(thataddr.sin_addr),
181 krb_unparse_name (&princ));
182 write_encrypted (sock, "ok", sizeof("ok") - 1, schedule,
183 &auth.session, &thisaddr, &thataddr);
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);
195 main (int argc, char **argv)
197 openlog ("kauthd", LOG_ODELAY, LOG_AUTH);
199 if(argc > 1 && strcmp(argv[1], "-i") == 0)
200 mini_inetd (k_getportbyname("kauth", "tcp", htons(KAUTH_PORT)));
201 return doit(STDIN_FILENO);