nsuser.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * Description (nsuser.c)
  8. *
  9. * This module contains routines for accessing information in a
  10. * Netscape user database. User information is returned in the
  11. * form of a user object (UserObj_t), defined in nsauth.h.
  12. */
  13. #include "base/systems.h"
  14. #include "netsite.h"
  15. #include "assert.h"
  16. #define __PRIVATE_NSUSER
  17. #include "libaccess/nsuser.h"
  18. /* Authentication facility name for error frame generation */
  19. char * NSAuth_Program = "NSAUTH";
  20. /*
  21. * Description (userDecode)
  22. *
  23. * This function decodes an external user DB record into a dynamically
  24. * allocated UserObj_t structure. The DB record is encoded as an
  25. * attribute record as defined in attrec.h.
  26. *
  27. * Arguments:
  28. *
  29. * name - pointer to user account name string
  30. * ureclen - length of the user DB record, in octets
  31. * urecptr - pointer to user DB record
  32. *
  33. * Returns:
  34. *
  35. * A pointer to the allocated UserObj_t structure is returned.
  36. */
  37. UserObj_t * userDecode(NTS_t name, int ureclen, ATR_t urecptr)
  38. {
  39. ATR_t cp = urecptr; /* current pointer into DB record */
  40. USI_t tag; /* attribute tag */
  41. USI_t len; /* attribute value encoding length */
  42. USI_t gcnt; /* number of group ids */
  43. USI_t * gids; /* pointer to array of group ids */
  44. int i; /* group id index */
  45. UserObj_t * uoptr; /* user object pointer */
  46. /* Allocate a user object structure */
  47. uoptr = (UserObj_t *)MALLOC(sizeof(UserObj_t));
  48. if (uoptr) {
  49. uoptr->uo_name = (unsigned char *) STRDUP((char *)name);
  50. uoptr->uo_pwd = 0;
  51. uoptr->uo_uid = 0;
  52. uoptr->uo_flags = 0;
  53. uoptr->uo_rname = 0;
  54. UILINIT(&uoptr->uo_groups);
  55. /* Parse user DB record */
  56. while ((cp - urecptr) < ureclen) {
  57. /* Get the attribute tag */
  58. cp = USIDECODE(cp, &tag);
  59. /* Get the length of the encoding of the attribute value */
  60. cp = USIDECODE(cp, &len);
  61. /* Process this attribute */
  62. switch (tag) {
  63. case UAT_PASSWORD: /* encrypted password */
  64. cp = NTSDECODE(cp, &uoptr->uo_pwd);
  65. break;
  66. case UAT_UID: /* user id */
  67. cp = USIDECODE(cp, &uoptr->uo_uid);
  68. break;
  69. case UAT_ACCFLAGS: /* account flags */
  70. cp = USIDECODE(cp, &uoptr->uo_flags);
  71. break;
  72. case UAT_REALNAME: /* real name of user */
  73. cp = NTSDECODE(cp, &uoptr->uo_rname);
  74. break;
  75. case UAT_GROUPS: /* groups which include user */
  76. /* First get the number of group ids following */
  77. cp = USIDECODE(cp, &gcnt);
  78. if (gcnt > 0) {
  79. /* Allocate space for group ids */
  80. gids = usiAlloc(&uoptr->uo_groups, gcnt);
  81. if (gids) {
  82. for (i = 0; i < gcnt; ++i) {
  83. cp = USIDECODE(cp, gids + i);
  84. }
  85. }
  86. }
  87. break;
  88. default: /* unrecognized attribute */
  89. /* Just skip it */
  90. cp += len;
  91. break;
  92. }
  93. }
  94. }
  95. return uoptr;
  96. }
  97. /*
  98. * Description (userEnumHelp)
  99. *
  100. * This is a local function that is called by NSDB during user
  101. * database enumeration. It decodes user records into user
  102. * objects, and presents them to the caller of userEnumerate().
  103. *
  104. * Arguments:
  105. *
  106. * errp - error frame list pointer (may be null)
  107. * parg - pointer to UserEnumArgs_t structure
  108. * namelen - user record key length including null
  109. * terminator
  110. * name - user record key (user account name)
  111. * reclen - length of user record
  112. * recptr - pointer to user record contents
  113. *
  114. * Returns:
  115. *
  116. * Returns whatever value is returned from the upcall to the caller
  117. * of userEnumerate().
  118. */
  119. static int userEnumHelp(NSErr_t * errp, void * parg,
  120. int namelen, char * name, int reclen, char * recptr)
  121. {
  122. UserEnumArgs_t * ue = (UserEnumArgs_t *)parg;
  123. UserObj_t * uoptr; /* user object pointer */
  124. int rv;
  125. uoptr = userDecode((NTS_t)name, reclen, (ATR_t)recptr);
  126. rv = (*ue->func)(errp, ue->user, uoptr);
  127. if (!(ue->flags & UOF_ENUMKEEP)) {
  128. userFree(uoptr);
  129. }
  130. return rv;
  131. }
  132. /*
  133. * Description (userEnumerate)
  134. *
  135. * This function enumerates all of the users in a specified user
  136. * database, calling a caller-specified function with a user object
  137. * for each user in the database. A 'flags' value of UOF_ENUMKEEP
  138. * can be specified to keep the user objects around (not free them)
  139. * after the caller's function returns. Otherwise, each user
  140. * object is freed after being presented to the caller's function.
  141. * The 'argp' argument is an opaque pointer, which is passed to
  142. * the caller's function as 'parg' on each call, along with a
  143. * user object pointer.
  144. *
  145. * Arguments:
  146. *
  147. * errp - error frame list pointer (may be null)
  148. * userdb - handle for user DB access
  149. * flags - bit flags:
  150. * UOF_ENUMKEEP - keep user objects
  151. * argp - passed to 'func' as 'parg'
  152. * func - pointer to caller's enumeration function
  153. *
  154. * Returns:
  155. *
  156. * If successful, the return value is zero. Otherwise it is a
  157. * non-zero error code, and an error frame is generated if an error
  158. * frame list was provided by the caller.
  159. */
  160. int userEnumerate(NSErr_t * errp, void * userdb, int flags, void * argp,
  161. int (*func)(NSErr_t * ferrp, void * parg, UserObj_t * uoptr))
  162. {
  163. int rv;
  164. UserEnumArgs_t args;
  165. args.userdb = userdb;
  166. args.flags = flags;
  167. args.func = func;
  168. args.user = argp;
  169. rv = ndbEnumerate(errp,
  170. userdb, NDBF_ENUMNORM, (void *)&args, userEnumHelp);
  171. return rv;
  172. }
  173. /*
  174. * Description (userFindByName)
  175. *
  176. * This function looks up a user record for a specified user account
  177. * name, converts the user record to the internal user object form,
  178. * and returns a pointer to the user object.
  179. *
  180. * Arguments:
  181. *
  182. * errp - error frame list pointer (may be null)
  183. * userdb - handle for user DB access
  184. * name - user account name to find
  185. *
  186. * Returns:
  187. *
  188. * If successful, the return value is a pointer to a user object
  189. * for the specified user. Otherwise it is 0, and an error frame
  190. * is generated if an error frame list was provided by the caller.
  191. */
  192. UserObj_t * userFindByName(NSErr_t * errp, void * userdb, NTS_t name)
  193. {
  194. UserObj_t * uoptr = 0;
  195. ATR_t urecptr;
  196. int ureclen;
  197. int rv;
  198. /* Look up the user name in the database */
  199. rv = ndbFindName(errp, userdb, 0, (char *) name, &ureclen, (char **)&urecptr);
  200. if (rv == 0) {
  201. /* Got the user record. Decode into a user object. */
  202. uoptr = userDecode(name, ureclen, urecptr);
  203. }
  204. return uoptr;
  205. }
  206. /*
  207. * Description (userFindByUid)
  208. *
  209. * This function looks up a user record for a specified user id,
  210. * converts the user record to the internal user object form, and
  211. * returns a pointer to the user object.
  212. *
  213. * Arguments:
  214. *
  215. * errp - error frame list pointer (may be null)
  216. * userdb - handle for user DB access
  217. * uid - user id to find
  218. *
  219. * Returns:
  220. *
  221. * If successful, the return value is a pointer to a user object
  222. * for the specified user. Otherwise it is 0, and an error frame
  223. * is generated if an error frame list was provided by the caller.
  224. */
  225. UserObj_t * userFindByUid(NSErr_t * errp, void * userdb, USI_t uid)
  226. {
  227. UserObj_t * uoptr = 0;
  228. NTS_t name;
  229. ATR_t urecptr;
  230. int ureclen;
  231. int rv;
  232. /* Get the user account name corresponding to the uid */
  233. rv = ndbIdToName(errp, userdb, uid, 0, (char **)&name);
  234. if (rv == 0) {
  235. rv = ndbFindName(errp, userdb, 0, (char *)name, &ureclen, (char **)&urecptr);
  236. if (rv == 0) {
  237. /* Got the user record. Decode into a user object. */
  238. uoptr = userDecode(name, ureclen, urecptr);
  239. }
  240. }
  241. return uoptr;
  242. }
  243. /*
  244. * Description (userFree)
  245. *
  246. * This function is called to free a user object. User objects
  247. * are not automatically freed when a user database is closed.
  248. *
  249. * Arguments:
  250. *
  251. * uoptr - user object pointer
  252. *
  253. */
  254. NSAPI_PUBLIC void userFree(UserObj_t * uoptr)
  255. {
  256. if (uoptr) {
  257. if (uoptr->uo_name) FREE(uoptr->uo_name);
  258. if (uoptr->uo_pwd) FREE(uoptr->uo_pwd);
  259. if (uoptr->uo_rname) FREE(uoptr->uo_rname);
  260. UILFREE(&uoptr->uo_groups);
  261. FREE(uoptr);
  262. }
  263. }