summaryrefslogtreecommitdiff
path: root/xmlrpc/validate-tcos.c
blob: 90b4e9850da4bcc3f8cbc198721489df314a245e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* validate-tcos.c part of tcosxmlrpc
*   => validate throught /etc/tcospasswd
* Copyright (C) 2006,2007,2008  mariodebian at gmail
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


#include "common.h"
#include "debug.h"
#include "validate.h"

#define _GNU_SOURCE
#include <string.h>

#include <pwd.h>
#include <sys/types.h>



void free_tokens( char **tokens) {
  int i=0;
  for(i = 0; tokens[i] != NULL; i++) {
    free(tokens[i]);
  }
  free(tokens);
}


char *validate_tcos(char *user, char *pass)
{
  /*struct passwd *pwd;*/
  char *cryptpass;
  FILE *fp;
  char line[BSIZE];
  char **tokens = NULL;
  int i=0;
  /*char *fret;*/
  struct info *login=malloc(sizeof(struct info));
  fp = fopen ("/etc/tcospasswd", "r" );
  if (fp == NULL) {
    dbgtcos("error validate_tcos(): file /etc/tcospasswd not exists.\n");
    return (char*) LOGIN_ERROR;
  }

  (void)fgets( line, sizeof line, fp);
  /*remove_line_break(line);*/
  fclose(fp);
  strncpy(login->line, line, BSIZE);

  /* split using ':' */
  tokens = split(login->line, ":");

  for(i = 0; tokens[i] != NULL; i++){
    if (i==0) {
      login->user=tokens[i];
      dbgtcos("split_login() USER FOUND tokens[%d]='%s' user='%s'\n",i,tokens[i], login->user);
    }
    if (i == 1) {
      login->pass=tokens[i];
      login->pass[strlen(login->pass)-1]='\0';
      #ifdef VISIBLE_PASSWD
        dbgtcos("split_login() PASSWD FOUND tokens[%d]='%s', pass='%s'\n",i,tokens[i], login->pass);
      #endif
    }
  }

  #ifdef VISIBLE_PASSWD
    dbgtcos( "validate_tcos() login->user=\"%s\" login->pass=\"%s\"\n", login->user, login->pass);
  #endif

  dbgtcos( "validate_tcos() check users user=\"%s\" my_user=\"%s\"\n", user, login->user);

  if ( strcmp(login->user, user) != 0 ) {
    dbgtcos("error validate_passwd(): BAD USER.\n");
    free_tokens(tokens);
    return LOGIN_NOUSER;
  }

  cryptpass=crypt(pass, PASS_ID );

  #ifdef VISIBLE_PASSWD
    dbgtcos("info validate_passwd(): the_pass=%s my_pass=%s \n", login->pass, cryptpass);
  #endif

  if ( strcmp(login->pass, cryptpass) == 0 ) {
    dbgtcos("info validate_passwd(): LOGIN OK.\n");
    free(login);
    free_tokens(tokens);
    return LOGIN_OK;
  }
  dbgtcos("info validate_passwd(): BAD PASSWORD.\n");
  free(login);
  free_tokens(tokens);
  return LOGIN_NOPASS;
}