Projects/psql-admtools
Projects
/
psql-admtools
Archived
13
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
psql-admtools/psqluseradm/psqladm.c

118 lines
3.2 KiB
C

/*-
* Copyright (c) 2005 Eirik A. Nygaard. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <string.h>
#include <err.h>
#include <unistd.h>
#include <ctype.h>
#include <libpq-fe.h>
#include "psql.config.h"
#include "psqladm.h"
PGconn *
db_connect(const char *dbuser, const char *password, const char *dbname, const char *host)
{
PGconn *conn;
char conninfo[1024];
snprintf(conninfo, sizeof(conninfo),
"dbname = '%s' host = '%s' user = '%s' password = '%s'",
dbname, host, dbuser, password);
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
errx(1, "Could not connect to database(%d): %s", PQstatus(conn), PQerrorMessage(conn));
return(conn);
}
int
verify_startwith(char *string, char *start)
{
if (!strncmp(string, start, strlen(start))) {
if (strlen(string) == strlen(start))
return(1);
if (string[strlen(start)] == '_')
return(1);
}
return(0);
}
uid_t
get_cred(void)
{
uid_t uid;
if ((int)(uid = getuid()) == -1)
err(1, "getuid");
return(uid);
}
void
getpassword(const char *prompt1, const char *prompt2, char *pass, size_t len)
{
char pass1[1024];
char pass2[1024];
char *passwd;
passwd = getpass(prompt1);
strlcpy(pass1, passwd, sizeof(pass1));
passwd = getpass(prompt2);
strlcpy(pass2, passwd, sizeof(pass2));
if (strcmp(pass1, pass2) != 0) {
errx(1, "Passwords doesn't match");
}
strlcpy(pass, pass1, len);
}
/*
* names does not contain spaces
* passwords does not contain spaces
* XXX: Add better checks later.
*/
int
verifystr(char *str, int type)
{
unsigned int i;
for (i = 0; i < strlen(str); i++) {
if (type == STR_TYPE_NAME) {
if (isspace((int)str[i]))
return(0);
}
if (type == STR_TYPE_PASSWORD) {
if (isspace((int)str[i]))
return(0);
}
}
return(1); /* Bad Bad, default case should be non-verified */
}