nsumgmt.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 (nsumgmt.c)
  8. *
  9. * This module contains routines for managing information in a
  10. * Netscape user database. Information for a particular user
  11. * is modified by retrieving the current information in the form
  12. * of a user object (UserObj_t), calling functions in this module,
  13. * to modify the user object, and then calling userStore() to
  14. * write the information in the user object back to the database.
  15. */
  16. #include "base/systems.h"
  17. #include "netsite.h"
  18. #include "assert.h"
  19. #include "libaccess/nsdbmgmt.h"
  20. #define __PRIVATE_NSUSER
  21. #include "libaccess/nsumgmt.h"
  22. /*
  23. * Description (userAddGroup)
  24. *
  25. * This function adds a group id to the list of group ids associated
  26. * with a user object.
  27. *
  28. * Arguments:
  29. *
  30. * uoptr - user object pointer
  31. * gid - group id to be added
  32. *
  33. * Returns:
  34. *
  35. * Returns zero if the group id is already present in the group id list.
  36. * Returns one if the group id was added successfully.
  37. * Returns a negative value if an error occurs.
  38. */
  39. int userAddGroup(UserObj_t * uoptr, USI_t gid)
  40. {
  41. int rv;
  42. rv = usiInsert(&uoptr->uo_groups, gid);
  43. if (rv > 0) {
  44. uoptr->uo_flags |= UOF_MODIFIED;
  45. }
  46. return rv;
  47. }
  48. /*
  49. * Description (userCreate)
  50. *
  51. * This function creates a user object, using information about
  52. * the user provided by the caller. The strings passed for the
  53. * user account name, password, and real user name may be on the
  54. * stack. The user id is set to zero, but the user object is
  55. * marked as being new. A user id will be assigned when
  56. * userStore() is called to add the user to a user database.
  57. *
  58. * Arguments:
  59. *
  60. * name - pointer to user account name string
  61. * pwd - pointer to (encrypted) password string
  62. * rname - real user name (gecos string)
  63. *
  64. * Returns:
  65. *
  66. * A pointer to a dynamically allocated UserObj_t structure is
  67. * returned.
  68. */
  69. NSAPI_PUBLIC UserObj_t * userCreate(NTS_t name, NTS_t pwd, NTS_t rname)
  70. {
  71. UserObj_t * uoptr; /* user object pointer */
  72. uoptr = (UserObj_t *)MALLOC(sizeof(UserObj_t));
  73. if (uoptr) {
  74. uoptr->uo_name = (NTS_t)STRDUP((char *)name);
  75. uoptr->uo_pwd = (pwd) ? (NTS_t)STRDUP((char *)pwd) : 0;
  76. uoptr->uo_uid = 0;
  77. uoptr->uo_flags = (UOF_MODIFIED | UOF_NEW);
  78. uoptr->uo_rname = (rname) ? (NTS_t)STRDUP((char *)rname) : 0;
  79. UILINIT(&uoptr->uo_groups);
  80. }
  81. return uoptr;
  82. }
  83. /*
  84. * Description (userDeleteGroup)
  85. *
  86. * This function removes a specified group id from a user object's
  87. * list of groups.
  88. *
  89. * Arguments:
  90. *
  91. * uoptr - pointer to user object
  92. * gid - group id to remove
  93. *
  94. * Returns:
  95. *
  96. * The return value is zero if the specified group id was not present
  97. * in the user object, or one if the group was successfully removed.
  98. */
  99. int userDeleteGroup(UserObj_t * uoptr, USI_t gid)
  100. {
  101. int rv; /* return value */
  102. rv = usiRemove(&uoptr->uo_groups, gid);
  103. if (rv > 0) {
  104. uoptr->uo_flags |= UOF_MODIFIED;
  105. }
  106. return rv;
  107. }
  108. /*
  109. * Description (userEncode)
  110. *
  111. * This function encodes a user object into a user DB record.
  112. *
  113. * Arguments:
  114. *
  115. * uoptr - pointer to user object
  116. * ureclen - pointer to returned record length
  117. * urecptr - pointer to returned record pointer
  118. *
  119. * Returns:
  120. *
  121. * The function return value is zero if successful. The length
  122. * and location of the created attribute record are returned
  123. * through 'ureclen' and 'urecptr'. A non-zero function value
  124. * is returned if there's an error.
  125. */
  126. int userEncode(UserObj_t * uoptr, int * ureclen, ATR_t * urecptr)
  127. {
  128. int reclen; /* length of DB record */
  129. ATR_t rptr; /* DB record pointer */
  130. ATR_t rstart = 0; /* pointer to beginning of DB record */
  131. ATR_t glptr; /* saved pointer to UAT_GROUPS length */
  132. ATR_t gptr; /* saved pointer to after length at glptr */
  133. int pwdlen; /* password encoding length */
  134. int uidlen; /* uid encoding length */
  135. int fllen; /* account flags encoding length */
  136. USI_t rnlen; /* real name encoding length */
  137. USI_t nglen; /* group count encoding length */
  138. USI_t gcnt; /* number of group ids */
  139. USI_t * gids; /* pointer to array of group ids */
  140. int i; /* group id index */
  141. int rv = -1;
  142. /*
  143. * First we need to figure out how long the generated record will be.
  144. * This doesn't have to be exact, but it must not be smaller than the
  145. * actual record size.
  146. */
  147. /* UAT_PASSWORD attribute: tag, length, NTS */
  148. pwdlen = NTSLENGTH(uoptr->uo_pwd);
  149. reclen = 1 + 1 + pwdlen;
  150. if (pwdlen > 127) goto punt;
  151. /* UAT_UID attribute: tag, length, USI */
  152. uidlen = USILENGTH(uoptr->uo_uid);
  153. reclen += (1 + 1 + uidlen);
  154. /* UAT_ACCFLAGS attribute: tag, length, USI */
  155. fllen = USILENGTH(uoptr->uo_flags & UOF_DBFLAGS);
  156. reclen += (1 + 1 + fllen);
  157. /* UAT_REALNAME attribute: tag, length, NTS */
  158. rnlen = NTSLENGTH(uoptr->uo_rname);
  159. reclen += (1 + USILENGTH(rnlen) + rnlen);
  160. /* UAT_GROUPS attribute: tag, length, USI(count), USI(gid)... */
  161. gcnt = UILCOUNT(&uoptr->uo_groups);
  162. nglen = USILENGTH(gcnt);
  163. reclen += (1 + USIALLOC() + nglen + (5 * gcnt));
  164. /* Allocate the attribute record buffer */
  165. rptr = (ATR_t)MALLOC(reclen);
  166. if (rptr) {
  167. /* Save pointer to start of record */
  168. rstart = rptr;
  169. /* Encode UAT_PASSWORD attribute */
  170. *rptr++ = UAT_PASSWORD;
  171. *rptr++ = pwdlen;
  172. rptr = NTSENCODE(rptr, uoptr->uo_pwd);
  173. /* Encode UAT_UID attribute */
  174. *rptr++ = UAT_UID;
  175. *rptr++ = uidlen;
  176. rptr = USIENCODE(rptr, uoptr->uo_uid);
  177. /* Encode UAT_ACCFLAGS attribute */
  178. *rptr++ = UAT_ACCFLAGS;
  179. *rptr++ = fllen;
  180. rptr = USIENCODE(rptr, (uoptr->uo_flags & UOF_DBFLAGS));
  181. /* Encode UAT_REALNAME attribute */
  182. *rptr++ = UAT_REALNAME;
  183. rptr = USIENCODE(rptr, rnlen);
  184. rptr = NTSENCODE(rptr, uoptr->uo_rname);
  185. /* Encode UAT_GROUPS attribute */
  186. *rptr++ = UAT_GROUPS;
  187. /*
  188. * Save a pointer to the attribute encoding length, and reserve
  189. * space for the maximum encoding size of a USI_t value.
  190. */
  191. glptr = rptr;
  192. rptr += USIALLOC();
  193. gptr = rptr;
  194. /* Encode number of groups */
  195. rptr = USIENCODE(rptr, gcnt);
  196. /* Generate group ids encodings */
  197. gids = UILLIST(&uoptr->uo_groups);
  198. for (i = 0; i < gcnt; ++i) {
  199. rptr = USIENCODE(rptr, gids[i]);
  200. }
  201. /* Now fix up the UAT_GROUPS attribute encoding length */
  202. glptr = USIINSERT(glptr, (USI_t)(rptr - gptr));
  203. /* Return record length and location if requested */
  204. if (ureclen) *ureclen = rptr - rstart;
  205. if (urecptr) *urecptr = rstart;
  206. /* Indicate success */
  207. rv = 0;
  208. }
  209. punt:
  210. return rv;
  211. }
  212. /*
  213. * Description (userRemove)
  214. *
  215. * This function is called to remove a user from a specified user
  216. * database. Both the primary DB file and the id-to-name DB file
  217. * are updated.
  218. *
  219. * Arguments:
  220. *
  221. * errp - error frame list pointer (may be null)
  222. * userdb - handle for user DB access
  223. * flags - (unused - must be zero)
  224. * name - pointer to user account name
  225. *
  226. * Returns:
  227. *
  228. * If successful, the return value is zero. Otherwise it is a
  229. * non-zero error code.
  230. */
  231. NSAPI_PUBLIC int userRemove(NSErr_t * errp, void * userdb, int flags, NTS_t name)
  232. {
  233. UserObj_t * uoptr; /* user object pointer */
  234. int rv;
  235. int rv2;
  236. /* First retrieve the user record */
  237. uoptr = userFindByName(errp, userdb, name);
  238. if (!uoptr) {
  239. /* Error - specified user not found */
  240. return NSAERRNAME;
  241. }
  242. /* Free the user id value, if any */
  243. rv = 0;
  244. if (uoptr->uo_uid != 0) {
  245. rv = ndbFreeId(errp, userdb, 0, (char *)name, uoptr->uo_uid);
  246. }
  247. rv2 = ndbDeleteName(errp, userdb, 0, 0, (char *)name);
  248. return (rv) ? rv : rv2;
  249. }
  250. /*
  251. * Description (userRename)
  252. *
  253. * This function is called to change the account name associated
  254. * with an existing user. The caller provides a pointer to a
  255. * user object for the existing user (with the current user account
  256. * name referenced by uo_name), and the new account name for this
  257. * user. A check is made to ensure the uniqueness of the new name
  258. * in the specified user database. The account name in the user
  259. * object is modified. The user database is not modified until
  260. * userStore() is called.
  261. *
  262. * Arguments:
  263. *
  264. * errp - error frame list pointer (may be null)
  265. * userdb - handle for user DB access
  266. * uoptr - user object pointer
  267. * newname - pointer to new account name string
  268. *
  269. * Returns:
  270. *
  271. * If successful, the return value is zero. Otherwise it is a
  272. * non-zero error code. The user object remains intact in either
  273. * case.
  274. */
  275. NSAPI_PUBLIC int userRename(NSErr_t * errp, void * userdb, UserObj_t * uoptr, NTS_t newname)
  276. {
  277. int reclen; /* user record length */
  278. ATR_t recptr = 0; /* user record pointer */
  279. char * oldname; /* old user account name */
  280. int eid; /* error id code */
  281. int rv; /* result value */
  282. /* Save the current account name and replace it with the new one */
  283. oldname = (char *)uoptr->uo_name;
  284. uoptr->uo_name = (unsigned char *) STRDUP((char *)newname);
  285. if ((oldname != 0) && !(uoptr->uo_flags & UOF_NEW)) {
  286. /* Convert the information in the user object to a DB record */
  287. rv = userEncode(uoptr, &reclen, &recptr);
  288. if (rv) goto err_nomem;
  289. /*
  290. * Store the record in the database
  291. * under the new user account name.
  292. */
  293. rv = ndbStoreName(errp, userdb, NDBF_NEWNAME,
  294. 0, (char *)uoptr->uo_name, reclen, (char *)recptr);
  295. if (rv) goto punt;
  296. /* Change the mapping of the user id to the new name */
  297. rv = ndbRenameId(errp, userdb, 0, (char *)uoptr->uo_name, uoptr->uo_uid);
  298. if (rv) goto punt;
  299. /* Delete the user record with the old account name */
  300. rv = ndbDeleteName(errp, userdb, 0, 0, oldname);
  301. if (rv) goto punt;
  302. }
  303. else {
  304. /* Set flags in user object for userStore() */
  305. uoptr->uo_flags |= UOF_MODIFIED;
  306. }
  307. punt:
  308. if (recptr) {
  309. FREE(recptr);
  310. }
  311. if (oldname) {
  312. FREE(oldname);
  313. }
  314. return rv;
  315. err_nomem:
  316. eid = NSAUERR1000;
  317. rv = NSAERRNOMEM;
  318. nserrGenerate(errp, rv, eid, NSAuth_Program, 0);
  319. goto punt;
  320. }
  321. /*
  322. * Description (userStore)
  323. *
  324. * This function is called to store a user object in the database.
  325. * If the object was created by userCreate(), it is assumed to be
  326. * a new user account, the user account name must not match any
  327. * existing user account names in the database, and a uid is
  328. * assigned before adding the user to the database. If the object
  329. * was created by userFindByName(), the information in the user
  330. * object will replace the existing database entry for the
  331. * indicated user account name.
  332. *
  333. * Arguments:
  334. *
  335. * errp - error frame list pointer (may be null)
  336. * userdb - handle for user DB access
  337. * flags - (unused - must be zero)
  338. * uoptr - user object pointer
  339. *
  340. * Returns:
  341. *
  342. * If successful, the return value is zero. Otherwise it is a
  343. * non-zero error code. The user object remains intact in either
  344. * case.
  345. */
  346. NSAPI_PUBLIC int userStore(NSErr_t * errp, void * userdb, int flags, UserObj_t * uoptr)
  347. {
  348. ATR_t recptr = 0;
  349. USI_t uid;
  350. int reclen = 0;
  351. int stflags = 0;
  352. int eid;
  353. int rv;
  354. /* If this is a new user, allocate a uid value */
  355. if (uoptr->uo_flags & UOF_NEW) {
  356. /*
  357. * Yes, allocate a user id and add a user id to user
  358. * account name mapping to the id-to-name DB file.
  359. */
  360. uid = 0;
  361. rv = ndbAllocId(errp, userdb, 0, (char *)uoptr->uo_name, &uid);
  362. if (rv) goto punt;
  363. uoptr->uo_uid = uid;
  364. /* Let the database manager know that this is a new entry */
  365. stflags = NDBF_NEWNAME;
  366. }
  367. /* Convert the information in the user object to a DB record */
  368. rv = userEncode(uoptr, &reclen, &recptr);
  369. if (rv) goto err_nomem;
  370. /* Store the record in the database under the user account name. */
  371. rv = ndbStoreName(errp, userdb, stflags,
  372. 0, (char *)uoptr->uo_name, reclen, (char *)recptr);
  373. if (rv) goto punt;
  374. FREE(recptr);
  375. recptr = 0;
  376. uoptr->uo_flags &= ~(UOF_NEW | UOF_MODIFIED);
  377. return 0;
  378. err_nomem:
  379. eid = NSAUERR1100;
  380. rv = NSAERRNOMEM;
  381. nserrGenerate(errp, rv, eid, NSAuth_Program, 0);
  382. punt:
  383. if (recptr) {
  384. FREE(recptr);
  385. }
  386. if ((uoptr->uo_flags & UOF_NEW) && (uid != 0)) {
  387. /* Free the user id value if we failed after allocating it */
  388. ndbFreeId(errp, userdb, 0, (char *)uoptr->uo_name, uid);
  389. }
  390. return rv;
  391. }