]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/heimdal/appl/su/su.c
import of heimdal 0.3f
[FreeBSD/FreeBSD.git] / crypto / heimdal / appl / su / su.c
1 /*
2  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32
33 #include <config.h>
34
35 RCSID("$Id: su.c,v 1.20 2001/02/20 01:44:48 assar Exp $");
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <syslog.h>
42
43 #ifdef HAVE_PATHS_H
44 #include <paths.h>
45 #endif
46
47 #ifdef HAVE_SHADOW_H
48 #include <shadow.h>
49 #endif
50
51 #include <pwd.h>
52
53 #ifdef HAVE_OPENSSL_DES_H
54 #include <openssl/des.h>
55 #else
56 #include <des.h>
57 #endif
58 #include <krb5.h>
59 #include <kafs.h>
60 #include <err.h>
61 #include <roken.h>
62 #include <getarg.h>
63 #include <kafs.h>
64
65 #ifndef _PATH_DEFPATH
66 #define _PATH_DEFPATH "/usr/bin:/bin"
67 #endif
68
69 #ifndef _PATH_BSHELL
70 #define _PATH_BSHELL "/bin/sh"
71 #endif
72
73 int kerberos_flag = 1;
74 int csh_f_flag;
75 int full_login;
76 int env_flag;
77 char *kerberos_instance = "root";
78 int help_flag;
79 int version_flag;
80 char *cmd;
81
82 struct getargs args[] = {
83     { "kerberos", 'K', arg_negative_flag, &kerberos_flag,
84       "don't use kerberos" },
85     { NULL,       'f', arg_flag,          &csh_f_flag,
86       "don't read .cshrc" },
87     { "full",     'l', arg_flag,          &full_login,
88       "simulate full login" },
89     { NULL,       'm', arg_flag,          &env_flag,
90       "leave environment unmodified" },
91     { "instance", 'i', arg_string,        &kerberos_instance,
92       "root instance to use" },
93     { "command",  'c', arg_string,        &cmd,
94       "command to execute" },
95     { "help",     'h', arg_flag,          &help_flag },
96     { "version",  0,   arg_flag,          &version_flag },
97 };
98
99
100 static void
101 usage (int ret)
102 {
103     arg_printusage (args,
104                     sizeof(args)/sizeof(*args),
105                     NULL,
106                     "[login [shell arguments]]");
107     exit (ret);
108 }
109
110 static struct passwd*
111 make_info(struct passwd *pwd)
112 {
113     struct passwd *info;
114     info = malloc(sizeof(*info));
115     if(info == NULL)
116         return NULL;
117     info->pw_name = strdup(pwd->pw_name);
118     info->pw_passwd = strdup(pwd->pw_passwd);
119     info->pw_uid = pwd->pw_uid;
120     info->pw_gid = pwd->pw_gid;
121     info->pw_dir = strdup(pwd->pw_dir);
122     info->pw_shell = strdup(pwd->pw_shell);
123     if(info->pw_name == NULL || info->pw_passwd == NULL ||
124        info->pw_dir == NULL || info->pw_shell == NULL)
125         return NULL;
126     return info;
127 }
128
129 #ifdef KRB5
130 static krb5_context context;
131 static krb5_ccache ccache;
132 #endif
133
134 static int
135 krb5_verify(struct passwd *login_info, struct passwd *su_info,
136             const char *kerberos_instance)
137 {
138 #ifdef KRB5
139     krb5_error_code ret;
140     krb5_principal p;
141         
142     ret = krb5_init_context (&context);
143     if (ret) {
144 #if 0
145         warnx("krb5_init_context failed: %d", ret);
146 #endif
147         return 1;
148     }
149         
150     if (strcmp (su_info->pw_name, "root") == 0)
151         ret = krb5_make_principal(context, &p, NULL, 
152                                   login_info->pw_name,
153                                   kerberos_instance,
154                                   NULL);
155     else
156         ret = krb5_make_principal(context, &p, NULL, 
157                                   su_info->pw_name,
158                                   NULL);
159     if(ret)
160         return 1;
161         
162     if(su_info->pw_uid != 0 || krb5_kuserok(context, p, su_info->pw_name)) {
163         ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
164         if(ret) {
165 #if 1
166             krb5_warn(context, ret, "krb5_cc_gen_new");
167 #endif
168             krb5_free_principal (context, p);
169             return 1;
170         }
171         ret = krb5_verify_user_lrealm(context, p, ccache, NULL, TRUE, NULL);
172         krb5_free_principal (context, p);
173         if(ret) {
174             krb5_cc_destroy(context, ccache);
175             switch (ret) {
176             case KRB5_LIBOS_PWDINTR :
177                 break;
178             case KRB5KRB_AP_ERR_BAD_INTEGRITY:
179             case KRB5KRB_AP_ERR_MODIFIED:
180                 krb5_warnx(context, "Password incorrect");
181                 break;
182             default :
183                 krb5_warn(context, ret, "krb5_verify_user");
184                 break;
185             }
186             return 1;
187         }
188         return 0;
189     }
190     krb5_free_principal (context, p);
191 #endif
192     return 1;
193 }
194
195 #ifdef KRB5
196 static int
197 krb5_start_session(void)
198 {
199     krb5_ccache ccache2;
200     char *cc_name;
201     int ret;
202
203     ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
204     if (ret) {
205         krb5_cc_destroy(context, ccache);
206         return 1;
207     }
208
209     ret = krb5_cc_copy_cache(context, ccache, ccache2);
210
211     asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
212              krb5_cc_get_name(context, ccache2));
213     esetenv("KRB5CCNAME", cc_name, 1);
214
215     /* we want to export this even if we don't directly support KRB4 */
216     {
217 #ifndef TKT_ROOT
218 #define TKT_ROOT "/tmp/tkt"
219 #endif
220         int fd;
221         char tkfile[256];
222         strlcpy(tkfile, TKT_ROOT, sizeof(tkfile));
223         strlcat(tkfile, "_XXXXXX", sizeof(tkfile));
224         fd = mkstemp(tkfile);
225         if(fd >= 0) {
226             close(fd);
227             esetenv("KRBTKFILE", tkfile, 1);
228         }
229     }
230             
231 #ifdef KRB4
232     /* convert creds? */
233     if(k_hasafs()) {
234         if (k_setpag() == 0)
235             krb5_afslog(context, ccache2, NULL, NULL);
236     }
237 #endif
238             
239     krb5_cc_close(context, ccache2);
240     krb5_cc_destroy(context, ccache);
241     return 0;
242 }
243 #endif
244
245 static int
246 verify_unix(struct passwd *su)
247 {
248     char prompt[128];
249     char pw_buf[1024];
250     char *pw;
251     int r;
252     if(su->pw_passwd != NULL && *su->pw_passwd != '\0') {
253         snprintf(prompt, sizeof(prompt), "%s's password: ", su->pw_name);
254         r = des_read_pw_string(pw_buf, sizeof(pw_buf), prompt, 0);
255         if(r != 0)
256             exit(0);
257         pw = crypt(pw_buf, su->pw_passwd);
258         memset(pw_buf, 0, sizeof(pw_buf));
259         if(strcmp(pw, su->pw_passwd) != 0)
260             return 1;
261     }
262     return 0;
263 }
264
265 int
266 main(int argc, char **argv)
267 {
268     int i, optind = 0;
269     char *su_user;
270     struct passwd *su_info;
271     char *login_user = NULL;
272     struct passwd *login_info;
273
274     struct passwd *pwd;
275
276     char *shell;
277
278     int ok = 0;
279     int kerberos_error=1;
280
281     setprogname (argv[0]);
282
283     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
284         usage(1);
285
286     for (i=0; i < optind; i++)
287       if (strcmp(argv[i], "-") == 0) {
288          full_login = 1;
289          break;
290       }
291         
292     if(help_flag)
293         usage(0);
294     if(version_flag) {
295         print_version(NULL);
296         exit(0);
297     }
298     if(optind >= argc)
299         su_user = "root";
300     else
301         su_user = argv[optind++];
302
303     pwd = k_getpwnam(su_user);
304     if(pwd == NULL)
305         errx (1, "unknown login %s", su_user);
306     if (pwd->pw_uid == 0 && strcmp ("root", su_user) != 0) {
307         syslog (LOG_ALERT, "NIS attack, user %s has uid 0", su_user);
308         errx (1, "unknown login %s", su_user);
309     }
310     su_info = make_info(pwd);
311     
312 #if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN)
313     login_user = getlogin();
314 #endif
315     if(login_user == NULL || (pwd = getpwnam(login_user)) == NULL)
316         pwd = getpwuid(getuid());
317     if(pwd == NULL)
318         errx(1, "who are you?");
319     login_info = make_info(pwd);
320     if(env_flag)
321         shell = login_info->pw_shell;
322     else
323         shell = su_info->pw_shell;
324     if(shell == NULL || *shell == '\0')
325         shell = _PATH_BSHELL;
326     
327     if(kerberos_flag && ok == 0 &&
328       (kerberos_error=krb5_verify(login_info, su_info, kerberos_instance)) == 0)
329         ok++;
330
331     if(ok == 0 && login_info->pw_uid && verify_unix(su_info) != 0) {
332         printf("Sorry!\n");
333         exit(1);
334     }
335
336 #ifdef HAVE_GETSPNAM
337    {  struct spwd *sp;
338       long    today;
339     
340     sp = getspnam(su_info->pw_name);
341     if (sp != NULL) {
342         today = time(0)/(24L * 60 * 60);
343         if (sp->sp_expire > 0) {
344             if (today >= sp->sp_expire) {
345                 if (login_info->pw_uid) 
346                     errx(1,"Your account has expired.");
347                 else
348                     printf("Your account has expired.");
349             }
350             else if (sp->sp_expire - today < 14) 
351                 printf("Your account will expire in %d days.\n",
352                        (int)(sp->sp_expire - today));
353         } 
354         if (sp->sp_max > 0) {
355             if (today >= sp->sp_lstchg + sp->sp_max) {
356                 if (login_info->pw_uid)    
357                     errx(1,"Your password has expired. Choose a new one.");
358                 else
359                     printf("Your password has expired. Choose a new one.");
360             }
361             else if (today >= sp->sp_lstchg + sp->sp_max - sp->sp_warn)
362                 printf("Your account will expire in %d days.\n",
363                        (int)(sp->sp_lstchg + sp->sp_max -today));
364         }
365     }
366     }
367 #endif
368     {
369         char *tty = ttyname (STDERR_FILENO);
370         syslog (LOG_NOTICE | LOG_AUTH, tty ? "%s to %s" : "%s to %s on %s",
371                 login_info->pw_name, su_info->pw_name, tty);
372     }
373
374
375     if(!env_flag) {
376         if(full_login) {
377             char *t = getenv ("TERM");
378             
379             environ = malloc (10 * sizeof (char *));
380             if (environ == NULL)
381                 err (1, "malloc");
382             environ[0] = NULL;
383             esetenv ("PATH", _PATH_DEFPATH, 1);
384             if (t)
385                 esetenv ("TERM", t, 1);
386             if (chdir (su_info->pw_dir) < 0)
387                 errx (1, "no directory");
388         }
389         if (full_login || su_info->pw_uid)
390             esetenv ("USER", su_info->pw_name, 1);
391         esetenv("HOME", su_info->pw_dir, 1);
392         esetenv("SHELL", shell, 1);
393     }
394
395     {
396         int i;
397         char **args;
398         char *p;
399
400         p = strrchr(shell, '/');
401         if(p)
402             p++;
403         else
404             p = shell;
405
406         if (strcmp(p, "csh") != 0)
407             csh_f_flag = 0;
408
409         args = malloc(((cmd ? 2 : 0) + 1 + argc - optind + 1 + csh_f_flag) * sizeof(*args));
410         if (args == NULL)
411             err (1, "malloc");
412         i = 0;
413         if(full_login)
414             asprintf(&args[i++], "-%s", p);
415         else
416             args[i++] = p;
417         if (cmd) {
418            args[i++] = "-c";
419            args[i++] = cmd;
420         }  
421            
422         if (csh_f_flag)
423             args[i++] = "-f";
424
425         for (argv += optind; *argv; ++argv)
426            args[i++] = *argv;
427         args[i] = NULL;
428         
429         if(setgid(su_info->pw_gid) < 0)
430             err(1, "setgid");
431         if (initgroups (su_info->pw_name, su_info->pw_gid) < 0)
432             err (1, "initgroups");
433         if(setuid(su_info->pw_uid) < 0
434            || (su_info->pw_uid != 0 && setuid(0) == 0))
435             err(1, "setuid");
436
437 #ifdef KRB5
438         if (!kerberos_error)
439            krb5_start_session();
440 #endif
441         execv(shell, args);
442     }
443     
444     exit(1);
445 }