credentials: Move code that doesn't need any external dependencies into
authorJelmer Vernooij <jelmer@samba.org>
Sun, 10 Oct 2010 23:20:42 +0000 (01:20 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 10 Oct 2010 23:54:04 +0000 (23:54 +0000)
credentials.c.

source4/auth/credentials/credentials.c
source4/auth/credentials/credentials_files.c
source4/auth/credentials/credentials_ntlm.c

index e1965b3f84ec5d68a40c3a78673f810e50835791..30ab46d0adf73ecd31f79747af11011b69e62bd2 100644 (file)
@@ -29,6 +29,7 @@
 #include "libcli/auth/libcli_auth.h"
 #include "lib/events/events.h"
 #include "param/param.h"
+#include "system/filesys.h"
 
 /**
  * Create a new credentials structure
@@ -853,3 +854,149 @@ _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
 
        return (cred->tries > 0);
 }
+
+_PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
+                                             const char **username, 
+                                             const char **domain) 
+{
+       if (cred->principal_obtained > cred->username_obtained) {
+               *domain = talloc_strdup(mem_ctx, "");
+               *username = cli_credentials_get_principal(cred, mem_ctx);
+       } else {
+               *domain = cli_credentials_get_domain(cred);
+               *username = cli_credentials_get_username(cred);
+       }
+}
+
+/**
+ * Read a named file, and parse it for username, domain, realm and password
+ *
+ * @param credentials Credentials structure on which to set the password
+ * @param file a named file to read the details from 
+ * @param obtained This enum describes how 'specified' this password is
+ */
+
+_PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained) 
+{
+       uint16_t len = 0;
+       char *ptr, *val, *param;
+       char **lines;
+       int i, numlines;
+
+       lines = file_lines_load(file, &numlines, 0, NULL);
+
+       if (lines == NULL)
+       {
+               /* fail if we can't open the credentials file */
+               d_printf("ERROR: Unable to open credentials file!\n");
+               return false;
+       }
+
+       for (i = 0; i < numlines; i++) {
+               len = strlen(lines[i]);
+
+               if (len == 0)
+                       continue;
+
+               /* break up the line into parameter & value.
+                * will need to eat a little whitespace possibly */
+               param = lines[i];
+               if (!(ptr = strchr_m (lines[i], '=')))
+                       continue;
+
+               val = ptr+1;
+               *ptr = '\0';
+
+               /* eat leading white space */
+               while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
+                       val++;
+
+               if (strwicmp("password", param) == 0) {
+                       cli_credentials_set_password(cred, val, obtained);
+               } else if (strwicmp("username", param) == 0) {
+                       cli_credentials_set_username(cred, val, obtained);
+               } else if (strwicmp("domain", param) == 0) {
+                       cli_credentials_set_domain(cred, val, obtained);
+               } else if (strwicmp("realm", param) == 0) {
+                       cli_credentials_set_realm(cred, val, obtained);
+               }
+               memset(lines[i], 0, len);
+       }
+
+       talloc_free(lines);
+
+       return true;
+}
+
+/**
+ * Read a named file, and parse it for a password
+ *
+ * @param credentials Credentials structure on which to set the password
+ * @param file a named file to read the password from 
+ * @param obtained This enum describes how 'specified' this password is
+ */
+
+_PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
+{
+       int fd = open(file, O_RDONLY, 0);
+       bool ret;
+
+       if (fd < 0) {
+               fprintf(stderr, "Error opening password file %s: %s\n",
+                               file, strerror(errno));
+               return false;
+       }
+
+       ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
+
+       close(fd);
+       
+       return ret;
+}
+
+
+/**
+ * Read a file descriptor, and parse it for a password (eg from a file or stdin)
+ *
+ * @param credentials Credentials structure on which to set the password
+ * @param fd open file descriptor to read the password from 
+ * @param obtained This enum describes how 'specified' this password is
+ */
+
+_PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
+                                      int fd, enum credentials_obtained obtained)
+{
+       char *p;
+       char pass[128];
+
+       for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
+               p && p - pass < sizeof(pass);) {
+               switch (read(fd, p, 1)) {
+               case 1:
+                       if (*p != '\n' && *p != '\0') {
+                               *++p = '\0'; /* advance p, and null-terminate pass */
+                               break;
+                       }
+                       /* fall through */
+               case 0:
+                       if (p - pass) {
+                               *p = '\0'; /* null-terminate it, just in case... */
+                               p = NULL; /* then force the loop condition to become false */
+                               break;
+                       } else {
+                               fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
+                               return false;
+                       }
+
+               default:
+                       fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
+                                       fd, strerror(errno));
+                       return false;
+               }
+       }
+
+       cli_credentials_set_password(credentials, pass, obtained);
+       return true;
+}
+
+
index e1990a87138c89b4d3a8a919b4584ee1237a3d4a..4a9ccf535873bdfc934453db7bdec7dd54f82b5d 100644 (file)
 #include "lib/events/events.h"
 #include "dsdb/samdb/samdb.h"
 
