]>
diplodocus.org Git - nmh/blob - sbr/ruserpass.c
2 * Portions of this code are
3 * Copyright (c) 1985 Regents of the University of California.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
11 * by the University of California, Berkeley. The name of the
12 * University may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 #ifndef MAXHOSTNAMELEN
27 # define MAXHOSTNAMELEN 64
38 static char tokval
[100];
45 static struct toktab toktabs
[] = {
46 { "default", DEFAULT
},
48 { "password", PASSWD
},
50 { "account", ACCOUNT
},
59 static int token(void);
63 ruserpass(char *host
, char **aname
, char **apass
)
65 char *hdir
, buf
[BUFSIZ
];
66 int t
, usedefault
= 0;
69 hdir
= getenv("HOME");
72 snprintf(buf
, sizeof(buf
), "%s/.netrc", hdir
);
73 cfile
= fopen(buf
, "r");
80 while ((t
= token())) {
91 * Allow match either for user's host name.
93 if (mh_strcasecmp(host
, tokval
) == 0)
98 while ((t
= token()) && t
!= MACH
&& t
!= DEFAULT
) {
101 if (token() && *aname
== 0) {
102 *aname
= mh_xmalloc((size_t) strlen(tokval
) + 1);
103 strcpy(*aname
, tokval
);
107 if (fstat(fileno(cfile
), &stb
) >= 0 &&
108 (stb
.st_mode
& 077) != 0) {
109 /* We make this a fatal error to force the user to correct it */
110 advise(NULL
, "Error - ~/.netrc file must not be world or group readable.");
111 adios(NULL
, "Remove password or correct file permissions.");
113 if (token() && *apass
== 0) {
114 *apass
= mh_xmalloc((size_t) strlen(tokval
) + 1);
115 strcpy(*apass
, tokval
);
125 fprintf(stderr
, "Unknown .netrc keyword %s\n", tokval
);
141 if ((myname
= getlogin()) == NULL
) {
144 if ((pp
= getpwuid (getuid())) != NULL
)
145 myname
= pp
->pw_name
;
147 printf("Name (%s:%s): ", host
, myname
);
149 fgets(tmp
, sizeof(tmp
) - 1, stdin
);
150 tmp
[strlen(tmp
) - 1] = '\0';
155 *aname
= mh_xmalloc((size_t) strlen(myname
) + 1);
156 strcpy (*aname
, myname
);
163 snprintf(prompt
, sizeof(prompt
), "Password (%s:%s): ", host
, *aname
);
164 mypass
= nmh_getpass(prompt
);
166 if (*mypass
== '\0') {
170 *apass
= mh_xmalloc((size_t) strlen(mypass
) + 1);
171 strcpy (*apass
, mypass
);
185 while ((c
= getc(cfile
)) != EOF
&&
186 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
192 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
199 while ((c
= getc(cfile
)) != EOF
200 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
209 for (t
= toktabs
; t
->tokstr
; t
++)
210 if (!strcmp(t
->tokstr
, tokval
))