/* Copyright ?1998 by Nem W Schlecht This file is public domain and comes with NO WARRANTY of any kind
MySQL SQL code for creating/deleting these functions:
CREATE FUNCTION mygetpwnam RETURNS STRING SONAME "userinf.so"; DROP FUNCTION mygetpwnam;
CREATE FUNCTION mygetpwuid RETURNS STRING SONAME "userinf.so"; DROP FUNCTION mygetpwuid;
CREATE FUNCTION mygetgrnam RETURNS STRING SONAME "userinf.so"; DROP FUNCTION mygetgrnam;
CREATE FUNCTION mygetgrgid RETURNS STRING SONAME "userinf.so"; DROP FUNCTION mygetgrgid;
*/
#ifdef STANDARD #include <stdio.h> #include <string.h> #else #include <global.h> #include <my_sys.h> #endif #include <mysql.h> #include <m_ctype.h>
/* * The following includes are for the user/group info functions */ #include <pwd.h> #include <grp.h>
#ifdef HAVE_DLOPEN
/* These must be right or mysqld will not find the symbol! */
extern "C" { my_bool mygetpwnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void mygetpwnam_deinit(UDF_INIT *initid); char *mygetpwnam(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
my_bool mygetpwuid_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void mygetpwuid_deinit(UDF_INIT *initid); char *mygetpwuid(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
char *_fill_pw_info(passwd *user, char *inf_sep, char *result);
}
my_bool mygetpwnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count == 0 || args->arg_count > 2 || args->arg_type[0] != STRING_RESULT) { strcpy(message,"Wrong arguments to getpwnam"); return 1; }
if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) { strcpy(message,"Wrong arguments to getpwnam"); return 1; }
initid->maybe_null = 1; return 0; }
void mygetpwnam_deinit(UDF_INIT *initid) { }
char *mygetpwnam(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { struct passwd *user; char *inf_sep;
if (args->arg_count > 1) { inf_sep = (char *) malloc(strlen(args->args[1])); strcpy(inf_sep, args->args[1]); } else { inf_sep = (char *) malloc(sizeof(char)); strcpy(inf_sep, ":"); }
user = getpwnam(args->args[0]); result = _fill_pw_info(user, inf_sep, result); if (!result) { *is_null = 1; return 0; } free(inf_sep); *length = (ulong) strlen(result); return result; }
my_bool mygetpwuid_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count == 0 || args->arg_count > 2 || args->arg_type[0] != INT_RESULT) { strcpy(message,"Wrong arguments to getpwuid"); return 1; }
if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) { strcpy(message,"Wrong arguments to getpwuid"); return 1; }
initid->maybe_null = 1; return 0; }
void mygetpwuid_deinit(UDF_INIT *initid) { }
char *mygetpwuid(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { struct passwd *user; char *inf_sep;
if (args->arg_count > 1) { inf_sep = (char *) malloc(strlen(args->args[1])); strcpy(inf_sep, args->args[1]); } else { inf_sep = (char *) malloc(sizeof(char)); strcpy(inf_sep, ":"); }
user = getpwuid( *((long long*) args->args[0]) ); result = _fill_pw_info(user, inf_sep, result); if (!result) { *is_null = 1; return 0; } free(inf_sep); *length = (ulong) strlen(result); return result; }
char *_fill_pw_info(passwd *user, char *inf_sep, char *result) { char buf[10];
if (user != 0) { strcpy(result, user->pw_name); strcat(result, inf_sep); strcat(result, user->pw_passwd); strcat(result, inf_sep);
sprintf(buf, "%d", user->pw_uid); strcat(result, buf); strcat(result, inf_sep);
sprintf(buf, "%d", user->pw_gid); strcat(result, buf); strcat(result, inf_sep);
/* strcat(result, user->pw_age); strcat(result, inf_sep);*/ /* strcat(result, user->pw_comment); strcat(result, inf_sep);*/ strcat(result, user->pw_gecos); strcat(result, inf_sep); strcat(result, user->pw_dir); strcat(result, inf_sep); strcat(result, user->pw_shell);
return result; } else { return 0; } }
/* * GROUP functions */
extern "C" { my_bool mygetgrnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void mygetgrnam_deinit(UDF_INIT *initid); char *mygetgrnam(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
my_bool mygetgrgid_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void mygetgrgid_deinit(UDF_INIT *initid); char *mygetgrgid(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
char *_fill_gr_info(group *group, char *inf_sep, char *mem_sep, char *result);
}
my_bool mygetgrnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count == 0 || args->arg_count > 3 || args->arg_type[0] != STRING_RESULT) { strcpy(message,"Wrong arguments to getgrgid; Use the source"); return 1; }
if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) { strcpy(message,"Wrong arguments to getgrgid"); return 1; }
if (args->arg_count == 3 && args->arg_type[2] != STRING_RESULT) { strcpy(message,"Wrong arguments to getgrgid"); return 1; }
initid->maybe_null = 1; return 0; }
void mygetgrnam_deinit(UDF_INIT *initid) { }
char *mygetgrnam(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { struct group *group; char *inf_sep; char *mem_sep;
if (args->args[1]) { inf_sep = (char *) malloc(strlen(args->args[1])); strcpy(inf_sep, args->args[1]); } else { inf_sep = (char *) malloc(sizeof(char)); strcpy(inf_sep, ":"); }
if (args->arg_count > 2) { mem_sep = (char *) malloc(strlen(args->args[2])); strcpy(mem_sep, args->args[2]); } else { mem_sep = (char *) malloc(sizeof(char)); strcpy(mem_sep, ","); }
group = getgrnam(args->args[0]); result = _fill_gr_info(group, inf_sep, mem_sep, result); if (!result) { *is_null = 1; return 0; }
*length = (ulong) strlen(result); free(inf_sep); free(mem_sep); return result; }
my_bool mygetgrgid_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count == 0 || args->arg_count > 3 || args->arg_type[0] != INT_RESULT) { strcpy(message,"Wrong arguments to getgrgid; Use the source"); return 1; }
if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) { strcpy(message,"Wrong arguments to getgrgid"); return 1; }
if (args->arg_count == 3 && args->arg_type[2] != STRING_RESULT) { strcpy(message,"Wrong arguments to getgrgid"); return 1; }
initid->maybe_null = 1; return 0; }
void mygetgrgid_deinit(UDF_INIT *initid) { }
char *mygetgrgid(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { struct group *group; char *inf_sep; char *mem_sep;
if (args->arg_count > 1) { inf_sep = (char *) malloc(strlen(args->args[1])); strcpy(inf_sep, args->args[1]); } else { inf_sep = (char *) malloc(sizeof(char)); strcpy(inf_sep, ":"); }
if (args->arg_count > 2) { mem_sep = (char *) malloc(strlen(args->args[2])); strcpy(mem_sep, args->args[2]); } else { mem_sep = (char *) malloc(sizeof(char)); strcpy(mem_sep, ","); }
group = getgrgid( *((long long*) args->args[0]) ); result = _fill_gr_info(group, inf_sep, mem_sep, result); if (!result) { *is_null = 1; return 0; }
*length = (ulong) strlen(result); free(inf_sep); free(mem_sep); return result; }
char *_fill_gr_info(group *group, char *inf_sep, char *mem_sep, char *result) { char buf[10]; char **elem;
if (group != 0) { strcpy(result, group->gr_name); strcat(result, inf_sep); strcat(result, group->gr_passwd); strcat(result, inf_sep);
sprintf(buf, "%d", group->gr_gid); strcat(result, buf); strcat(result, inf_sep);
for (elem = group->gr_mem; elem && *elem; elem++) { strcat(result, *elem); strcat(result, mem_sep); } result[(strlen(result)-strlen(mem_sep))]='\0'; return result; } else { return 0; } }
#endif /* HAVE_DLOPEN */
|