11 setenv(char *var, char *val)
15 fd = create(var, OWRITE, 0644);
17 print("init: can't open %s\n", var);
25 * become the authenticated user
33 fd = open("#ยค/capuse", OWRITE);
35 sysfatal("can't change uid: %r");
36 rv = write(fd, ai->cap, strlen(ai->cap));
39 sysfatal("can't change uid: %r");
46 mountfactotum(char *srvname)
51 fd = open(srvname, ORDWR);
53 sysfatal("opening factotum: %r");
54 mount(fd, -1, "/mnt", MBEFORE, "");
70 sysname = getenv("sysname");
72 return strdup("cs.bell-labs.com");
75 t = csipinfo(nil, "sys", sysname, &s, 1);
77 for(p = t; p != nil; p = p->entry)
78 if(strcmp(p->attr, s) == 0){
79 authdom = strdup(p->val);
87 * start a new factotum and pass it the username and password
90 startfactotum(char *user, char *password, char *srvname)
94 strcpy(srvname, "/srv/factotum.XXXXXXXXXXX");
99 sysfatal("can't start factotum: %r");
101 execl("/boot/factotum", "loginfactotum", "-ns", srvname+5, nil);
102 sysfatal("starting factotum: %r");
108 mountfactotum(srvname);
110 /* write in new key */
111 fd = open("/mnt/factotum/ctl", ORDWR);
113 sysfatal("opening factotum: %r");
114 fprint(fd, "key proto=dp9ik dom=%q user=%q !password=%q\n", getauthdom(), user, password);
115 fprint(fd, "key proto=p9sk1 dom=%q user=%q !password=%q\n", getauthdom(), user, password);
122 fprint(2, "usage: %s [-a authdom] user\n", argv0);
127 main(int argc, char *argv[])
129 char buf[2*ANAMELEN];
130 char home[2*ANAMELEN];
131 char srvname[2*ANAMELEN];
132 char *user, *pass, *sysname, *tz, *cputype, *service;
139 authdom = EARGF(usage());
149 rfork(RFENVG|RFNAMEG);
151 service = getenv("service");
152 if(strcmp(service, "cpu") == 0)
153 fprint(2, "login: warning: running on a cpu server!\n");
155 fprint(2, "usage: login username\n");
159 pass = readcons("Password", nil, 1);
161 exits("no password");
164 ai = auth_userpasswd(user, pass);
165 if(ai == nil || ai->cap == nil)
166 sysfatal("login incorrect");
171 /* start a new factotum and hand it a new key */
172 startfactotum(user, pass, srvname);
174 memset(pass, 0, strlen(pass));
177 /* set up new namespace */
178 newns(ai->cuid, nil);
181 /* remount the factotum */
182 mountfactotum(srvname);
184 /* get rid of srvname */
187 /* set up a new environment */
188 cputype = getenv("cputype");
189 sysname = getenv("sysname");
190 tz = getenv("timezone");
192 setenv("#e/service", "con");
193 setenv("#e/user", user);
194 snprint(home, sizeof(home), "/usr/%s", user);
195 setenv("#e/home", home);
196 setenv("#e/cputype", cputype);
197 setenv("#e/objtype", cputype);
199 setenv("#e/sysname", sysname);
201 setenv("#e/timezone", tz);
203 /* go to new home directory */
204 snprint(buf, sizeof(buf), "/usr/%s", user);
208 /* read profile and start interactive rc */
209 execl("/bin/rc", "rc", "-li", nil);