Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenruserpass.c
Go to the documentation of this file.
00001 /* 00002 * Copyright (c) 1985 Regents of the University of California. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms are permitted 00006 * provided that the above copyright notice and this paragraph are 00007 * duplicated in all such forms and that any documentation, 00008 * advertising materials, and other materials related to such 00009 * distribution and use acknowledge that the software was developed 00010 * by the University of California, Berkeley. The name of the 00011 * University may not be used to endorse or promote products derived 00012 * from this software without specific prior written permission. 00013 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 00014 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 00015 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00016 */ 00017 00018 #ifndef lint 00019 static char sccsid[] = "@(#)ruserpass.c 5.1 (Berkeley) 3/1/89"; 00020 #endif /* not lint */ 00021 00022 #include "precomp.h" 00023 //#include <utmp.h> 00024 00025 struct utmp *getutmp(); 00026 static FILE *cfile; 00027 00028 #ifndef MAXHOSTNAMELEN 00029 #define MAXHOSTNAMELEN 64 00030 #endif 00031 00032 #define DEFAULT 1 00033 #define LOGIN 2 00034 #define PASSWD 3 00035 #define ACCOUNT 4 00036 #define MACDEF 5 00037 #define ID 10 00038 #define MACH 11 00039 00040 static char tokval[100]; 00041 00042 static struct toktab { 00043 const char *tokstr; 00044 int tval; 00045 } toktab[]= { 00046 {"default", DEFAULT}, 00047 {"login", LOGIN}, 00048 {"password", PASSWD}, 00049 {"passwd", PASSWD}, 00050 {"account", ACCOUNT}, 00051 {"machine", MACH}, 00052 {"macdef", MACDEF}, 00053 {0, 0} 00054 }; 00055 00056 extern char *hostname; 00057 static int token(void); 00058 00059 int ruserpass(const char *host, char **aname, char **apass, char **aacct) 00060 { 00061 const char *hdir, *mydomain; 00062 char buf[BUFSIZ], *tmp; 00063 char myname[MAXHOSTNAMELEN]; 00064 int t, i, c, usedefault = 0; 00065 struct stat stb; 00066 00067 hdir = getenv("HOME"); 00068 if (hdir == NULL) 00069 hdir = "."; 00070 (void) sprintf(buf, "%s/.netrc", hdir); 00071 cfile = fopen(buf, "r"); 00072 if (cfile == NULL) { 00073 if (errno != ENOENT) 00074 perror(buf); 00075 return(0); 00076 } 00077 00078 00079 if (gethostname(myname, sizeof(myname)) < 0) 00080 myname[0] = '\0'; 00081 if ((mydomain = index(myname, '.')) == NULL) 00082 mydomain = ""; 00083 next: 00084 while ((t = token())) switch(t) { 00085 00086 case DEFAULT: 00087 usedefault = 1; 00088 /* FALL THROUGH */ 00089 00090 case MACH: 00091 if (!usedefault) { 00092 if (token() != ID) 00093 continue; 00094 /* 00095 * Allow match either for user's input host name 00096 * or official hostname. Also allow match of 00097 * incompletely-specified host in local domain. 00098 */ 00099 if (strcasecmp(host, tokval) == 0) 00100 goto match; 00101 if (strcasecmp(hostname, tokval) == 0) 00102 goto match; 00103 if ((tmp = index(hostname, '.')) != NULL && 00104 strcasecmp(tmp, mydomain) == 0 && 00105 strncasecmp(hostname, tokval, tmp - hostname) == 0 && 00106 tokval[tmp - hostname] == '\0') 00107 goto match; 00108 if ((tmp = index(host, '.')) != NULL && 00109 strcasecmp(tmp, mydomain) == 0 && 00110 strncasecmp(host, tokval, tmp - host) == 0 && 00111 tokval[tmp - host] == '\0') 00112 goto match; 00113 continue; 00114 } 00115 match: 00116 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { 00117 00118 case LOGIN: 00119 if (token()) { 00120 if (*aname == 0) { 00121 *aname = malloc((unsigned) strlen(tokval) + 1); 00122 (void) strcpy(*aname, tokval); 00123 } else { 00124 if (strcmp(*aname, tokval)) 00125 goto next; 00126 } 00127 } 00128 break; 00129 case PASSWD: 00130 if (strcmp(*aname, "anonymous") && 00131 fstat(fileno(cfile), &stb) >= 0 && 00132 (stb.st_mode & 077) != 0) { 00133 fprintf(stderr, "Error - .netrc file not correct mode.\n"); 00134 fprintf(stderr, "Remove password or correct mode.\n"); 00135 goto bad; 00136 } 00137 if (token() && *apass == 0) { 00138 *apass = malloc((unsigned) strlen(tokval) + 1); 00139 (void) strcpy(*apass, tokval); 00140 } 00141 break; 00142 case ACCOUNT: 00143 if (fstat(fileno(cfile), &stb) >= 0 00144 && (stb.st_mode & 077) != 0) { 00145 fprintf(stderr, "Error - .netrc file not correct mode.\n"); 00146 fprintf(stderr, "Remove account or correct mode.\n"); 00147 goto bad; 00148 } 00149 if (token() && *aacct == 0) { 00150 *aacct = malloc((unsigned) strlen(tokval) + 1); 00151 (void) strcpy(*aacct, tokval); 00152 } 00153 break; 00154 case MACDEF: 00155 if (proxy) { 00156 (void) fclose(cfile); 00157 return(0); 00158 } 00159 while ((c=getc(cfile)) != EOF && (c == ' ' || c == '\t')); 00160 if (c == EOF || c == '\n') { 00161 printf("Missing macdef name argument.\n"); 00162 goto bad; 00163 } 00164 if (macnum == 16) { 00165 printf("Limit of 16 macros have already been defined\n"); 00166 goto bad; 00167 } 00168 tmp = macros[macnum].mac_name; 00169 *tmp++ = c; 00170 for (i=0; i < 8 && (c=getc(cfile)) != EOF && 00171 !isspace(c); ++i) { 00172 *tmp++ = c; 00173 } 00174 if (c == EOF) { 00175 printf("Macro definition missing null line terminator.\n"); 00176 goto bad; 00177 } 00178 *tmp = '\0'; 00179 if (c != '\n') { 00180 while ((c=getc(cfile)) != EOF && c != '\n'); 00181 } 00182 if (c == EOF) { 00183 printf("Macro definition missing null line terminator.\n"); 00184 goto bad; 00185 } 00186 if (macnum == 0) { 00187 macros[macnum].mac_start = macbuf; 00188 } 00189 else { 00190 macros[macnum].mac_start = macros[macnum-1].mac_end + 1; 00191 } 00192 tmp = macros[macnum].mac_start; 00193 while (tmp != macbuf + 4096) { 00194 if ((c=getc(cfile)) == EOF) { 00195 printf("Macro definition missing null line terminator.\n"); 00196 goto bad; 00197 } 00198 *tmp = c; 00199 if (*tmp == '\n') { 00200 if (*(tmp-1) == '\0') { 00201 macros[macnum++].mac_end = tmp - 1; 00202 break; 00203 } 00204 *tmp = '\0'; 00205 } 00206 tmp++; 00207 } 00208 if (tmp == macbuf + 4096) { 00209 printf("4K macro buffer exceeded\n"); 00210 goto bad; 00211 } 00212 break; 00213 default: 00214 fprintf(stderr, "Unknown .netrc keyword %s\n", tokval); 00215 break; 00216 } 00217 goto done; 00218 } 00219 done: 00220 (void) fclose(cfile); 00221 return(0); 00222 bad: 00223 (void) fclose(cfile); 00224 return(-1); 00225 } 00226 00227 static int token(void) 00228 { 00229 char *cp; 00230 int c; 00231 struct toktab *t; 00232 00233 if (feof(cfile)) 00234 return (0); 00235 while ((c = getc(cfile)) != EOF && 00236 (c == '\r' || c == '\n' || c == '\t' || c == ' ' || c == ',')) 00237 continue; 00238 if (c == EOF) 00239 return (0); 00240 cp = tokval; 00241 if (c == '"') { 00242 while ((c = getc(cfile)) != EOF && c != '"') { 00243 if (c == '\\') 00244 c = getc(cfile); 00245 *cp++ = c; 00246 } 00247 } else { 00248 *cp++ = c; 00249 while ((c = getc(cfile)) != EOF 00250 && c != '\n' && c != '\t' && c != ' ' && c != ',' && c != '\r') { 00251 if (c == '\\') 00252 c = getc(cfile); 00253 *cp++ = c; 00254 } 00255 } 00256 *cp = 0; 00257 if (tokval[0] == 0) 00258 return (0); 00259 for (t = toktab; t->tokstr; t++) 00260 if (!strcmp(t->tokstr, tokval)) 00261 return (t->tval); 00262 return (ID); 00263 } Generated on Sun May 27 2012 04:17:14 for ReactOS by
1.7.6.1
|