Change __signed to signed.
[dragonfly.git] / crypto / kerberosIV / kadmin / kpasswd.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
4    Export of this software from the United States of America is assumed
5    to require a specific license from the United States Government.
6    It is the responsibility of any person or organization contemplating
7    export to obtain such a license before exporting.
8
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission.  M.I.T. makes no representations about the suitability of
17 this software for any purpose.  It is provided "as is" without express
18 or implied warranty.
19
20   */
21
22 /*
23  * change your password with kerberos
24  */
25
26 #include "kadm_locl.h"
27
28 RCSID("$Id: kpasswd.c,v 1.29 1999/11/13 06:33:20 assar Exp $");
29
30 static void
31 usage(int value)
32 {
33     fprintf(stderr, "Usage: ");
34     fprintf(stderr, "kpasswd [-h ] [-n user] [-i instance] [-r realm] ");
35     fprintf(stderr, "[-u fullname]\n");
36     exit(value);
37 }
38
39 int
40 main(int argc, char **argv)
41 {
42     krb_principal principal;
43     krb_principal default_principal;
44     int realm_given = 0;        /* True if realm was give on cmdline */
45     int use_default = 1;        /* True if we should use default name */
46     int status;                 /* return code */
47     char pword[MAX_KPW_LEN];
48     int c;
49     char tktstring[MaxPathLen];
50     
51     set_progname (argv[0]);
52
53     memset (&principal, 0, sizeof(principal));
54     memset (&default_principal, 0, sizeof(default_principal));
55     
56     krb_get_default_principal (default_principal.name,
57                                default_principal.instance,
58                                default_principal.realm);
59
60     while ((c = getopt(argc, argv, "u:n:i:r:h")) != -1) {
61         switch (c) {
62         case 'u':
63             status = krb_parse_name (optarg, &principal);
64             if (status != KSUCCESS)
65                 errx (2, "%s", krb_get_err_text(status));
66             if (principal.realm[0])
67                 realm_given++;
68             else if (krb_get_lrealm(principal.realm, 1) != KSUCCESS)
69                 errx (1, "Could not find default realm!");
70             break;
71         case 'n':
72             if (k_isname(optarg))
73                 strlcpy(principal.name,
74                                 optarg,
75                                 sizeof(principal.name));
76             else {
77                 warnx("Bad name: %s", optarg);
78                 usage(1);
79             }
80             break;
81         case 'i':
82             if (k_isinst(optarg))
83                 strlcpy(principal.instance,
84                                 optarg,
85                                 sizeof(principal.instance));
86             else {
87                 warnx("Bad instance: %s", optarg);
88                 usage(1);
89             }
90             break;
91         case 'r':
92             if (k_isrealm(optarg)) {
93                 strlcpy(principal.realm,
94                                 optarg,
95                                 sizeof(principal.realm));
96                 realm_given++; 
97             } else {
98                 warnx("Bad realm: %s", optarg);
99                 usage(1);
100             }
101             break;
102         case 'h':
103             usage(0);
104             break;
105         default:
106             usage(1);
107             break;
108         }
109         use_default = 0;
110     }
111     if (optind < argc) {
112         use_default = 0;
113         status = krb_parse_name (argv[optind], &principal);
114         if(status != KSUCCESS)
115             errx (1, "%s", krb_get_err_text (status));
116     }
117
118     if (use_default) {
119         strlcpy(principal.name,
120                         default_principal.name,
121                         sizeof(principal.name));
122         strlcpy(principal.instance,
123                         default_principal.instance,
124                         sizeof(principal.instance));
125         strlcpy(principal.realm,
126                         default_principal.realm,
127                         sizeof(principal.realm));
128     } else {
129         if (!principal.name[0])
130             strlcpy(principal.name,
131                             default_principal.name,
132                             sizeof(principal.name));
133         if (!principal.realm[0])
134             strlcpy(principal.realm,
135                             default_principal.realm,
136                             sizeof(principal.realm));
137     }
138
139     snprintf(tktstring, sizeof(tktstring), "%s_cpw_%u",
140              TKT_ROOT, (unsigned)getpid());
141     krb_set_tkt_string(tktstring);
142     
143     if (get_pw_new_pwd(pword, sizeof(pword), &principal,
144                        realm_given)) {
145         dest_tkt ();
146         exit(1);
147     }
148     
149     status = kadm_init_link (PWSERV_NAME, KRB_MASTER, principal.realm);
150     if (status != KADM_SUCCESS) 
151         com_err(argv[0], status, "while initializing");
152     else {
153         des_cblock newkey;
154         char *pw_msg; /* message from server */
155
156         des_string_to_key(pword, &newkey);
157         status = kadm_change_pw_plain((unsigned char*)&newkey, pword, &pw_msg);
158         memset(newkey, 0, sizeof(newkey));
159       
160         if (status == KADM_INSECURE_PW)
161             warnx ("Insecure password: %s", pw_msg);
162         else if (status != KADM_SUCCESS)
163             com_err(argv[0], status, " attempting to change password.");
164     }
165     memset(pword, 0, sizeof(pword));
166
167     if (status != KADM_SUCCESS)
168         fprintf(stderr,"Password NOT changed.\n");
169     else
170         printf("Password changed.\n");
171
172     dest_tkt();
173     if (status)
174         return 2;
175     else 
176         return 0;
177 }