unixnames.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. static void uxfree(Unixid*);
  11. static Unixid * xfree;
  12. Unixidmap *idhead, *idtail;
  13. Unixscmap *scmap;
  14. #define UNUSED 0x7FFFFFFF
  15. /*
  16. * Sadly we have to use the IP address, since some systems (FreeBSD in particular)
  17. * do not believe it to be safe to depend on the hostname and so refuse to send it.
  18. * I dislike making this IP-centric, but so be it.
  19. * We keep a cache of host names in getdom.
  20. */
  21. Unixidmap *
  22. pair2idmap(char *server, uint32_t clientip)
  23. {
  24. Resub match;
  25. Unixscmap *m, *mp;
  26. Unixidmap *r;
  27. char dom[256];
  28. for(mp=0,m=scmap; m; mp=m,m=m->next){
  29. if(m->server[0] != server[0])
  30. continue;
  31. if(strcmp(m->server, server))
  32. continue;
  33. if(m->clientip != clientip)
  34. continue;
  35. if(mp){
  36. mp->next = m->next;
  37. m->next = scmap;
  38. scmap = m;
  39. }
  40. r = m->map;
  41. if(r->u.timestamp != 0 && r->g.timestamp != 0)
  42. return r;
  43. scmap = m->next;
  44. free(m);
  45. break;
  46. }
  47. if(rpcdebug)
  48. fprint(2, "looking for %lux\n", clientip);
  49. if(getdom(clientip, dom, sizeof dom)<0){
  50. clog("auth: unknown ip address");
  51. return nil;
  52. }
  53. if(rpcdebug)
  54. fprint(2, "dom is %s\n", dom);
  55. for(r=idhead; r; r=r->next){
  56. if(r->u.timestamp == 0 || r->g.timestamp == 0)
  57. continue;
  58. match.sp = match.ep = 0;
  59. if(regexec(r->sexp, server, &match, 1) == 0)
  60. continue;
  61. if(match.sp != server || match.ep <= match.sp || *match.ep)
  62. continue;
  63. match.sp = match.ep = 0;
  64. if(regexec(r->cexp, dom, &match, 1) == 0)
  65. continue;
  66. if(match.sp != dom || match.ep <= match.sp || *match.ep)
  67. continue;
  68. m = malloc(sizeof(Unixscmap));
  69. m->next = scmap;
  70. scmap = m;
  71. m->server = strstore(server);
  72. m->clientip = clientip;
  73. m->map = r;
  74. break;
  75. }
  76. return r;
  77. }
  78. int
  79. readunixidmaps(char *file)
  80. {
  81. Waitmsg *w;
  82. Biobuf *in;
  83. Unixidmap *m;
  84. int i, arc; char *arv[16], buf[256];
  85. char *l;
  86. // int32_t savalarm;
  87. // savalarm = alarm(0);
  88. in = Bopen(file, OREAD);
  89. if(in == 0){
  90. clog("readunixidmaps can't open %s: %r\n", file);
  91. // alarm(savalarm);
  92. return -1;
  93. }
  94. for(m=idhead; m; m=m->next)
  95. m->flag = 0;
  96. while(l = Brdline(in, '\n')){ /* assign = */
  97. l[Blinelen(in)-1] = 0;
  98. arc = strparse(l, nelem(arv), arv);
  99. if(arc > 0 && arv[0][0] == '!'){
  100. ++arv[0];
  101. snprint(buf, sizeof buf, "/bin/%s", arv[0]);
  102. if(chatty){
  103. chat("!");
  104. for(i=0; i<arc; i++)
  105. chat(" %s", arv[i]);
  106. chat("...");
  107. }
  108. w = system(buf, arv);
  109. if(w == nil)
  110. chat("err: %r\n");
  111. else if(w->msg && w->msg[0])
  112. chat("status: %s\n", w->msg);
  113. else
  114. chat("OK\n");
  115. free(w);
  116. continue;
  117. }
  118. if(arc != 4)
  119. continue;
  120. for(m=idhead; m; m=m->next)
  121. if(strcmp(arv[0], m->server) == 0 &&
  122. strcmp(arv[1], m->client) == 0)
  123. break;
  124. if(m == 0){
  125. m = malloc(sizeof(Unixidmap));
  126. if(idtail)
  127. idtail->next = m;
  128. else
  129. idhead = m;
  130. idtail = m;
  131. m->next = 0;
  132. m->server = strstore(arv[0]);
  133. m->client = strstore(arv[1]);
  134. m->sexp = regcomp(m->server);
  135. m->cexp = regcomp(m->client);
  136. m->u.file = strstore(arv[2]);
  137. m->u.style = 'u';
  138. m->u.timestamp = 0;
  139. m->u.ids = 0;
  140. m->g.file = strstore(arv[3]);
  141. m->g.style = 'u';
  142. m->g.timestamp = 0;
  143. m->g.ids = 0;
  144. }else{
  145. if(!m->u.file || strcmp(m->u.file, arv[2]) != 0){
  146. m->u.file = strstore(arv[2]);
  147. m->u.timestamp = 0;
  148. }
  149. if(!m->g.file || strcmp(m->g.file, arv[3]) != 0){
  150. m->g.file = strstore(arv[3]);
  151. m->g.timestamp = 0;
  152. }
  153. }
  154. m->flag = 1;
  155. checkunixmap(&m->u);
  156. checkunixmap(&m->g);
  157. }
  158. Bterm(in);
  159. for(m=idhead; m; m=m->next)
  160. if(m->flag == 0){
  161. m->u.file = 0;
  162. m->u.timestamp = 0;
  163. uxfree(m->u.ids);
  164. m->u.ids = 0;
  165. m->g.file = 0;
  166. m->g.timestamp = 0;
  167. uxfree(m->g.ids);
  168. m->g.ids = 0;
  169. }
  170. // alarm(savalarm);
  171. return 0;
  172. }
  173. static void
  174. uxfree(Unixid *x)
  175. {
  176. Unixid *tail;
  177. int count=0;
  178. if(x){
  179. tail = x;
  180. if(tail->id < 0)
  181. abort();
  182. tail->id = UNUSED;
  183. while(tail->next){
  184. tail = tail->next;
  185. ++count;
  186. if(tail->id == UNUSED)
  187. abort();
  188. tail->id = UNUSED;
  189. }
  190. tail->next = xfree;
  191. xfree = x;
  192. }
  193. }
  194. int
  195. checkunixmap(Unixmap *u)
  196. {
  197. Dir *dir;
  198. dir = dirstat(u->file);
  199. if(dir == nil){
  200. clog("checkunixmap can't stat %s: %r\n", u->file);
  201. return -1;
  202. }
  203. if(u->timestamp > dir->mtime){
  204. free(dir);
  205. return 0;
  206. }
  207. uxfree(u->ids);
  208. u->ids = readunixids(u->file, u->style);
  209. u->timestamp = time(0);
  210. free(dir);
  211. return 1;
  212. }
  213. int
  214. name2id(Unixid **list, char *name)
  215. {
  216. Unixid *x, *xp;
  217. for(xp=0,x=*list; x; xp=x,x=x->next){
  218. if(x->name[0] == name[0] && strcmp(x->name, name) == 0){
  219. if(xp){
  220. xp->next = x->next;
  221. x->next = *list;
  222. *list = x;
  223. }
  224. return x->id;
  225. }
  226. }
  227. return -1;
  228. }
  229. char *
  230. id2name(Unixid **list, int id)
  231. {
  232. Unixid *x, *xp;
  233. for(xp=0,x=*list; x; xp=x,x=x->next){
  234. if(x->id == id){
  235. if(xp){
  236. xp->next = x->next;
  237. x->next = *list;
  238. *list = x;
  239. }
  240. return x->name;
  241. }
  242. }
  243. return "none";
  244. }
  245. void
  246. idprint(int fd, Unixid *xp)
  247. {
  248. while(xp){
  249. fprint(fd, "%d\t%s\n", xp->id, xp->name);
  250. xp = xp->next;
  251. }
  252. }
  253. /*
  254. * style '9': 3:tom:tom:
  255. * style 'u': sysadm:*:0:0:System-Administrator:/usr/admin:/bin/sh
  256. */
  257. Unixid *
  258. readunixids(char *file, int style)
  259. {
  260. Biobuf *in;
  261. char *l, *name = 0;
  262. Unixid *x, *xp = 0;
  263. int id = 0;
  264. in = Bopen(file, OREAD);
  265. if(in == 0){
  266. clog("readunixids can't open %s: %r\n", file);
  267. return 0;
  268. }
  269. while(l = Brdline(in, '\n')){ /* assign = */
  270. l[Blinelen(in)-1] = 0;
  271. switch(style){
  272. case '9':
  273. id = strtol(l, &l, 10);
  274. if(*l != ':')
  275. continue;
  276. name = ++l;
  277. l = strchr(l, ':');
  278. if(l == 0)
  279. continue;
  280. *l = 0;
  281. break;
  282. case 'u':
  283. name = l;
  284. l = strchr(l, ':');
  285. if(l == 0)
  286. continue;
  287. *l++ = 0;
  288. /* skip password */
  289. l = strchr(l, ':');
  290. if(l == 0)
  291. continue;
  292. id = strtol(l+1, 0, 10);
  293. break;
  294. default:
  295. panic("unknown unixid style %d\n", style);
  296. }
  297. if(id == UNUSED)
  298. id = -1; /* any value will do */
  299. if(!(x = xfree)) /* assign = */
  300. x = listalloc(1024/sizeof(Unixid), sizeof(Unixid));
  301. xfree = x->next;
  302. x->id = id;
  303. x->name = strstore(name);
  304. x->next = xp;
  305. xp = x;
  306. }
  307. Bterm(in);
  308. return xp;
  309. }