2  * Copyright (c) 2003-2005,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@ 
  23  * db_commands.cpp -- commands to directly manipulate Db's using the DL API. 
  26 #include "db_commands.h" 
  28 #include "readline_cssm.h" 
  29 #include "security_tool.h" 
  34 #include <security_cdsa_client/dlclient.h> 
  36 using namespace CssmClient
; 
  39 do_db_create(const CSSM_GUID guid
, const char *dbname
, Boolean do_openparams
, Boolean do_autocommit
, Boolean do_mode
, mode_t mode
, Boolean do_version_0_params
) 
  45                 CSSM_APPLEDL_OPEN_PARAMETERS openParameters 
= { sizeof(CSSM_APPLEDL_OPEN_PARAMETERS
), 
  46                         (do_version_0_params 
? 0 : CSSM_APPLEDL_OPEN_PARAMETERS_VERSION
) }; 
  48                 Module 
module(guid
, cssm
); 
  54                         openParameters
.autoCommit 
= do_autocommit
; 
  55                         if (!do_version_0_params 
&& do_mode
) 
  57                                 openParameters
.mask 
|= kCSSM_APPLEDL_MASK_MODE
; 
  58                                 openParameters
.mode 
= mode
; 
  61                         db
->openParameters(&openParameters
); 
  66         catch (const CommonError 
&e
) 
  68                 OSStatus status 
= e
.osStatus(); 
  69                 sec_error("CSSM_DbCreate %s: %s", dbname
, sec_errstr(status
)); 
  80 parse_guid(const char *name
, CSSM_GUID 
*guid
) 
  82         size_t len 
= strlen(name
); 
  84         if (!strncmp("dl", name
, len
)) 
  85                 *guid 
= gGuidAppleFileDL
; 
  86         else if (!strncmp("cspdl", name
, len
)) 
  87                 *guid 
= gGuidAppleCSPDL
; 
  90                 sec_error("Invalid guid: %s", name
); 
  91                 return SHOW_USAGE_MESSAGE
; 
  99 parse_mode(const char *name
, mode_t 
*pmode
) 
 105         if (!name 
|| !pmode 
|| *name 
!= '0') 
 111         for (p 
= name 
+ 1; *p
; ++p
) 
 113                 if (*p 
< '0' || *p 
> '7') 
 119                 mode 
= (mode 
<< 3) + *p 
- '0'; 
 126         sec_error("Invalid mode: %s", name
); 
 131 db_create(int argc
, char * const *argv
) 
 136         bool do_autocommit 
= true, do_mode 
= false; 
 137         bool do_openparams 
= false, do_version_0_params 
= false; 
 139         CSSM_GUID guid 
= gGuidAppleFileDL
; 
 141         while ((ch 
= getopt(argc
, argv
, "0ahg:m:o")) != -1) 
 146                         do_version_0_params 
= true; 
 147                         do_openparams 
= true; 
 150                         do_autocommit 
= false; 
 151                         do_openparams 
= true; 
 154                         result 
= parse_guid(optarg
, &guid
); 
 159                         result 
= parse_mode(optarg
, &mode
); 
 163                         do_openparams 
= true; 
 166                         do_openparams 
= true; 
 170                         return SHOW_USAGE_MESSAGE
; 
 181                 fprintf(stderr
, "db to create: "); 
 182                 dbname 
= readline(NULL
, 0); 
 196                 result 
= do_db_create(guid
, dbname
, do_openparams
, do_autocommit
, do_mode
, mode
, do_version_0_params
);