ubase

suckless linux base utils
git clone git://git.suckless.org/ubase
Log | Files | Refs | README | LICENSE

passwd.c (1384B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/resource.h>
      3 #include <sys/time.h>
      4 
      5 #include <errno.h>
      6 #include <pwd.h>
      7 #include <shadow.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 #include <unistd.h>
     12 
     13 #include "../passwd.h"
     14 #include "../text.h"
     15 #include "../util.h"
     16 
     17 /* Returns -1 on error, 0 for incorrect password
     18  * and 1 if all went OK */
     19 int
     20 pw_check(const struct passwd *pw, const char *pass)
     21 {
     22 	char *cryptpass, *p;
     23 	struct spwd *spw;
     24 
     25 	p = pw->pw_passwd;
     26 	if (p[0] == '!' || p[0] == '*') {
     27 		weprintf("denied\n");
     28 		return -1;
     29 	}
     30 
     31 	if (pw->pw_passwd[0] == '\0') {
     32 		if (pass[0] == '\0')
     33 			return 1;
     34 		weprintf("incorrect password\n");
     35 		return 0;
     36 	}
     37 
     38 	if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') {
     39 		errno = 0;
     40 		spw = getspnam(pw->pw_name);
     41 		if (!spw) {
     42 			if (errno)
     43 				weprintf("getspnam: %s:", pw->pw_name);
     44 			else
     45 				weprintf("who are you?\n");
     46 			return -1;
     47 		}
     48 		p = spw->sp_pwdp;
     49 		if (p[0] == '!' || p[0] == '*') {
     50 			weprintf("denied\n");
     51 			return -1;
     52 		}
     53 	}
     54 
     55 	cryptpass = crypt(pass, p);
     56 	if (!cryptpass) {
     57 		weprintf("crypt:");
     58 		return -1;
     59 	}
     60 	if (strcmp(cryptpass, p) != 0) {
     61 		weprintf("incorrect password\n");
     62 		return 0;
     63 	}
     64 	return 1;
     65 }
     66 
     67 int
     68 pw_init(void)
     69 {
     70 	struct rlimit rlim;
     71 
     72 	rlim.rlim_cur = 0;
     73 	rlim.rlim_max = 0;
     74 	if (setrlimit(RLIMIT_CORE, &rlim) < 0)
     75 		eprintf("setrlimit:");
     76 	return 0;
     77 }