Home Index

System CGI Prog. in Windows NT/2000, Change Password CGI prograam
 
Date: June  01 2001
Subject: Online Programmer Ezine, Volume 2, No.5
Article: System CGI Prog. in Windows NT/2000, Change Password CGI prograam

In previous issues of this ezine we have seen sample codes 
for CGI programming in C. We saw how we can do this from
base with our own code.

We also saw how we can write simple cgi applications.
If you have not received this articles you can see
them on our site.

In this issue we want to see how we can write system
administration CGI programs for windows NT/2000 
platforms. To be able to focus on system programming
issues we will use a ready CGI Library.

You can download a copy of "CGI-LIB Library by
Noel V Aguilar" from internet. After download you must
compile library source codes.

We have used a Win32 Console Application project in
Visual C++ environment. We have also added two library 
files to our project. First one is NETAPI32.LIB that
comes with Visual C++ . Second one is CGILIB.LIB that
you have obtained from compiling the source code of 
CGI-LIB . If you want you can obtain all of the project
files from our web site.

In this article we will use "NetUserChangePassword" 
function from NetAPI to change user passwords from a 
remote browser.

We have used a few of the most useful functions of
CGI-LIB. 

cgi_input_parse() function receives data that is sent
from browser with post and get method and parses it.

After this we will be able to get the value of a
variable sent by browser with find_val() function.
This function will use a pointer to parsed string that
we obtained in previous step.

mime_header() function will prepare needed headers for
a html/text type output for web server. This will 
determine type of data that will be sent to web server.

html_begin() function will add <html>, <head> and <title>
tags to web page that we are trying to create.
It accepts a title parameter and will use it between 
<title>...</title> tags in our web page.

We have also used "MultiByteToWideChar" function to convert
ordinary Ansi strings to WideString that is needed for
"MultiByteToWideChar" function parameters.

After calling "MultiByteToWideChar" function we will 
see return values to handle possible errors.

We have blocked changing Administrator password over the
web for security reasons.

And at the end we have used html_end() CGI-LIB function
to add </BODY> and </HTML> tags to the end of web page.

And finally we have done it.


In bellow source code we have forced to break long lines 
into two or more lines.



#ifndef UNICODE
#define UNICODE
#endif


#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 
#include <lm.h>
#include <tchar.h>
#include "cgi-lib.h"
#include "html-lib.h"

void handle_error(int n);

int main()
{

DWORD dwError = 0;
NET_API_STATUS nStatus;
int ln,c;

wchar_t name[20],newpassword[20],oldpassword[20];

char npassword[20],opassword[20],id[20];

LIST *head;

head = cgi_input_parse();
mime_header("text/html");

html_begin("Change Password Confirmation",NULL);
if(head == NULL)
handle_error(1);

strcpy(id,find_val(head,"id"));
strcpy(npassword,find_val(head,"newpass"));
strcpy(opassword,find_val(head,"oldpass"));

for(c=0;c<strlen(id);c++)
id[c]=(char)tolower(id[c]);

ln=MultiByteToWideChar(CP_UTF8,0,id,strlen(id)+1,name,20);

ln=MultiByteToWideChar
(CP_UTF8,0,npassword,strlen(npassword)+1,newpassword,20);

ln=MultiByteToWideChar
(CP_UTF8,0,opassword,strlen(opassword)+1,oldpassword,20);

wprintf(L"<br>Changing Password for \"%s\"<br>",name);

if(strcmp(id,"administrator"))
{
nStatus = 
NetUserChangePassword(NULL,name,oldpassword,newpassword);

if (nStatus == NERR_Success)
printf("<br><br><b>Password for user \"%s\" has been 
successfully changed.</b>",id );
else if (nStatus == 2221)
printf("<br><b>User not found</b>");
else if (nStatus == 86)
printf("<br><b>Old Password Not Correct</b>");
else 
printf("\n<br><br><b><font color=\"#FF0000\">Error 
changing password : %4d</FONT></b>", nStatus);
}
else
printf("<br><b>Password for administrator can not be 
changed.</b>");

html_end();

return 0;

}


void handle_error(int n)
{
printf("Error in Cgi input parameters");

html_end();
exit(1);
}


If you have any question you can ask your questions 
in our support forums at:

http://op.htmsoft.com


Home Index
© 2001 OnlineProgrammer and (Htmsoft.com) . All Rights Reserved