utmp

simple login manager
git clone git://git.suckless.org/utmp
Log | Files | Refs | README | LICENSE

bsd.c (1143B)


      1 
      2 #include <ctype.h>
      3 #include <errno.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include <unistd.h>
      8 #include <util.h>
      9 #include <grp.h>
     10 #include <utmp.h>
     11 #include <pwd.h>
     12 
     13 extern void die(const char *fmt, ...);
     14 extern struct passwd *pw;
     15 extern gid_t egid, gid;
     16 static struct utmp utmp;
     17 
     18 void
     19 addutmp(void)
     20 {
     21 	unsigned ptyid;
     22 	char *pts, *cp, *host;
     23 
     24 
     25 	if (!(host = getenv("DISPLAY")))
     26 		host = "-";
     27 
     28 	if (strlen(pw->pw_name) > sizeof(utmp.ut_name))
     29 		die("utmp:incorrect username %s", pw->pw_name);
     30 
     31 	if ((pts = ttyname(STDIN_FILENO)) == NULL)
     32 		die("utmp:error getting pty name:%s", strerror(errno));
     33 
     34 	for (cp = pts + strlen(pts) - 1; isdigit(*cp); --cp)
     35 		/* nothing */;
     36 
     37 	ptyid = atoi(++cp);
     38 	if (ptyid > 999 || strlen(pts + 5) > sizeof(utmp.ut_line))
     39 		die("utmp:Incorrect pts name %s\n", pts);
     40 
     41 	/* remove /dev/ from pts */
     42 	strncpy(utmp.ut_line, pts + 5, sizeof(utmp.ut_line));
     43 	strncpy(utmp.ut_name, pw->pw_name, sizeof(utmp.ut_name));
     44 	strncpy(utmp.ut_host, host, sizeof(utmp.ut_host));
     45 	time(&utmp.ut_time);
     46 
     47 	setegid(egid);
     48 	login(&utmp);
     49 	setegid(gid);
     50 }
     51 
     52 void
     53 delutmp(void)
     54 {
     55 	setgid(egid);
     56 	logout(utmp.ut_line);
     57 	setgid(gid);
     58 }
     59