newns.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include <authsrv.h>
  6. #include "authlocal.h"
  7. enum
  8. {
  9. NARG = 15, /* max number of arguments */
  10. MAXARG = 10*ANAMELEN, /* max length of an argument */
  11. };
  12. static int setenv(char*, char*);
  13. static char *expandarg(char*, char*);
  14. static int splitargs(char*, char*[], char*, int);
  15. static int nsfile(Biobuf *, AuthRpc *);
  16. static int nsop(int, char*[], AuthRpc*);
  17. static int callexport(char*, char*);
  18. static int catch(void*, char*);
  19. static int
  20. buildns(int newns, char *user, char *file)
  21. {
  22. Biobuf *b;
  23. char home[4*ANAMELEN];
  24. int afd;
  25. AuthRpc *rpc;
  26. int cdroot;
  27. char *path;
  28. rpc = nil;
  29. /* try for factotum now because later is impossible */
  30. afd = open("/mnt/factotum/rpc", ORDWR);
  31. if(afd >= 0){
  32. rpc = auth_allocrpc(afd);
  33. if(rpc == nil){
  34. close(afd);
  35. afd = -1;
  36. }
  37. }
  38. if(file == nil){
  39. if(!newns){
  40. werrstr("no namespace file specified");
  41. return -1;
  42. }
  43. file = "/lib/namespace";
  44. }
  45. b = Bopen(file, OREAD);
  46. if(b == 0){
  47. werrstr("can't open %s: %r", file);
  48. close(afd);
  49. auth_freerpc(rpc);
  50. return -1;
  51. }
  52. if(newns){
  53. rfork(RFENVG|RFCNAMEG);
  54. setenv("user", user);
  55. snprint(home, 2*ANAMELEN, "/usr/%s", user);
  56. setenv("home", home);
  57. }
  58. cdroot = nsfile(b, rpc);
  59. Bterm(b);
  60. if(rpc){
  61. close(rpc->afd);
  62. auth_freerpc(rpc);
  63. }
  64. /* make sure we managed to cd into the new name space */
  65. if(newns && !cdroot){
  66. path = malloc(1024);
  67. if(path == nil || getwd(path, 1024) == 0 || chdir(path) < 0)
  68. chdir("/");
  69. if(path != nil)
  70. free(path);
  71. }
  72. return 0;
  73. }
  74. static int
  75. nsfile(Biobuf *b, AuthRpc *rpc)
  76. {
  77. int argc;
  78. char *cmd, *argv[NARG+1], argbuf[MAXARG*NARG];
  79. int cdroot = 0;
  80. atnotify(catch, 1);
  81. while(cmd = Brdline(b, '\n')){
  82. cmd[Blinelen(b)-1] = '\0';
  83. while(*cmd==' ' || *cmd=='\t')
  84. cmd++;
  85. if(*cmd == '#')
  86. continue;
  87. argc = splitargs(cmd, argv, argbuf, NARG);
  88. if(argc)
  89. cdroot |= nsop(argc, argv, rpc);
  90. }
  91. atnotify(catch, 0);
  92. return cdroot;
  93. }
  94. int
  95. newns(char *user, char *file)
  96. {
  97. return buildns(1, user, file);
  98. }
  99. int
  100. addns(char *user, char *file)
  101. {
  102. return buildns(0, user, file);
  103. }
  104. static int
  105. famount(int fd, AuthRpc *rpc, char *mntpt, int flags, char *aname)
  106. {
  107. int afd;
  108. AuthInfo *ai;
  109. afd = fauth(fd, aname);
  110. if(afd >= 0){
  111. ai = fauth_proxy(afd, rpc, amount_getkey, "proto=p9any role=client");
  112. if(ai != nil)
  113. auth_freeAI(ai);
  114. }
  115. return mount(fd, afd, mntpt, flags, aname);
  116. }
  117. static int
  118. nsop(int argc, char *argv[], AuthRpc *rpc)
  119. {
  120. char *argv0;
  121. ulong flags;
  122. int fd;
  123. Biobuf *b;
  124. int cdroot = 0;
  125. flags = 0;
  126. argv0 = 0;
  127. ARGBEGIN{
  128. case 'a':
  129. flags |= MAFTER;
  130. break;
  131. case 'b':
  132. flags |= MBEFORE;
  133. break;
  134. case 'c':
  135. flags |= MCREATE;
  136. break;
  137. case 'C':
  138. flags |= MCACHE;
  139. break;
  140. }ARGEND
  141. if(!(flags & (MAFTER|MBEFORE)))
  142. flags |= MREPL;
  143. if(strcmp(argv0, ".") == 0 && argc == 1){
  144. b = Bopen(argv[0], OREAD);
  145. if(b == nil)
  146. return 0;
  147. cdroot |= nsfile(b, rpc);
  148. Bterm(b);
  149. } else if(strcmp(argv0, "clear") == 0 && argc == 0)
  150. rfork(RFCNAMEG);
  151. else if(strcmp(argv0, "bind") == 0 && argc == 2)
  152. bind(argv[0], argv[1], flags);
  153. else if(strcmp(argv0, "unmount") == 0){
  154. if(argc == 1)
  155. unmount(nil, argv[0]);
  156. else if(argc == 2)
  157. unmount(argv[0], argv[1]);
  158. } else if(strcmp(argv0, "mount") == 0){
  159. fd = open(argv[0], ORDWR);
  160. if(argc == 2)
  161. famount(fd, rpc, argv[1], flags, "");
  162. else if(argc == 3)
  163. famount(fd, rpc, argv[1], flags, argv[2]);
  164. close(fd);
  165. } else if(strcmp(argv0, "import") == 0){
  166. fd = callexport(argv[0], argv[1]);
  167. if(argc == 2)
  168. famount(fd, rpc, argv[1], flags, "");
  169. else if(argc == 3)
  170. famount(fd, rpc, argv[2], flags, "");
  171. close(fd);
  172. } else if(strcmp(argv0, "cd") == 0 && argc == 1)
  173. if(chdir(argv[0]) == 0 && *argv[0] == '/')
  174. cdroot = 1;
  175. return cdroot;
  176. }
  177. static char *wocp = "sys: write on closed pipe";
  178. static int
  179. catch(void *x, char *m)
  180. {
  181. USED(x);
  182. return strncmp(m, wocp, strlen(wocp)) == 0;
  183. }
  184. static int
  185. callexport(char *sys, char *tree)
  186. {
  187. char *na, buf[3];
  188. int fd;
  189. AuthInfo *ai;
  190. na = netmkaddr(sys, 0, "exportfs");
  191. if((fd = dial(na, 0, 0, 0)) < 0)
  192. return -1;
  193. if((ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client")) == nil
  194. || write(fd, tree, strlen(tree)) < 0
  195. || read(fd, buf, 3) != 2 || buf[0]!='O' || buf[1]!= 'K'){
  196. close(fd);
  197. auth_freeAI(ai);
  198. return -1;
  199. }
  200. auth_freeAI(ai);
  201. return fd;
  202. }
  203. static int
  204. splitargs(char *p, char *argv[], char *argbuf, int nargv)
  205. {
  206. char *q;
  207. int i, n;
  208. n = gettokens(p, argv, nargv, " \t'\r");
  209. if(n == nargv)
  210. return 0;
  211. for(i = 0; i < n; i++){
  212. q = argv[i];
  213. argv[i] = argbuf;
  214. argbuf = expandarg(q, argbuf);
  215. if(!argbuf)
  216. return 0;
  217. }
  218. return n;
  219. }
  220. /*
  221. * copy the arg into the buffer,
  222. * expanding any environment variables.
  223. * environment variables are assumed to be
  224. * names (ie. < ANAMELEN long)
  225. * the entire argument is expanded to be at
  226. * most MAXARG long and null terminated
  227. * the address of the byte after the terminating null is returned
  228. * any problems cause a 0 return;
  229. */
  230. static char *
  231. expandarg(char *arg, char *buf)
  232. {
  233. char env[3+ANAMELEN], *p, *q, *x;
  234. int fd, n, len;
  235. n = 0;
  236. while(p = utfrune(arg, L'$')){
  237. len = p - arg;
  238. if(n + len + ANAMELEN >= MAXARG-1)
  239. return 0;
  240. memmove(&buf[n], arg, len);
  241. n += len;
  242. p++;
  243. arg = utfrune(p, L'\0');
  244. q = utfrune(p, L'/');
  245. if(q && q < arg)
  246. arg = q;
  247. q = utfrune(p, L'.');
  248. if(q && q < arg)
  249. arg = q;
  250. q = utfrune(p, L'$');
  251. if(q && q < arg)
  252. arg = q;
  253. len = arg - p;
  254. if(len >= ANAMELEN)
  255. continue;
  256. strcpy(env, "#e/");
  257. strncpy(env+3, p, len);
  258. env[3+len] = '\0';
  259. fd = open(env, OREAD);
  260. if(fd >= 0){
  261. len = read(fd, &buf[n], ANAMELEN - 1);
  262. /* some singleton environment variables have trailing NULs */
  263. /* lists separate entries with NULs; we arbitrarily take the first element */
  264. if(len > 0){
  265. x = memchr(&buf[n], 0, len);
  266. if(x != nil)
  267. len = x - &buf[n];
  268. n += len;
  269. }
  270. close(fd);
  271. }
  272. }
  273. len = strlen(arg);
  274. if(n + len >= MAXARG - 1)
  275. return 0;
  276. strcpy(&buf[n], arg);
  277. return &buf[n+len+1];
  278. }
  279. static int
  280. setenv(char *name, char *val)
  281. {
  282. int f;
  283. char ename[ANAMELEN+6];
  284. long s;
  285. sprint(ename, "#e/%s", name);
  286. f = create(ename, OWRITE, 0664);
  287. if(f < 0)
  288. return -1;
  289. s = strlen(val);
  290. if(write(f, val, s) != s){
  291. close(f);
  292. return -1;
  293. }
  294. close(f);
  295. return 0;
  296. }