2 * Copyright 2012, Haiku Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Michael Lotz, mmlr@mlotz.ch
10 #include <Application.h>
17 add_password(const char* keyring
, const char* identifier
,
18 const char* secondaryIdentifier
, const char* passwordString
)
21 BPasswordKey
password(passwordString
, B_KEY_PURPOSE_GENERIC
, identifier
,
24 status_t result
= keyStore
.AddKey(keyring
, password
);
26 printf("failed to add password: %s\n", strerror(result
));
35 remove_password(const char* keyring
, const char* identifier
,
36 const char* secondaryIdentifier
)
39 BPasswordKey password
;
41 status_t result
= keyStore
.GetKey(keyring
, B_KEY_TYPE_PASSWORD
, identifier
,
42 secondaryIdentifier
, false, password
);
44 printf("failed to get password \"%s\": %s\n", identifier
,
49 result
= keyStore
.RemoveKey(keyring
, password
);
51 printf("failed to remove password: %s\n", strerror(result
));
60 add_keyring(const char* keyring
)
64 status_t result
= keyStore
.AddKeyring(keyring
);
66 printf("failed to add keyring: %s\n", strerror(result
));
75 remove_keyring(const char* keyring
)
79 status_t result
= keyStore
.RemoveKeyring(keyring
);
81 printf("failed to remove keyring: %s\n", strerror(result
));
90 list_passwords(const char* keyring
)
96 BPasswordKey password
;
97 status_t result
= keyStore
.GetNextKey(keyring
, B_KEY_TYPE_PASSWORD
,
98 B_KEY_PURPOSE_ANY
, cookie
, password
);
99 if (result
== B_ENTRY_NOT_FOUND
)
102 if (result
!= B_OK
) {
103 printf("failed to get next key with: %s\n", strerror(result
));
107 password
.PrintToStream();
122 status_t result
= keyStore
.GetNextKeyring(cookie
, keyring
);
123 if (result
== B_ENTRY_NOT_FOUND
)
126 if (result
!= B_OK
) {
127 printf("failed to get next key with: %s\n", strerror(result
));
131 printf("keyring: \"%s\"\n", keyring
.String());
139 show_status(const char* keyring
)
142 printf("keyring \"%s\" is %slocked\n", keyring
,
143 keyStore
.IsKeyringUnlocked(keyring
) ? "un" : "");
149 lock_keyring(const char* keyring
)
152 status_t result
= keyStore
.LockKeyring(keyring
);
153 if (result
!= B_OK
) {
154 printf("failed to lock keyring \"%s\": %s\n", keyring
,
164 add_keyring_to_master(const char* keyring
)
167 status_t result
= keyStore
.AddKeyringToMaster(keyring
);
168 if (result
!= B_OK
) {
169 printf("failed to add keyring \"%s\" to master: %s\n", keyring
,
179 remove_keyring_from_master(const char* keyring
)
182 status_t result
= keyStore
.RemoveKeyringFromMaster(keyring
);
183 if (result
!= B_OK
) {
184 printf("failed to remove keyring \"%s\" from master: %s\n", keyring
,
194 list_applications(const char* keyring
)
201 status_t result
= keyStore
.GetNextApplication(keyring
,
203 if (result
== B_ENTRY_NOT_FOUND
)
206 if (result
!= B_OK
) {
207 printf("failed to get next application: %s\n", strerror(result
));
211 printf("application: \"%s\"\n", signature
.String());
219 remove_application(const char* keyring
, const char* signature
)
223 status_t result
= keyStore
.RemoveApplication(keyring
, signature
);
224 if (result
!= B_OK
) {
225 printf("failed to remove application: %s\n", strerror(result
));
234 set_unlock_key(const char* keyring
, const char* passwordString
)
237 BPasswordKey
password(passwordString
, B_KEY_PURPOSE_KEYRING
, NULL
);
239 status_t result
= keyStore
.SetUnlockKey(keyring
, password
);
240 if (result
!= B_OK
) {
241 printf("failed to set unlock key: %s\n", strerror(result
));
250 remove_unlock_key(const char* keyring
)
254 status_t result
= keyStore
.RemoveUnlockKey(keyring
);
255 if (result
!= B_OK
) {
256 printf("failed to remove unlock key: %s\n", strerror(result
));
265 print_usage(const char* name
)
268 printf("\t%s list passwords [<fromKeyring>]\n", name
);
269 printf("\t\tLists all passwords of the specified keyring or from the"
270 " master keyring if none is supplied.\n");
271 printf("\t%s list keyrings\n", name
);
272 printf("\t\tLists all keyrings.\n");
273 printf("\t%s list applications [<fromKeyring>]\n", name
);
274 printf("\t\tLists the applications that have been granted permanent access"
275 " to a keyring once it is unlocked.\n\n");
277 printf("\t%s add password <identifier> [<secondaryIdentifier>] <password>"
279 printf("\t\tAdds the specified password to the master keyring.\n");
280 printf("\t%s add password to <keyring> <identifier> [<secondaryIdentifier>]"
281 " <password>\n", name
);
282 printf("\t\tAdds the specified password to the specified keyring.\n\n");
284 printf("\t%s remove password <identifier> [<secondaryIdentifier>]\n", name
);
285 printf("\t\tRemoves the specified password from the master keyring.\n");
286 printf("\t%s remove password from <keyring> <identifier>"
287 " [<secondaryIdentifier>]\n", name
);
288 printf("\t\tRemoves the specified password from the specified keyring.\n\n");
290 printf("\t%s add keyring <name>\n", name
);
291 printf("\t\tAdds a new keyring with the specified name.\n");
292 printf("\t%s remove keyring <name>\n", name
);
293 printf("\t\tRemoves the specified keyring.\n\n");
295 printf("\t%s status [<keyring>]\n", name
);
296 printf("\t\tShows the lock state of the specified keyring, or the"
297 " master keyring if none is supplied.\n\n");
299 printf("\t%s lock [<keyring>]\n", name
);
300 printf("\t\tLock the specified keyring, or the master keyring if none is"
303 printf("\t%s master add <keyring>\n", name
);
304 printf("\t\tAdd the access key for the specified keyring to the master"
307 printf("\t%s master remove <keyring>\n", name
);
308 printf("\t\tRemove the access key for the specified keyring from the"
309 " master keyring.\n\n");
311 printf("\t%s remove application <signature>\n", name
);
312 printf("\t\tRemove permanent access for the application with the given"
313 " signature from the master keyring.\n");
314 printf("\t%s remove application from <keyring> <signature>\n", name
);
315 printf("\t\tRemove permanent access for the application with the given"
316 " signature from the specified keyring.\n\n");
318 printf("\t%s key set <keyring> <password>\n", name
);
319 printf("\t\tSet the unlock key of the specified keyring to the given"
321 printf("\t%s key remove <keyring>\n", name
);
322 printf("\t\tRemove the unlock key of the specified keyring.\n");
328 main(int argc
, char* argv
[])
330 BApplication
app("application/x-vnd.Haiku-keystore-cli");
333 return print_usage(argv
[0]);
335 if (strcmp(argv
[1], "list") == 0) {
337 return print_usage(argv
[0]);
339 if (strcmp(argv
[2], "passwords") == 0)
340 return list_passwords(argc
> 3 ? argv
[3] : NULL
);
341 if (strcmp(argv
[2], "keyrings") == 0)
342 return list_keyrings();
343 if (strcmp(argv
[2], "applications") == 0)
344 return list_applications(argc
> 3 ? argv
[3] : NULL
);
345 } else if (strcmp(argv
[1], "add") == 0) {
347 return print_usage(argv
[0]);
349 if (strcmp(argv
[2], "password") == 0) {
351 return print_usage(argv
[0]);
353 const char* keyring
= NULL
;
354 const char* identifier
= NULL
;
355 const char* secondaryIdentifier
= NULL
;
356 const char* password
= NULL
;
357 if (argc
>= 7 && argc
<= 8 && strcmp(argv
[3], "to") == 0) {
359 identifier
= argv
[5];
363 secondaryIdentifier
= argv
[6];
366 } else if (argc
<= 6) {
367 identifier
= argv
[3];
371 secondaryIdentifier
= argv
[4];
376 if (password
!= NULL
) {
377 return add_password(keyring
, identifier
, secondaryIdentifier
,
380 } else if (strcmp(argv
[2], "keyring") == 0) {
382 return print_usage(argv
[0]);
384 return add_keyring(argv
[3]);
386 } else if (strcmp(argv
[1], "remove") == 0) {
388 return print_usage(argv
[0]);
390 if (strcmp(argv
[2], "password") == 0) {
392 return print_usage(argv
[0]);
394 const char* keyring
= NULL
;
395 const char* identifier
= NULL
;
396 const char* secondaryIdentifier
= NULL
;
397 if (argc
>= 6 && argc
<= 7 && strcmp(argv
[3], "from") == 0) {
399 identifier
= argv
[5];
401 secondaryIdentifier
= argv
[6];
402 } else if (argc
<= 5) {
403 identifier
= argv
[3];
405 secondaryIdentifier
= argv
[4];
408 if (identifier
!= NULL
) {
409 return remove_password(keyring
, identifier
,
410 secondaryIdentifier
);
412 } else if (strcmp(argv
[2], "keyring") == 0) {
414 return remove_keyring(argv
[3]);
415 } else if (strcmp(argv
[2], "application") == 0) {
416 const char* keyring
= NULL
;
417 const char* signature
= NULL
;
418 if (argc
== 6 && strcmp(argv
[3], "from") == 0) {
421 } else if (argc
== 4)
424 if (signature
!= NULL
)
425 return remove_application(keyring
, signature
);
427 } else if (strcmp(argv
[1], "status") == 0) {
428 if (argc
!= 2 && argc
!= 3)
429 return print_usage(argv
[0]);
431 return show_status(argc
== 3 ? argv
[2] : "");
432 } else if (strcmp(argv
[1], "lock") == 0) {
433 if (argc
!= 2 && argc
!= 3)
434 return print_usage(argv
[0]);
436 return lock_keyring(argc
== 3 ? argv
[2] : "");
437 } else if (strcmp(argv
[1], "master") == 0) {
439 return print_usage(argv
[0]);
441 if (strcmp(argv
[2], "add") == 0)
442 return add_keyring_to_master(argv
[3]);
443 if (strcmp(argv
[2], "remove") == 0)
444 return remove_keyring_from_master(argv
[3]);
445 } else if (strcmp(argv
[1], "key") == 0) {
447 return print_usage(argv
[0]);
449 if (strcmp(argv
[2], "set") == 0) {
451 return set_unlock_key(argv
[3], argv
[4]);
452 } else if (strcmp(argv
[2], "remove") == 0) {
454 return remove_unlock_key(argv
[3]);
458 return print_usage(argv
[0]);