aboutsummaryrefslogtreecommitdiff
path: root/src/kadmin/import/ovsec_adm_import.c
blob: 4ca920434e9ecbd1f5b25e6fd726ed0fb1d02cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
 *
 * $Header$
 */

#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif

#include    <stdio.h>
#include    <string.h>
#include    <unistd.h>

#include    <kadm5/adb.h>
#include    "import_err.h"
#include    "import.h"

#define	TMP_POLICY_FMT   "/krb5/#ovsec_import_policy.%d"

int
main(int argc, char *argv[])
{
    char		*filename,
			*whoami;
    int			ret, merge_princs;
    FILE		*fp;
    osa_adb_policy_t	policy_db;
    char		pol_dbfile[BUFSIZ];
    kadm5_config_params	params;
    krb5_context context;
    
    filename = NULL;
    initialize_imp_error_table();
    initialize_adb_error_table();
    krb5_init_context(&context);
    krb5_init_ets(context);
    
    whoami = argv[0];
    merge_princs = 0;
    while(--argc) {
	if(*++argv == NULL)
	    break;
	if (!strcmp(*argv, "-merge_princs")) {
	     merge_princs++;
	     continue;
	}
	if (*argv[0] == '-') {
	    com_err(whoami, IMPORT_UNK_OPTION, NULL);
	    exit(2);
	}
	if(filename == NULL) 
	    filename = *argv;
	else {
	    com_err(whoami, IMPORT_UNK_OPTION, NULL);
	    exit(2);
	}
    }
    if(filename != NULL) {
	if ((fp = fopen(filename, "r")) == NULL) {
	    com_err(whoami, errno, "%s (%s)", error_message(IMPORT_OPEN_DUMP),
		    filename);
	    exit(2);
	}
    } else fp = stdin;

    sprintf(pol_dbfile, TMP_POLICY_FMT, getpid());
    if(access(pol_dbfile, F_OK) == 0) {
	if(unlink(pol_dbfile) != 0)
	    return errno;
    }

    params.mask = 0;
    if (ret = kadm5_get_config_params(context, NULL, NULL, &params,
				      &params)) {
	 com_err(whoami, ret, error_message(IMPORT_GET_PARAMS));
	 exit(2);
    }
#define REQUIRED_MASK (KADM5_CONFIG_DBNAME | \
		       KADM5_CONFIG_ADBNAME)
    if ((params.mask & REQUIRED_MASK) != REQUIRED_MASK) {
	 com_err(whoami, KADM5_BAD_SERVER_PARAMS,
		 error_message(IMPORT_GET_PARAMS));
	 exit(2);
    }
    /*
     * This trick lets me use the temporary policy db name but the
     * standard policy db lockfile, thus ensuring that no one changes
     * the policy while this program is working.
     */
    params.admin_dbname = pol_dbfile;
    
    if((ret = osa_adb_open_policy(&policy_db, &params)) != OSA_ADB_OK) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_OPEN));
	 exit(2);
    }
    if ((ret = osa_adb_get_lock(policy_db, OSA_ADB_PERMANENT) != OSA_ADB_OK)) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_LOCK));
	 exit(2);
    }
    if (merge_princs) {
	 if ((ret = krb5_db_set_name(context, params.dbname)) ||
	     (ret = krb5_db_init(context))) {
	      com_err(whoami, ret, error_message(IMPORT_RENAME_OPEN));
	      exit(2);
	 }
    }
    
    if((ret = import_file(context, fp, merge_princs, policy_db)) !=
       OSA_ADB_OK) { 
	unlink(pol_dbfile);
	com_err(whoami, ret, error_message(IMPORT_IMPORT));
	exit(2);
    }

    if (merge_princs && (ret = krb5_db_fini(context))) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_CLOSE));
	 exit(2);
    }

    kadm5_free_config_params(context, &params);
    params.mask = 0;
    if (ret = kadm5_get_config_params(context, NULL, NULL, &params,
				      &params)) {
	 com_err(whoami, ret, error_message(IMPORT_GET_PARAMS));
	 exit(2);
    }
    
    if (access(params.admin_dbname, F_OK) == 0) {
	puts(error_message(IMPORT_WARN_DB));
	if(!confirm()) {
	    com_err(whoami, IMPORT_FAILED, NULL);
	    exit(2);
	}
    }
    
    if((ret = osa_adb_open_policy(&policy_db, &params)) != OSA_ADB_OK) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_OPEN));
	 exit(2);
    }
    if ((ret = osa_adb_get_lock(policy_db, OSA_ADB_PERMANENT) != OSA_ADB_OK)) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_LOCK));
	 exit(2);
    }
    if (rename(pol_dbfile, params.admin_dbname) != 0) {
	 com_err(whoami, IMPORT_RENAME_FAILED, NULL);

	 /* WARNING!  Permanent lock is not replaced.  This will */
	 /* require manual administrative action! */
	 exit(2);
    }
    if ((ret = osa_adb_release_lock(policy_db)) != OSA_ADB_OK) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_UNLOCK));

	 /* WARNING!  Permanent lock is not replaced.  This will */
	 /* require manual administrative action! */
	 exit(2);
    }
    if ((ret = osa_adb_close_policy(policy_db)) != OSA_ADB_OK) {
	 com_err(whoami, ret, error_message(IMPORT_RENAME_CLOSE));
	 exit(2);
    }
    exit(0);
}