PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ncpfs-2.2.6/lib/o_ndslib.c

#
C | 272 lines | 213 code | 22 blank | 37 comment | 48 complexity | f7cc93c58c961f0ff3bd3efcf9976dc0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. NDS client for ncpfs
  3. Copyright (C) 1997 Arne de Bruijn
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. Revision history:
  16. 1.00 1999, November 20 Petr Vandrovec <vandrove@vc.cvut.cz>
  17. Mark these calls obsolete.
  18. Modify them to use new API.
  19. */
  20. #define NCP_OBSOLETE
  21. extern int bindery_only;
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include <unistd.h>
  26. #include <sys/time.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include "nwnet_i.h"
  30. int strlen_u(const uni_char *s) {
  31. return unilen(s);
  32. }
  33. uni_char getchr_u(const uni_char* s) {
  34. return WVAL_LH((const void*)s, 0);
  35. }
  36. void strcpy_uc(char *d, const uni_char *s) {
  37. while ((*d++ = getchr_u(s++)) != 0);
  38. }
  39. void strcpy_cu(uni_char *d, const char *s) {
  40. do {
  41. WSET_LH((void*)(d++), 0, *s);
  42. } while (*s++);
  43. }
  44. long nds_get_server_name(struct ncp_conn *conn, uni_char **server_name) {
  45. NWDSContextHandle ctx;
  46. NWDSCCODE err;
  47. void* outbuf;
  48. u_int32_t flags;
  49. outbuf = malloc(4096);
  50. if (!outbuf)
  51. return ENOMEM;
  52. err = NWDSCreateContextHandle(&ctx);
  53. if (err)
  54. goto q_mem;
  55. flags = 0;
  56. err = NWDSSetContext(ctx, DCK_FLAGS, &flags);
  57. if (err)
  58. goto q_ctx;
  59. err = NWDSGetServerDN(ctx, conn, outbuf);
  60. if (!err) {
  61. *server_name = outbuf;
  62. NWDSFreeContext(ctx);
  63. return 0;
  64. }
  65. q_ctx:;
  66. NWDSFreeContext(ctx);
  67. q_mem:;
  68. free(outbuf);
  69. return err;
  70. }
  71. long nds_get_tree_name(struct ncp_conn *conn, char *name, int name_buf_len) {
  72. char buf[MAX_TREE_NAME_CHARS+1];
  73. if (bindery_only) return -1;
  74. if (!NWIsDSServer(conn, buf))
  75. return -1;
  76. if (name) {
  77. size_t size;
  78. char* p;
  79. p = strchr(buf, '\0') - 1;
  80. while ((p >= buf) && (*p == '_'))
  81. p--;
  82. size = p - buf + 1;
  83. if (size >= (size_t)name_buf_len)
  84. return -1;
  85. memcpy(name, buf, size);
  86. name[size] = 0;
  87. }
  88. return 0;
  89. }
  90. /* for login */
  91. long nds_resolve_name(struct ncp_conn *conn, int flags, uni_char *entry_name,
  92. int *entry_id, int *remote, UNUSED(struct sockaddr *serv_addr), size_t *addr_len) {
  93. NWDSCCODE err;
  94. NWDSContextHandle ctx;
  95. u_int32_t ctxflags;
  96. u_int32_t replytype;
  97. Buf_T* rp;
  98. err = NWDSCreateContextHandle(&ctx);
  99. if (err)
  100. return err;
  101. flags = 0;
  102. err = NWDSSetContext(ctx, DCK_FLAGS, &ctxflags);
  103. if (err)
  104. goto q_ctx;
  105. err = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &rp);
  106. if (err)
  107. goto q_ctx;
  108. err = NWDSResolveNameInt(ctx, conn, DS_RESOLVE_V0, flags, (const NWDSChar*)entry_name, rp);
  109. if (err)
  110. goto q_buf;
  111. err = NWDSBufGetLE32(rp, &replytype);
  112. if (err)
  113. goto q_buf;
  114. switch (replytype) {
  115. case DS_RESOLVE_REPLY_LOCAL_ENTRY:
  116. {
  117. NWObjectID ID;
  118. err = NWDSBufGetID(rp, &ID);
  119. if (!err) {
  120. if (entry_id)
  121. *entry_id = ID;
  122. if (remote)
  123. *remote = 0;
  124. }
  125. }
  126. break;
  127. case DS_RESOLVE_REPLY_REMOTE_ENTRY:
  128. {
  129. NWObjectID ID;
  130. err = NWDSBufGetID(rp, &ID);
  131. if (err)
  132. goto q_buf;
  133. if (entry_id)
  134. *entry_id = ID;
  135. err = NWDSBufGetLE32(rp, &flags);
  136. if (err)
  137. goto q_buf;
  138. if (remote)
  139. *remote = 1;
  140. /* we do not support address passing... use NWDSResolveName instead */
  141. if (addr_len)
  142. *addr_len = 0;
  143. break;
  144. }
  145. break;
  146. default:
  147. err = ERR_INVALID_SERVER_RESPONSE;
  148. break;
  149. }
  150. q_buf:;
  151. NWDSFreeBuf(rp);
  152. q_ctx:;
  153. NWDSFreeContext(ctx);
  154. return err;
  155. }
  156. long nds_read(struct ncp_conn *conn, u_int32_t obj_id, uni_char *propname,
  157. u_int32_t* type, void **outbuf, size_t *outlen) {
  158. NWDSContextHandle ctx;
  159. NWDSCCODE err;
  160. u_int32_t flags;
  161. Buf_T* attr;
  162. Buf_T* rp;
  163. u_int32_t iterHandle = NO_MORE_ITERATIONS;
  164. NWObjectCount cnt;
  165. enum SYNTAX synt;
  166. size_t size;
  167. Octet_String_T* buf;
  168. err = NWDSCreateContextHandle(&ctx);
  169. if (err)
  170. return err;
  171. flags = 0;
  172. err = NWDSSetContext(ctx, DCK_FLAGS, &flags);
  173. if (err)
  174. goto q_ctx;
  175. err = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &attr);
  176. if (err)
  177. goto q_ctx;
  178. err = NWDSInitBuf(ctx, DSV_READ, attr);
  179. if (err)
  180. goto q_attr;
  181. err = NWDSPutAttrName(ctx, attr, (const NWDSChar*)propname);
  182. if (err)
  183. goto q_attr;
  184. err = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &rp);
  185. if (err)
  186. goto q_attr;
  187. err = __NWDSReadV1(conn, 0, obj_id, DS_ATTRIBUTE_VALUES, 0, attr, &iterHandle, NULL, rp);
  188. if (err)
  189. goto q_rp;
  190. err = NWDSGetAttrCount(ctx, rp, &cnt);
  191. if (err)
  192. goto q_rp;
  193. /* we asked for one attribute, so we expect one attribute */
  194. if (cnt != 1) {
  195. err = ERR_INVALID_SERVER_RESPONSE;
  196. goto q_rp;
  197. }
  198. err = NWDSGetAttrName(ctx, rp, NULL, &cnt, &synt);
  199. if (err)
  200. goto q_rp;
  201. /* we do not support multivalued attributes */
  202. if (cnt != 1) {
  203. err = ERR_INVALID_SERVER_RESPONSE;
  204. goto q_rp;
  205. }
  206. if (type)
  207. *type = synt;
  208. /* retrieve it as raw string */
  209. err = NWDSComputeAttrValSize(ctx, rp, SYN_OCTET_STRING, &size);
  210. if (err)
  211. goto q_rp;
  212. buf = (Octet_String_T*)malloc(size);
  213. if (!buf) {
  214. err = ENOMEM;
  215. goto q_rp;
  216. }
  217. err = NWDSGetAttrVal(ctx, rp, SYN_OCTET_STRING, buf);
  218. if (err) {
  219. free(buf);
  220. goto q_rp;
  221. }
  222. if (outlen)
  223. *outlen = buf->length;
  224. /* move buffer to the start of allocated region */
  225. if (outbuf) {
  226. *outbuf = buf;
  227. memmove(buf, buf->data, buf->length);
  228. } else {
  229. free(buf);
  230. }
  231. q_rp:;
  232. NWDSFreeBuf(rp);
  233. q_attr:
  234. NWDSFreeBuf(attr);
  235. q_ctx:
  236. NWDSFreeContext(ctx);
  237. return err;
  238. }
  239. /* this lives in ndslib.c... for now...
  240. long nds_login_auth(
  241. struct ncp_conn *conn,
  242. const char *user,
  243. const char *pwd);
  244. */