1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* $Id$
*
* This file is to be used to test the pwdb functions.
* In order to utilize fully the library, it should be tested by root.
*/
#define SHADOW_FILE "/etc/shadow"
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <pwdb/pwdb_map.h>
#define testlog stdout
int main(void) {
/* int id; */
int have_shadow;
const struct passwd *pw;
const struct spwd *sp;
struct passwd pwent;
struct spwd spent;
setpwent();
{
FILE *test;
test = fopen(SHADOW_FILE, "r");
if (test == NULL) {
have_shadow = 0;
} else {
fclose(test);
have_shadow = 1;
}
}
if (have_shadow) {
setspent();
} else {
fprintf(testlog,"[cannot read Shadow file]\n");
}
/*
for (id=0; id<1000; ++id) {
if ((pw = getpwuid(id)) != NULL) {
char *user = strdup(pw->pw_name);
if ((pw = getpwnam(user)) == NULL) {
fprintf(testlog,"failed to pw_lookup user `%s'\n", user);
continue ;
} else if (strcmp(user, pw->pw_name))
fprintf(testlog,"user pw_lookup[%s] differs from uid[%s] one\n", pw->pw_name, user);
if ((sp = getspnam(user)) == NULL) {
fprintf(testlog,"failed to sp_lookup user `%s'\n", user);
continue;
} else if (strcmp(user, sp->sp_namp))
fprintf(testlog,"user splookup[%s] differs from uid[%s] one\n", sp->sp_namp, user);
free(user);
fprintf(testlog,
"user: %s\n"
"\tPassword: %s (%s)\n"
"\tUID=%d, GID=%d\n"
"\tgecos: %s\n"
"\tHOME: %s\n"
"\tSHELL: %s\n"
, pw->pw_name, pw->pw_passwd, sp->sp_pwdp, pw->pw_uid, pw->pw_gid
, pw->pw_gecos, pw->pw_dir, pw->pw_shell
);
}
}
*/
/* Now test the update code */
/* passwd */
if (__pwdb_lckpwdf()!=0) {
printf("Can not lock passwd file !\n");
goto end;
};
if (!__pwdb_pw_open(O_RDWR)) {
printf("can not open passwd file!\n");
goto end;
};
pw = __pwdb_pw_locate("gafton1");
if (!pw) {
printf("can not pw_locate\n");
goto end;
};
pwent = *pw;
pwent.pw_gid++;
if (! __pwdb_pw_update(&pwent)) {
printf("update passwd error\n");
goto end;
};
if (! __pwdb_pw_close()) {
printf("error closing.\n");
goto end;
};
if (!__pwdb_spw_open(O_RDWR)) {
printf("Can not open shadow file !\n");
goto end;
};
sp = __pwdb_spw_locate("gafton1");
if (!sp) {
printf("can not sp_locate\n");
goto end;
};
spent = *sp;
spent.sp_lstchg++;
if (!__pwdb_spw_update(&spent)) {
printf("update shadow error\n");
goto end;
};
if (!__pwdb_spw_close()) {
printf("can not close shadow\n");
goto end;
};
end:
__pwdb_ulckpwdf();
if (have_shadow) {
endspent();
}
endpwent();
return 0;
}
|