* PQencryptPasswordConn -- exported routine to encrypt a password * * This is intended to be used by client applications that wish to send * commands like ALTER USER joe PASSWORD 'pwd'. The password need not * be sent in cleartext if it is encrypted on the client side. This is * good because it ensures the cleartext password won't end up in logs, * pg_stat displays, etc. We export the fun
| 1196 | * returns NULL. |
| 1197 | */ |
| 1198 | char * |
| 1199 | PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, |
| 1200 | const char *algorithm) |
| 1201 | { |
| 1202 | #define MAX_ALGORITHM_NAME_LEN 50 |
| 1203 | char algobuf[MAX_ALGORITHM_NAME_LEN + 1]; |
| 1204 | char *crypt_pwd = NULL; |
| 1205 | |
| 1206 | if (!conn) |
| 1207 | return NULL; |
| 1208 | |
| 1209 | resetPQExpBuffer(&conn->errorMessage); |
| 1210 | |
| 1211 | /* If no algorithm was given, ask the server. */ |
| 1212 | if (algorithm == NULL) |
| 1213 | { |
| 1214 | PGresult *res; |
| 1215 | char *val; |
| 1216 | |
| 1217 | res = PQexec(conn, "show password_encryption"); |
| 1218 | if (res == NULL) |
| 1219 | { |
| 1220 | /* PQexec() should've set conn->errorMessage already */ |
| 1221 | return NULL; |
| 1222 | } |
| 1223 | if (PQresultStatus(res) != PGRES_TUPLES_OK) |
| 1224 | { |
| 1225 | /* PQexec() should've set conn->errorMessage already */ |
| 1226 | PQclear(res); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | if (PQntuples(res) != 1 || PQnfields(res) != 1) |
| 1230 | { |
| 1231 | PQclear(res); |
| 1232 | appendPQExpBufferStr(&conn->errorMessage, |
| 1233 | libpq_gettext("unexpected shape of result set returned for SHOW\n")); |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | val = PQgetvalue(res, 0, 0); |
| 1237 | |
| 1238 | if (strlen(val) > MAX_ALGORITHM_NAME_LEN) |
| 1239 | { |
| 1240 | PQclear(res); |
| 1241 | appendPQExpBufferStr(&conn->errorMessage, |
| 1242 | libpq_gettext("password_encryption value too long\n")); |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | strcpy(algobuf, val); |
| 1246 | PQclear(res); |
| 1247 | |
| 1248 | algorithm = algobuf; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * Also accept "on" and "off" as aliases for "md5", because |
| 1253 | * password_encryption was a boolean before PostgreSQL 10. We refuse to |
| 1254 | * send the password in plaintext even if it was "off". |
| 1255 | */ |
no test coverage detected