-/**
- * Read a file descriptor, and parse it for a password (eg from a file or stdin)
- *
- * @param credentials Credentials structure on which to set the password
- * @param fd open file descriptor to read the password from 
- * @param obtained This enum describes how 'specified' this password is
- */
-
-_PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
-                                      int fd, enum credentials_obtained obtained)
-{
-       char *p;
-       char pass[128];
-
-       for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
-               p && p - pass < sizeof(pass);) {
-               switch (read(fd, p, 1)) {
-               case 1:
-                       if (*p != '\n' && *p != '\0') {
-                               *++p = '\0'; /* advance p, and null-terminate pass */
-                               break;
-                       }
-                       /* fall through */
-               case 0:
-                       if (p - pass) {
-                               *p = '\0'; /* null-terminate it, just in case... */
-                               p = NULL; /* then force the loop condition to become false */
-                               break;
-                       } else {
-                               fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
-                               return false;
-                       }
-
-               default:
-                       fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
-                                       fd, strerror(errno));
-                       return false;
-               }
-       }
-
-       cli_credentials_set_password(credentials, pass, obtained);
-       return true;
-}
-
-/**
- * Read a named file, and parse it for a password
- *
- * @param credentials Credentials structure on which to set the password
- * @param file a named file to read the password from 
- * @param obtained This enum describes how 'specified' this password is
- */
-
-_PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
-{
-       int fd = open(file, O_RDONLY, 0);
-       bool ret;
-
-       if (fd < 0) {
-               fprintf(stderr, "Error opening password file %s: %s\n",
-                               file, strerror(errno));
-               return false;
-       }
-
-       ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
-
-       close(fd);
-       
-       return ret;
-}
-
-/**
- * Read a named file, and parse it for username, domain, realm and password
- *
- * @param credentials Credentials structure on which to set the password
- * @param file a named file to read the details from 
- * @param obtained This enum describes how 'specified' this password is
- */
-
-_PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained) 
-{
-       uint16_t len = 0;
-       char *ptr, *val, *param;
-       char **lines;
-       int i, numlines;
-
-       lines = file_lines_load(file, &numlines, 0, NULL);
-
-       if (lines == NULL)
-       {
-               /* fail if we can't open the credentials file */
-               d_printf("ERROR: Unable to open credentials file!\n");
-               return false;
-       }
-
-       for (i = 0; i < numlines; i++) {
-               len = strlen(lines[i]);
-
-               if (len == 0)
-                       continue;
-
-               /* break up the line into parameter & value.
-                * will need to eat a little whitespace possibly */
-               param = lines[i];
-               if (!(ptr = strchr_m (lines[i], '=')))
-                       continue;
-
-               val = ptr+1;
-               *ptr = '\0';
-
-               /* eat leading white space */
-               while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
-                       val++;
-
-               if (strwicmp("password", param) == 0) {
-                       cli_credentials_set_password(cred, val, obtained);
-               } else if (strwicmp("username", param) == 0) {
-                       cli_credentials_set_username(cred, val, obtained);
-               } else if (strwicmp("domain", param) == 0) {
-                       cli_credentials_set_domain(cred, val, obtained);
-               } else if (strwicmp("realm", param) == 0) {
-                       cli_credentials_set_realm(cred, val, obtained);
-               }
-               memset(lines[i], 0, len);
-       }
-
-       talloc_free(lines);
-
-       return true;
-}
-
-
 /**
  * Fill in credentials for the machine trust account, from the secrets database.
  * 
index ef41971462b0db0d8c0162789dd01091e2b03731..7f4af4f08cd8183b829e5bf6b9f6aefe47e30cf4 100644 (file)
 #include "libcli/auth/libcli_auth.h"
 #include "auth/credentials/credentials.h"
 
-_PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
-                                             const char **username, 
-                                             const char **domain) 
-{
-       if (cred->principal_obtained > cred->username_obtained) {
-               *domain = talloc_strdup(mem_ctx, "");
-               *username = cli_credentials_get_principal(cred, mem_ctx);
-       } else {
-               *domain = cli_credentials_get_domain(cred);
-               *username = cli_credentials_get_username(cred);
-       }
-}
-
 _PUBLIC_ NTSTATUS cli_credentials_get_ntlm_response(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
                                           int *flags,
                                           DATA_BLOB challenge, DATA_BLOB target_info,