1 /* cvm/cvm-unix.c - UNIX/POSIX-standard CVM module
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 <sys/types.h>
19 #include <errno.h>
20 #include <grp.h>
21 #include <pwd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <bglibs/sysdeps.h>
26 
27 #include "module.h"
28 
29 const char program[] = "cvm-unix";
30 
31 extern char* crypt(const char* key, const char* salt);
32 
cvm_module_init(void)33 int cvm_module_init(void)
34 {
35   return 0;
36 }
37 
38 extern int cvm_getpwnam(const char*, struct passwd**);
39 
40 static struct passwd* pw;
41 static struct group* gr;
42 
cvm_module_lookup(void)43 int cvm_module_lookup(void)
44 {
45   int err;
46   if ((err = cvm_getpwnam(cvm_module_credentials[CVM_CRED_ACCOUNT].s, &pw)) != 0)
47     return err;
48   if (pw->pw_passwd == 0) return CVME_PERMFAIL;
49   return 0;
50 }
51 
cvm_module_authenticate(void)52 int cvm_module_authenticate(void)
53 {
54   CVM_CRED_REQUIRED(PASSWORD);
55   if (strcmp(crypt(cvm_module_credentials[CVM_CRED_PASSWORD].s, pw->pw_passwd),
56 	     pw->pw_passwd) != 0)
57     return CVME_PERMFAIL;
58   return 0;
59 }
60 
cvm_module_results(void)61 int cvm_module_results(void)
62 {
63   char* tmp;
64   if ((tmp = strchr(pw->pw_gecos, ',')) != 0)
65     *tmp = 0;
66 
67   cvm_fact_username = pw->pw_name;
68   cvm_fact_userid = pw->pw_uid;
69   cvm_fact_groupid = pw->pw_gid;
70   cvm_fact_realname = pw->pw_gecos;
71   cvm_fact_directory = pw->pw_dir;
72   cvm_fact_shell = pw->pw_shell;
73 
74   cvm_module_fact_uint(CVM_FACT_SUPP_GROUPID, pw->pw_gid);
75   if (cvm_fact_groupname) free((char*)cvm_fact_groupname);
76   cvm_fact_groupname = 0;
77   setgrent();
78   while ((gr = getgrent()) != 0) {
79     if (gr->gr_gid == pw->pw_gid)
80       cvm_fact_groupname = strdup(gr->gr_name);
81     else {
82       unsigned i;
83       for (i = 0; gr->gr_mem[i]; i++)
84 	if (strcmp(gr->gr_mem[i], pw->pw_name) == 0) {
85 	  cvm_module_fact_uint(CVM_FACT_SUPP_GROUPID, gr->gr_gid);
86 	  break;
87 	}
88     }
89   }
90   endgrent();
91 
92   return 0;
93 }
94 
cvm_module_stop(void)95 void cvm_module_stop(void)
96 {
97 }
98