Cover V05, I06
Article
Figure 1
Listing 1
Listing 2
Listing 3
Listing 4
Sidebar 1

jun96.tar


Listing 2: The server passwdd program

/*
* passwdd -- the daemon that looks after changing passwords from afar.
*      Run as a login shell, passed the userid via the TERM env-
*      ironment variable.
*/
#include
#include

char *ProgName;
#define ERR (-1)
#define YES     0
#define NO      1
#define MAXLINE 255

/*ARGSUSED*/
main(argc, argv) int argc; char* argv[];  {
char    *remoteName, *getenv();
struct passwd *p, *getpwnam();
unsigned short  getuid(), geteuid();
int     rc;

ProgName = argv[0];
(void) fprintf(stderr,"Connected to passwd server...\n");
/* Become root if you aren't already. */
if (setuid((int) geteuid()) == ERR) {
(void) fprintf(stderr,"%s: unable to change to root, halting\n",
ProgName);
(void) exit(1);
}

if ((remoteName= getenv("TERM")) == NULL) {
(void) fprintf(stderr,"%s: no TERM variable supplied, halting\n",
ProgName);
(void) exit(1);
}
if ((p= getpwnam(remoteName)) == NULL) {
(void) fprintf(stderr,"%s: no userid %s known, halting\n",
ProgName,remoteName);
(void) exit(1);
}
rc = changePass(p,remoteName);
(void) exit(rc);
/*NOTREACHED*/
}

int
changePass(p,name) struct passwd *p; char *name; {
char    *oldPasswd, *getpass(),
*thing, *crypt(),
salt[2], buffer[MAXLINE];

(void) fprintf(stderr,"Changing password for %s.\n",name);
oldPasswd = getpass("Old password: ");
salt[0] = p->pw_passwd[0];
salt[1] = p->pw_passwd[1];
thing = crypt(oldPasswd,salt);
#ifdef DEBUG
(void) fprintf(stderr,"entered=%s,old=%s,new=%s\n",
oldPasswd,p->pw_passwd,thing);
#endif
if (strncmp(thing,p->pw_passwd,13) != 0) {
(void) fprintf(stderr,"Sorry\n");
return NO;
}

(void) sprintf(buffer,"/bin/passwd %s",name);
if (system(buffer) == 0) {
(void) fprintf(stderr,"Your password will be distributed within the hour.\r\n");
return YES;
}
else {
return NO;
}
}

# End of File