2 * Copyright (c) 2003-2004,2008,2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include "keychain_create.h"
28 #include "readline_cssm.h"
29 #include "security_tool.h"
36 #include <Security/SecKeychain.h>
39 do_create(const char *keychain
, const char *password
, Boolean do_prompt
)
41 SecKeychainRef keychainRef
= NULL
;
44 result
= SecKeychainCreate(keychain
, password
? (UInt32
) strlen(password
) : 0, password
, do_prompt
, NULL
, &keychainRef
);
46 CFRelease(keychainRef
);
49 sec_error("SecKeychainCreate %s: %s", keychain
, sec_errstr(result
));
55 keychain_create(int argc
, char * const *argv
)
57 int free_keychain
= 0, zero_password
= 0;
58 char *password
= NULL
, *keychain
= NULL
;
60 Boolean do_prompt
= FALSE
;
62 /* AG: getopts optstring name [args]
63 AG: while loop calling getopt is used to extract password from cl from user
64 password is the only option to keychain_create
65 optstring is a string containing the legitimate option
66 characters. If such a character is followed by a colon,
67 the option requires an argument, so getopt places a
68 pointer to the following text in the same argv-element, or
69 the text of the following argv-element, in optarg.
71 while ((ch
= getopt(argc
, argv
, "hp:P")) != -1)
83 return SHOW_USAGE_MESSAGE
;
87 AG: The external variable optind is the index of the next
88 array element of argv[] to be processed; it communicates
89 from one call of getopt() to the next which element to
91 The variable optind is the index of the next element of the argv[] vector to be processed. It shall be initialized to 1 by the system, and getopt() shall update it when it finishes with each element of argv[]. When an element of argv[] contains multiple option characters, it is unspecified how getopt() determines which options have already been processed.
101 fprintf(stderr
, "keychain to create: ");
102 keychain
= readline(NULL
, 0);
110 if (*keychain
== '\0')
114 if (!password
&& !do_prompt
)
119 for (tries
= 3; tries
-- > 0;)
123 password
= getpass("password for new keychain: ");
130 firstpass
= malloc(strlen(password
) + 1);
131 strcpy(firstpass
, password
);
132 password
= getpass("retype password for new keychain: ");
133 compare
= password
? strcmp(password
, firstpass
) : 1;
134 memset(firstpass
, 0, strlen(firstpass
));
144 fprintf(stderr
, "passwords don't match\n");
145 memset(password
, 0, strlen(password
));
163 result
= do_create(keychain
, password
, do_prompt
);
165 memset(password
, 0, strlen(password
));