screenlock.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* screenlock - lock a terminal */
  10. #include <u.h>
  11. #include <libc.h>
  12. #include <libsec.h>
  13. #include <draw.h>
  14. #include <thread.h>
  15. #include <auth.h>
  16. char pic[] = "/lib/bunny.bit";
  17. int vgactl;
  18. int debug;
  19. int doblank;
  20. int chatty = 0;
  21. char user[256];
  22. void
  23. blankscreen(int blank)
  24. {
  25. if(vgactl < 0)
  26. return;
  27. seek(vgactl, 0, 0);
  28. if(fprint(vgactl, blank? "blank": "unblank") < 0)
  29. fprint(2, "blankscreen: can't blank: %r\n");
  30. }
  31. void
  32. error(char *fmt, ...)
  33. {
  34. Fmt f;
  35. char buf[64];
  36. va_list arg;
  37. fmtfdinit(&f, 1, buf, sizeof buf);
  38. fmtprint(&f, "screenlock: ");
  39. va_start(arg, fmt);
  40. fmtvprint(&f, fmt, arg);
  41. va_end(arg);
  42. fmtprint(&f, "\n");
  43. fmtfdflush(&f);
  44. threadexitsall("fatal error");
  45. }
  46. void
  47. usage(void)
  48. {
  49. fprint(2, "usage: %s\n", argv0);
  50. exits("usage");
  51. }
  52. void
  53. readfile(char *name, char *buf, int nbuf, int addnul)
  54. {
  55. int fd;
  56. fd = open(name, OREAD);
  57. if(fd == -1)
  58. error("%s - can't open: %r", name);
  59. nbuf = read(fd, buf, nbuf-addnul);
  60. close(fd);
  61. if(nbuf == -1)
  62. error("%s - can't can't read: %r", name);
  63. if(addnul)
  64. buf[nbuf] = '\0';
  65. }
  66. void
  67. readline(char *buf, int nbuf)
  68. {
  69. char c;
  70. int i;
  71. i = 0;
  72. while(i < nbuf-1)
  73. if(read(0, &c, 1) != 1 || c == '\04' || c == '\177'){
  74. i = 0;
  75. break;
  76. } else if(c == '\n')
  77. break;
  78. else if(c == '\b' && i > 0)
  79. --i;
  80. else if(c == ('u' & 037))
  81. i = 0;
  82. else
  83. buf[i++] = c;
  84. buf[i] = '\0';
  85. }
  86. void
  87. checkpassword(void)
  88. {
  89. int fd, consctl, must;
  90. char buf[256];
  91. AuthInfo *ai;
  92. static int opened;
  93. must = 1;
  94. if(!opened){
  95. fd = open("/dev/cons", OREAD);
  96. if(fd == -1)
  97. error("can't open cons: %r");
  98. dup(fd, 0);
  99. close(fd);
  100. fd = open("/dev/cons", OWRITE);
  101. if(fd == -1)
  102. error("can't open cons: %r");
  103. dup(fd, 1);
  104. dup(1, 2);
  105. close(fd);
  106. consctl = open("/dev/consctl", OWRITE);
  107. if(consctl == -1)
  108. error("can't open consctl: %r");
  109. if(write(consctl, "rawon", 5) != 5)
  110. error("can't turn off echo\n");
  111. opened = 1;
  112. }
  113. for(;;){
  114. if(chatty || !must)
  115. fprint(2, "%s's screenlock password: ", user);
  116. memset(buf, 0, sizeof buf);
  117. readline(buf, sizeof buf);
  118. blankscreen(0);
  119. if(chatty || !must)
  120. fprint(2, "\n");
  121. if(buf[0] == '\0' || buf[0] == '\04'){
  122. if(must)
  123. continue;
  124. error("no password typed");
  125. }
  126. /* authenticate */
  127. ai = auth_userpasswd(user, buf);
  128. if(ai != nil && ai->cap != nil)
  129. break;
  130. auth_freeAI(ai);
  131. if(chatty || !must)
  132. fprint(2, "password mismatch\n");
  133. doblank = 1;
  134. }
  135. memset(buf, 0, sizeof buf);
  136. blankscreen(0);
  137. }
  138. void
  139. blanker(void *v)
  140. {
  141. int tics;
  142. tics = 0;
  143. for(;;){
  144. if(doblank > 0){
  145. doblank = 0;
  146. tics = 10;
  147. }
  148. if(tics > 0 && --tics == 0)
  149. blankscreen(1);
  150. sleep(1000);
  151. }
  152. }
  153. void
  154. grabmouse(void *v)
  155. {
  156. int fd, x, y;
  157. char ibuf[256], obuf[256];
  158. if(debug)
  159. return;
  160. fd = open("/dev/mouse", ORDWR);
  161. if(fd < 0)
  162. error("can't open /dev/mouse: %r");
  163. snprint(obuf, sizeof obuf, "m %d %d",
  164. screen->r.min.x + Dx(screen->r)/2,
  165. screen->r.min.y + Dy(screen->r)/2);
  166. while(read(fd, ibuf, sizeof ibuf) > 0){
  167. ibuf[12] = 0;
  168. ibuf[24] = 0;
  169. x = atoi(ibuf+1);
  170. y = atoi(ibuf+13);
  171. if(x != screen->r.min.x + Dx(screen->r)/2 ||
  172. y != screen->r.min.y + Dy(screen->r)/2){
  173. fprint(fd, "%s", obuf);
  174. doblank = 1;
  175. }
  176. }
  177. }
  178. /* lay down text at `p' */
  179. static void
  180. screenstring(Point p, char *s)
  181. {
  182. string(screen, p, screen->display->white, ZP, font, s);
  183. flushimage(display, 1);
  184. }
  185. void
  186. lockscreen(void)
  187. {
  188. enum { Nfld = 5, Fldlen = 12, Cursorlen = 2*4 + 2*2*16, };
  189. char *s;
  190. char buf[Nfld*Fldlen], *flds[Nfld], newcmd[128], cbuf[Cursorlen];
  191. int fd, dx, dy;
  192. Image *i;
  193. Point p;
  194. Rectangle r;
  195. Tm *tm;
  196. fd = open("/dev/screen", OREAD);
  197. if(fd < 0)
  198. error("can't open /dev/screen: %r");
  199. if(read(fd, buf, Nfld*Fldlen) != Nfld*Fldlen)
  200. error("can't read /dev/screen: %r");
  201. close(fd);
  202. buf[sizeof buf-1] = 0;
  203. if(tokenize(buf, flds, Nfld) != Nfld)
  204. error("can't tokenize /dev/screen header");
  205. snprint(newcmd, sizeof newcmd, "-r %s %s %d %d",
  206. flds[1], flds[2], atoi(flds[3]) - 1, atoi(flds[4]) - 1);
  207. newwindow(newcmd);
  208. if (initdraw(nil, nil, "screenlock") < 0)
  209. sysfatal("initdraw failed");
  210. if(display == nil)
  211. error("no display");
  212. /* screen is now open and covered. grab mouse and hold on tight */
  213. procrfork(grabmouse, nil, 4096, RFFDG);
  214. procrfork(blanker, nil, 4096, RFFDG);
  215. fd = open(pic, OREAD);
  216. if(fd > 0){
  217. i = readimage(display, fd, 0);
  218. if(i){
  219. r = screen->r;
  220. p = Pt(r.max.x / 2, r.max.y * 2 / 3);
  221. dx = (Dx(screen->r) - Dx(i->r)) / 2;
  222. r.min.x += dx;
  223. r.max.x -= dx;
  224. dy = (Dy(screen->r) - Dy(i->r)) / 2;
  225. r.min.y += dy;
  226. r.max.y -= dy;
  227. draw(screen, screen->r, display->black, nil, ZP);
  228. draw(screen, r, i, nil, i->r.min);
  229. flushimage(display, 1);
  230. }
  231. close(fd);
  232. /* identify the user on screen, centered */
  233. tm = localtime(time(0));
  234. s = smprint("user %s at %d:%02.2d", getuser(), tm->hour, tm->min);
  235. p = subpt(p, Pt(stringwidth(font, "m") * strlen(s) / 2, 0));
  236. screenstring(p, s);
  237. }
  238. /* clear the cursor */
  239. fd = open("/dev/cursor", OWRITE);
  240. if(fd > 0){
  241. memset(cbuf, 0, sizeof cbuf);
  242. write(fd, cbuf, sizeof cbuf);
  243. /* leave it open */
  244. }
  245. }
  246. void
  247. threadmain(int argc, char *argv[])
  248. {
  249. readfile("#c/user", user, sizeof user, 1);
  250. if((vgactl = open("/dev/vgactl", OWRITE)) < 0)
  251. vgactl = open("#v/vgactl", OWRITE);
  252. ARGBEGIN{
  253. case 'd':
  254. debug++;
  255. break;
  256. default:
  257. usage();
  258. }ARGEND
  259. if(argc != 0)
  260. usage();
  261. doblank = 1;
  262. lockscreen();
  263. checkpassword();
  264. threadexitsall(nil);
  265. }