~ubuntu-branches/ubuntu/lucid/krb5/lucid-updates

« back to all changes in this revision

Viewing changes to src/clients/kpasswd/kpasswd.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2010-01-13 19:00:37 UTC
  • mfrom: (13.4.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100113190037-hrvrxs7cftafjotm
Tags: 1.8+dfsg~alpha1-4
* Add replaces to deal with moving files from krb5-multidev to
  libkrb5-dev, Closes: #565217 
* This is definitely the getting all the conflicts combinations right is
  tricky series of releases.  Sorry about the wasted cycles.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
1
2
#include <stdio.h>
2
3
#include <sys/types.h>
3
4
#include "autoconf.h"
23
24
    struct passwd *pw;
24
25
    krb5_error_code code;
25
26
    if ((pw = getpwuid(getuid()))) {
26
 
        if ((code = krb5_parse_name(kcontext, pw->pw_name, me))) {
27
 
            com_err (program_name, code, "when parsing name %s", pw->pw_name);
28
 
            exit(1);
29
 
        }
 
27
        if ((code = krb5_parse_name(kcontext, pw->pw_name, me))) {
 
28
            com_err (program_name, code, "when parsing name %s", pw->pw_name);
 
29
            exit(1);
 
30
        }
30
31
    } else {
31
 
        fprintf(stderr, "Unable to identify user from password file\n");
32
 
        exit(1);
 
32
        fprintf(stderr, "Unable to identify user from password file\n");
 
33
        exit(1);
33
34
    }
34
35
}
35
36
#else /* HAVE_PWD_H */
44
45
 
