2001-11-25 01:41:16 +01:00
|
|
|
|
/*
|
2007-02-27 15:10:08 +01:00
|
|
|
|
* @(#) $Header: /tmp/cvs/mysql-admutils/common.c,v 1.9 2007-02-27 14:10:07 geirha Exp $
|
2001-11-25 01:41:16 +01:00
|
|
|
|
*
|
|
|
|
|
* functions used by mysql-dbadm.c and mysql-useradm.c
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
2007-02-27 15:10:08 +01:00
|
|
|
|
#include <stdlib.h>
|
2001-11-25 01:41:16 +01:00
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <mysql.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <pwd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <string.h>
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#include <grp.h>
|
2001-11-25 01:41:16 +01:00
|
|
|
|
#include "mysql-admutils.h"
|
|
|
|
|
|
|
|
|
|
char *program_name;
|
|
|
|
|
|
2007-02-27 15:10:08 +01:00
|
|
|
|
static char *rcsheader = "@(#) " PACKAGE " " VERSION " orakel@ntnu.no $Header: /tmp/cvs/mysql-admutils/common.c,v 1.9 2007-02-27 14:10:07 geirha Exp $";
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
version()
|
|
|
|
|
{
|
|
|
|
|
printf("%s %s\n", program_name, rcsheader);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
wrong_use(char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
if (format) {
|
|
|
|
|
fprintf(stderr, "%s: ", program_name);
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
|
|
fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* always returns 1. */
|
|
|
|
|
int
|
|
|
|
|
dberror(MYSQL *pmysql, char *format, ...)
|
|
|
|
|
{
|
2007-02-27 15:10:08 +01:00
|
|
|
|
const char *errmsg;
|
2001-11-25 01:41:16 +01:00
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: ", program_name);
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
if (pmysql) {
|
|
|
|
|
errmsg = mysql_error(pmysql);
|
|
|
|
|
if ((errmsg) && (strcmp(errmsg, "") != 0))
|
|
|
|
|
fprintf(stderr, "mysql: %s\n", errmsg);
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
mysql_close(pmysql);
|
|
|
|
|
}
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* always returns 1. */
|
|
|
|
|
int
|
|
|
|
|
fatal_error(char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: ", program_name);
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* decides if the UNIX user is entitled to the MySQL database or MySQL user. */
|
|
|
|
|
int
|
|
|
|
|
owner(char *name)
|
|
|
|
|
{
|
|
|
|
|
struct passwd *p;
|
|
|
|
|
|
|
|
|
|
p = getpwuid(getuid());
|
2006-08-15 13:51:59 +02:00
|
|
|
|
if (!p) {
|
|
|
|
|
dberror(NULL, "Failed to look up your UNIX username.");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
|
|
if (strcmp(name, p->pw_name) == 0)
|
|
|
|
|
return 1; /* OK */
|
|
|
|
|
|
|
|
|
|
if ((strncmp(name, p->pw_name, strlen(p->pw_name)) == 0) &&
|
|
|
|
|
(*(name + strlen(p->pw_name)) == '_'))
|
|
|
|
|
return 1; /* OK */
|
|
|
|
|
|
|
|
|
|
return 0; /* not owner if we get as far as this */
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-27 09:49:48 +01:00
|
|
|
|
/**
|
|
|
|
|
* Decides if the user is member of a group. The Unix group can't contain any
|
|
|
|
|
* '_'. i.e 'fidi_s' won't be accepted.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
member(char *gr) {
|
|
|
|
|
char *username;
|
|
|
|
|
|
2012-12-03 13:20:20 +01:00
|
|
|
|
char group[65];
|
2002-02-27 09:49:48 +01:00
|
|
|
|
struct group *g;
|
|
|
|
|
|
|
|
|
|
struct passwd *p;
|
|
|
|
|
char *foo;
|
|
|
|
|
|
|
|
|
|
/* size_t i = 0; */
|
|
|
|
|
|
|
|
|
|
/* Get username */
|
|
|
|
|
p = getpwuid(getuid());
|
|
|
|
|
if (!p) {
|
|
|
|
|
fprintf(stderr, "Failed to look up your UNIX username.");
|
2004-11-16 21:58:54 +01:00
|
|
|
|
return 0;
|
2002-02-27 09:49:48 +01:00
|
|
|
|
}
|
|
|
|
|
username = p->pw_name;
|
|
|
|
|
|
|
|
|
|
/* Copy string, but cut at '_' */
|
2012-12-03 13:20:20 +01:00
|
|
|
|
strncpy(group, gr, 64);
|
|
|
|
|
group[64] = '\0';
|
2002-02-27 09:49:48 +01:00
|
|
|
|
|
2004-11-16 21:58:54 +01:00
|
|
|
|
// ettersom man kan f<> inn gruppenavn med underscore i, m<> man rett og
|
|
|
|
|
// slett pr<70>ve seg fram for <20> sjekke om det er en gruppe personen er med
|
|
|
|
|
// i.
|
|
|
|
|
// eksempel: www_esb_test_users, hvor personen ikke er med i www_est_test
|
|
|
|
|
// (som kanskje finnes), men er med i www_esb.
|
|
|
|
|
// group er databasenavnet som brukeren ville ha. Vi m<> finne gruppenavnet
|
|
|
|
|
// ut av den, og stripper av en og en underscore til vi finner noe som
|
|
|
|
|
// passer.
|
|
|
|
|
while ( 1 ) {
|
|
|
|
|
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#if DEBUG
|
2004-11-16 21:58:54 +01:00
|
|
|
|
// printf("gr = %s, group = %s, foo = %s\n", gr, group, foo);
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#endif
|
2004-11-16 21:58:54 +01:00
|
|
|
|
g = getgrnam(group);
|
|
|
|
|
#if DEBUG
|
|
|
|
|
printf("tror gruppenavnet er: %s\n", group);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// hvis gruppen ikke finnes, m<> vi i allefall pr<70>ve <20> se om
|
|
|
|
|
// det er noe lengre inn.
|
|
|
|
|
if (g) {
|
|
|
|
|
/* Check if user is member of group */
|
|
|
|
|
while(*g->gr_mem != NULL) {
|
2012-12-03 13:20:20 +01:00
|
|
|
|
char * member = *g->gr_mem++;
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#if DEBUG
|
2012-12-03 13:20:20 +01:00
|
|
|
|
printf("Medlem: %s\n", member);
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2004-11-16 21:58:54 +01:00
|
|
|
|
if (strcmp(member,username) == 0) {
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#if DEBUG
|
2004-11-16 21:58:54 +01:00
|
|
|
|
printf("You have access to '%s'\n", gr);
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#endif
|
2004-11-16 21:58:54 +01:00
|
|
|
|
return 1; /* OK */
|
2006-08-15 13:51:59 +02:00
|
|
|
|
}
|
2004-11-16 21:58:54 +01:00
|
|
|
|
}
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#if DEBUG
|
2004-11-16 21:58:54 +01:00
|
|
|
|
printf("You have no access to '%s'\n", gr);
|
2002-02-27 09:49:48 +01:00
|
|
|
|
#endif
|
2004-11-16 21:58:54 +01:00
|
|
|
|
} // if
|
|
|
|
|
// brukeren var ikke med i gruppen, s<> vi m<> pr<70>ve neste.
|
|
|
|
|
|
|
|
|
|
// finner den siste _-en i navnet.
|
2006-08-15 13:51:59 +02:00
|
|
|
|
foo = strrchr(group, '_');
|
|
|
|
|
if (foo == NULL) {
|
2004-11-16 21:58:54 +01:00
|
|
|
|
// hvis vi har kommet helt til bunn, og fremdeles
|
|
|
|
|
// ikke har blitt avbrutt, fantes det ingen slik gruppe.
|
2006-08-15 13:51:59 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// det som n<> er bak siste underscore er passe uinteressant, s<>
|
|
|
|
|
// vi flytter slutten av strengen litt framover.
|
|
|
|
|
*foo = '\0';
|
2002-02-27 09:49:48 +01:00
|
|
|
|
|
2004-11-16 21:58:54 +01:00
|
|
|
|
} // while
|
2002-02-27 09:49:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-06 18:05:05 +01:00
|
|
|
|
/* return a list of the user's groupnames */
|
|
|
|
|
/* numgroups is the total number of groups found */
|
|
|
|
|
char **get_group_names(int *numgroups)
|
|
|
|
|
{
|
2006-08-15 13:51:59 +02:00
|
|
|
|
char **grouplist;
|
|
|
|
|
gid_t gids[33];
|
|
|
|
|
int real_nr_groups, nr_groups, i;
|
|
|
|
|
struct group *g;
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
nr_groups = 0;
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
nr_groups = getgroups(32, &gids[0]); /* Allow a max of 32 groups */
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
if (nr_groups == -1) {
|
|
|
|
|
dberror(NULL, "Error while trying to fetch group info");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2012-12-03 13:20:20 +01:00
|
|
|
|
grouplist = malloc(33 * sizeof(char *));
|
2006-08-15 13:51:59 +02:00
|
|
|
|
real_nr_groups = 0;
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
for (i = 0; i < nr_groups; i++) {
|
|
|
|
|
g = getgrgid(gids[i]);
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
/* Go to next grp if it doesn't have a name */
|
|
|
|
|
if (g != NULL) {
|
2012-12-03 13:20:20 +01:00
|
|
|
|
grouplist[real_nr_groups++] = strdup(g->gr_name);
|
2006-08-15 13:51:59 +02:00
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "Omitting gid %d, no entry in group-file.\n", gids[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
grouplist[real_nr_groups] = NULL;
|
2002-06-05 10:01:31 +02:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
*numgroups = real_nr_groups;
|
2002-03-06 18:05:05 +01:00
|
|
|
|
|
2006-08-15 13:51:59 +02:00
|
|
|
|
return grouplist;
|
2002-03-06 18:05:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-25 01:41:16 +01:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
reload(MYSQL *pmysql)
|
|
|
|
|
{
|
|
|
|
|
return mysql_reload(pmysql);
|
|
|
|
|
}
|
2012-12-03 13:20:20 +01:00
|
|
|
|
|
|
|
|
|
/* same as strcpy, but returns a pointer to the end of dest instead of start */
|
|
|
|
|
char *strmov(char *dest, const char *src) {
|
|
|
|
|
while ((*dest++ = *src++))
|
|
|
|
|
;
|
|
|
|
|
return dest-1;
|
|
|
|
|
}
|