1 /* cvm/v2client_wrappers.c - CVM version 2 client library wrapper functions
2  * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <string.h>
19 #include "v2client.h"
20 #include "credentials.h"
21 
add(struct cvm_credential * creds,unsigned i,unsigned type,const char * value)22 static unsigned add(struct cvm_credential* creds,
23 		    unsigned i, unsigned type, const char* value)
24 {
25   if (value == 0)
26     return i;
27   if (value[0] == 0)
28     return i;
29   creds[i].type = type;
30   if (!str_copys(&creds[i].value, value))
31     return 0;
32   return i + 1;
33 }
34 
doit(struct cvm_credential creds[],const char * module,const char * account,const char * domain,const char * password,int split_account)35 static int doit(struct cvm_credential creds[],
36 		const char* module,
37 		const char* account,
38 		const char* domain,
39 		const char* password,
40 		int split_account)
41 {
42   unsigned i;
43   creds[0].type = CVM_CRED_ACCOUNT;
44   if (!str_copys(&creds[0].value, account))
45     return CVME_IO;
46   if ((i = add(creds, 1, CVM_CRED_DOMAIN, domain)) == 0)
47     return CVME_IO;
48   if (split_account) {
49     cvm_client_split_account(&creds[0].value, &creds[1].value);
50     if (i == 1)
51       if ((i = add(creds, i, CVM_CRED_DOMAIN, creds[i].value.s)) == 0)
52 	return CVME_IO;
53   }
54   if ((i = add(creds, i, CVM_CRED_PASSWORD, password)) == 0)
55     return CVME_IO;
56   return cvm_client_authenticate(module, i, creds);
57 }
58 
cvm_client_authenticate_password(const char * module,const char * account,const char * domain,const char * password,int split_account)59 int cvm_client_authenticate_password(const char* module,
60 			      const char* account,
61 			      const char* domain,
62 			      const char* password,
63 			      int split_account)
64 {
65   struct cvm_credential creds[3];
66   unsigned i;
67   int result;
68   memset(creds, 0, sizeof creds);
69   result = doit(creds, module, account, domain, password, split_account);
70   for (i = 0; i < 3; ++i)
71     str_free(&creds[i].value);
72   return result;
73 }
74