static int virAuthGetCredential(const char *servicename, const char *hostname, const char *credname, const char *path, char **value) { int ret = -1; virAuthConfigPtr config = NULL; const char *tmp; *value = NULL; if (path == NULL) return 0; if (!(config = virAuthConfigNew(path))) goto cleanup; if (virAuthConfigLookup(config, servicename, hostname, credname, &tmp) < 0) goto cleanup; if (VIR_STRDUP(*value, tmp) < 0) goto cleanup; ret = 0; cleanup: virAuthConfigFree(config); return ret; }
virAuthConfigPtr virAuthConfigNewData(const char *path, const char *data, size_t len) { virAuthConfigPtr auth; if (VIR_ALLOC(auth) < 0) { virReportOOMError(); goto error; } if (!(auth->path = strdup(path))) { virReportOOMError(); goto error; } if (!(auth->keyfile = virKeyFileNew())) goto error; if (virKeyFileLoadData(auth->keyfile, path, data, len) < 0) goto error; return auth; error: virAuthConfigFree(auth); return NULL; }
static int mymain(void) { int ret = 0; virAuthConfigPtr config; signal(SIGPIPE, SIG_IGN); #define TEST_LOOKUP(config, hostname, service, credname, expect) \ do { \ const struct ConfigLookupData data = { \ config, hostname, service, credname, expect \ }; \ if (virtTestRun("Test Lookup " hostname "-" service "-" credname, \ 1, testAuthLookup, &data) < 0) \ ret = -1; \ } while (0) const char *confdata = "[credentials-test]\n" "username=fred\n" "password=123456\n" "\n" "[credentials-prod]\n" "username=bar\n" "password=letmein\n" "\n" "[auth-libvirt-test1.example.com]\n" "credentials=test\n" "\n" "[auth-libvirt-test2.example.com]\n" "credentials=test\n" "\n" "[auth-libvirt-demo3.example.com]\n" "credentials=test\n" "\n" "[auth-libvirt-prod1.example.com]\n" "credentials=prod\n"; if (!(config = virAuthConfigNewData("auth.conf", confdata, strlen(confdata)))) return EXIT_FAILURE; TEST_LOOKUP(config, "test1.example.com", "libvirt", "username", "fred"); TEST_LOOKUP(config, "test1.example.com", "vnc", "username", NULL); TEST_LOOKUP(config, "test1.example.com", "libvirt", "realm", NULL); TEST_LOOKUP(config, "test66.example.com", "libvirt", "username", NULL); TEST_LOOKUP(config, "prod1.example.com", "libvirt", "username", "bar"); TEST_LOOKUP(config, "prod1.example.com", "libvirt", "password", "letmein"); virAuthConfigFree(config); return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE; }
static int virAuthGetCredential(virConnectPtr conn, const char *servicename, const char *credname, char **value) { int ret = -1; char *path = NULL; virAuthConfigPtr config = NULL; const char *tmp; *value = NULL; if (virAuthGetConfigFilePath(conn, &path) < 0) goto cleanup; if (path == NULL) { ret = 0; goto cleanup; } if (!(config = virAuthConfigNew(path))) goto cleanup; if (virAuthConfigLookup(config, servicename, conn->uri->server, credname, &tmp) < 0) goto cleanup; if (tmp && !(*value = strdup(tmp))) { virReportOOMError(); goto cleanup; } ret = 0; cleanup: virAuthConfigFree(config); VIR_FREE(path); return ret; }