1 /* cvm/client_setenv.c - CVM client standard setenv calls
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 <bglibs/sysdeps.h>
19
20 #include "v1client.h"
21
utoa_rec(unsigned i,char * buf)22 static char* utoa_rec(unsigned i, char* buf)
23 {
24 if (i < 10)
25 *buf = i + '0';
26 else {
27 buf = utoa_rec(i / 10, buf);
28 *buf = (i % 10) + '0';
29 }
30 *++buf = 0;
31 return buf;
32 }
33
34 static char utoa_buf[32];
35
utoa(unsigned i)36 static char* utoa(unsigned i)
37 {
38 utoa_rec(i, utoa_buf);
39 return utoa_buf;
40 }
41
utoa_len(unsigned i)42 static int utoa_len(unsigned i)
43 {
44 return utoa_rec(i, utoa_buf) - utoa_buf;
45 }
46
set_gids(void)47 static int set_gids(void)
48 {
49 unsigned long gid;
50 long len;
51 char* start;
52 char* ptr;
53 int result;
54
55 len = 0;
56 while (cvm_client_fact_uint(CVM_FACT_SUPP_GROUPID, &gid) == 0)
57 len += utoa_len(gid) + 1;
58 /* Don't set $GIDS if no supplementary group IDs were listed */
59 if (len == 0) return 1;
60 /* Reset to the start of facts list */
61 cvm_client_fact_uint(-1, &gid);
62 ptr = start = malloc(len);
63 while (cvm_client_fact_uint(CVM_FACT_SUPP_GROUPID, &gid) == 0) {
64 if (ptr > start) *ptr++ = ',';
65 ptr = utoa_rec(gid, ptr);
66 }
67 result = setenv("GIDS", start, 1) == 0;
68 free(start);
69 return result;
70 }
71
cvm_client_setenv(void)72 int cvm_client_setenv(void)
73 {
74 if (setenv("USER", cvm_fact_username, 1) != 0) return 0;
75 if (setenv("UID", utoa(cvm_fact_userid), 1) != 0) return 0;
76 if (setenv("GID", utoa(cvm_fact_groupid), 1) != 0) return 0;
77 if (cvm_fact_realname &&
78 setenv("NAME", cvm_fact_realname, 1) != 0) return 0;
79 if (setenv("HOME", cvm_fact_directory, 1) != 0) return 0;
80 if (cvm_fact_shell &&
81 setenv("SHELL", cvm_fact_shell, 1) != 0) return 0;
82 if (cvm_fact_groupname &&
83 setenv("GROUP", cvm_fact_groupname, 1) != 0) return 0;
84 if (cvm_fact_domain &&
85 setenv("DOMAIN", cvm_fact_domain, 1) != 0) return 0;
86 if (cvm_fact_mailbox &&
87 (setenv("MAIL", cvm_fact_mailbox, 1) != 0
88 || setenv("MAILBOX", cvm_fact_mailbox, 1) != 0
89 || setenv("MAILDIR", cvm_fact_mailbox, 1)))
90 return 0;
91 if (!set_gids()) return 0;
92 return 1;
93 }
94