nick2ldif.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
00004 the GPL version 2 or any later version at your choice available at
00005 http://www.fsf.org/licenses/gpl.txt
00006 
00007 */
00008 
00009 #include <iostream>
00010 
00011 extern "C" {
00012     #include "define.h"
00013 }
00014 
00015 char *ldap_base  = NULL;
00016 char *ldap_org   = NULL;
00017 char *ldap_class = NULL;
00018 
00019 using namespace std;
00020 
00021 int main(int argc, char* const* argv) {
00022     char c;
00023     char *temp;
00024     while ((c = getopt(argc, argv, "b:c:"))!= -1) {
00025         switch (c) {
00026         case 'b':
00027             ldap_base = optarg;
00028             temp = strchr(ldap_base, ',');
00029             if (temp) {
00030                 *temp = '\0';
00031                 ldap_org = strdup(ldap_base);
00032                 *temp = ',';
00033             }
00034             break;
00035         case 'c':
00036             ldap_class = optarg;
00037             break;
00038         default:
00039             break;
00040         }
00041     }
00042 
00043     const int LINE_SIZE = 2000;
00044     char line[LINE_SIZE];
00045     while (!cin.eof()) {
00046         cin.getline(line, LINE_SIZE);
00047         int n = strlen(line);
00048         if (!n) continue;
00049         if (strncmp(line, "alias", 5) != 0) continue;   // not alias
00050         char *f = line + 6;     // skip alias keyword
00051         char *e;
00052         if (*f == '"') {
00053             f++;
00054             e = strchr(f, '"');
00055         }
00056         else {
00057             e = strchr(f, ' ');
00058         }
00059         if (!e) continue;
00060         *e = '\0';
00061         char *m = e+1;
00062         while (*m == ' ') m++;
00063         if (*m != '\0') {
00064             char cn[1000], givenName[1000], sn[1000];
00065             snprintf(cn, sizeof(cn), "%s", f);
00066             char *ff = strchr(f, ' ');
00067             if (ff) {
00068                 strncpy(givenName, ff+1, sizeof(givenName)-1);
00069                 *ff = '\0';
00070                 strncpy(sn, f, sizeof(sn)-1);
00071             }
00072             else {
00073                 strcpy(givenName, cn);
00074                 strcpy(sn, cn);
00075             }
00076             printf("dn: cn=%s, %s\n", cn, ldap_base);
00077             printf("cn: %s\n", cn);
00078             printf("givenName: %s\n", givenName);
00079             printf("sn: %s\n", sn);
00080             printf("mail: %s\n", m);
00081             printf("objectClass: %s\n\n", ldap_class);
00082         }
00083     }
00084 }

Generated on 6 Jul 2016 for 'LibPst' by  doxygen 1.6.1