1 /* cvm/cvm-v1checkpassword.c - Checkpassword emulator for CVM
2 * Copyright (C) 2010 Bruce Guenter <bruce@untroubled.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <bglibs/ucspi.h>
23 #include <bglibs/msg.h>
24 #include "v1client.h"
25
26 const char program[] = "cvm-v1checkpassword";
27 const int msg_show_pid = 0;
28
29 static char buffer[513];
30 static char* pass;
31
get_data(void)32 void get_data(void)
33 {
34 unsigned buflen;
35 unsigned rd;
36
37 for (buflen = 0; buflen < sizeof buffer; buflen += rd) {
38 do
39 rd = read(3, buffer+buflen, sizeof buffer - buflen);
40 while ((rd == (unsigned)-1) && (errno == EINTR));
41 if (rd == (unsigned)-1) exit(111); /* Read error */
42 if (rd == 0) break;
43 }
44 close(3);
45 if (buflen >= sizeof buffer) exit(2); /* Buffer too long */
46 if ((pass = memchr(buffer, 0, buflen)) == 0) exit(2); /* No password */
47 ++pass;
48 if (memchr(pass, 0, buflen-(pass-buffer)) == 0) exit(2); /* No terminator */
49 }
50
main(int argc,char ** argv)51 int main(int argc, char** argv)
52 {
53 int i;
54 const char* tokens[2];
55
56 if (argc < 3)
57 die3(111, "usage: ", program, " cvmodule program [args ...]");
58
59 get_data();
60 tokens[0] = pass;
61 tokens[1] = 0;
62 if ((i = cvm_client_authenticate(argv[1], buffer, ucspi_localhost(),
63 tokens, 1)) != 0) return i;
64
65 if (!cvm_client_setugid()) return 111;
66 if (!cvm_client_setenv()) return 111;
67
68 execvp(argv[2], argv+2);
69 return 111;
70 }
71