vcard.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /** --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. --- END COPYRIGHT BLOCK --- */
  38. /*
  39. * vcard.c -- vCard utility functions -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #include "dbtdsgw.h"
  43. #include "ldif.h"
  44. static int entry2vcard( LDAP *ld, char *dn, char *mimetype,
  45. dsgwvcprop *vcprops, char **lderrtxtp );
  46. static void write_vcard_property( char *prop, char *val, char *val2,
  47. int is_mls );
  48. static void emit_vcard_headers( char *mimetype );
  49. static char **vcard_ldapattrs( dsgwvcprop *vcprops );
  50. static void dsgw_puts( char *s );
  51. #define DSGW_VCARD_MIMEHDR_TEXTDIR "text/directory;profile=vcard"
  52. #define DSGW_VCARD_MIMEHDR_XVCARD "text/x-vcard"
  53. #define DSGW_VCARD_VERSION "2.1"
  54. #define DSGW_VCARD_PROP_VERSION "VERSION"
  55. #define DSGW_VCARD_PROP_BEGIN "BEGIN"
  56. #define DSGW_VCARD_PROP_END "END"
  57. #define DSGW_VCARD_BEGINEND_VALUE "vCard"
  58. void
  59. dsgw_vcard_from_entry( LDAP *ld, char *dn, char *mimetype )
  60. {
  61. int lderr;
  62. char *lderrtxt;
  63. if (( lderr = entry2vcard( ld, dn, mimetype, gc->gc_vcardproperties,
  64. &lderrtxt )) != LDAP_SUCCESS ) {
  65. dsgw_error( DSGW_ERR_LDAPGENERAL, NULL, DSGW_ERROPT_EXIT, lderr,
  66. lderrtxt );
  67. }
  68. }
  69. /*
  70. * Retrieve the LDAP entry "dn" and write a vCard representation of it
  71. * to stdout.
  72. */
  73. static int
  74. entry2vcard( LDAP *ld, char *dn, char *mimetype, dsgwvcprop *vcprops,
  75. char **lderrtxtp )
  76. {
  77. int i, rc, is_mls;
  78. char **ldattrs, **vals, **vals2;
  79. dsgwvcprop *vcp;
  80. LDAPMessage *msgp, *entry;
  81. ldattrs = vcard_ldapattrs( vcprops );
  82. /* Read the entry. */
  83. if (( rc = ldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
  84. ldattrs, 0, &msgp )) != LDAP_SUCCESS ) {
  85. (void)ldap_get_lderrno( ld, NULL, lderrtxtp );
  86. return( rc );
  87. }
  88. if (( entry = ldap_first_entry( ld, msgp )) == NULL ) {
  89. ldap_msgfree( msgp );
  90. return( ldap_get_lderrno( ld, NULL, lderrtxtp ));
  91. }
  92. /*
  93. * Output the vCard headers plus the BEGIN marker and VERSION tag.
  94. * once we do this we are committed to producing a vCard MIME object
  95. * so we must return LDAP_SUCCESS.
  96. */
  97. emit_vcard_headers( mimetype );
  98. write_vcard_property( DSGW_VCARD_PROP_BEGIN, DSGW_VCARD_BEGINEND_VALUE,
  99. NULL, 0 );
  100. write_vcard_property( DSGW_VCARD_PROP_VERSION, DSGW_VCARD_VERSION,
  101. NULL, 0 );
  102. /* Output the properties.
  103. * Note that for the secondary LDAP attribute we only use the
  104. * first value returned by the server. I am sure someone won't
  105. * like this but anything else is silly since the main vCard
  106. * property we use a secondary LDAP attribute for is the "N"
  107. * property which looks like "sn;givenName". We really have no way
  108. * of knowing which surname goes with which givenName so it looks
  109. * better not to create lots of "N" properties if there are multiple
  110. * givenNames.
  111. */
  112. for ( vcp = vcprops; vcp != NULL; vcp = vcp->dsgwvcprop_next ) {
  113. vals = ldap_get_values( ld, entry, vcp->dsgwvcprop_ldaptype );
  114. if ( vcp->dsgwvcprop_ldaptype2 == NULL ) {
  115. vals2 = NULL;
  116. } else {
  117. vals2 = ldap_get_values( ld, entry, vcp->dsgwvcprop_ldaptype2 );
  118. }
  119. if ( vals == NULL && vals2 == NULL ) {
  120. continue;
  121. }
  122. is_mls = ( strcmp( vcp->dsgwvcprop_syntax, "mls" ) == 0 );
  123. if ( vals != NULL ) {
  124. for ( i = 0; vals[ i ] != NULL; ++i ) {
  125. write_vcard_property( vcp->dsgwvcprop_property,
  126. vals[i], vals2 == NULL ? NULL : vals2[0], is_mls );
  127. }
  128. } else {
  129. for ( i = 0; vals2[ i ] != NULL; ++i ) {
  130. write_vcard_property( vcp->dsgwvcprop_property,
  131. NULL, vals2[i], is_mls );
  132. }
  133. }
  134. if ( vals != NULL ) {
  135. ldap_value_free( vals );
  136. }
  137. if ( vals2 != NULL ) {
  138. ldap_value_free( vals2 );
  139. }
  140. }
  141. /* Output the vCard END marker. */
  142. write_vcard_property( DSGW_VCARD_PROP_END, DSGW_VCARD_BEGINEND_VALUE,
  143. NULL, 0 );
  144. /* Cleanup after ourselves. */
  145. ldap_msgfree( msgp );
  146. return( LDAP_SUCCESS );
  147. }
  148. /*
  149. * output a single vCard text property.
  150. */
  151. static void
  152. write_vcard_property( char *prop, char *val, char *val2, int is_mls )
  153. {
  154. char *s, *p, *tmpv, *mlsv;
  155. tmpv = mlsv = NULL;
  156. if ( val == NULL ) {
  157. val = "";
  158. }
  159. if ( val2 != NULL ) {
  160. tmpv = (char *)dsgw_ch_malloc( strlen( val ) + strlen( val2 ) + 2 );
  161. sprintf( tmpv, "%s;%s", val, val2 );
  162. val = tmpv;
  163. }
  164. if ( is_mls ) {
  165. val = mlsv = dsgw_mls_convertlines( val, ";", NULL, 0, 0 );
  166. }
  167. if (( s = ldif_type_and_value( prop, val, strlen( val ))) != NULL ) {
  168. /*
  169. * vCard base64 rules are different than for LDIF so check and repair
  170. * if necessary.
  171. */
  172. if (( p = strchr( s, ':' )) != NULL && *(p+1) == ':' ) {
  173. *p++ = '\0'; ++p;
  174. dsgw_emits( s );
  175. dsgw_emits( ";BASE64:\n " );
  176. dsgw_emits( p );
  177. dsgw_emits( "\n" );
  178. } else {
  179. dsgw_emits( s );
  180. }
  181. free( s );
  182. }
  183. if ( tmpv != NULL ) {
  184. free( tmpv );
  185. }
  186. if ( mlsv != NULL ) {
  187. free( mlsv );
  188. }
  189. }
  190. /*
  191. * emit vCard Content-Type header, etc.
  192. */
  193. static void
  194. emit_vcard_headers( char *mimetype )
  195. {
  196. if ( mimetype == NULL || *mimetype == '\0' ) {
  197. mimetype = DSGW_VCARD_MIMEHDR_TEXTDIR; /* default */
  198. }
  199. dsgw_puts( "Content-Type: " );
  200. dsgw_puts( mimetype );
  201. if ( gc->gc_charset != NULL && *gc->gc_charset != '\0' ) {
  202. dsgw_puts( ";charset=" );
  203. dsgw_puts( gc->gc_charset );
  204. }
  205. dsgw_puts( "\n\n" );
  206. }
  207. /*
  208. * output a simple string without charset conversion (used for MIME headers)
  209. */
  210. static void
  211. dsgw_puts( char *s )
  212. {
  213. dsgw_fputn( stdout, s, strlen( s ));
  214. }
  215. /*
  216. * return list of LDAP attributes we need to fetch
  217. */
  218. static char **
  219. vcard_ldapattrs( dsgwvcprop *vcprops )
  220. {
  221. dsgwvcprop *vcp;
  222. int count;
  223. static char **attrs = NULL;
  224. if ( attrs != NULL ) {
  225. return( attrs );
  226. }
  227. count = 0;
  228. for ( vcp = vcprops; vcp != NULL; vcp = vcp->dsgwvcprop_next ) {
  229. ++count;
  230. if ( vcp->dsgwvcprop_ldaptype2 != NULL ) {
  231. ++count;
  232. }
  233. }
  234. attrs = (char **)dsgw_ch_malloc(( count + 1 ) * sizeof( char * ));
  235. count = 0;
  236. for ( vcp = vcprops; vcp != NULL; vcp = vcp->dsgwvcprop_next ) {
  237. attrs[ count++ ] = vcp->dsgwvcprop_ldaptype;
  238. if ( vcp->dsgwvcprop_ldaptype2 != NULL ) {
  239. attrs[ count++ ] = vcp->dsgwvcprop_ldaptype2;
  240. }
  241. }
  242. attrs[ count ] = NULL;
  243. return( attrs );
  244. }