45
46
int main(int argc, char *argv[])
46
47
{
47
 
   krb5_error_code ret;
48
 
   krb5_context context;
49
 
   krb5_principal princ;
50
 
   char *pname;
51
 
   krb5_ccache ccache;
52
 
   krb5_get_init_creds_opt *opts = NULL;
53
 
   krb5_creds creds;
54
 
 
55
 
   char pw[1024];
56
 
   unsigned int pwlen;
57
 
   int result_code;
58
 
   krb5_data result_code_string, result_string;
59
 
 
60
 
   if (argc > 2) {
61
 
      fprintf(stderr, "usage: %s [principal]\n", argv[0]);
62
 
      exit(1);
63
 
   }
64
 
 
65
 
   pname = argv[1];
66
 
 
67
 
   ret = krb5_init_context(&context);
68
 
   if (ret) {
69
 
      com_err(argv[0], ret, "initializing kerberos library");
70
 
      exit(1);
71
 
   }
72
 
 
73
 
   /* in order, use the first of:
74
 
      - a name specified on the command line
75
 
      - the principal name from an existing ccache
76
 
      - the name corresponding to the ruid of the process
77
 
 
78
 
      otherwise, it's an error.
79
 
      */
80
 
 
81
 
   if (pname) {
82
 
      if ((ret = krb5_parse_name(context, pname, &princ))) {
83
 
         com_err(argv[0], ret, "parsing client name");
84
 
         exit(1);
85
 
      }
86
 
   } else if ((ret = krb5_cc_default(context, &ccache)) != KRB5_CC_NOTFOUND) {
87
 
      if (ret) {
88
 
         com_err(argv[0], ret, "opening default ccache");
89
 
         exit(1);
90
 
      }
91
 
 
92
 
      if ((ret = krb5_cc_get_principal(context, ccache, &princ))) {
93
 
         com_err(argv[0], ret, "getting principal from ccache");
94
 
         exit(1);
95
 
      }
96
 
 
97
 
      if ((ret = krb5_cc_close(context, ccache))) {
98
 
         com_err(argv[0], ret, "closing ccache");
99
 
         exit(1);
100
 
      }
101
 
   } else {
102
 
       get_name_from_passwd_file(argv[0], context, &princ);
103
 
   }
104
 
 
105
 
   if ((ret = krb5_get_init_creds_opt_alloc(context, &opts))) {
106
 
         com_err(argv[0], ret, "allocating krb5_get_init_creds_opt");
107
 
         exit(1);
108
 
   }
109
 
   krb5_get_init_creds_opt_set_tkt_life(opts, 5*60);
110
 
   krb5_get_init_creds_opt_set_renew_life(opts, 0);
111
 
   krb5_get_init_creds_opt_set_forwardable(opts, 0);
112
 
   krb5_get_init_creds_opt_set_proxiable(opts, 0);
113
 
 
114
 
   if ((ret = krb5_get_init_creds_password(context, &creds, princ, NULL,
115
 
                                           krb5_prompter_posix, NULL, 
116
 
                                           0, "kadmin/changepw", opts))) {
117
 
      if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
118
 
         com_err(argv[0], 0,
119
 
                 "Password incorrect while getting initial ticket");
120
 
      else
121
 
         com_err(argv[0], ret, "getting initial ticket");
122
 
      krb5_get_init_creds_opt_free(context, opts);
123
 
      exit(1);
124
 
   }
125
 
 
126
 
   pwlen = sizeof(pw);
127
 
   if ((ret = krb5_read_password(context, P1, P2, pw, &pwlen))) {
128
 
      com_err(argv[0], ret, "while reading password");
129
 
      krb5_get_init_creds_opt_free(context, opts);
130
 
      exit(1);
131
 
   }
132
 
 
133
 
   if ((ret = krb5_change_password(context, &creds, pw,
134
 
                                   &result_code, &result_code_string,
135
 
                                   &result_string))) {
136
 
      com_err(argv[0], ret, "changing password");
137
 
      krb5_get_init_creds_opt_free(context, opts);
138
 
      exit(1);
139
 
   }
140
 
 
141
 
   if (result_code) {
142
 
      printf("%.*s%s%.*s\n",
143
 
             (int) result_code_string.length, result_code_string.data,
144
 
             result_string.length?": ":"",
145
 
             (int) result_string.length,
146
 
             result_string.data ? result_string.data : "");
147
 
      krb5_get_init_creds_opt_free(context, opts);
148
 
      exit(2);
149
 
   }
150
 
 
151
 
   if (result_string.data != NULL) 
152
 
       free(result_string.data);
153
 
   if (result_code_string.data != NULL)
154
 
       free(result_code_string.data);
155
 
   krb5_get_init_creds_opt_free(context, opts);
156
 
 
157
 
   printf("Password changed.\n");
158
 
   exit(0);
 
48
    krb5_error_code ret;
 
49
    krb5_context context;
 
50
    krb5_principal princ;
 
51
    char *pname;
 
52
    krb5_ccache ccache;
 
53
    krb5_get_init_creds_opt *opts = NULL;
 
54
    krb5_creds creds;
 
55
 
 
56
    char pw[1024];
 
57
    unsigned int pwlen;
 
58
    int result_code;
 
59
    krb5_data result_code_string, result_string;
 
60
 
 
61
    if (argc > 2) {
 
62
        fprintf(stderr, "usage: %s [principal]\n", argv[0]);
 
63
        exit(1);
 
64
    }
 
65
 
 
66
    pname = argv[1];
 
67
 
 
68
    ret = krb5_init_context(&context);
 
69
    if (ret) {
 
70
        com_err(argv[0], ret, "initializing kerberos library");
 
71
        exit(1);
 
72
    }
 
73
 
 
74
    /* in order, use the first of:
 
75
       - a name specified on the command line
 
76
       - the principal name from an existing ccache
 
77
       - the name corresponding to the ruid of the process
 
78
 
 
79
       otherwise, it's an error.
 
80
    */
 
81
 
 
82
    if (pname) {
 
83
        if ((ret = krb5_parse_name(context, pname, &princ))) {
 
84
            com_err(argv[0], ret, "parsing client name");
 
85
            exit(1);
 
86
        }
 
87
    } else if ((ret = krb5_cc_default(context, &ccache)) != KRB5_CC_NOTFOUND) {
 
88
        if (ret) {
 
89
            com_err(argv[0], ret, "opening default ccache");
 
90
            exit(1);
 
91
        }
 
92
 
 
93
        if ((ret = krb5_cc_get_principal(context, ccache, &princ))) {
 
94
            com_err(argv[0], ret, "getting principal from ccache");
 
95
            exit(1);
 
96
        }
 
97
 
 
98
        if ((ret = krb5_cc_close(context, ccache))) {
 
99
            com_err(argv[0], ret, "closing ccache");
 
100
            exit(1);
 
101
        }
 
102
    } else {
 
103
        get_name_from_passwd_file(argv[0], context, &princ);
 
104
    }
 
105
 
 
106
    if ((ret = krb5_get_init_creds_opt_alloc(context, &opts))) {
 
107
        com_err(argv[0], ret, "allocating krb5_get_init_creds_opt");
 
108
        exit(1);
 
109
    }
 
110
    krb5_get_init_creds_opt_set_tkt_life(opts, 5*60);
 
111
    krb5_get_init_creds_opt_set_renew_life(opts, 0);
 
112
    krb5_get_init_creds_opt_set_forwardable(opts, 0);
 
113
    krb5_get_init_creds_opt_set_proxiable(opts, 0);
 
114
 
 
115
    if ((ret = krb5_get_init_creds_password(context, &creds, princ, NULL,
 
116
                                            krb5_prompter_posix, NULL,
 
117
                                            0, "kadmin/changepw", opts))) {
 
118
        if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
 
119
            com_err(argv[0], 0,
 
120
                    "Password incorrect while getting initial ticket");
 
121
        else
 
122
            com_err(argv[0], ret, "getting initial ticket");
 
123
        krb5_get_init_creds_opt_free(context, opts);
 
124
        exit(1);
 
125
    }
 
126
 
 
127
    pwlen = sizeof(pw);
 
128
    if ((ret = krb5_read_password(context, P1, P2, pw, &pwlen))) {
 
129
        com_err(argv[0], ret, "while reading password");
 
130
        krb5_get_init_creds_opt_free(context, opts);
 
131
        exit(1);
 
132
    }
 
133
 
 
134
    if ((ret = krb5_change_password(context, &creds, pw,
 
135
                                    &result_code, &result_code_string,
 
136
                                    &result_string))) {
 
137
        com_err(argv[0], ret, "changing password");
 
138
        krb5_get_init_creds_opt_free(context, opts);
 
139
        exit(1);
 
140
    }
 
141
 
 
142
    if (result_code) {
 
143
        printf("%.*s%s%.*s\n",
 
144
               (int) result_code_string.length, result_code_string.data,
 
145
               result_string.length?": ":"",
 
146
               (int) result_string.length,
 
147
               result_string.data ? result_string.data : "");
 
148
        krb5_get_init_creds_opt_free(context, opts);
 
149
        exit(2);
 
150
    }
 
151
 
 
152
    if (result_string.data != NULL)
 
153
        free(result_string.data);
 
154
    if (result_code_string.data != NULL)
 
155
        free(result_code_string.data);
 
156
    krb5_get_init_creds_opt_free(context, opts);
 
157
 
 
158
    printf("Password changed.\n");
 
159
    exit(0);
159
160
}