#include #include #include #include #include #include #include PGDLLEXPORT Datum read_password(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(read_password); #define BUFFER_SIZE 4096 extern char *EmailProfileFilePath; Datum read_password(PG_FUNCTION_ARGS) { FILE *fp = NULL; text *data; StringInfoData name_profile; char buffer[BUFFER_SIZE]; if (PG_ARGISNULL(0)) PG_RETURN_NULL(); data = PG_GETARG_TEXT_PP(0); initStringInfo(&name_profile); appendBinaryStringInfo(&name_profile, VARDATA_ANY(data), VARSIZE_ANY_EXHDR(data)); PG_FREE_IF_COPY(data, 0); fp = fopen(EmailProfileFilePath, "r"); if (fp == NULL) elog(ERROR, "Error open password file: %s", EmailProfileFilePath); while (fgets(buffer, BUFFER_SIZE, fp)) { char *str = buffer; char profile[BUFFER_SIZE]; buffer[BUFFER_SIZE - 1] = '\0'; if (strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = '\0'; str = strtok(buffer, " \t"); if (str == NULL) continue; strcpy(profile, str); str = strtok(NULL, " \t"); if (str != NULL && strcmp(name_profile.data, profile) == 0) { char login[BUFFER_SIZE]; char password[BUFFER_SIZE]; StringInfoData result; strncpy(login, str, BUFFER_SIZE); login[BUFFER_SIZE - 1] = '\0'; initStringInfo(&result); str = strtok(NULL, " \t"); if (str == NULL) { appendStringInfo(&result, "%s", login); } else { strncpy(password, str, BUFFER_SIZE); password[BUFFER_SIZE - 1] = '\0'; appendStringInfo(&result, "%s %s", login, password); } fclose(fp); PG_RETURN_TEXT_P(cstring_to_text(result.data)); } } fclose(fp); PG_RETURN_NULL(); }