/nis/nis_defaults.c

https://github.com/nitinkamble/glibc · C · 455 lines · 384 code · 47 blank · 24 comment · 140 complexity · 99fcb6bfafaabc289f4ea7674367474e MD5 · raw file

  1. /* Copyright (c) 1997, 1998, 2004, 2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <rpc/rpc.h>
  22. #include <rpcsvc/nis.h>
  23. #define DEFAULT_TTL 43200
  24. /*
  25. ** Some functions for parsing the -D param and NIS_DEFAULTS Environ
  26. */
  27. static nis_name
  28. searchXYX (char *str, const char *what)
  29. {
  30. assert (strlen (what) == 6);
  31. assert (strncmp (str, what, 6) == 0);
  32. str += 6; /* Points to the begin of the parameters. */
  33. int i = 0;
  34. while (str[i] != '\0' && str[i] != ':')
  35. ++i;
  36. if (i == 0) /* only "<WHAT>=" ? */
  37. return strdup ("");
  38. return strndup (str, i);
  39. }
  40. static nis_name
  41. searchgroup (char *str)
  42. {
  43. return searchXYX (str, "group=");
  44. }
  45. static nis_name
  46. searchowner (char *str)
  47. {
  48. return searchXYX (str, "owner=");
  49. }
  50. static uint32_t
  51. searchttl (char *str)
  52. {
  53. char buf[strlen (str) + 1];
  54. char *cptr, *dptr;
  55. uint32_t time;
  56. int i;
  57. dptr = strstr (str, "ttl=");
  58. if (dptr == NULL) /* should (could) not happen */
  59. return DEFAULT_TTL;;
  60. dptr += 4; /* points to the begin of the new ttl */
  61. i = 0;
  62. while (dptr[i] != '\0' && dptr[i] != ':')
  63. i++;
  64. if (i == 0) /* only "ttl=" ? */
  65. return DEFAULT_TTL;
  66. strncpy (buf, dptr, i);
  67. buf[i] = '\0';
  68. time = 0;
  69. dptr = buf;
  70. cptr = strchr (dptr, 'd');
  71. if (cptr != NULL)
  72. {
  73. *cptr = '\0';
  74. cptr++;
  75. time += atoi (dptr) * 60 * 60 * 24;
  76. dptr = cptr;
  77. }
  78. cptr = strchr (dptr, 'h');
  79. if (cptr != NULL)
  80. {
  81. *cptr = '\0';
  82. cptr++;
  83. time += atoi (dptr) * 60 * 60;
  84. dptr = cptr;
  85. }
  86. cptr = strchr (dptr, 'm');
  87. if (cptr != NULL)
  88. {
  89. *cptr = '\0';
  90. cptr++;
  91. time += atoi (dptr) * 60;
  92. dptr = cptr;
  93. }
  94. cptr = strchr (dptr, 's');
  95. if (cptr != NULL)
  96. *cptr = '\0';
  97. time += atoi (dptr);
  98. return time;
  99. }
  100. static unsigned int
  101. searchaccess (char *str, unsigned int access)
  102. {
  103. char buf[strlen (str) + 1];
  104. char *cptr;
  105. unsigned int result = access;
  106. int i;
  107. int n, o, g, w;
  108. cptr = strstr (str, "access=");
  109. if (cptr == NULL)
  110. return 0;
  111. cptr += 7; /* points to the begin of the access string */
  112. i = 0;
  113. while (cptr[i] != '\0' && cptr[i] != ':')
  114. i++;
  115. if (i == 0) /* only "access=" ? */
  116. return 0;
  117. strncpy (buf, cptr, i);
  118. buf[i] = '\0';
  119. n = o = g = w = 0;
  120. cptr = buf;
  121. if (*cptr == ',') /* Fix for stupid Solaris scripts */
  122. ++cptr;
  123. while (*cptr != '\0')
  124. {
  125. switch (*cptr)
  126. {
  127. case 'n':
  128. n = 1;
  129. break;
  130. case 'o':
  131. o = 1;
  132. break;
  133. case 'g':
  134. g = 1;
  135. break;
  136. case 'w':
  137. w = 1;
  138. break;
  139. case 'a':
  140. o = g = w = 1;
  141. break;
  142. case '-':
  143. cptr++; /* Remove "-" from beginning */
  144. while (*cptr != '\0' && *cptr != ',')
  145. {
  146. switch (*cptr)
  147. {
  148. case 'r':
  149. if (n)
  150. result = result & ~(NIS_READ_ACC << 24);
  151. if (o)
  152. result = result & ~(NIS_READ_ACC << 16);
  153. if (g)
  154. result = result & ~(NIS_READ_ACC << 8);
  155. if (w)
  156. result = result & ~(NIS_READ_ACC);
  157. break;
  158. case 'm':
  159. if (n)
  160. result = result & ~(NIS_MODIFY_ACC << 24);
  161. if (o)
  162. result = result & ~(NIS_MODIFY_ACC << 16);
  163. if (g)
  164. result = result & ~(NIS_MODIFY_ACC << 8);
  165. if (w)
  166. result = result & ~(NIS_MODIFY_ACC);
  167. break;
  168. case 'c':
  169. if (n)
  170. result = result & ~(NIS_CREATE_ACC << 24);
  171. if (o)
  172. result = result & ~(NIS_CREATE_ACC << 16);
  173. if (g)
  174. result = result & ~(NIS_CREATE_ACC << 8);
  175. if (w)
  176. result = result & ~(NIS_CREATE_ACC);
  177. break;
  178. case 'd':
  179. if (n)
  180. result = result & ~(NIS_DESTROY_ACC << 24);
  181. if (o)
  182. result = result & ~(NIS_DESTROY_ACC << 16);
  183. if (g)
  184. result = result & ~(NIS_DESTROY_ACC << 8);
  185. if (w)
  186. result = result & ~(NIS_DESTROY_ACC);
  187. break;
  188. default:
  189. return (~0U);
  190. }
  191. cptr++;
  192. }
  193. n = o = g = w = 0;
  194. break;
  195. case '+':
  196. cptr++; /* Remove "+" from beginning */
  197. while (*cptr != '\0' && *cptr != ',')
  198. {
  199. switch (*cptr)
  200. {
  201. case 'r':
  202. if (n)
  203. result = result | (NIS_READ_ACC << 24);
  204. if (o)
  205. result = result | (NIS_READ_ACC << 16);
  206. if (g)
  207. result = result | (NIS_READ_ACC << 8);
  208. if (w)
  209. result = result | (NIS_READ_ACC);
  210. break;
  211. case 'm':
  212. if (n)
  213. result = result | (NIS_MODIFY_ACC << 24);
  214. if (o)
  215. result = result | (NIS_MODIFY_ACC << 16);
  216. if (g)
  217. result = result | (NIS_MODIFY_ACC << 8);
  218. if (w)
  219. result = result | (NIS_MODIFY_ACC);
  220. break;
  221. case 'c':
  222. if (n)
  223. result = result | (NIS_CREATE_ACC << 24);
  224. if (o)
  225. result = result | (NIS_CREATE_ACC << 16);
  226. if (g)
  227. result = result | (NIS_CREATE_ACC << 8);
  228. if (w)
  229. result = result | (NIS_CREATE_ACC);
  230. break;
  231. case 'd':
  232. if (n)
  233. result = result | (NIS_DESTROY_ACC << 24);
  234. if (o)
  235. result = result | (NIS_DESTROY_ACC << 16);
  236. if (g)
  237. result = result | (NIS_DESTROY_ACC << 8);
  238. if (w)
  239. result = result | (NIS_DESTROY_ACC);
  240. break;
  241. default:
  242. return (~0U);
  243. }
  244. cptr++;
  245. }
  246. n = o = g = w = 0;
  247. break;
  248. case '=':
  249. cptr++; /* Remove "=" from beginning */
  250. /* Clear */
  251. if (n)
  252. result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
  253. NIS_CREATE_ACC + NIS_DESTROY_ACC) << 24);
  254. if (o)
  255. result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
  256. NIS_CREATE_ACC + NIS_DESTROY_ACC) << 16);
  257. if (g)
  258. result = result & ~((NIS_READ_ACC + NIS_MODIFY_ACC +
  259. NIS_CREATE_ACC + NIS_DESTROY_ACC) << 8);
  260. if (w)
  261. result = result & ~(NIS_READ_ACC + NIS_MODIFY_ACC +
  262. NIS_CREATE_ACC + NIS_DESTROY_ACC);
  263. while (*cptr != '\0' && *cptr != ',')
  264. {
  265. switch (*cptr)
  266. {
  267. case 'r':
  268. if (n)
  269. result = result | (NIS_READ_ACC << 24);
  270. if (o)
  271. result = result | (NIS_READ_ACC << 16);
  272. if (g)
  273. result = result | (NIS_READ_ACC << 8);
  274. if (w)
  275. result = result | (NIS_READ_ACC);
  276. break;
  277. case 'm':
  278. if (n)
  279. result = result | (NIS_MODIFY_ACC << 24);
  280. if (o)
  281. result = result | (NIS_MODIFY_ACC << 16);
  282. if (g)
  283. result = result | (NIS_MODIFY_ACC << 8);
  284. if (w)
  285. result = result | (NIS_MODIFY_ACC);
  286. break;
  287. case 'c':
  288. if (n)
  289. result = result | (NIS_CREATE_ACC << 24);
  290. if (o)
  291. result = result | (NIS_CREATE_ACC << 16);
  292. if (g)
  293. result = result | (NIS_CREATE_ACC << 8);
  294. if (w)
  295. result = result | (NIS_CREATE_ACC);
  296. break;
  297. case 'd':
  298. if (n)
  299. result = result | (NIS_DESTROY_ACC << 24);
  300. if (o)
  301. result = result | (NIS_DESTROY_ACC << 16);
  302. if (g)
  303. result = result | (NIS_DESTROY_ACC << 8);
  304. if (w)
  305. result = result | (NIS_DESTROY_ACC);
  306. break;
  307. default:
  308. return result = (~0U);
  309. }
  310. cptr++;
  311. }
  312. n = o = g = w = 0;
  313. break;
  314. default:
  315. return result = (~0U);
  316. }
  317. if (*cptr != '\0')
  318. cptr++;
  319. }
  320. return result;
  321. }
  322. nis_name
  323. __nis_default_owner (char *defaults)
  324. {
  325. char *default_owner = NULL;
  326. char *cptr = defaults;
  327. if (cptr == NULL)
  328. cptr = getenv ("NIS_DEFAULTS");
  329. if (cptr != NULL)
  330. {
  331. char *dptr = strstr (cptr, "owner=");
  332. if (dptr != NULL)
  333. {
  334. char *p = searchowner (dptr);
  335. if (p == NULL)
  336. return NULL;
  337. default_owner = strdupa (p);
  338. free (p);
  339. }
  340. }
  341. return strdup (default_owner ?: nis_local_principal ());
  342. }
  343. libnsl_hidden_def (__nis_default_owner)
  344. nis_name
  345. __nis_default_group (char *defaults)
  346. {
  347. char *default_group = NULL;
  348. char *cptr = defaults;
  349. if (cptr == NULL)
  350. cptr = getenv ("NIS_DEFAULTS");
  351. if (cptr != NULL)
  352. {
  353. char *dptr = strstr (cptr, "group=");
  354. if (dptr != NULL)
  355. {
  356. char *p = searchgroup (dptr);
  357. if (p == NULL)
  358. return NULL;
  359. default_group = strdupa (p);
  360. free (p);
  361. }
  362. }
  363. return strdup (default_group ?: nis_local_group ());
  364. }
  365. libnsl_hidden_def (__nis_default_group)
  366. uint32_t
  367. __nis_default_ttl (char *defaults)
  368. {
  369. char *cptr, *dptr;
  370. if (defaults != NULL)
  371. {
  372. dptr = strstr (defaults, "ttl=");
  373. if (dptr != NULL)
  374. return searchttl (defaults);
  375. }
  376. cptr = getenv ("NIS_DEFAULTS");
  377. if (cptr == NULL)
  378. return DEFAULT_TTL;
  379. dptr = strstr (cptr, "ttl=");
  380. if (dptr == NULL)
  381. return DEFAULT_TTL;
  382. return searchttl (cptr);
  383. }
  384. /* Default access rights are ----rmcdr---r---, but we could change
  385. this with the NIS_DEFAULTS variable. */
  386. unsigned int
  387. __nis_default_access (char *param, unsigned int defaults)
  388. {
  389. unsigned int result;
  390. char *cptr;
  391. if (defaults == 0)
  392. result = 0 | OWNER_DEFAULT | GROUP_DEFAULT | WORLD_DEFAULT;
  393. else
  394. result = defaults;
  395. if (param != NULL && strstr (param, "access=") != NULL)
  396. result = searchaccess (param, result);
  397. else
  398. {
  399. cptr = getenv ("NIS_DEFAULTS");
  400. if (cptr != NULL && strstr (cptr, "access=") != NULL)
  401. result = searchaccess (cptr, result);
  402. }
  403. return result;
  404. }
  405. libnsl_hidden_def (__nis_default_access)