H49878 s 00000/00000/00093 d D 1.2 02/06/03 17:56:47 patch 3 2 c Import patch patch.37 cC cF25393 cK34906 cPsrc/doc/login.html e s 00093/00000/00000 d D 1.1 02/06/03 17:56:41 patch 2 1 c Import patch patch.01 cC cF25308 cK09664 cO-rw-rw-r-- e s 00000/00000/00000 d D 1.0 02/06/03 17:56:41 patch 1 0 c BitKeeper file /home/orc/Src/ndialog/nd/doc/login.html cBorc@pacific.pell.portland.or.us|ChangeSet|20020604075610|31977|6e29fa5631e78430 cF25307 cHpacific.pell.portland.or.us cK05255 cPnd/doc/login.html cRaf908144624541f4 cV4 cX0x821 cZ-07:00 e u U f e 0 f x 0x821 t T I 2
This code can be called by a login program to get a username and password. It expects that the world has been prepared by an init_dialog before it's called, and it leaves it up to the caller to call end_dialog when it wants to.
/* * cc -c getuser.o getuser.c */ #include <ndialog.h> #include <pwd.h> #include <sys/types.h> #include <unistd.h> #define ESIZE 20 static char user[ESIZE]; static char pass[ESIZE]; static struct passwd *pw; static int retries; /* * pwcallback() is a local function that authenticates the user after they * enter a password. */ static int pwcallback(void* obj, void* display) { char *ency; if ((pw = getpwnam(user)) != 0) { if (pw->pw_passwd[0] == 0 && pass[0] == 0) { /* no password, so don't even check */ return -1; } ency = crypt(pass, pw->pw_passwd); memset(pass, 0, ESIZE); if (strcmp(ency, pw->pw_passwd) == 0) { /* password matched; we can leave now */ return -1; } } /* complain bitterly and return */ Error("login incorrect"); return (--retries >= 0) ? 0 : -1; } /* pwcallback */ /* * getuser() asks for a username and password. If it's given a valid username * and password, it returns a pwent for that user, otherwise it spits up a * `login incorrect' error box and asks again, up to login_attempts times. * After that, it returns 0 and leaves the proper punishment up to the caller. */ struct passwd * getuser(int login_attempts) { int rc; void *u_obj, *p_obj, *cancel, *chain; /* allocate the display fields */ u_obj = newString( 0,0, ESIZE, ESIZE, user, 0, "Username", 0, 0); p_obj = newPWString(0,3, ESIZE, ESIZE, pass, 0, "Password", pwcallback, 0); cancel= newCancelButton(0, "Forget it", 0, 0); retries = login_attempts; /* build them into a ObjChain */ chain = ObjChain(ObjChain(u_obj, p_obj), cancel); /* get the user input and clean up */ rc = MENU(chain, -1, -1, 0, "Enter your username and password", 0); deleteObjChain(chain); /* and return the user info if there is any */ return (rc == MENU_OK) ? pw : 0; } /* getuser */